Marc:
The compiler can expand (sqrt x) as
(define (sqrt x) (cond ((##fixnum? x) (if (and (##fixnum.<= 0 x) (or (##not (##fixnum? 4294967296)) ; 32-bit fixnums (##fixnum.<= x 4503599627370496))) (##flonum->fixnum (##flonum.sqrt (##flonum.<-fixnum x))) (##sqrt x))) ((and (##flonum? x) (##flonum.<= 0. x)) (##flonum.sqrt x)) (else (##sqrt x))))
It's very worthwhile for some programs (and some micro-benchmarks ;-).
Brad
Afficher les réponses par date
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
On 19-May-07, at 11:28 PM, Bradley Lucier wrote:
Marc:
The compiler can expand (sqrt x) as
(define (sqrt x) (cond ((##fixnum? x) (if (and (##fixnum.<= 0 x) (or (##not (##fixnum? 4294967296)) ; 32-bit fixnums (##fixnum.<= x 4503599627370496))) (##flonum->fixnum (##flonum.sqrt (##flonum.<-fixnum x))) (##sqrt x))) ((and (##flonum? x) (##flonum.<= 0. x)) (##flonum.sqrt x)) (else (##sqrt x))))
It's very worthwhile for some programs (and some micro-benchmarks ;-).
This definition is incorrect because:
(sqrt 17)
4
You need something like this:
(define (sqrt x) (cond ((##fixnum? x) (if (and (##not (##fixnum.negative? x)) (or (##not (##fixnum? 4294967296)) ;; 32-bit fixnums (##fixnum.<= x 4503599627370496))) (let* ((f (##flonum.sqrt (##flonum.<-fixnum x))) (i (##flonum->fixnum s))) (if (##fixnum.= x (##fixnum.* i i)) i (##sqrt x))) (##sqrt x))) ((and (##flonum? x) (##not (##flonum.negative? x))) (##flonum.sqrt x)) (else (##sqrt x))))
But that's a lot of code! Can you give good examples of where this pays off?
Marc
On May 20, 2007, at 11:24 AM, Marc Feeley wrote:
This definition is incorrect because:
(sqrt 17)
4
Sorry, what I needed was an expansion for integer-sqrt, which would be something like
(define (integer-sqrt x) (declare (standard-bindings)(extended-bindings)(block)(fixnum) (not safe)) (if (and (##fixnum? x) (##fixnum.<= 0 x) (or (##not (##fixnum? 4294967296)) ; 32-bit fixnums (##fixnum.<= x 4503599627370496))) (##flonum->fixnum (##flonum.sqrt (##flonum.<-fixnum x))) (##sqrt x)))
Brad