From cc1dd45568ca36ef7ace7a996758944f2f90f514 Mon Sep 17 00:00:00 2001 From: Vincent St-Amour Date: Sat, 13 Oct 2012 17:49:48 -0400 Subject: [PATCH] More float conversion optimizations. original commit: 149d8535eb2988fab0bed739d375874f172db7c8 --- .../optimizer/tests/exact-inexact.rkt | 21 +++++++++++++++++- collects/typed-racket/optimizer/float.rkt | 22 +++++++++++++++++-- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/collects/tests/typed-racket/optimizer/tests/exact-inexact.rkt b/collects/tests/typed-racket/optimizer/tests/exact-inexact.rkt index 245c7255..5fc0cb2a 100644 --- a/collects/tests/typed-racket/optimizer/tests/exact-inexact.rkt +++ b/collects/tests/typed-racket/optimizer/tests/exact-inexact.rkt @@ -1,10 +1,29 @@ #; ( -TR opt: exact-inexact.rkt 10:0 (exact->inexact (expt 10 100)) -- int to float +TR opt: exact-inexact.rkt 22:0 (exact->inexact (expt 10 100)) -- int to float +TR opt: exact-inexact.rkt 23:7 (exact->inexact (expt 2.3 3.2)) -- float to float +TR opt: exact-inexact.rkt 23:0 (round (exact->inexact (expt 2.3 3.2))) -- unary float +TR opt: exact-inexact.rkt 24:0 (real->double-flonum (expt 10 100)) -- int to float +TR opt: exact-inexact.rkt 25:7 (real->double-flonum (expt 2.3 3.2)) -- float to float +TR opt: exact-inexact.rkt 25:0 (round (real->double-flonum (expt 2.3 3.2))) -- unary float +TR opt: exact-inexact.rkt 28:0 (exact->inexact (expt 1.0f0 2.0f0)) -- single-float to single-float +TR opt: exact-inexact.rkt 29:0 (real->single-flonum (expt 1.0f0 2.0f0)) -- single-float to single-float 1e+100 +14.0 +1e+100 +14.0 +1.0f0 +1.0f0 ) #lang typed/scheme #:optimize (exact->inexact (expt 10 100)) ; must not be a fixnum +(round (exact->inexact (expt 2.3 3.2))) ; already a float +(real->double-flonum (expt 10 100)) ; must not be a fixnum +(round (real->double-flonum (expt 2.3 3.2))) ; already a float + +;; single floats +(exact->inexact (expt 1.0f0 2.0f0)) +(real->single-flonum (expt 1.0f0 2.0f0)) diff --git a/collects/typed-racket/optimizer/float.rkt b/collects/typed-racket/optimizer/float.rkt index a46b0cc6..78efa0b2 100644 --- a/collects/typed-racket/optimizer/float.rkt +++ b/collects/typed-racket/optimizer/float.rkt @@ -40,6 +40,11 @@ (pattern e:expr #:when (subtypeof? #'e -Flonum) #:with opt ((optimize) #'e))) +(define-syntax-class single-float-expr + #:commit + (pattern e:expr + #:when (subtypeof? #'e -SingleFlonum) + #:with opt ((optimize) #'e))) (define-syntax-class int-expr #:commit (pattern e:expr @@ -188,17 +193,30 @@ #'(let ([tmp f.opt]) (unsafe-fl* tmp tmp)))) ;; we can optimize exact->inexact if we know we're giving it an Integer - (pattern (#%plain-app (~and op (~literal exact->inexact)) n:int-expr) + (pattern (#%plain-app (~and op (~or (~literal exact->inexact) + (~literal real->double-flonum))) + n:int-expr) #:with opt (begin (log-optimization "int to float" float-opt-msg this-syntax) (add-disappeared-use #'op) #'(->fl n.opt))) ;; we can get rid of it altogether if we're giving it a float - (pattern (#%plain-app (~and op (~literal exact->inexact)) f:float-expr) + (pattern (#%plain-app (~and op (~or (~literal exact->inexact) + (~literal real->double-flonum))) + f:float-expr) #:with opt (begin (log-optimization "float to float" float-opt-msg this-syntax) (add-disappeared-use #'op) #'f.opt)) + ;; same for single-flonums + (pattern (#%plain-app (~and op (~or (~literal exact->inexact) + (~literal real->single-flonum))) + f:single-float-expr) + #:with opt + (begin (log-optimization "single-float to single-float" + float-opt-msg this-syntax) + (add-disappeared-use #'op) + #'f.opt)) (pattern (#%plain-app (~and op (~literal zero?)) f:float-expr) #:with opt