-(def-math-rtn "tanh" 1)
-(def-math-rtn "asinh" 1)
-(def-math-rtn "acosh" 1)
-(def-math-rtn "atanh" 1)
+#!-win32
+(progn
+ (def-math-rtn "tanh" 1)
+ (def-math-rtn "asinh" 1)
+ (def-math-rtn "acosh" 1)
+ (def-math-rtn "atanh" 1))
+#!+win32
+(progn
+ (declaim (inline %tanh))
+ (defun %tanh (number)
+ (/ (%sinh number) (%cosh number)))
+ (declaim (inline %asinh))
+ (defun %asinh (number)
+ (log (+ number (sqrt (+ (* number number) 1.0d0))) #.(exp 1.0d0)))
+ (declaim (inline %acosh))
+ (defun %acosh (number)
+ (log (+ number (sqrt (- (* number number) 1.0d0))) #.(exp 1.0d0)))
+ (declaim (inline %atanh))
+ (defun %atanh (number)
+ (let ((ratio (/ (+ 1 number) (- 1 number))))
+ ;; Were we effectively zero?
+ (if (= ratio -1.0d0)
+ 0.0d0
+ (/ (log ratio #.(exp 1.0d0)) 2.0d0)))))
- ;; 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)))))))))
- (((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)))))
+ (((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 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))))
+ (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)))
- ;; 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)
+ ;; 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)