Skip to content

Default to SSE2 floating point math on x86 - #430

Merged
skeeto merged 1 commit into
skeeto:masterfrom
Peter0x44:gcc-fpmath-sse
Oct 27, 2026
Merged

skeeto merged 1 commit into
skeeto:masterfrom
Peter0x44:gcc-fpmath-sse

Conversation

@Peter0x44

Copy link
Copy Markdown
Collaborator

The x86 kit and the x64 kit's -m32 target pentium4, so SSE2 is always enabled, yet GCC still defaulted to -mfpmath=387 for 32-bit code. x87 math keeps 80-bit intermediates (FLT_EVAL_METHOD 2), so a value can change when spilled to memory, and results differ from x64's. Configure GCC --with-fpmath=sse, which makes SSE2 math the default whenever SSE2 is enabled: -march=i686 still gets x87. long double stays x87, and the 32-bit ABI still returns floats in st(0).

The x86 kit and the x64 kit's -m32 target pentium4, so SSE2 is always
enabled, yet GCC still defaulted to -mfpmath=387 for 32-bit code. x87
math keeps 80-bit intermediates (FLT_EVAL_METHOD 2), so a value can
change when spilled to memory, and results differ from x64's. Configure
GCC --with-fpmath=sse, which makes SSE2 math the default whenever SSE2
is enabled: -march=i686 still gets x87. long double stays x87, and the
32-bit ABI still returns floats in st(0).
@Peter0x44

Copy link
Copy Markdown
Collaborator Author

For 32-bits, the only use of x87 in the ABI is for float return values. They end up in st(0). Very short functions that aren't inlined might have to do an extra copy there, but it's almost certainly a net perf gain.

Surprised we haven't noticed all of these years!

@skeeto

skeeto commented Oct 27, 2026

Copy link
Copy Markdown
Owner

Surprised we haven't noticed all of these years!

Yeah! How did you notice this? My Mandelbrot test at -O2 is about 2x faster on my XP laptop. Though they also curiously differ slightly in output, presumably due to rounding differences.

(This is making me re-think my possible no PR policy elsewhere that we discussed. Sometimes it's simpler to express an idea with code than to explain it.)

@skeeto
skeeto merged commit 26f8a17 into skeeto:master Oct 27, 2026
1 check passed
@Peter0x44
Peter0x44 deleted the gcc-fpmath-sse branch October 27, 2026 18:56
@Peter0x44

Copy link
Copy Markdown
Collaborator Author

I noticed because I was working on the 32-bit XP prospero build and a bunch of tests failed due to the different precision.

I've also been separately researching different float formats and trying to learn more about them. I sent a patch to gcc regarding the configurable precision of x87 that I CC'd you on that you may have noticed.

I'll put that info in a blog post at some point. You'll probably find it interesting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants