Floating Point with SSE
Integers live in the general-purpose registers; floating-point numbers live in the sixteen 128-bit XMM registers, xmm0–xmm15, and are processed by SSE instructions. (The old x87 floating-point stack still exists, but 64-bit code uses SSE for everything.) An XMM register can hold one double in its low 64 bits — the scalar use in this lesson — or several values at once, which is the topic of SIMD.
Scalar double-precision instructions
The suffix tells you the type: sd = scalar double (64-bit double), ss = scalar single (32-bit float).
| Instruction | Effect |
|---|---|
movsd xmm, [mem] / movsd [mem], xmm |
load / store one double |
movq xmm, r64 / movq r64, xmm |
copy raw bits between register files |
addsd, subsd, mulsd, divsd |
arithmetic: xmm0 = xmm0 op src |
sqrtsd xmm0, xmm1 |
square root |
minsd, maxsd |
minimum / maximum |
cvtsi2sd xmm, r64 |
integer → double |
cvttsd2si r64, xmm |
double → integer, truncating toward zero |
cvtsd2si r64, xmm |
double → integer, rounding (to nearest, ties to even) |
ucomisd xmm0, xmm1 |
compare, setting ZF/CF/PF like an unsigned compare |
There are no floating-point immediates: constants go in memory (dq 3.14159 produces the 8-byte IEEE 754 encoding) and are loaded with movsd.
Printing floating-point results
Converting a double to decimal text properly is a hard problem (C's printf uses hundreds of lines for it). We take a simpler route: scale and round to an integer, then print that. Multiply by 100, round with cvtsd2si, and print the result with two decimal places using a helper, print_fixed2. It is the print_uint digit loop with one addition: after two digits it inserts a ., and it always writes at least three digits so that 5 hundredths prints as 0.05.
section .data
pi: dq 3.141592653589793
radius: dq 2.5
hundred: dq 100.0
section .text
global _start
_start:
movsd xmm0, [radius]
mulsd xmm0, xmm0 ; r * r
mulsd xmm0, [pi] ; pi * r * r = 19.6349...
mulsd xmm0, [hundred] ; scale to hundredths
cvtsd2si rdi, xmm0 ; round to the nearest integer: 1963
call print_fixed2
mov eax, 60
xor edi, edi
syscall
; ---- print_fixed2: print rdi hundredths as a decimal number with 2 places ----
print_fixed2:
mov rax, rdi ; e.g. 31415 -> "314.15"
sub rsp, 32
lea rsi, [rsp + 31]
mov byte [rsi], 10
mov ecx, 10
xor r8d, r8d ; digits written so far
.digit:
cmp r8d, 2
jne .no_point
dec rsi
mov byte [rsi], '.' ; the point goes after two digits
.no_point:
xor edx, edx
div rcx
add dl, '0'
dec rsi
mov [rsi], dl
inc r8d
cmp r8d, 3
jb .digit ; always at least three digits: "0.05"
test rax, rax
jnz .digit
lea rdx, [rsp + 32]
sub rdx, rsi
mov eax, 1
mov edi, 1
syscall
add rsp, 32
ret19.63
Note:
print_fixed2treats its argument as unsigned. For negative results, print a-first and pass the absolute value (asprint_intdoes).
Converting between integers and doubles
cvttsd2si (with the extra t) truncates like a C cast; cvtsd2si rounds according to the current rounding mode, which by default is round to nearest, ties to even — so 2.5 becomes 2 but 3.5 becomes 4:
section .data
values: dq 2.5, 3.5, -2.7, 7.9
section .text
global _start
_start:
xor ebx, ebx
.next:
movsd xmm0, [values + rbx*8]
cvttsd2si rdi, xmm0 ; truncate
call print_int
movsd xmm0, [values + rbx*8]
cvtsd2si rdi, xmm0 ; round to nearest even
call print_int
inc ebx
cmp ebx, 4
jb .next
mov rdi, 7
cvtsi2sd xmm0, rdi ; integer -> double
mulsd xmm0, xmm0 ; 49.0
cvttsd2si rdi, xmm0
call print_int
mov eax, 60
xor edi, edi
syscall
; ---- print_int: print rdi as a signed decimal number and a newline ----
print_int:
test rdi, rdi
jns print_uint ; not negative: print it as it is
neg rdi ; make it positive
push rdi ; keep it (write and syscall clobber registers)
push '-' ; the minus sign, as a byte on the stack
mov eax, 1 ; write(1, rsp, 1)
mov edi, 1
mov rsi, rsp
mov edx, 1
syscall
pop rdi ; drop the '-'
pop rdi ; the positive value again
jmp print_uint ; print_uint's ret returns to our caller
; ---- print_uint: print rdi as an unsigned decimal number and a newline ----
print_uint:
mov rax, rdi ; rax = the number to convert
sub rsp, 32 ; room for up to 20 digits and a newline
lea rsi, [rsp + 31] ; fill the buffer from its end
mov byte [rsi], 10 ; the newline is the last character
mov ecx, 10
.next_digit:
xor edx, edx ; the dividend is rdx:rax, so clear rdx
div rcx ; rax = quotient, rdx = remainder (0..9)
add dl, '0' ; remainder -> ASCII digit
dec rsi
mov [rsi], dl ; store it in front of the previous one
test rax, rax
jnz .next_digit ; repeat until the quotient is 0
lea rdx, [rsp + 32]
sub rdx, rsi ; rdx = number of bytes to print
mov eax, 1 ; write(1, rsi, rdx)
mov edi, 1
syscall
add rsp, 32
ret2
2
3
4
-2
-3
7
8
49
Notice that the program reloads xmm0 after print_int: all XMM registers are caller-saved, so any function may change them.
Square roots and comparisons
ucomisd compares two doubles and sets ZF and CF as if for unsigned integers, so you use the unsigned jumps jb, jbe, ja, jae. (PF is set if either value is NaN, "not a number".) Here we compute the length of a 2D vector and check it against a limit:
section .data
dx_: dq 3.0
dy_: dq 4.0
limit: dq 4.5
hundred: dq 100.0
ok_msg: db "within limit", 10
far_msg: db "too far", 10
section .text
global _start
_start:
movsd xmm0, [dx_]
mulsd xmm0, xmm0 ; dx^2
movsd xmm1, [dy_]
mulsd xmm1, xmm1 ; dy^2
addsd xmm0, xmm1
sqrtsd xmm0, xmm0 ; length = 5.0
movsd xmm2, xmm0 ; keep a copy
mulsd xmm0, [hundred]
cvtsd2si rdi, xmm0
call print_fixed2 ; 5.00
ucomisd xmm2, [limit] ; compare length with 4.5
jbe .ok
mov rsi, far_msg
mov edx, 8
jmp .print
.ok:
mov rsi, ok_msg
mov edx, 13
.print:
mov eax, 1
mov edi, 1
syscall
mov eax, 60
xor edi, edi
syscall
; ---- print_fixed2: print rdi hundredths as a decimal number with 2 places ----
print_fixed2:
mov rax, rdi ; e.g. 31415 -> "314.15"
sub rsp, 32
lea rsi, [rsp + 31]
mov byte [rsi], 10
mov ecx, 10
xor r8d, r8d ; digits written so far
.digit:
cmp r8d, 2
jne .no_point
dec rsi
mov byte [rsi], '.' ; the point goes after two digits
.no_point:
xor edx, edx
div rcx
add dl, '0'
dec rsi
mov [rsi], dl
inc r8d
cmp r8d, 3
jb .digit ; always at least three digits: "0.05"
test rax, rax
jnz .digit
lea rdx, [rsp + 32]
sub rdx, rsi
mov eax, 1
mov edi, 1
syscall
add rsp, 32
ret5.00
too far
Note:
xmm2survives the call toprint_fixed2only because that particular routine never touches XMM registers. When you call functions you didn't write, store such values in memory first. (The labelsdx_anddy_have underscores becausedxis a register name.)
Averaging a list of measurements
Sum the readings, divide by the count (converted to double), and print with two decimals:
section .data
readings: dq 21.5, 23.25, 19.75, 22.0, 24.5, 20.0
COUNT equ ($ - readings) / 8
hundred: dq 100.0
section .text
global _start
_start:
pxor xmm0, xmm0 ; sum = 0.0 (xor of all bits)
xor ecx, ecx
.add:
addsd xmm0, [readings + rcx*8]
inc ecx
cmp ecx, COUNT
jb .add
mov eax, COUNT
cvtsi2sd xmm1, rax
divsd xmm0, xmm1 ; average = sum / count
mulsd xmm0, [hundred]
cvtsd2si rdi, xmm0
call print_fixed2
mov eax, 60
xor edi, edi
syscall
; ---- print_fixed2: print rdi hundredths as a decimal number with 2 places ----
print_fixed2:
mov rax, rdi ; e.g. 31415 -> "314.15"
sub rsp, 32
lea rsi, [rsp + 31]
mov byte [rsi], 10
mov ecx, 10
xor r8d, r8d ; digits written so far
.digit:
cmp r8d, 2
jne .no_point
dec rsi
mov byte [rsi], '.' ; the point goes after two digits
.no_point:
xor edx, edx
div rcx
add dl, '0'
dec rsi
mov [rsi], dl
inc r8d
cmp r8d, 3
jb .digit ; always at least three digits: "0.05"
test rax, rax
jnz .digit
lea rdx, [rsp + 32]
sub rdx, rsi
mov eax, 1
mov edi, 1
syscall
add rsp, 32
ret21.83
The exact average is 131 / 6 = 21.8333…, rounded to two places.
Floating-point pitfalls
- 0.1 is not exact in binary floating point;
0.1 + 0.2is not exactly0.3. Compare with a tolerance, or work in integers (cents) when exactness matters. - Converting a value that doesn't fit (or NaN) into an integer gives the "integer indefinite" value
0x8000000000000000. - Use
ucomisd+ unsigned jumps; signed jumps (jl,jg) test SF/OF, whichucomisdclears. - In the calling convention,
doublearguments go inxmm0–xmm7and adoubleresult comes back inxmm0.
Test yourself
What does the suffix sd in addsd mean?
- signed doubleword
- scalar double precision
- single direction
- stack data
What does cvttsd2si rax, xmm0 produce for -2.7?
- -3
- -2
- 3
- 2
Which jumps should follow ucomisd xmm0, xmm1?
- jl / jg
- jb / ja (and their variants)
- js / jns
- jo / jno