Skip to content
Tutorials Center
My learning

Topics

Lesson 30 of 40 ยท Systems and Performance

Floating Point and SIMD

Learn how assembly handles floating-point numbers and SIMD: xmm registers, addsd, packed SSE and AVX operations, ARM NEON, and compiler vectorization.

So far every instruction in this tutorial worked on whole numbers in general-purpose registers. Real programs also compute with fractions (physics, graphics, audio, machine learning), and they often do the same operation on huge arrays of values. Modern CPUs have a second set of registers and instructions for exactly this: floating-point math, and SIMD (single instruction, multiple data) math that handles several values with one instruction.

This lesson shows how both look in x86-64 and ARM64 assembly. You will see the xmm registers and instructions such as addsd, learn why floating-point comparisons need special care, meet packed operations in SSE, AVX and NEON, and run a model of a four-lane vector add. The real assembly is read-only; the TryIts are stand-ins and one real x86-64 program using compiler intrinsics.

Floating-point in registers

On x86-64, float and double values live in the xmm registers (xmm0 to xmm15), 128 bits wide and separate from rax and friends. The SSE2 scalar instructions operate on just the lowest value in each register. The suffix tells the type: ss is scalar single (32-bit float), sd is scalar double (64-bit double).

Instruction Meaning
movsd xmm0, [rdi] load a double
addsd xmm0, xmm1 xmm0 = xmm0 + xmm1
subsd, mulsd, divsd subtract, multiply, divide
sqrtsd xmm0, xmm1 square root
cvtsi2sd xmm0, eax convert an integer to a double
cvttsd2si eax, xmm0 convert a double to an integer, truncating toward zero
ucomisd xmm0, xmm1 compare and set flags

The System V calling convention passes the first eight floating-point arguments in xmm0 to xmm7 and returns a float or double in xmm0 (see Calling Conventions). So this C function:

C
double hyp2(double a, double b) {
    return a * a + b * b;
}

compiles at -O2 to something like this (Intel syntax):

ASM
hyp2:
  mulsd  xmm0, xmm0      ; a*a
  mulsd  xmm1, xmm1      ; b*b
  addsd  xmm0, xmm1      ; sum, returned in xmm0
  ret

On ARM64 the scalar floating-point registers are d0 to d31 (doubles) and s0 to s31 (floats), views of the same 128-bit vector registers v0 to v31. The same function is:

ASM
hyp2:
  fmul   d0, d0, d0
  fmul   d1, d1, d1
  fadd   d0, d0, d1
  ret

Both architectures follow the IEEE 754 standard, so a given operation gives the same result on both, apart from some details of fused multiply-add (fma) instructions, which round once instead of twice.

Comparisons and NaN

A subtlety: floating-point values include NaN ("not a number", the result of 0.0/0.0 or sqrt(-1)), and NaN is not equal, less than or greater than anything, not even itself. A compare therefore has four outcomes (less, equal, greater, unordered), and the flags must express all of them. On x86, ucomisd sets ZF, PF and CF:

Result ZF PF CF
a > b 0 0 0
a < b 0 0 1
a == b 1 0 0
unordered (a NaN) 1 1 1

After ucomisd, you use the unsigned-style jumps: ja (above: CF=0 and ZF=0), jb, je. A NaN sets all three flags, so ja is not taken, while je would be taken unless the compiler also checks the parity flag. That is why compilers emit an extra jp (jump if parity) check when they compile a == b for floating-point numbers.

The next demo exposes the raw bits of a double and the compare flags. It also shows the classic 0.1 + 0.2 surprise: the stored values are binary approximations, and their sum lands one step away from the double nearest to 0.3.

JS Doubles, bits and ucomisd flags

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The lesson for your own code is to avoid comparing computed floating-point values with == and to remember that a NaN breaks the usual rules.

SIMD: one instruction, many values

Suppose you want to add two arrays of 1,000 floats. A scalar loop uses 1,000 additions. A SIMD machine puts several elements into one wide register, called a vector, and adds all of them with a single instruction. Each element position is a lane, and lanes do not interact: lane 0 of the result only depends on lane 0 of each input.

flowchart LR
  subgraph A["register A (128 bits)"]
    a0["a0"] --- a1["a1"] --- a2["a2"] --- a3["a3"]
  end
  subgraph B["register B (128 bits)"]
    b0["b0"] --- b1["b1"] --- b2["b2"] --- b3["b3"]
  end
  subgraph C["result (one instruction)"]
    c0["a0+b0"] --- c1["a1+b1"] --- c2["a2+b2"] --- c3["a3+b3"]
  end
  A --> C
  B --> C

A packed add works on all four lanes at once; the lanes are independent.

The same register can be viewed with different lane layouts, and the instruction mnemonic says which:

Layout in a 128-bit register x86 SSE add ARM NEON add
4 x 32-bit floats addps xmm0, xmm1 fadd v0.4s, v1.4s, v2.4s
2 x 64-bit doubles addpd xmm0, xmm1 fadd v0.2d, v1.2d, v2.2d
4 x 32-bit integers paddd xmm0, xmm1 add v0.4s, v1.4s, v2.4s
16 x 8-bit integers paddb xmm0, xmm1 add v0.16b, v1.16b, v2.16b

The p in paddd stands for "packed", and ps and pd mean packed single and packed double. On NEON the lane layout is written after the register name (.4s is four 32-bit "single" lanes, .2d is two doubles, .16b sixteen bytes).

The model below mimics a 4-lane integer vector unit. It adds 10 elements with a scalar loop (10 operations) and with a vector loop (two vector operations plus a two-element tail), and it shows that integer lanes wrap around independently, as paddd does.

JS A toy 4-lane vector add

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Real code works exactly this way, with one added trick: loops handle blocks of four (or eight, or sixteen) elements, then clean up the tail with scalar code or a masked operation.

SSE, AVX and NEON

The SIMD capabilities differ by generation.

  • SSE and SSE2 are 128-bit and part of the x86-64 baseline: every 64-bit x86 CPU has them, so compilers use them without any special flags.
  • AVX and AVX2 widen the registers to 256 bits (ymm0 to ymm15). The xmm registers become the lower halves of the ymm registers. AVX uses a three-operand form (vaddps ymm0, ymm1, ymm2, destination separate from sources) and AVX2 adds integer operations on 256 bits. A program needs a CPU that supports them, so code usually checks with cpuid or compiles several versions and picks one at run time.
  • AVX-512 goes to 512-bit zmm registers and adds mask registers, but is available only on some CPUs.
  • NEON (Advanced SIMD) is part of the ARM64 baseline: 32 registers v0 to v31, each 128 bits. Larger and scalable vectors exist as the optional SVE extension, whose vector length is chosen by the hardware.

Wider is not automatically faster. Wide vector instructions need aligned, contiguous data, and some processors lower their clock speed under heavy 512-bit loads.

The compiler can vectorize for you

You rarely write these instructions by hand. Take a plain loop:

C
void add_arrays(int *restrict c, const int *a, const int *b, int n) {
    for (int i = 0; i < n; i++) c[i] = a[i] + b[i];
}

With -O3 (and in recent GCC and Clang versions, in some cases at lower levels), the compiler auto-vectorizes it. It emits a main loop that processes four integers per iteration, in roughly this shape, followed by a scalar loop for the leftover elements:

ASM
.Lvector_loop:
  movdqu  xmm0, [rsi+rax]      ; load 4 ints from a
  movdqu  xmm1, [rdx+rax]      ; load 4 ints from b
  paddd   xmm0, xmm1           ; 4 additions at once
  movups  [rdi+rax], xmm0      ; store 4 results to c
  add     rax, 16
  cmp     rax, rcx
  jne     .Lvector_loop

The restrict keyword promises that c does not overlap a or b; without that promise the compiler must either prove it at run time or leave the loop scalar, because a vector store could overwrite values that a later lane still needs to read. Floating-point reductions such as summing an array are not vectorized by default, because changing the order of additions changes rounding, unless you allow it with -ffast-math or -fassociative-math.

When the compiler does not do what you want, intrinsics give you control without writing assembly. They are C functions that map to single instructions, declared in <immintrin.h> for x86 and <arm_neon.h> for ARM. The program below uses SSE2 intrinsics on the site's real x86-64 compiler. SSE2 is part of the x86-64 baseline, so no compiler flags are needed, and the code falls back to a plain message on other CPUs.

C Real SSE intrinsics

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Look at the second line: lane 0 wraps from the largest 32-bit integer to the smallest negative number, but lane 1 simply becomes 6. And the mask output, with -1 (all bits set) for true lanes and 0 for false lanes, is how SIMD code represents a comparison: instead of branching per element, you combine data with masks using bitwise and and or so that every lane follows the same instructions.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does the instruction addsd xmm0, xmm1 do?

    The sd suffix means scalar double: one 64-bit double in the low lane. The packed versions end in ps or pd.
  2. 2. Where does a function return a double in the System V x86-64 convention?

    Floating-point arguments use xmm0 to xmm7 and a float or double result is returned in xmm0.
  3. 3. Why can't the flags from ucomisd always be treated like an integer compare?

    A comparison involving NaN is neither less, equal nor greater. x86 reports 'unordered' with all three flags set, so compilers add a parity check for equality.
  4. 4. What does paddd xmm0, xmm1 compute?

    paddd is a packed add of doublewords (32-bit integers). Lanes are independent, so overflow in one lane does not carry into the next.
  5. 5. Why does the compiler need restrict (or a run-time check) to vectorize c[i] = a[i] + b[i]?

    If the arrays may overlap, processing four elements at once could give different results from the one-by-one loop, so the compiler must prove or check that they do not.

Summary

  • Floating-point values live in the xmm registers on x86-64 (d0 to d31 and s0 to s31 on ARM64); sd and ss mean scalar double and single.
  • Floating-point results follow IEEE 754; compare with care, since NaN is unordered and 0.1 + 0.2 is not exactly 0.3.
  • SIMD instructions work on lanes of a wide register: one instruction, several independent values (paddd, addps, fadd v0.4s).
  • SSE2 (128-bit) is baseline on x86-64, AVX/AVX2 give 256-bit ymm, AVX-512 gives 512-bit zmm; ARM64 has 128-bit NEON and optional scalable SVE.
  • Compilers auto-vectorize simple loops (often needing -O3 and restrict) and handle leftovers with a scalar tail; intrinsics give direct control.
  • Vector comparisons produce masks instead of branches.

Next: Performance: Pipelines, Caches and Branches, where you see why some code runs much faster than other code with the same instruction count.