RESOLVED FIXED 185115
Math.round() produces wrong result for value prior to 0.5
https://bugs.webkit.org/show_bug.cgi?id=185115
Summary Math.round() produces wrong result for value prior to 0.5
corydoras
Reported 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.
Attachments
Patch (2.86 KB, patch)
2018-04-28 16:20 PDT, corydoras
no flags
Patch (3.91 KB, patch)
2018-04-28 16:33 PDT, corydoras
no flags
Patch (10.70 KB, patch)
2019-09-05 18:31 PDT, Ross Kirsling
no flags
Patch for landing (11.41 KB, patch)
2019-09-06 14:58 PDT, Ross Kirsling
no flags
corydoras
Comment 1 2018-04-28 16:20:03 PDT
corydoras
Comment 2 2018-04-28 16:33:18 PDT
Saam Barati
Comment 3 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
Ross Kirsling
Comment 4 2019-09-05 18:31:25 PDT
Ross Kirsling
Comment 5 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. :)
Saam Barati
Comment 6 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.
Ross Kirsling
Comment 7 2019-09-06 14:58:58 PDT
Created attachment 378241 [details] Patch for landing
WebKit Commit Bot
Comment 8 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>
WebKit Commit Bot
Comment 9 2019-09-06 15:50:04 PDT
All reviewed patches have been landed. Closing bug.
Radar WebKit Bug Importer
Comment 10 2019-09-06 15:51:17 PDT
Note You need to log in before you can comment on or make changes to this bug.