cs356: discussion #4 · 2021. 3. 9. · cs356: discussion #3 floating points & assembly...

17
CS356: Discussion #3 Floating points & Assembly Instructions

Upload: others

Post on 17-Mar-2021

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS356: Discussion #4 · 2021. 3. 9. · CS356: Discussion #3 Floating points & Assembly Instructions. IEEE 754 Standard: 32-bit Binary32 Format (float)

CS356: Discussion #3Floating points & Assembly Instructions

Page 2: CS356: Discussion #4 · 2021. 3. 9. · CS356: Discussion #3 Floating points & Assembly Instructions. IEEE 754 Standard: 32-bit Binary32 Format (float)

IEEE 754 Standard: 32-bit

Binary32 Format (float)

● Exponent encodes values [-126, 127] as unsigned integers with bias

● Exponent of all 0’s reserved for:

○ Zeros: 0x00000000 (0.0), 0x80000000 (-0.0)

○ Denormalized values: (-1)sign ×0.(fraction) ×2 1-127 (nonzero fraction)

● Exponent of all 1’s reserved for:

○ Infinity: 0x7F800000 (∞), 0xFF800000 (-∞)

○ NaN: with any nonzero fraction

● Decimal value (Normalized): (-1)sign ×1.(fraction) ×2 exponent - 127

● Decimal range: (7 significant decimal digits) ×10±38

sign exponent fraction

1 bit 8 bits 23 bits

Page 3: CS356: Discussion #4 · 2021. 3. 9. · CS356: Discussion #3 Floating points & Assembly Instructions. IEEE 754 Standard: 32-bit Binary32 Format (float)

Special Numbers (32-bit)

Description exp

(8 bits)

frac

(23 bits)

Lower 31 bits

(hex)

Decimal value

Zero 00…00 00…00 0x00000000 0.0

Smallest Pos Denormalized 00…00 00…01 0x00000001 2-23× 2-126

Largest Denormalized 00…00 11…11 0x007FFFFF (1.0-ε) × 2-126

Smallest Pos Normalized 00…01 00…00 0x00800000 1.0 × 2-126

One 01...11 00…00 0x3F800000 1.0

Largest Normalized 11…10 11…11 0x7F7FFFFF (2.0-ε) × 2127

Infinity 11…11 00…00 0x7F800000 Infinity

NaN 11…11 Nonzero > 0x7F800000 NaN

Page 4: CS356: Discussion #4 · 2021. 3. 9. · CS356: Discussion #3 Floating points & Assembly Instructions. IEEE 754 Standard: 32-bit Binary32 Format (float)

IEEE 754 Standard: 64-bit

Binary64 Format (double)

● Exponent encodes values [-1022, 1023] as unsigned integers with bias

● Exponent of all 0’s reserved for:

○ Zeros: 0x0000000000000000 (0.0), 0x8000000000000000 (-0.0)

○ Denormalized values: (-1)sign ×0.(fraction) ×2 1-1023 (nonzero fraction)

● Exponent of all 1’s reserved for:

○ Infinity: 0x7FF0000000000000 (∞), 0xFFF0000000000000 (-∞)

○ NaN: any nonzero fraction

● Decimal value (Normalized): (-1)sign ×1.(fraction) ×2 exponent - 1023

● Decimal range: (≃ 16 significant decimal digits) ×10 ±308

sign exponent fraction

1 bit 11 bits 52 bits

Page 5: CS356: Discussion #4 · 2021. 3. 9. · CS356: Discussion #3 Floating points & Assembly Instructions. IEEE 754 Standard: 32-bit Binary32 Format (float)

Other formats, same patterns

1 sign bit, k bits for exponent, m bits for fraction

Bias = 2k-1-1

Normalized: (-1)sign ×1.(fraction) ×2exponent - Bias

Denormalized: (-1)sign ×0.(fraction) ×21-Bias

To negate, just flip the sign bit (except NaN)

Page 6: CS356: Discussion #4 · 2021. 3. 9. · CS356: Discussion #3 Floating points & Assembly Instructions. IEEE 754 Standard: 32-bit Binary32 Format (float)

Exercise: Detect Denormalized Numbers

Write a function int denorm(unsigned int x) that returns 1 if x is

denormalized, and 0 otherwise.

Page 7: CS356: Discussion #4 · 2021. 3. 9. · CS356: Discussion #3 Floating points & Assembly Instructions. IEEE 754 Standard: 32-bit Binary32 Format (float)

Exercise: Detect Denormalized Numbers

Write a function int denorm(unsigned int x) that returns 1 if x is

denormalized, and 0 otherwise.

Solution 1 (5 Operators)

int denorm(unsigned int x) {

return !((x >> 23) & 0xFF) && (x & 0x007FFFFF);

}

Page 8: CS356: Discussion #4 · 2021. 3. 9. · CS356: Discussion #3 Floating points & Assembly Instructions. IEEE 754 Standard: 32-bit Binary32 Format (float)

Exercise: Detect Denormalized Numbers

Write a function int denorm(unsigned int x) that returns 1 if x is

denormalized, and 0 otherwise.

Solution 1 (5 Operators)

int denorm(unsigned int x) {

return !((x >> 23) & 0xFF) && (x & 0x007FFFFF);

}

Solution 2 (3 Operators)

int denorm(unsigned int x) {

int t = x & 0x7FFFFFFF;

if (x < 0x800000 && x > 0)

return 1;

else

return 0;

}

