diff --git a/pkgs/typed-racket-pkgs/typed-racket-lib/typed-racket/optimizer/fixnum.rkt b/pkgs/typed-racket-pkgs/typed-racket-lib/typed-racket/optimizer/fixnum.rkt index 45393036..b01e837a 100644 --- a/pkgs/typed-racket-pkgs/typed-racket-lib/typed-racket/optimizer/fixnum.rkt +++ b/pkgs/typed-racket-pkgs/typed-racket-lib/typed-racket/optimizer/fixnum.rkt @@ -62,6 +62,8 @@ (define-bitwise-syntax-class (and ior xor not)) (define-unsafe-syntax-class exact->inexact^ (exact->inexact) unsafe-fx->fl) +(define-unsafe-syntax-class fx->fl^ (fx->fl) unsafe-fx->fl) +(define-merged-syntax-class fixnum-coercion-op (exact->inexact^ fx->fl^)) (define-literal-syntax-class add1) (define-literal-syntax-class sub1) (define-literal-syntax-class zero?) @@ -158,7 +160,7 @@ #:do [(log-fx-opt "unary fixnum")] #:with opt #'(op.unsafe 0 f.opt)) - (pattern (op:exact->inexact^ n:fixnum-expr) + (pattern (op:fixnum-coercion-op n:fixnum-expr) #:do [(log-fx-opt "fixnum to float")] #:with opt #'(op.unsafe n.opt)) diff --git a/pkgs/typed-racket-pkgs/typed-racket-test/tests/typed-racket/optimizer/tests/fx2fl.rkt b/pkgs/typed-racket-pkgs/typed-racket-test/tests/typed-racket/optimizer/tests/fx2fl.rkt new file mode 100644 index 00000000..de8e650a --- /dev/null +++ b/pkgs/typed-racket-pkgs/typed-racket-test/tests/typed-racket/optimizer/tests/fx2fl.rkt @@ -0,0 +1,38 @@ +#;#; +#<fl i))) -- binary float +TR opt: fx2fl.rkt 10:25 (/ 1.0 (fx->fl i)) -- binary float +TR opt: fx2fl.rkt 10:32 (fx->fl i) -- fixnum to float +TR opt: fx2fl.rkt 12:9 (for/fold: : Float ((s : Float 0.0)) ((i : Fixnum (in-range 1 100000001))) (+ s (/ 1.0 (fx->fl (* 1024 i))))) -- dead else branch +TR opt: fx2fl.rkt 12:9 (for/fold: : Float ((s : Float 0.0)) ((i : Fixnum (in-range 1 100000001))) (+ s (/ 1.0 (fx->fl (* 1024 i))))) -- dead else branch +TR opt: fx2fl.rkt 12:9 (for/fold: : Float ((s : Float 0.0)) ((i : Fixnum (in-range 1 100000001))) (+ s (/ 1.0 (fx->fl (* 1024 i))))) -- dead else branch +TR opt: fx2fl.rkt 13:22 s -- dead else branch +TR opt: fx2fl.rkt 13:22 s -- dead else branch +TR opt: fx2fl.rkt 15:20 (+ s (/ 1.0 (fx->fl (* 1024 i)))) -- binary float +TR opt: fx2fl.rkt 15:25 (/ 1.0 (fx->fl (* 1024 i))) -- binary float +TR opt: fx2fl.rkt 7:9 (for/fold: : Float ((s : Float 0.0)) ((i : Fixnum (in-range 1 100000001))) (+ s (/ 1.0 (fx->fl i)))) -- dead else branch +TR opt: fx2fl.rkt 7:9 (for/fold: : Float ((s : Float 0.0)) ((i : Fixnum (in-range 1 100000001))) (+ s (/ 1.0 (fx->fl i)))) -- dead else branch +TR opt: fx2fl.rkt 7:9 (for/fold: : Float ((s : Float 0.0)) ((i : Fixnum (in-range 1 100000001))) (+ s (/ 1.0 (fx->fl i)))) -- dead else branch +TR opt: fx2fl.rkt 8:22 s -- dead else branch +TR opt: fx2fl.rkt 8:22 s -- dead else branch +END +"" +#lang typed/racket +#:optimize +#reader tests/typed-racket/optimizer/reset-port + +(require racket/fixnum) + +;; from http://stackoverflow.com/questions/22868795/how-to-optimize-this-piece-of-racket-code + +(define: (test) : Float + (for/fold: : Float + ([s : Float 0.0]) + ([i : Fixnum (in-range 1 100000001)]) + (+ s (/ 1.0 (fx->fl i))))) ; should be optimized +(define: (test2) : Float + (for/fold: : Float + ([s : Float 0.0]) + ([i : Fixnum (in-range 1 100000001)]) + (+ s (/ 1.0 (fx->fl (* 1024 i)))))) ; should not