-
-#!+x86 ;; These are needed for use by byte-compiled files.
-(progn
- (defun %sin (x)
- (declare (double-float x)
- (values double-float))
- (%sin x))
- (defun %sin-quick (x)
- (declare (double-float x)
- (values double-float))
- (%sin-quick x))
- (defun %cos (x)
- (declare (double-float x)
- (values double-float))
- (%cos x))
- (defun %cos-quick (x)
- (declare (double-float x)
- (values double-float))
- (%cos-quick x))
- (defun %tan (x)
- (declare (double-float x)
- (values double-float))
- (%tan x))
- (defun %tan-quick (x)
- (declare (double-float x)
- (values double-float))
- (%tan-quick x))
- (defun %atan (x)
- (declare (double-float x)
- (values double-float))
- (%atan x))
- (defun %atan2 (x y)
- (declare (double-float x y)
- (values double-float))
- (%atan2 x y))
- (defun %exp (x)
- (declare (double-float x)
- (values double-float))
- (%exp x))
- (defun %log (x)
- (declare (double-float x)
- (values double-float))
- (%log x))
- (defun %log10 (x)
- (declare (double-float x)
- (values double-float))
- (%log10 x))
- #+nil ;; notyet
- (defun %pow (x y)
- (declare (type (double-float 0d0) x)
- (double-float y)
- (values (double-float 0d0)))
- (%pow x y))
- (defun %sqrt (x)
- (declare (double-float x)
- (values double-float))
- (%sqrt x))
- (defun %scalbn (f ex)
- (declare (double-float f)
- (type (signed-byte 32) ex)
- (values double-float))
- (%scalbn f ex))
- (defun %scalb (f ex)
- (declare (double-float f ex)
- (values double-float))
- (%scalb f ex))
- (defun %logb (x)
- (declare (double-float x)
- (values double-float))
- (%logb x))
- (defun %log1p (x)
- (declare (double-float x)
- (values double-float))
- (%log1p x))
- ) ; progn
- ;; 0 - not an integer
- ;; 1 - an odd int
- ;; 2 - an even int
- (isint (ihi lo)
- (declare (type (unsigned-byte 31) ihi)
- (type (unsigned-byte 32) lo)
- (optimize (speed 3) (safety 0)))
- (let ((isint 0))
- (declare (type fixnum isint))
- (cond ((>= ihi #x43400000) ; exponent >= 53
- (setq isint 2))
- ((>= ihi #x3ff00000)
- (let ((k (- (ash ihi -20) #x3ff))) ; exponent
- (declare (type (mod 53) k))
- (cond ((> k 20)
- (let* ((shift (- 52 k))
- (j (logand (ash lo (- shift))))
- (j2 (ash j shift)))
- (declare (type (mod 32) shift)
- (type (unsigned-byte 32) j j2))
- (when (= j2 lo)
- (setq isint (- 2 (logand j 1))))))
- ((= lo 0)
- (let* ((shift (- 20 k))
- (j (ash ihi (- shift)))
- (j2 (ash j shift)))
- (declare (type (mod 32) shift)
- (type (unsigned-byte 31) j j2))
- (when (= j2 ihi)
- (setq isint (- 2 (logand j 1))))))))))
- isint))
- (real-expt (x y rtype)
- (let ((x (coerce x 'double-float))
- (y (coerce y 'double-float)))
- (declare (double-float x y))
- (let* ((x-hi (sb!kernel:double-float-high-bits x))
- (x-lo (sb!kernel:double-float-low-bits x))
- (x-ihi (logand x-hi #x7fffffff))
- (y-hi (sb!kernel:double-float-high-bits y))
- (y-lo (sb!kernel:double-float-low-bits y))
- (y-ihi (logand y-hi #x7fffffff)))
- (declare (type (signed-byte 32) x-hi y-hi)
- (type (unsigned-byte 31) x-ihi y-ihi)
- (type (unsigned-byte 32) x-lo y-lo))
- ;; y==zero: x**0 = 1
- (when (zerop (logior y-ihi y-lo))
- (return-from real-expt (coerce 1d0 rtype)))
- ;; +-NaN return x+y
- (when (or (> x-ihi #x7ff00000)
- (and (= x-ihi #x7ff00000) (/= x-lo 0))
- (> y-ihi #x7ff00000)
- (and (= y-ihi #x7ff00000) (/= y-lo 0)))
- (return-from real-expt (coerce (+ x y) rtype)))
- (let ((yisint (if (< x-hi 0) (isint y-ihi y-lo) 0)))
- (declare (type fixnum yisint))
- ;; special value of y
- (when (and (zerop y-lo) (= y-ihi #x7ff00000))
- ;; y is +-inf
- (return-from real-expt
- (cond ((and (= x-ihi #x3ff00000) (zerop x-lo))
- ;; +-1**inf is NaN
- (coerce (- y y) rtype))
- ((>= x-ihi #x3ff00000)
- ;; (|x|>1)**+-inf = inf,0
- (if (>= y-hi 0)
- (coerce y rtype)
- (coerce 0 rtype)))
- (t
- ;; (|x|<1)**-,+inf = inf,0
- (if (< y-hi 0)
- (coerce (- y) rtype)
- (coerce 0 rtype))))))
+ ;; 0 - not an integer
+ ;; 1 - an odd int
+ ;; 2 - an even int
+ (isint (ihi lo)
+ (declare (type (unsigned-byte 31) ihi)
+ (type (unsigned-byte 32) lo)
+ (optimize (speed 3) (safety 0)))
+ (let ((isint 0))
+ (declare (type fixnum isint))
+ (cond ((>= ihi #x43400000) ; exponent >= 53
+ (setq isint 2))
+ ((>= ihi #x3ff00000)
+ (let ((k (- (ash ihi -20) #x3ff))) ; exponent
+ (declare (type (mod 53) k))
+ (cond ((> k 20)
+ (let* ((shift (- 52 k))
+ (j (logand (ash lo (- shift))))
+ (j2 (ash j shift)))
+ (declare (type (mod 32) shift)
+ (type (unsigned-byte 32) j j2))
+ (when (= j2 lo)
+ (setq isint (- 2 (logand j 1))))))
+ ((= lo 0)
+ (let* ((shift (- 20 k))
+ (j (ash ihi (- shift)))
+ (j2 (ash j shift)))
+ (declare (type (mod 32) shift)
+ (type (unsigned-byte 31) j j2))
+ (when (= j2 ihi)
+ (setq isint (- 2 (logand j 1))))))))))
+ isint))
+ (real-expt (x y rtype)
+ (let ((x (coerce x 'double-float))
+ (y (coerce y 'double-float)))
+ (declare (double-float x y))
+ (let* ((x-hi (sb!kernel:double-float-high-bits x))
+ (x-lo (sb!kernel:double-float-low-bits x))
+ (x-ihi (logand x-hi #x7fffffff))
+ (y-hi (sb!kernel:double-float-high-bits y))
+ (y-lo (sb!kernel:double-float-low-bits y))
+ (y-ihi (logand y-hi #x7fffffff)))
+ (declare (type (signed-byte 32) x-hi y-hi)
+ (type (unsigned-byte 31) x-ihi y-ihi)
+ (type (unsigned-byte 32) x-lo y-lo))
+ ;; y==zero: x**0 = 1
+ (when (zerop (logior y-ihi y-lo))
+ (return-from real-expt (coerce 1d0 rtype)))
+ ;; +-NaN return x+y
+ (when (or (> x-ihi #x7ff00000)
+ (and (= x-ihi #x7ff00000) (/= x-lo 0))
+ (> y-ihi #x7ff00000)
+ (and (= y-ihi #x7ff00000) (/= y-lo 0)))
+ (return-from real-expt (coerce (+ x y) rtype)))
+ (let ((yisint (if (< x-hi 0) (isint y-ihi y-lo) 0)))
+ (declare (type fixnum yisint))
+ ;; special value of y
+ (when (and (zerop y-lo) (= y-ihi #x7ff00000))
+ ;; y is +-inf
+ (return-from real-expt
+ (cond ((and (= x-ihi #x3ff00000) (zerop x-lo))
+ ;; +-1**inf is NaN
+ (coerce (- y y) rtype))
+ ((>= x-ihi #x3ff00000)
+ ;; (|x|>1)**+-inf = inf,0
+ (if (>= y-hi 0)
+ (coerce y rtype)
+ (coerce 0 rtype)))
+ (t
+ ;; (|x|<1)**-,+inf = inf,0
+ (if (< y-hi 0)
+ (coerce (- y) rtype)
+ (coerce 0 rtype))))))
- (let ((abs-x (abs x)))
- (declare (double-float abs-x))
- ;; special value of x
- (when (and (zerop x-lo)
- (or (= x-ihi #x7ff00000) (zerop x-ihi)
- (= x-ihi #x3ff00000)))
- ;; x is +-0,+-inf,+-1
- (let ((z (if (< y-hi 0)
- (/ 1 abs-x) ; z = (1/|x|)
- abs-x)))
- (declare (double-float z))
- (when (< x-hi 0)
- (cond ((and (= x-ihi #x3ff00000) (zerop yisint))
- ;; (-1)**non-int
- (let ((y*pi (* y pi)))
- (declare (double-float y*pi))
- (return-from real-expt
- (complex
- (coerce (%cos y*pi) rtype)
- (coerce (%sin y*pi) rtype)))))
- ((= yisint 1)
- ;; (x<0)**odd = -(|x|**odd)
- (setq z (- z)))))
- (return-from real-expt (coerce z rtype))))
+ (let ((abs-x (abs x)))
+ (declare (double-float abs-x))
+ ;; special value of x
+ (when (and (zerop x-lo)
+ (or (= x-ihi #x7ff00000) (zerop x-ihi)
+ (= x-ihi #x3ff00000)))
+ ;; x is +-0,+-inf,+-1
+ (let ((z (if (< y-hi 0)
+ (/ 1 abs-x) ; z = (1/|x|)
+ abs-x)))
+ (declare (double-float z))
+ (when (< x-hi 0)
+ (cond ((and (= x-ihi #x3ff00000) (zerop yisint))
+ ;; (-1)**non-int
+ (let ((y*pi (* y pi)))
+ (declare (double-float y*pi))
+ (return-from real-expt
+ (complex
+ (coerce (%cos y*pi) rtype)
+ (coerce (%sin y*pi) rtype)))))
+ ((= yisint 1)
+ ;; (x<0)**odd = -(|x|**odd)
+ (setq z (- z)))))
+ (return-from real-expt (coerce z rtype))))
- (((foreach fixnum (or bignum ratio) (complex rational)) integer)
- (intexp base power))
- (((foreach single-float double-float) rational)
- (real-expt base power '(dispatch-type base)))
- (((foreach fixnum (or bignum ratio) single-float)
- (foreach ratio single-float))
- (real-expt base power 'single-float))
- (((foreach fixnum (or bignum ratio) single-float double-float)
- double-float)
- (real-expt base power 'double-float))
- ((double-float single-float)
- (real-expt base power 'double-float))
- (((foreach (complex rational) (complex float)) rational)
- (* (expt (abs base) power)
- (cis (* power (phase base)))))
- (((foreach fixnum (or bignum ratio) single-float double-float)
- complex)
- (if (and (zerop base) (plusp (realpart power)))
- (* base power)
- (exp (* power (log base)))))
- (((foreach (complex float) (complex rational))
- (foreach complex double-float single-float))
- (if (and (zerop base) (plusp (realpart power)))
- (* base power)
- (exp (* power (log base)))))))))
+ (((foreach fixnum (or bignum ratio) (complex rational)) integer)
+ (intexp base power))
+ (((foreach single-float double-float) rational)
+ (real-expt base power '(dispatch-type base)))
+ (((foreach fixnum (or bignum ratio) single-float)
+ (foreach ratio single-float))
+ (real-expt base power 'single-float))
+ (((foreach fixnum (or bignum ratio) single-float double-float)
+ double-float)
+ (real-expt base power 'double-float))
+ ((double-float single-float)
+ (real-expt base power 'double-float))
+ (((foreach (complex rational) (complex float)) rational)
+ (* (expt (abs base) power)
+ (cis (* power (phase base)))))
+ (((foreach fixnum (or bignum ratio) single-float double-float)
+ complex)
+ (if (and (zerop base) (plusp (realpart power)))
+ (* base power)
+ (exp (* power (log base)))))
+ (((foreach (complex float) (complex rational))
+ (foreach complex double-float single-float))
+ (if (and (zerop base) (plusp (realpart power)))
+ (* base power)
+ (exp (* power (log base)))))))))
+
+;;; FIXME: Maybe rename this so that it's clearer that it only works
+;;; on integers?
+(defun log2 (x)
+ (declare (type integer x))
+ ;; CMUCL comment:
+ ;;
+ ;; Write x = 2^n*f where 1/2 < f <= 1. Then log2(x) = n +
+ ;; log2(f). So we grab the top few bits of x and scale that
+ ;; appropriately, take the log of it and add it to n.
+ ;;
+ ;; Motivated by an attempt to get LOG to work better on bignums.
+ (let ((n (integer-length x)))
+ (if (< n sb!vm:double-float-digits)
+ (log (coerce x 'double-float) 2.0d0)
+ (let ((f (ldb (byte sb!vm:double-float-digits
+ (- n sb!vm:double-float-digits))
+ x)))
+ (+ n (log (scale-float (coerce f 'double-float)
+ (- sb!vm:double-float-digits))
+ 2.0d0))))))
- (((foreach fixnum bignum ratio))
- (if (minusp number)
- (complex (log (- number)) (coerce pi 'single-float))
- (coerce (%log (coerce number 'double-float)) 'single-float)))
- (((foreach single-float double-float))
- ;; Is (log -0) -infinity (libm.a) or -infinity + i*pi (Kahan)?
- ;; Since this doesn't seem to be an implementation issue
- ;; I (pw) take the Kahan result.
- (if (< (float-sign number)
- (coerce 0 '(dispatch-type number)))
- (complex (log (- number)) (coerce pi '(dispatch-type number)))
- (coerce (%log (coerce number 'double-float))
- '(dispatch-type number))))
- ((complex)
- (complex-log number)))))
+ (((foreach fixnum bignum))
+ (if (minusp number)
+ (complex (log (- number)) (coerce pi 'single-float))
+ (coerce (/ (log2 number) (log (exp 1.0d0) 2.0d0)) 'single-float)))
+ ((ratio)
+ (if (minusp number)
+ (complex (log (- number)) (coerce pi 'single-float))
+ (let ((numerator (numerator number))
+ (denominator (denominator number)))
+ (if (= (integer-length numerator)
+ (integer-length denominator))
+ (coerce (%log1p (coerce (- number 1) 'double-float))
+ 'single-float)
+ (coerce (/ (- (log2 numerator) (log2 denominator))
+ (log (exp 1.0d0) 2.0d0))
+ 'single-float)))))
+ (((foreach single-float double-float))
+ ;; Is (log -0) -infinity (libm.a) or -infinity + i*pi (Kahan)?
+ ;; Since this doesn't seem to be an implementation issue
+ ;; I (pw) take the Kahan result.
+ (if (< (float-sign number)
+ (coerce 0 '(dispatch-type number)))
+ (complex (log (- number)) (coerce pi '(dispatch-type number)))
+ (coerce (%log (coerce number 'double-float))
+ '(dispatch-type number))))
+ ((complex)
+ (complex-log number)))))
- (declare (type double-float y x)
- (values double-float))
- (if (zerop x)
- (if (zerop y)
- (if (plusp (float-sign x))
- y
- (float-sign y pi))
- (float-sign y (/ pi 2)))
- (%atan2 y x))))
- (number-dispatch ((y number) (x number))
- ((double-float
- (foreach double-float single-float fixnum bignum ratio))
- (atan2 y (coerce x 'double-float)))
- (((foreach single-float fixnum bignum ratio)
- double-float)
- (atan2 (coerce y 'double-float) x))
- (((foreach single-float fixnum bignum ratio)
- (foreach single-float fixnum bignum ratio))
- (coerce (atan2 (coerce y 'double-float) (coerce x 'double-float))
- 'single-float))))
+ (declare (type double-float y x)
+ (values double-float))
+ (if (zerop x)
+ (if (zerop y)
+ (if (plusp (float-sign x))
+ y
+ (float-sign y pi))
+ (float-sign y (/ pi 2)))
+ (%atan2 y x))))
+ (number-dispatch ((y real) (x real))
+ ((double-float
+ (foreach double-float single-float fixnum bignum ratio))
+ (atan2 y (coerce x 'double-float)))
+ (((foreach single-float fixnum bignum ratio)
+ double-float)
+ (atan2 (coerce y 'double-float) x))
+ (((foreach single-float fixnum bignum ratio)
+ (foreach single-float fixnum bignum ratio))
+ (coerce (atan2 (coerce y 'double-float) (coerce x 'double-float))
+ 'single-float))))
-;;; should be used instead?
+;;; should be used instead? (KLUDGED 2004-03-08 CSR, by replacing the
+;;; special variable references with (probably equally slow)
+;;; constructors)
+;;;
+;;; FIXME: As of 2004-05, when PFD noted that IMAGPART and COMPLEX
+;;; differ in their interpretations of the real line, IMAGPART was
+;;; patch, which without a certain amount of effort would have altered
+;;; all the branch cut treatment. Clients of these COMPLEX- routines
+;;; were patched to use explicit COMPLEX, rather than implicitly
+;;; passing in real numbers for treatment with IMAGPART, and these
+;;; COMPLEX- functions altered to require arguments of type COMPLEX;
+;;; however, someone needs to go back to Kahan for the definitive
+;;; answer for treatment of negative real floating point numbers and
+;;; branch cuts. If adjustment is needed, it is probably the removal
+;;; of explicit calls to COMPLEX in the clients of irrational
+;;; functions. -- a slightly bitter CSR, 2004-05-16
- (float-infinity-p rho))
- (or (float-infinity-p (abs x))
- (float-infinity-p (abs y))))
- (values sb!ext:double-float-positive-infinity 0))
- ((let ((threshold #.(/ least-positive-double-float
- double-float-epsilon))
- (traps (ldb sb!vm::float-sticky-bits
- (sb!vm:floating-point-modes))))
+ (float-infinity-p rho))
+ (or (float-infinity-p (abs x))
+ (float-infinity-p (abs y))))
+ ;; DOUBLE-FLOAT-POSITIVE-INFINITY
+ (values
+ (double-from-bits 0 (1+ sb!vm:double-float-normal-exponent-max) 0)
+ 0))
+ ((let ((threshold #.(/ least-positive-double-float
+ double-float-epsilon))
+ (traps (ldb sb!vm::float-sticky-bits
+ (sb!vm:floating-point-modes))))
;; The constants t0, t1, t2 should be evaluated to machine
;; precision. In addition, Kahan says the accuracy of log1p
;; influences the choices of these constants but doesn't say how to
;; choose them. We'll just assume his choices matches our
;; implementation of log1p.
(let ((t0 #.(/ 1 (sqrt 2.0d0)))
;; The constants t0, t1, t2 should be evaluated to machine
;; precision. In addition, Kahan says the accuracy of log1p
;; influences the choices of these constants but doesn't say how to
;; choose them. We'll just assume his choices matches our
;; implementation of log1p.
(let ((t0 #.(/ 1 (sqrt 2.0d0)))
- (> (abs y) theta))
- ;; To avoid overflow...
- (setf eta (float-sign y half-pi))
- ;; nu is real part of 1/(x + iy). This is x/(x^2+y^2),
- ;; which can cause overflow. Arrange this computation so
- ;; that it won't overflow.
- (setf nu (let* ((x-bigger (> x (abs y)))
- (r (if x-bigger (/ y x) (/ x y)))
- (d (+ 1.0d0 (* r r))))
- (if x-bigger
- (/ (/ x) d)
- (/ (/ r y) d)))))
- ((= x 1.0d0)
- ;; Should this be changed so that if y is zero, eta is set
- ;; to +infinity instead of approx 176? In any case
- ;; tanh(176) is 1.0d0 within working precision.
- (let ((t1 (+ 4d0 (square y)))
- (t2 (+ (abs y) rho)))
- (setf eta (log (/ (sqrt (sqrt t1)))
- (sqrt t2)))
- (setf nu (* 0.5d0
- (float-sign y
- (+ half-pi (atan (* 0.5d0 t2))))))))
- (t
- (let ((t1 (+ (abs y) rho)))
+ (> (abs y) theta))
+ ;; To avoid overflow...
+ (setf nu (float-sign y half-pi))
+ ;; ETA is real part of 1/(x + iy). This is x/(x^2+y^2),
+ ;; which can cause overflow. Arrange this computation so
+ ;; that it won't overflow.
+ (setf eta (let* ((x-bigger (> x (abs y)))
+ (r (if x-bigger (/ y x) (/ x y)))
+ (d (+ 1.0d0 (* r r))))
+ (if x-bigger
+ (/ (/ x) d)
+ (/ (/ r y) d)))))
+ ((= x 1.0d0)
+ ;; Should this be changed so that if y is zero, eta is set
+ ;; to +infinity instead of approx 176? In any case
+ ;; tanh(176) is 1.0d0 within working precision.
+ (let ((t1 (+ 4d0 (square y)))
+ (t2 (+ (abs y) rho)))
+ (setf eta (log (/ (sqrt (sqrt t1))
+ (sqrt t2))))
+ (setf nu (* 0.5d0
+ (float-sign y
+ (+ half-pi (atan (* 0.5d0 t2))))))))
+ (t
+ (let ((t1 (+ (abs y) rho)))
- #-(or linux hpux) #.(/ (asinh most-positive-double-float) 4d0)
- ;; This is more accurate under linux.
- #+(or linux hpux) #.(/ (+ (log 2.0d0)
- (log most-positive-double-float)) 4d0))
- (coerce-to-complex-type (float-sign x)
- (float-sign y) z))
- (t
- (let* ((tv (%tan y))
- (beta (+ 1.0d0 (* tv tv)))
- (s (sinh x))
- (rho (sqrt (+ 1.0d0 (* s s)))))
- (if (float-infinity-p (abs tv))
- (coerce-to-complex-type (/ rho s)
- (/ tv)
- z)
- (let ((den (+ 1.0d0 (* beta s s))))
- (coerce-to-complex-type (/ (* beta rho s)
- den)
- (/ tv den)
+ ;; FIXME: this form is hideously broken wrt
+ ;; cross-compilation portability. Much else in this
+ ;; file is too, of course, sometimes hidden by
+ ;; constant-folding, but this one in particular clearly
+ ;; depends on host and target
+ ;; MOST-POSITIVE-DOUBLE-FLOATs being equal. -- CSR,
+ ;; 2003-04-20
+ #.(/ (+ (log 2.0d0)
+ (log most-positive-double-float))
+ 4d0))
+ (coerce-to-complex-type (float-sign x)
+ (float-sign y) z))
+ (t
+ (let* ((tv (%tan y))
+ (beta (+ 1.0d0 (* tv tv)))
+ (s (sinh x))
+ (rho (sqrt (+ 1.0d0 (* s s)))))
+ (if (float-infinity-p (abs tv))
+ (coerce-to-complex-type (/ rho s)
+ (/ tv)
+ z)
+ (let ((den (+ 1.0d0 (* beta s s))))
+ (coerce-to-complex-type (/ (* beta rho s)
+ den)
+ (/ tv den)