[gambit-list] Compiler expansion for sqrt
Marc Feeley
feeley at iro.umontreal.ca
Sun May 20 11:24:37 EDT 2007
-----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
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (Darwin)
iD8DBQFGUGg1//V9Zc2T/v4RAtl7AJ9IhnS5RTtU9dXUbNGn32ye8AUlEwCghs6M
DtT/ryJpErNWxIdW70PoRrc=
=eHcw
-----END PGP SIGNATURE-----
More information about the Gambit-list
mailing list