Skip to content

Fix inaccurate round while being fast #7308

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -1566,30 +1566,34 @@ LibraryManager.library = {

round__asm: true,
round__sig: 'dd',
round__deps: ['llvm_copysign_f64'],
round: function(d) {
d = +d;
return d >= +0 ? +Math_floor(d + +0.5) : +Math_ceil(d - +0.5);
return +Math_trunc(d + +_llvm_copysign_f64(+0.49999999999999994, d));
Copy link
Member

Choose a reason for hiding this comment

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

Note that this is handwritten JS code, and in particular, llvm_copysign_* is present here as a fallback, implementing it in JS when it can't be done otherwise. It's a slow code path, see https://github.com/kripken/emscripten/blob/incoming/src/library.js#L1559

To actually use copysign, this would need to be written in C, perhaps in ./system/lib/libc/extras.c, and it could use a copysign intrinsic there.

Copy link
Member

Choose a reason for hiding this comment

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

I think the CI failures are because this function is marked as asm.js, and I don't think trunc is a math intrinsic there.

Copy link
Author

Choose a reason for hiding this comment

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

Yeah, between these two things and f32, it sounds like writing it in C is the better choice.

I could do it here with a ternary and floor, but it would be slightly inferior wasm, with a branch. I'll have a try at the C version.

},

roundf__asm: true,
roundf__sig: 'ff',
roundf__deps: ['llvm_copysign_f64'],
roundf: function(d) {
d = +d;
return d >= +0 ? +Math_floor(d + +0.5) : +Math_ceil(d - +0.5);
return +Math_trunc(d + +_llvm_copysign_f64(+0.49999999999999994, d));
},

llvm_round_f64__asm: true,
llvm_round_f64__sig: 'dd',
llvm_round_f64__deps: ['llvm_copysign_f64'],
llvm_round_f64: function(d) {
d = +d;
return d >= +0 ? +Math_floor(d + +0.5) : +Math_ceil(d - +0.5);
return +Math_trunc(d + +_llvm_copysign_f64(+0.49999999999999994, d));
},

llvm_round_f32__asm: true,
llvm_round_f32__sig: 'ff',
llvm_round_f32__deps: ['llvm_copysign_f64'],
llvm_round_f32: function(f) {
f = +f;
return f >= +0 ? +Math_floor(f + +0.5) : +Math_ceil(f - +0.5); // TODO: use fround?
return +Math_trunc(f + +_llvm_copysign_f64(+0.49999999999999994, f));
},

rintf__asm: true,
Expand Down