Skip to content

Fix JS number rounding on x87#1244

Merged
bnoordhuis merged 2 commits intoquickjs-ng:masterfrom
bnoordhuis:fix1236
Nov 21, 2025
Merged

Fix JS number rounding on x87#1244
bnoordhuis merged 2 commits intoquickjs-ng:masterfrom
bnoordhuis:fix1236

Conversation

@bnoordhuis
Copy link
Copy Markdown
Contributor

Disable 80-bits extended precision, use standard 64-bits precision. The extra precision affects rounding and is user-observable, meaning it causes test262 tests to fail.

The rounding mode is a per-CPU (or, more accurately, per-FPU) property, and since quickjs is often used as a library, take care to tweak the FPU control word before sensitive operations, and restore it afterwards.

Only affects x87. SSE is IEEE-754 conforming.

Fixes: #1236

(new CI job can fail, may need to link explicitly to libm)

Copy link
Copy Markdown
Contributor

@saghul saghul left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not going to pretend to understand what's going on here 😅

@bnoordhuis
Copy link
Copy Markdown
Contributor Author

/home/runner/work/_temp/d0c8a954-89f3-4555-9dc2-6e488b6f3ac2: line 1: git: not found

Which I guess makes sense because git isn't part of build-base but then how the heck do other jobs pass? Anyway, I'll add it and let's see how it goes.

Disable 80-bits extended precision, use standard 64-bits precision.
The extra precision affects rounding and is user-observable, meaning
it causes test262 tests to fail.

The rounding mode is a per-CPU (or, more accurately, per-FPU) property,
and since quickjs is often used as a library, take care to tweak the FPU
control word before sensitive operations, and restore it afterwards.

Only affects x87. SSE is IEEE-754 conforming.

Fixes: quickjs-ng#1236
@bnoordhuis
Copy link
Copy Markdown
Contributor Author

I'm not going to pretend to understand what's going on here 😅

The x87 coprocessor has a register that controls various options, like rounding, exceptions, etc. Its floating-point registers are 80 bits wide, which is good if you want extra precision, but bad if you want strict IEEE-754 conformance.

I made it turn off the extra precision while we add/subtract/etc., then restore the coprocessor to its original state, so we don't affect other parts of the (embedder's) program, that may depend on that extra precision.

@saghul
Copy link
Copy Markdown
Contributor

saghul commented Nov 21, 2025

I'm not going to pretend to understand what's going on here 😅

The x87 coprocessor has a register that controls various options, like rounding, exceptions, etc. Its floating-point registers are 80 bits wide, which is good if you want extra precision, but bad if you want strict IEEE-754 conformance.

I made it turn off the extra precision while we add/subtract/etc., then restore the coprocessor to its original state, so we don't affect other parts of the (embedder's) program, that may depend on that extra precision.

Today in "learning stuff with Ben"! Thank you, TIL!

@bnoordhuis bnoordhuis merged commit bd4ee68 into quickjs-ng:master Nov 21, 2025
128 checks passed
@bnoordhuis bnoordhuis deleted the fix1236 branch November 21, 2025 10:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants