Fix magnitude opt to not overflow.

May have been found with random testing, a while ago.
This commit is contained in:
Vincent St-Amour 2015-11-12 15:36:44 -06:00
parent 53e501bb8b
commit b88b4a8829
2 changed files with 6 additions and 4 deletions

View File

@ -565,9 +565,11 @@
[(#%plain-app op:magnitude^ c:unboxed-float-complex-opt-expr)
(log-unboxing-opt "unboxed unary float complex")
#`(let*-values (c.bindings ...)
(unsafe-flsqrt
(unsafe-fl+ (unsafe-fl* c.real-binding c.real-binding)
(unsafe-fl* c.imag-binding c.imag-binding))))])))
;; reuses the algorithm used by the Racket runtime
(let-values ([(q) (unsafe-fl/ c.real-binding c.imag-binding)])
(unsafe-fl* c.imag-binding
(unsafe-flsqrt (unsafe-fl+ 1.0
(unsafe-fl* q q))))))])))
(pattern (#%plain-app op:float-complex-op e:expr ...)

View File

@ -85,7 +85,7 @@
(good-opt (+ (expt 10 501) (expt -10 501) 1.0+1.0i))
;; Magnitude should not overflow unless necessary
(bad-opt (magnitude 3.0e300+4.0e300i))
(good-opt (magnitude 3.0e300+4.0e300i))
;; Negation should correctly compute sign of 0.0
(good-opt (- 0.0+0.0i))