From a3a3bd1e2e56bab8167b073c0a2e62e52954293e Mon Sep 17 00:00:00 2001 From: Vincent St-Amour Date: Fri, 2 Jul 2010 20:56:26 -0400 Subject: [PATCH] Added coercion of fixnums to floats to the optimizer. original commit: 556734a223f4193c1c23ded76f204f1e6bb25295 --- .../optimizer/generic/float-promotion.rkt | 3 +- collects/typed-scheme/private/optimize.rkt | 30 +++++++++---------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/collects/tests/typed-scheme/optimizer/generic/float-promotion.rkt b/collects/tests/typed-scheme/optimizer/generic/float-promotion.rkt index 3df6f684..1fc32fa9 100644 --- a/collects/tests/typed-scheme/optimizer/generic/float-promotion.rkt +++ b/collects/tests/typed-scheme/optimizer/generic/float-promotion.rkt @@ -1,3 +1,4 @@ (module float-promotion typed/scheme #:optimize (require racket/unsafe/ops racket/flonum) - (+ 1 2.0)) + (+ 1 2.0) + (+ (expt 100 100) 2.0)) diff --git a/collects/typed-scheme/private/optimize.rkt b/collects/typed-scheme/private/optimize.rkt index 1feeb4b7..269e42a9 100644 --- a/collects/typed-scheme/private/optimize.rkt +++ b/collects/typed-scheme/private/optimize.rkt @@ -11,34 +11,23 @@ (match (type-of s) [(tc-result1: (== t (lambda (x y) (subtype y x)))) #t] [_ #f])) + (define-syntax-class float-opt-expr (pattern e:opt-expr #:when (subtypeof #'e -Flonum) #:with opt #'e.opt)) - (define-syntax-class int-opt-expr (pattern e:opt-expr #:when (subtypeof #'e -Integer) #:with opt #'e.opt)) -(define-syntax-class fixnum-opt-expr - (pattern e:opt-expr - #:when (subtypeof #'e -Fixnum) - #:with opt #'e.opt)) -(define-syntax-class nonzero-fixnum-opt-expr - (pattern e:opt-expr - #:when (match (type-of #'e) - [(tc-result1: (== -PositiveFixnum type-equal?)) #t] - [(tc-result1: (== -NegativeFixnum type-equal?)) #t] - [_ #f]) - #:with opt #'e.opt)) - - ;; if the result of an operation is of type float, its non float arguments ;; can be promoted, and we can use unsafe float operations ;; note: none of the unary operations have types where non-float arguments ;; can result in float (as opposed to real) results (define-syntax-class float-arg-expr + (pattern e:fixnum-opt-expr + #:with opt #'(unsafe-fx->fl e.opt)) (pattern e:int-opt-expr #:with opt #'(->fl e.opt)) (pattern e:float-opt-expr @@ -66,6 +55,18 @@ #:with unsafe (dict-ref tbl #'i))) +(define-syntax-class fixnum-opt-expr + (pattern e:opt-expr + #:when (subtypeof #'e -Fixnum) + #:with opt #'e.opt)) +(define-syntax-class nonzero-fixnum-opt-expr + (pattern e:opt-expr + #:when (match (type-of #'e) + [(tc-result1: (== -PositiveFixnum type-equal?)) #t] + [(tc-result1: (== -NegativeFixnum type-equal?)) #t] + [_ #f]) + #:with opt #'e.opt)) + (define (mk-fixnum-tbl generic) (mk-unsafe-tbl generic "fx~a" "unsafe-fx~a")) @@ -192,7 +193,6 @@ #:with opt (begin (log-optimization "fixnum to float" #'op) #'(unsafe-fx->fl n.opt))) - ;; we can optimize exact->inexact if we know we're giving it an Integer (pattern (#%plain-app (~and op (~literal exact->inexact)) n:int-opt-expr) #:with opt