Page 9: CS356: Discussion #4 · 2021. 3. 9. · CS356: Discussion #3 Floating points & Assembly Instructions. IEEE 754 Standard: 32-bit Binary32 Format (float)

Rounding and Casting in C

The IEEE 754 standard defines four rounding modes:

● Round to nearest, ties to even: default rounding in C for float/double ops

● Round towards zero (truncation): used to cast float/double to int

● Round up (ceiling): go towards +∞ (gives an upper bound)

● Round down (floor): go towards -∞ (gives a lower bound)

Example: 8-bit frac → 4-bit frac, Round to nearest, ties to even

10101001 → 1011

10100110 → 1010

10101000 → 1010

10111000 → 1100

Page 10: CS356: Discussion #4 · 2021. 3. 9. · CS356: Discussion #3 Floating points & Assembly Instructions. IEEE 754 Standard: 32-bit Binary32 Format (float)

Exercise: Casting

Do the following statements always hold?

• (float) ((double) f) == f• (double) ((float) d) == d

• (int) ((double) x) == x• (int) ((float) x) == x• (short) ((float) s) == s

short s; int i; float f; double d;

Page 11: CS356: Discussion #4 · 2021. 3. 9. · CS356: Discussion #3 Floating points & Assembly Instructions. IEEE 754 Standard: 32-bit Binary32 Format (float)

Exercise: Casting

Do the following statements always hold?

• (float) ((double) f) == f YES• (double) ((float) d) == d NO

• (int) ((double) x) == x YES• (int) ((float) x) == x NO• (short) ((float) s) == s YES

short s; int i; float f; double d;

Page 12: CS356: Discussion #4 · 2021. 3. 9. · CS356: Discussion #3 Floating points & Assembly Instructions. IEEE 754 Standard: 32-bit Binary32 Format (float)

Floating point operations in C

Floating point operations

● Addition and subtraction are not associative

○ Add small-magnitude numbers before large-magnitude ones

● Multiplication and division are not associative (nor distributive)

○ Control magnitude with divisions (if possible)

(big1 * big2) / (big3 * big4) overflows on first multiplication

1/big3 * 1/big4 * big1 * big2 underflows on first multiplication

(big1 / big3) * (big2 / big4) is likely better

● Comparison should use fabs(x-y) < epsilon instead of x==y

● Instead: 2’s complement is associative (even after overflow), can use x==y

Page 13: CS356: Discussion #4 · 2021. 3. 9. · CS356: Discussion #3 Floating points & Assembly Instructions. IEEE 754 Standard: 32-bit Binary32 Format (float)

What about programs that operate on data?

Integer and Floating-Point Formats

● Two’s Complement

● IEEE 754

Machine-Level Programs

● Operand specifiers

● Data movement

● Arithmetic and logic operations

● Stack manipulation

● Control structures

● Procedures

High-Level Programs

● C/C++

● Java

● Python

Page 14: CS356: Discussion #4 · 2021. 3. 9. · CS356: Discussion #3 Floating points & Assembly Instructions. IEEE 754 Standard: 32-bit Binary32 Format (float)

Why learning assembly?

Understanding the machine

● Reverse engineering

● Security analysis

● Performance tuning (rarely)

Beware: the compiler applies several optimizations!

● Rearrange execution order.

● Eliminate unneeded computations.

● Replace slow operations with faster ones.

● Change recursive operations with iterative ones.

Compilation

● C → ASM (compiler) → object program (assembler) → executable (linker)

● “gcc -Og -S input.c” produces the assembly of the input program

Page 15: CS356: Discussion #4 · 2021. 3. 9. · CS356: Discussion #3 Floating points & Assembly Instructions. IEEE 754 Standard: 32-bit Binary32 Format (float)

Programmer-Visible State

● Instructions and data must be read from main memory.

● Instructions are executed on registers.

CPU

PC

Registers

Memory

StackHeapDataCode

Addresses

Data

InstructionsConditionCodes

Page 16: CS356: Discussion #4 · 2021. 3. 9. · CS356: Discussion #3 Floating points & Assembly Instructions. IEEE 754 Standard: 32-bit Binary32 Format (float)

%rax

%rbx

%rcx

%rdx

%rsi

%rdi

%rsp

%rbp

16 ⨉ 64-bit general registers

%eax

%ebx

%ecx

%edx

%esi

%edi

%esp

%ebp

%ax

%bx

%cx

%dx

● In addition: %r8 to %r15 (%r8d / %r8w / %r8b for lower 4 / 2 / 1 bytes)

accumulate

base

counter

data

source index

destination index

stack pointer

base pointer

%si

%di

%sp

%bp

q (8 bytes)l (4 bytes)

w (2 bytes)b (1 byte)

%al

%bl

%cl

%dl

%sil

%dil

%spl

%bpl

Page 17: CS356: Discussion #4 · 2021. 3. 9. · CS356: Discussion #3 Floating points & Assembly Instructions. IEEE 754 Standard: 32-bit Binary32 Format (float)

Data Movement: Instructions

Move to register/memory (register operands must match size codes)

● movb src, dst (1 byte)

● movw src, dst (2 bytes)

● movl src, dst (4 bytes / with register destination, the others are set to 0)

● movq src, dst (8 bytes)

● movabsq imm, reg (8 bytes / 64-bit source value allowed into register)

(movq only supports a 32-bit immediate; movabsq allows a 64-bit immediate)

(Either src or dst can refer to a memory location, not both; no imm as dst.)

imm

mem reg