Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(1847)

Issue 151750043: code review 151750043: math: avoid assumption of denormalized math mode in Sincos (Closed)

Can't Edit
Can't Publish+Mail
Start Review
Created:
9 years, 7 months ago by rsc
Modified:
9 years, 7 months ago
Reviewers:
r, mtj1, gmk, josharian
CC:
golang-codereviews, josharian, iant
Visibility:
Public.

Description

math: avoid assumption of denormalized math mode in Sincos The extra-clever code in Sincos is trying to do if v&2 == 0 { mask = 0xffffffffffffffff } else { mask = 0 } It does this by turning v&2 into a float64 X0 and then using MOVSD $0.0, X3 CMPSD X0, X3, 0 That CMPSD is defined to behave like: if X0 == X3 { X3 = 0xffffffffffffffff } else { X3 = 0 } which gives the desired mask in X3. The goal in using the CMPSD was to avoid a conditional branch. This code fails when called from a PortAudio callback. In particular, the failure behavior is exactly as if the CMPSD always chose the 'true' execution. Notice that the comparison X0 == X3 is comparing as floating point values the 64-bit pattern v&2 and the actual floating point value zero. The only possible values for v&2 are 0x0000000000000000 (floating point zero) and 0x0000000000000002 (floating point 1e-323, a denormal). If they are both comparing equal to zero, I conclude that in a PortAudio callback (whatever that means), the processor is running in "denormals are zero" mode. I confirmed this by placing the processor into that mode and running the test case in the bug; it produces the incorrect output reported in the bug. In general, if a Go program changes the floating point math modes to something other than what Go expects, the math library is not going to work exactly as intended, so we might be justified in not fixing this at all. However, it seems reasonable that the client code might have expected "denormals are zero" mode to only affect actual processing of denormals. This code has produced what is in effect a gratuitous denormal by being extra clever. There is nothing about the computation being requested that fundamentally requires a denormal. It is also easy to do this computation in integer math instead: mask = ((v&2)>>1)-1 Do that. For the record, the other math tests that fail if you put the processor in "denormals are zero" mode are the tests for Frexp, Ilogb, Ldexp, Logb, Log2, and FloatMinMax, but all fail processing denormal inputs. Sincos was the only function for which that mode causes incorrect behavior on non-denormal inputs. The existing tests check that the new assembly is correct. There is no test for behavior in "denormals are zero" mode, because I don't want to add assembly to change that. Fixes issue 8623.

Patch Set 1 #

Patch Set 2 : diff -r d62cdebe7513197cbf70b87bbade7154d4359a55 https://code.google.com/p/go/ #

Patch Set 3 : diff -r 0f8fd4e12ee57e0a73dfecba332bd94a33bf69e1 https://code.google.com/p/go/ #

Total comments: 1

Patch Set 4 : diff -r 9dcfa0f1bee557143b1d29013d2011822be056bf https://code.google.com/p/go/ #

Unified diffs Side-by-side diffs Delta from patch set Stats (+5 lines, -8 lines) Patch
M src/math/sincos_amd64.s View 1 2 3 2 chunks +5 lines, -8 lines 0 comments Download

Messages

Total messages: 6
rsc
Hello golang-codereviews@googlegroups.com (cc: iant, josharian, r), I'd like you to review this change to https://code.google.com/p/go/
9 years, 7 months ago (2014-09-26 21:00:38 UTC) #1
josharian
LGTM Thanks for the write-up. I had no idea that a "denormals are zero" mode ...
9 years, 7 months ago (2014-09-26 21:11:59 UTC) #2
rsc
*** Submitted as https://code.google.com/p/go/source/detail?r=ba0c3d698c40 *** math: avoid assumption of denormalized math mode in Sincos The ...
9 years, 7 months ago (2014-09-26 21:13:28 UTC) #3
r
Nice analysis. I hate denormalized floats. That's a personal point but depending on them working ...
9 years, 7 months ago (2014-09-26 21:43:24 UTC) #4
mtj1
Brilliant diagnostic analysis Russ! On Fri, Sep 26, 2014 at 5:43 PM, <r@golang.org> wrote: > ...
9 years, 7 months ago (2014-09-26 21:55:24 UTC) #5
gmk
9 years, 7 months ago (2014-09-27 06:34:05 UTC) #6
Message was sent while issue was closed.
I attest that this fixes the issue.  Nice one, Russ!
Sign in to reply to this message.

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b