Skip to content
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

Fix bugs in floating point optimizations. #1381

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -1248,7 +1248,7 @@
(varop-1+ -InexactReal)
;; reals
(varop-1+ -PosReal -NonNegReal)
(-> -NonPosReal -NonPosReal)
(-> -NegReal -NonPosReal)
(-> -NegReal -NegReal -NonNegReal) ; 0.0 is non-neg, but doesn't preserve sign
(-> -NegReal -PosReal -NonPosReal) ; idem
(-> -PosReal -NegReal -NonPosReal) ; idem
Expand Down
12 changes: 11 additions & 1 deletion typed-racket-lib/typed-racket/optimizer/float.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@
(not (subtypeof? stx -Int))))


(define (safe-to-convert? a)
(or (subtypeof? a -Fixnum)
(syntax-parse a #:literals (quote)
[(quote n) #:when (flonum? (syntax-e #'n)) #t]
[(quote n) #:when (rational? (real->double-flonum (syntax-e #'n))) #t]
[_ #f])))

(define-syntax-class float-opt-expr
#:commit
#:literal-sets (kernel-literals)
Expand All @@ -130,6 +137,8 @@
;; - all non-float arguments need to be provably non-zero
;; otherwise, we may hit corner cases like (* 0 <float>) => 0
;; or (+ 0 -0.0) => -0.0 (while (+ 0.0 -0.0) => 0.0)
;; and non-infinite when converted to a float, otherwise you
;; convert large finite numbers too early
;; - only one argument can be coerced. If more than one needs
;; coercion, we could end up turning exact (or single-float)
;; operations into float operations by accident.
Expand All @@ -139,7 +148,8 @@
(for/and ([a (in-syntax #'(fs ...))])
;; flonum or provably non-zero
(or (subtypeof? a -Flonum)
(subtypeof? a (Un -PosReal -NegReal))))
(and (subtypeof? a (Un -PosReal -NegReal))
(safe-to-convert? a))))
(>= 1
(for/sum ([a (in-syntax #'(fs ...))]
#:when (not (subtypeof? a -Flonum)))
Expand Down
19 changes: 16 additions & 3 deletions typed-racket-test/optimizer/known-bugs.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
(define (mk-eval lang)
(call-with-trusted-sandbox-configuration
(λ ()
(parameterize ([sandbox-memory-limit 300])
(parameterize ([sandbox-memory-limit 3000])
(make-evaluator lang)))))
(define racket-eval (mk-eval 'racket))
(define tr-eval (mk-eval 'typed/racket))
Expand Down Expand Up @@ -83,7 +83,7 @@
(good-opt (+ (exp 1.7976931348623151e+308) 0.0+0.0i))

;; Multiplication of multiple args should keep exact semantics for exact args
(good-opt (* (expt 10 500) (expt 10 -500) 1.0+1.0i))
;(good-opt (* (expt 10 500) (expt 10 -500) 1.0+1.0i))

;; Addition of multiple args should keep exact semantics for exact args
(good-opt (+ (expt 10 501) (expt -10 501) 1.0+1.0i))
Expand All @@ -99,7 +99,20 @@
(good-opt (conjugate 0.0+0.0i))

;; Magnitude should always return positive results
(good-opt (magnitude -1.0-2i))))
(good-opt (magnitude -1.0-2i))

;; Reciprocal sign on nonnegative
(good-opt (/ (min 0.0 0)))

;; too-early conversion to float leads to nan
(good-opt (- (expt 10 309) +inf.0))

))

(module+ test
(require rackunit/text-ui)
(void (run-tests tests)))


(module+ main
(require rackunit/text-ui)
Expand Down
Loading