Bug 185115 - Math.round() produces wrong result for value prior to 0.5
Summary: Math.round() produces wrong result for value prior to 0.5
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: JavaScriptCore (show other bugs)
Version: WebKit Nightly Build
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Ross Kirsling
URL: http://ridiculousfish.com/blog/posts/...
Keywords: InRadar
Depends on:
Blocks:
 
Reported: 2018-04-28 16:12 PDT by corydoras
Modified: 2019-09-06 15:51 PDT (History)
11 users (show)

See Also:


Attachments
Patch (2.86 KB, patch)
2018-04-28 16:20 PDT, corydoras
no flags Details | Formatted Diff | Diff
Patch (3.91 KB, patch)
2018-04-28 16:33 PDT, corydoras
no flags Details | Formatted Diff | Diff
Patch (10.70 KB, patch)
2019-09-05 18:31 PDT, Ross Kirsling
no flags Details | Formatted Diff | Diff
Patch for landing (11.41 KB, patch)
2019-09-06 14:58 PDT, Ross Kirsling
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description corydoras 2018-04-28 16:12:58 PDT
Math.round(0.499999999999999944) produces 1.0; it should produce 0.0. Likewise for the negated input.

Note 0.499999999999999944 is the largest representable double less than 0.5.
Comment 1 corydoras 2018-04-28 16:20:03 PDT
Created attachment 339079 [details]
Patch
Comment 2 corydoras 2018-04-28 16:33:18 PDT
Created attachment 339080 [details]
Patch
Comment 3 Saam Barati 2018-04-28 17:02:40 PDT
Comment on attachment 339080 [details]
Patch

This doesn't address the bug in our JITs as well. Check out DFG/FTL ArithRound
Comment 4 Ross Kirsling 2019-09-05 18:31:25 PDT
Created attachment 378145 [details]
Patch
Comment 5 Ross Kirsling 2019-09-05 18:54:20 PDT
Comment on attachment 378145 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=378145&action=review

> Source/JavaScriptCore/jit/ThunkGenerators.cpp:1009
> +        jit.compareDouble(MacroAssembler::DoubleGreaterThan, SpecializedThunkJIT::fpRegT2, SpecializedThunkJIT::fpRegT0, SpecializedThunkJIT::regT0);
> +        jit.convertInt32ToDouble(SpecializedThunkJIT::regT0, SpecializedThunkJIT::fpRegT2);
> +        jit.subDouble(SpecializedThunkJIT::fpRegT1, SpecializedThunkJIT::fpRegT2, SpecializedThunkJIT::fpRegT0);

Note: I'm doing this instead of branching and subtracting a constant 1, because even in the non-subtracting case we would still need to move the result value to fpRegT0. I think this is probably fine (especially since neither branch is more likely than the other) but if I'm overlooking an obvious improvement please let me know. :)
Comment 6 Saam Barati 2019-09-06 14:27:55 PDT
Comment on attachment 378145 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=378145&action=review

r=me

> Source/JavaScriptCore/ChangeLog:8
> +        Our Math.round implementation goes in the wrong direction for double values like 0.49999999999999994.

🤔

Gotta keep developers on their toes

> Source/JavaScriptCore/ChangeLog:9
> +        This requires just a subtle adjustment for three of our four versions; only baseline JIT needed a full rewrite. 

why didn't it work? Would be good to say why it didn't work too. Writing out why it didn't work would be helpful for folks referring back to this.

> Source/JavaScriptCore/ftl/FTLLowerDFGToB3.cpp:2955
> +                LValue scratch = m_out.doubleSub(integerValue, m_out.constDouble(0.5));

style nit: we can probably pick a better name than scratch

>> Source/JavaScriptCore/jit/ThunkGenerators.cpp:1009
>> +        jit.subDouble(SpecializedThunkJIT::fpRegT1, SpecializedThunkJIT::fpRegT2, SpecializedThunkJIT::fpRegT0);
> 
> Note: I'm doing this instead of branching and subtracting a constant 1, because even in the non-subtracting case we would still need to move the result value to fpRegT0. I think this is probably fine (especially since neither branch is more likely than the other) but if I'm overlooking an obvious improvement please let me know. :)

my blind guess is the branch might be faster than convertInt32ToDouble, but I haven't benchmarked it.
Comment 7 Ross Kirsling 2019-09-06 14:58:58 PDT
Created attachment 378241 [details]
Patch for landing
Comment 8 WebKit Commit Bot 2019-09-06 15:50:03 PDT
Comment on attachment 378241 [details]
Patch for landing

Clearing flags on attachment: 378241

Committed r249597: <https://trac.webkit.org/changeset/249597>
Comment 9 WebKit Commit Bot 2019-09-06 15:50:04 PDT
All reviewed patches have been landed.  Closing bug.
Comment 10 Radar WebKit Bug Importer 2019-09-06 15:51:17 PDT
<rdar://problem/55131492>