1 ;;;; This file contains floating-point-specific transforms, and may be
2 ;;;; somewhat implementation-dependent in its assumptions of what the
5 ;;;; This software is part of the SBCL system. See the README file for
8 ;;;; This software is derived from the CMU CL system, which was
9 ;;;; written at Carnegie Mellon University and released into the
10 ;;;; public domain. The software is in the public domain and is
11 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
12 ;;;; files for more information.
18 (defknown %single-float (real) single-float (movable foldable flushable))
19 (defknown %double-float (real) double-float (movable foldable flushable))
21 (deftransform float ((n &optional f) (* &optional single-float) * :when :both)
24 (deftransform float ((n f) (* double-float) * :when :both)
27 (deftransform %single-float ((n) (single-float) * :when :both)
30 (deftransform %double-float ((n) (double-float) * :when :both)
34 (macrolet ((frob (fun type)
35 `(deftransform random ((num &optional state)
38 "Use inline float operations."
39 '(,fun num (or state *random-state*)))))
40 (frob %random-single-float single-float)
41 (frob %random-double-float double-float))
43 ;;; Mersenne Twister RNG
45 ;;; FIXME: It's unpleasant to have RANDOM functionality scattered
46 ;;; through the code this way. It would be nice to move this into the
47 ;;; same file as the other RANDOM definitions.
48 (deftransform random ((num &optional state)
49 ((integer 1 #.(expt 2 32)) &optional *))
50 ;; FIXME: I almost conditionalized this as #!+sb-doc. Find some way
51 ;; of automatically finding #!+sb-doc in proximity to DEFTRANSFORM
52 ;; to let me scan for places that I made this mistake and didn't
54 "use inline (UNSIGNED-BYTE 32) operations"
55 (let ((num-high (numeric-type-high (continuation-type num))))
57 (give-up-ir1-transform))
58 (cond ((constant-continuation-p num)
59 ;; Check the worst case sum absolute error for the random number
61 (let ((rem (rem (expt 2 32) num-high)))
62 (unless (< (/ (* 2 rem (- num-high rem)) num-high (expt 2 32))
63 (expt 2 (- sb!kernel::random-integer-extra-bits)))
64 (give-up-ir1-transform
65 "The random number expectations are inaccurate."))
66 (if (= num-high (expt 2 32))
67 '(random-chunk (or state *random-state*))
68 #!-x86 '(rem (random-chunk (or state *random-state*)) num)
70 ;; Use multiplication, which is faster.
71 '(values (sb!bignum::%multiply
72 (random-chunk (or state *random-state*))
74 ((> num-high random-fixnum-max)
75 (give-up-ir1-transform
76 "The range is too large to ensure an accurate result."))
78 ((< num-high (expt 2 32))
79 '(values (sb!bignum::%multiply (random-chunk (or state
83 '(rem (random-chunk (or state *random-state*)) num)))))
87 (defknown make-single-float ((signed-byte 32)) single-float
88 (movable foldable flushable))
90 (defknown make-double-float ((signed-byte 32) (unsigned-byte 32)) double-float
91 (movable foldable flushable))
93 (defknown single-float-bits (single-float) (signed-byte 32)
94 (movable foldable flushable))
96 (defknown double-float-high-bits (double-float) (signed-byte 32)
97 (movable foldable flushable))
99 (defknown double-float-low-bits (double-float) (unsigned-byte 32)
100 (movable foldable flushable))
102 (deftransform float-sign ((float &optional float2)
103 (single-float &optional single-float) *)
105 (let ((temp (gensym)))
106 `(let ((,temp (abs float2)))
107 (if (minusp (single-float-bits float)) (- ,temp) ,temp)))
108 '(if (minusp (single-float-bits float)) -1f0 1f0)))
110 (deftransform float-sign ((float &optional float2)
111 (double-float &optional double-float) *)
113 (let ((temp (gensym)))
114 `(let ((,temp (abs float2)))
115 (if (minusp (double-float-high-bits float)) (- ,temp) ,temp)))
116 '(if (minusp (double-float-high-bits float)) -1d0 1d0)))
118 ;;;; DECODE-FLOAT, INTEGER-DECODE-FLOAT, and SCALE-FLOAT
120 (defknown decode-single-float (single-float)
121 (values single-float single-float-exponent (single-float -1f0 1f0))
122 (movable foldable flushable))
124 (defknown decode-double-float (double-float)
125 (values double-float double-float-exponent (double-float -1d0 1d0))
126 (movable foldable flushable))
128 (defknown integer-decode-single-float (single-float)
129 (values single-float-significand single-float-int-exponent (integer -1 1))
130 (movable foldable flushable))
132 (defknown integer-decode-double-float (double-float)
133 (values double-float-significand double-float-int-exponent (integer -1 1))
134 (movable foldable flushable))
136 (defknown scale-single-float (single-float fixnum) single-float
137 (movable foldable flushable))
139 (defknown scale-double-float (double-float fixnum) double-float
140 (movable foldable flushable))
142 (deftransform decode-float ((x) (single-float) * :when :both)
143 '(decode-single-float x))
145 (deftransform decode-float ((x) (double-float) * :when :both)
146 '(decode-double-float x))
148 (deftransform integer-decode-float ((x) (single-float) * :when :both)
149 '(integer-decode-single-float x))
151 (deftransform integer-decode-float ((x) (double-float) * :when :both)
152 '(integer-decode-double-float x))
154 (deftransform scale-float ((f ex) (single-float *) * :when :both)
155 (if (and #!+x86 t #!-x86 nil
156 (csubtypep (continuation-type ex)
157 (specifier-type '(signed-byte 32))))
158 '(coerce (%scalbn (coerce f 'double-float) ex) 'single-float)
159 '(scale-single-float f ex)))
161 (deftransform scale-float ((f ex) (double-float *) * :when :both)
162 (if (and #!+x86 t #!-x86 nil
163 (csubtypep (continuation-type ex)
164 (specifier-type '(signed-byte 32))))
166 '(scale-double-float f ex)))
168 ;;; What is the CROSS-FLOAT-INFINITY-KLUDGE?
170 ;;; SBCL's own implementation of floating point supports floating
171 ;;; point infinities. Some of the old CMU CL :PROPAGATE-FLOAT-TYPE and
172 ;;; :PROPAGATE-FUN-TYPE code, like the DEFOPTIMIZERs below, uses this
173 ;;; floating point support. Thus, we have to avoid running it on the
174 ;;; cross-compilation host, since we're not guaranteed that the
175 ;;; cross-compilation host will support floating point infinities.
177 ;;; If we wanted to live dangerously, we could conditionalize the code
178 ;;; with #+(OR SBCL SB-XC) instead. That way, if the cross-compilation
179 ;;; host happened to be SBCL, we'd be able to run the infinity-using
181 ;;; * SBCL itself gets built with more complete optimization.
183 ;;; * You get a different SBCL depending on what your cross-compilation
185 ;;; So far the pros and cons seem seem to be mostly academic, since
186 ;;; AFAIK (WHN 2001-08-28) the propagate-foo-type optimizations aren't
187 ;;; actually important in compiling SBCL itself. If this changes, then
188 ;;; we have to decide:
189 ;;; * Go for simplicity, leaving things as they are.
190 ;;; * Go for performance at the expense of conceptual clarity,
191 ;;; using #+(OR SBCL SB-XC) and otherwise leaving the build
193 ;;; * Go for performance at the expense of build time, using
194 ;;; #+(OR SBCL SB-XC) and also making SBCL do not just
195 ;;; make-host-1.sh and make-host-2.sh, but a third step
196 ;;; make-host-3.sh where it builds itself under itself. (Such a
197 ;;; 3-step build process could also help with other things, e.g.
198 ;;; using specialized arrays to represent debug information.)
199 ;;; * Rewrite the code so that it doesn't depend on unportable
200 ;;; floating point infinities.
202 ;;; optimizers for SCALE-FLOAT. If the float has bounds, new bounds
203 ;;; are computed for the result, if possible.
204 #-sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
207 (defun scale-float-derive-type-aux (f ex same-arg)
208 (declare (ignore same-arg))
209 (flet ((scale-bound (x n)
210 ;; We need to be a bit careful here and catch any overflows
211 ;; that might occur. We can ignore underflows which become
215 (scale-float (type-bound-number x) n)
216 (floating-point-overflow ()
219 (when (and (numeric-type-p f) (numeric-type-p ex))
220 (let ((f-lo (numeric-type-low f))
221 (f-hi (numeric-type-high f))
222 (ex-lo (numeric-type-low ex))
223 (ex-hi (numeric-type-high ex))
226 (when (and f-hi ex-hi)
227 (setf new-hi (scale-bound f-hi ex-hi)))
228 (when (and f-lo ex-lo)
229 (setf new-lo (scale-bound f-lo ex-lo)))
230 (make-numeric-type :class (numeric-type-class f)
231 :format (numeric-type-format f)
235 (defoptimizer (scale-single-float derive-type) ((f ex))
236 (two-arg-derive-type f ex #'scale-float-derive-type-aux
237 #'scale-single-float t))
238 (defoptimizer (scale-double-float derive-type) ((f ex))
239 (two-arg-derive-type f ex #'scale-float-derive-type-aux
240 #'scale-double-float t))
242 ;;; DEFOPTIMIZERs for %SINGLE-FLOAT and %DOUBLE-FLOAT. This makes the
243 ;;; FLOAT function return the correct ranges if the input has some
244 ;;; defined range. Quite useful if we want to convert some type of
245 ;;; bounded integer into a float.
248 (let ((aux-name (symbolicate fun "-DERIVE-TYPE-AUX")))
250 (defun ,aux-name (num)
251 ;; When converting a number to a float, the limits are
253 (let* ((lo (bound-func (lambda (x)
255 (numeric-type-low num)))
256 (hi (bound-func (lambda (x)
258 (numeric-type-high num))))
259 (specifier-type `(,',type ,(or lo '*) ,(or hi '*)))))
261 (defoptimizer (,fun derive-type) ((num))
262 (one-arg-derive-type num #',aux-name #',fun))))))
263 (frob %single-float single-float)
264 (frob %double-float double-float))
269 ;;; Do some stuff to recognize when the loser is doing mixed float and
270 ;;; rational arithmetic, or different float types, and fix it up. If
271 ;;; we don't, he won't even get so much as an efficiency note.
272 (deftransform float-contagion-arg1 ((x y) * * :defun-only t :node node)
273 `(,(continuation-fun-name (basic-combination-fun node))
275 (deftransform float-contagion-arg2 ((x y) * * :defun-only t :node node)
276 `(,(continuation-fun-name (basic-combination-fun node))
279 (dolist (x '(+ * / -))
280 (%deftransform x '(function (rational float) *) #'float-contagion-arg1)
281 (%deftransform x '(function (float rational) *) #'float-contagion-arg2))
283 (dolist (x '(= < > + * / -))
284 (%deftransform x '(function (single-float double-float) *)
285 #'float-contagion-arg1)
286 (%deftransform x '(function (double-float single-float) *)
287 #'float-contagion-arg2))
289 ;;; Prevent ZEROP, PLUSP, and MINUSP from losing horribly. We can't in
290 ;;; general float rational args to comparison, since Common Lisp
291 ;;; semantics says we are supposed to compare as rationals, but we can
292 ;;; do it for any rational that has a precise representation as a
293 ;;; float (such as 0).
294 (macrolet ((frob (op)
295 `(deftransform ,op ((x y) (float rational) * :when :both)
296 "open-code FLOAT to RATIONAL comparison"
297 (unless (constant-continuation-p y)
298 (give-up-ir1-transform
299 "The RATIONAL value isn't known at compile time."))
300 (let ((val (continuation-value y)))
301 (unless (eql (rational (float val)) val)
302 (give-up-ir1-transform
303 "~S doesn't have a precise float representation."
305 `(,',op x (float y x)))))
310 ;;;; irrational derive-type methods
312 ;;; Derive the result to be float for argument types in the
313 ;;; appropriate domain.
314 #+sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
315 (dolist (stuff '((asin (real -1.0 1.0))
316 (acos (real -1.0 1.0))
318 (atanh (real -1.0 1.0))
320 (destructuring-bind (name type) stuff
321 (let ((type (specifier-type type)))
322 (setf (fun-info-derive-type (fun-info-or-lose name))
324 (declare (type combination call))
325 (when (csubtypep (continuation-type
326 (first (combination-args call)))
328 (specifier-type 'float)))))))
330 #+sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
331 (defoptimizer (log derive-type) ((x &optional y))
332 (when (and (csubtypep (continuation-type x)
333 (specifier-type '(real 0.0)))
335 (csubtypep (continuation-type y)
336 (specifier-type '(real 0.0)))))
337 (specifier-type 'float)))
339 ;;;; irrational transforms
341 (defknown (%tan %sinh %asinh %atanh %log %logb %log10 %tan-quick)
342 (double-float) double-float
343 (movable foldable flushable))
345 (defknown (%sin %cos %tanh %sin-quick %cos-quick)
346 (double-float) (double-float -1.0d0 1.0d0)
347 (movable foldable flushable))
349 (defknown (%asin %atan)
351 (double-float #.(coerce (- (/ pi 2)) 'double-float)
352 #.(coerce (/ pi 2) 'double-float))
353 (movable foldable flushable))
356 (double-float) (double-float 0.0d0 #.(coerce pi 'double-float))
357 (movable foldable flushable))
360 (double-float) (double-float 1.0d0)
361 (movable foldable flushable))
363 (defknown (%acosh %exp %sqrt)
364 (double-float) (double-float 0.0d0)
365 (movable foldable flushable))
368 (double-float) (double-float -1d0)
369 (movable foldable flushable))
372 (double-float double-float) (double-float 0d0)
373 (movable foldable flushable))
376 (double-float double-float) double-float
377 (movable foldable flushable))
380 (double-float double-float)
381 (double-float #.(coerce (- pi) 'double-float)
382 #.(coerce pi 'double-float))
383 (movable foldable flushable))
386 (double-float double-float) double-float
387 (movable foldable flushable))
390 (double-float (signed-byte 32)) double-float
391 (movable foldable flushable))
394 (double-float) double-float
395 (movable foldable flushable))
397 (macrolet ((def (name prim rtype)
399 (deftransform ,name ((x) (single-float) ,rtype)
400 `(coerce (,',prim (coerce x 'double-float)) 'single-float))
401 (deftransform ,name ((x) (double-float) ,rtype :when :both)
405 (def sqrt %sqrt float)
406 (def asin %asin float)
407 (def acos %acos float)
413 (def acosh %acosh float)
414 (def atanh %atanh float))
416 ;;; The argument range is limited on the x86 FP trig. functions. A
417 ;;; post-test can detect a failure (and load a suitable result), but
418 ;;; this test is avoided if possible.
419 (macrolet ((def (name prim prim-quick)
420 (declare (ignorable prim-quick))
422 (deftransform ,name ((x) (single-float) *)
423 #!+x86 (cond ((csubtypep (continuation-type x)
424 (specifier-type '(single-float
425 (#.(- (expt 2f0 64)))
427 `(coerce (,',prim-quick (coerce x 'double-float))
431 "unable to avoid inline argument range check~@
432 because the argument range (~S) was not within 2^64"
433 (type-specifier (continuation-type x)))
434 `(coerce (,',prim (coerce x 'double-float)) 'single-float)))
435 #!-x86 `(coerce (,',prim (coerce x 'double-float)) 'single-float))
436 (deftransform ,name ((x) (double-float) * :when :both)
437 #!+x86 (cond ((csubtypep (continuation-type x)
438 (specifier-type '(double-float
439 (#.(- (expt 2d0 64)))
444 "unable to avoid inline argument range check~@
445 because the argument range (~S) was not within 2^64"
446 (type-specifier (continuation-type x)))
448 #!-x86 `(,',prim x)))))
449 (def sin %sin %sin-quick)
450 (def cos %cos %cos-quick)
451 (def tan %tan %tan-quick))
453 (deftransform atan ((x y) (single-float single-float) *)
454 `(coerce (%atan2 (coerce x 'double-float) (coerce y 'double-float))
456 (deftransform atan ((x y) (double-float double-float) * :when :both)
459 (deftransform expt ((x y) ((single-float 0f0) single-float) *)
460 `(coerce (%pow (coerce x 'double-float) (coerce y 'double-float))
462 (deftransform expt ((x y) ((double-float 0d0) double-float) * :when :both)
464 (deftransform expt ((x y) ((single-float 0f0) (signed-byte 32)) *)
465 `(coerce (%pow (coerce x 'double-float) (coerce y 'double-float))
467 (deftransform expt ((x y) ((double-float 0d0) (signed-byte 32)) * :when :both)
468 `(%pow x (coerce y 'double-float)))
470 ;;; ANSI says log with base zero returns zero.
471 (deftransform log ((x y) (float float) float)
472 '(if (zerop y) y (/ (log x) (log y))))
474 ;;; Handle some simple transformations.
476 (deftransform abs ((x) ((complex double-float)) double-float :when :both)
477 '(%hypot (realpart x) (imagpart x)))
479 (deftransform abs ((x) ((complex single-float)) single-float)
480 '(coerce (%hypot (coerce (realpart x) 'double-float)
481 (coerce (imagpart x) 'double-float))
484 (deftransform phase ((x) ((complex double-float)) double-float :when :both)
485 '(%atan2 (imagpart x) (realpart x)))
487 (deftransform phase ((x) ((complex single-float)) single-float)
488 '(coerce (%atan2 (coerce (imagpart x) 'double-float)
489 (coerce (realpart x) 'double-float))
492 (deftransform phase ((x) ((float)) float :when :both)
493 '(if (minusp (float-sign x))
497 ;;; The number is of type REAL.
498 (defun numeric-type-real-p (type)
499 (and (numeric-type-p type)
500 (eq (numeric-type-complexp type) :real)))
502 ;;; Coerce a numeric type bound to the given type while handling
503 ;;; exclusive bounds.
504 (defun coerce-numeric-bound (bound type)
507 (list (coerce (car bound) type))
508 (coerce bound type))))
510 #-sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
513 ;;;; optimizers for elementary functions
515 ;;;; These optimizers compute the output range of the elementary
516 ;;;; function, based on the domain of the input.
518 ;;; Generate a specifier for a complex type specialized to the same
519 ;;; type as the argument.
520 (defun complex-float-type (arg)
521 (declare (type numeric-type arg))
522 (let* ((format (case (numeric-type-class arg)
523 ((integer rational) 'single-float)
524 (t (numeric-type-format arg))))
525 (float-type (or format 'float)))
526 (specifier-type `(complex ,float-type))))
528 ;;; Compute a specifier like '(OR FLOAT (COMPLEX FLOAT)), except float
529 ;;; should be the right kind of float. Allow bounds for the float
531 (defun float-or-complex-float-type (arg &optional lo hi)
532 (declare (type numeric-type arg))
533 (let* ((format (case (numeric-type-class arg)
534 ((integer rational) 'single-float)
535 (t (numeric-type-format arg))))
536 (float-type (or format 'float))
537 (lo (coerce-numeric-bound lo float-type))
538 (hi (coerce-numeric-bound hi float-type)))
539 (specifier-type `(or (,float-type ,(or lo '*) ,(or hi '*))
540 (complex ,float-type)))))
542 ;;; Test whether the numeric-type ARG is within in domain specified by
543 ;;; DOMAIN-LOW and DOMAIN-HIGH, consider negative and positive zero to
544 ;;; be distinct as for the :NEGATIVE-ZERO-IS-NOT-ZERO feature. With
545 ;;; the :NEGATIVE-ZERO-IS-NOT-ZERO feature this could be handled by
546 ;;; the numeric subtype code in type.lisp.
547 (defun domain-subtypep (arg domain-low domain-high)
548 (declare (type numeric-type arg)
549 (type (or real null) domain-low domain-high))
550 (let* ((arg-lo (numeric-type-low arg))
551 (arg-lo-val (type-bound-number arg-lo))
552 (arg-hi (numeric-type-high arg))
553 (arg-hi-val (type-bound-number arg-hi)))
554 ;; Check that the ARG bounds are correctly canonicalized.
555 (when (and arg-lo (floatp arg-lo-val) (zerop arg-lo-val) (consp arg-lo)
556 (minusp (float-sign arg-lo-val)))
557 (compiler-note "float zero bound ~S not correctly canonicalized?" arg-lo)
558 (setq arg-lo '(0l0) arg-lo-val 0l0))
559 (when (and arg-hi (zerop arg-hi-val) (floatp arg-hi-val) (consp arg-hi)
560 (plusp (float-sign arg-hi-val)))
561 (compiler-note "float zero bound ~S not correctly canonicalized?" arg-hi)
562 (setq arg-hi '(-0l0) arg-hi-val -0l0))
563 (and (or (null domain-low)
564 (and arg-lo (>= arg-lo-val domain-low)
565 (not (and (zerop domain-low) (floatp domain-low)
566 (plusp (float-sign domain-low))
567 (zerop arg-lo-val) (floatp arg-lo-val)
569 (plusp (float-sign arg-lo-val))
570 (minusp (float-sign arg-lo-val)))))))
571 (or (null domain-high)
572 (and arg-hi (<= arg-hi-val domain-high)
573 (not (and (zerop domain-high) (floatp domain-high)
574 (minusp (float-sign domain-high))
575 (zerop arg-hi-val) (floatp arg-hi-val)
577 (minusp (float-sign arg-hi-val))
578 (plusp (float-sign arg-hi-val))))))))))
580 ;;; Handle monotonic functions of a single variable whose domain is
581 ;;; possibly part of the real line. ARG is the variable, FCN is the
582 ;;; function, and DOMAIN is a specifier that gives the (real) domain
583 ;;; of the function. If ARG is a subset of the DOMAIN, we compute the
584 ;;; bounds directly. Otherwise, we compute the bounds for the
585 ;;; intersection between ARG and DOMAIN, and then append a complex
586 ;;; result, which occurs for the parts of ARG not in the DOMAIN.
588 ;;; Negative and positive zero are considered distinct within
589 ;;; DOMAIN-LOW and DOMAIN-HIGH, as for the :negative-zero-is-not-zero
592 ;;; DEFAULT-LOW and DEFAULT-HIGH are the lower and upper bounds if we
593 ;;; can't compute the bounds using FCN.
594 (defun elfun-derive-type-simple (arg fcn domain-low domain-high
595 default-low default-high
596 &optional (increasingp t))
597 (declare (type (or null real) domain-low domain-high))
600 (cond ((eq (numeric-type-complexp arg) :complex)
601 (make-numeric-type :class (numeric-type-class arg)
602 :format (numeric-type-format arg)
604 ((numeric-type-real-p arg)
605 ;; The argument is real, so let's find the intersection
606 ;; between the argument and the domain of the function.
607 ;; We compute the bounds on the intersection, and for
608 ;; everything else, we return a complex number of the
610 (multiple-value-bind (intersection difference)
611 (interval-intersection/difference (numeric-type->interval arg)
617 ;; Process the intersection.
618 (let* ((low (interval-low intersection))
619 (high (interval-high intersection))
620 (res-lo (or (bound-func fcn (if increasingp low high))
622 (res-hi (or (bound-func fcn (if increasingp high low))
624 (format (case (numeric-type-class arg)
625 ((integer rational) 'single-float)
626 (t (numeric-type-format arg))))
627 (bound-type (or format 'float))
632 :low (coerce-numeric-bound res-lo bound-type)
633 :high (coerce-numeric-bound res-hi bound-type))))
634 ;; If the ARG is a subset of the domain, we don't
635 ;; have to worry about the difference, because that
637 (if (or (null difference)
638 ;; Check whether the arg is within the domain.
639 (domain-subtypep arg domain-low domain-high))
642 (specifier-type `(complex ,bound-type))))))
644 ;; No intersection so the result must be purely complex.
645 (complex-float-type arg)))))
647 (float-or-complex-float-type arg default-low default-high))))))
650 ((frob (name domain-low domain-high def-low-bnd def-high-bnd
651 &key (increasingp t))
652 (let ((num (gensym)))
653 `(defoptimizer (,name derive-type) ((,num))
657 (elfun-derive-type-simple arg #',name
658 ,domain-low ,domain-high
659 ,def-low-bnd ,def-high-bnd
662 ;; These functions are easy because they are defined for the whole
664 (frob exp nil nil 0 nil)
665 (frob sinh nil nil nil nil)
666 (frob tanh nil nil -1 1)
667 (frob asinh nil nil nil nil)
669 ;; These functions are only defined for part of the real line. The
670 ;; condition selects the desired part of the line.
671 (frob asin -1d0 1d0 (- (/ pi 2)) (/ pi 2))
672 ;; Acos is monotonic decreasing, so we need to swap the function
673 ;; values at the lower and upper bounds of the input domain.
674 (frob acos -1d0 1d0 0 pi :increasingp nil)
675 (frob acosh 1d0 nil nil nil)
676 (frob atanh -1d0 1d0 -1 1)
677 ;; Kahan says that (sqrt -0.0) is -0.0, so use a specifier that
679 (frob sqrt -0d0 nil 0 nil))
681 ;;; Compute bounds for (expt x y). This should be easy since (expt x
682 ;;; y) = (exp (* y (log x))). However, computations done this way
683 ;;; have too much roundoff. Thus we have to do it the hard way.
684 (defun safe-expt (x y)
690 ;;; Handle the case when x >= 1.
691 (defun interval-expt-> (x y)
692 (case (sb!c::interval-range-info y 0d0)
694 ;; Y is positive and log X >= 0. The range of exp(y * log(x)) is
695 ;; obviously non-negative. We just have to be careful for
696 ;; infinite bounds (given by nil).
697 (let ((lo (safe-expt (type-bound-number (sb!c::interval-low x))
698 (type-bound-number (sb!c::interval-low y))))
699 (hi (safe-expt (type-bound-number (sb!c::interval-high x))
700 (type-bound-number (sb!c::interval-high y)))))
701 (list (sb!c::make-interval :low (or lo 1) :high hi))))
703 ;; Y is negative and log x >= 0. The range of exp(y * log(x)) is
704 ;; obviously [0, 1]. However, underflow (nil) means 0 is the
706 (let ((lo (safe-expt (type-bound-number (sb!c::interval-high x))
707 (type-bound-number (sb!c::interval-low y))))
708 (hi (safe-expt (type-bound-number (sb!c::interval-low x))
709 (type-bound-number (sb!c::interval-high y)))))
710 (list (sb!c::make-interval :low (or lo 0) :high (or hi 1)))))
712 ;; Split the interval in half.
713 (destructuring-bind (y- y+)
714 (sb!c::interval-split 0 y t)
715 (list (interval-expt-> x y-)
716 (interval-expt-> x y+))))))
718 ;;; Handle the case when x <= 1
719 (defun interval-expt-< (x y)
720 (case (sb!c::interval-range-info x 0d0)
722 ;; The case of 0 <= x <= 1 is easy
723 (case (sb!c::interval-range-info y)
725 ;; Y is positive and log X <= 0. The range of exp(y * log(x)) is
726 ;; obviously [0, 1]. We just have to be careful for infinite bounds
728 (let ((lo (safe-expt (type-bound-number (sb!c::interval-low x))
729 (type-bound-number (sb!c::interval-high y))))
730 (hi (safe-expt (type-bound-number (sb!c::interval-high x))
731 (type-bound-number (sb!c::interval-low y)))))
732 (list (sb!c::make-interval :low (or lo 0) :high (or hi 1)))))
734 ;; Y is negative and log x <= 0. The range of exp(y * log(x)) is
735 ;; obviously [1, inf].
736 (let ((hi (safe-expt (type-bound-number (sb!c::interval-low x))
737 (type-bound-number (sb!c::interval-low y))))
738 (lo (safe-expt (type-bound-number (sb!c::interval-high x))
739 (type-bound-number (sb!c::interval-high y)))))
740 (list (sb!c::make-interval :low (or lo 1) :high hi))))
742 ;; Split the interval in half
743 (destructuring-bind (y- y+)
744 (sb!c::interval-split 0 y t)
745 (list (interval-expt-< x y-)
746 (interval-expt-< x y+))))))
748 ;; The case where x <= 0. Y MUST be an INTEGER for this to work!
749 ;; The calling function must insure this! For now we'll just
750 ;; return the appropriate unbounded float type.
751 (list (sb!c::make-interval :low nil :high nil)))
753 (destructuring-bind (neg pos)
754 (interval-split 0 x t t)
755 (list (interval-expt-< neg y)
756 (interval-expt-< pos y))))))
758 ;;; Compute bounds for (expt x y).
759 (defun interval-expt (x y)
760 (case (interval-range-info x 1)
763 (interval-expt-> x y))
766 (interval-expt-< x y))
768 (destructuring-bind (left right)
769 (interval-split 1 x t t)
770 (list (interval-expt left y)
771 (interval-expt right y))))))
773 (defun fixup-interval-expt (bnd x-int y-int x-type y-type)
774 (declare (ignore x-int))
775 ;; Figure out what the return type should be, given the argument
776 ;; types and bounds and the result type and bounds.
777 (cond ((csubtypep x-type (specifier-type 'integer))
778 ;; an integer to some power
779 (case (numeric-type-class y-type)
781 ;; Positive integer to an integer power is either an
782 ;; integer or a rational.
783 (let ((lo (or (interval-low bnd) '*))
784 (hi (or (interval-high bnd) '*)))
785 (if (and (interval-low y-int)
786 (>= (type-bound-number (interval-low y-int)) 0))
787 (specifier-type `(integer ,lo ,hi))
788 (specifier-type `(rational ,lo ,hi)))))
790 ;; Positive integer to rational power is either a rational
791 ;; or a single-float.
792 (let* ((lo (interval-low bnd))
793 (hi (interval-high bnd))
795 (floor (type-bound-number lo))
798 (ceiling (type-bound-number hi))
801 (bound-func #'float lo)
804 (bound-func #'float hi)
806 (specifier-type `(or (rational ,int-lo ,int-hi)
807 (single-float ,f-lo, f-hi)))))
809 ;; A positive integer to a float power is a float.
810 (modified-numeric-type y-type
811 :low (interval-low bnd)
812 :high (interval-high bnd)))
814 ;; A positive integer to a number is a number (for now).
815 (specifier-type 'number))))
816 ((csubtypep x-type (specifier-type 'rational))
817 ;; a rational to some power
818 (case (numeric-type-class y-type)
820 ;; A positive rational to an integer power is always a rational.
821 (specifier-type `(rational ,(or (interval-low bnd) '*)
822 ,(or (interval-high bnd) '*))))
824 ;; A positive rational to rational power is either a rational
825 ;; or a single-float.
826 (let* ((lo (interval-low bnd))
827 (hi (interval-high bnd))
829 (floor (type-bound-number lo))
832 (ceiling (type-bound-number hi))
835 (bound-func #'float lo)
838 (bound-func #'float hi)
840 (specifier-type `(or (rational ,int-lo ,int-hi)
841 (single-float ,f-lo, f-hi)))))
843 ;; A positive rational to a float power is a float.
844 (modified-numeric-type y-type
845 :low (interval-low bnd)
846 :high (interval-high bnd)))
848 ;; A positive rational to a number is a number (for now).
849 (specifier-type 'number))))
850 ((csubtypep x-type (specifier-type 'float))
851 ;; a float to some power
852 (case (numeric-type-class y-type)
853 ((or integer rational)
854 ;; A positive float to an integer or rational power is
858 :format (numeric-type-format x-type)
859 :low (interval-low bnd)
860 :high (interval-high bnd)))
862 ;; A positive float to a float power is a float of the
866 :format (float-format-max (numeric-type-format x-type)
867 (numeric-type-format y-type))
868 :low (interval-low bnd)
869 :high (interval-high bnd)))
871 ;; A positive float to a number is a number (for now)
872 (specifier-type 'number))))
874 ;; A number to some power is a number.
875 (specifier-type 'number))))
877 (defun merged-interval-expt (x y)
878 (let* ((x-int (numeric-type->interval x))
879 (y-int (numeric-type->interval y)))
880 (mapcar (lambda (type)
881 (fixup-interval-expt type x-int y-int x y))
882 (flatten-list (interval-expt x-int y-int)))))
884 (defun expt-derive-type-aux (x y same-arg)
885 (declare (ignore same-arg))
886 (cond ((or (not (numeric-type-real-p x))
887 (not (numeric-type-real-p y)))
888 ;; Use numeric contagion if either is not real.
889 (numeric-contagion x y))
890 ((csubtypep y (specifier-type 'integer))
891 ;; A real raised to an integer power is well-defined.
892 (merged-interval-expt x y))
894 ;; A real raised to a non-integral power can be a float or a
896 (cond ((or (csubtypep x (specifier-type '(rational 0)))
897 (csubtypep x (specifier-type '(float (0d0)))))
898 ;; But a positive real to any power is well-defined.
899 (merged-interval-expt x y))
901 ;; a real to some power. The result could be a real
903 (float-or-complex-float-type (numeric-contagion x y)))))))
905 (defoptimizer (expt derive-type) ((x y))
906 (two-arg-derive-type x y #'expt-derive-type-aux #'expt))
908 ;;; Note we must assume that a type including 0.0 may also include
909 ;;; -0.0 and thus the result may be complex -infinity + i*pi.
910 (defun log-derive-type-aux-1 (x)
911 (elfun-derive-type-simple x #'log 0d0 nil nil nil))
913 (defun log-derive-type-aux-2 (x y same-arg)
914 (let ((log-x (log-derive-type-aux-1 x))
915 (log-y (log-derive-type-aux-1 y))
916 (accumulated-list nil))
917 ;; LOG-X or LOG-Y might be union types. We need to run through
918 ;; the union types ourselves because /-DERIVE-TYPE-AUX doesn't.
919 (dolist (x-type (prepare-arg-for-derive-type log-x))
920 (dolist (y-type (prepare-arg-for-derive-type log-y))
921 (push (/-derive-type-aux x-type y-type same-arg) accumulated-list)))
922 (apply #'type-union (flatten-list accumulated-list))))
924 (defoptimizer (log derive-type) ((x &optional y))
926 (two-arg-derive-type x y #'log-derive-type-aux-2 #'log)
927 (one-arg-derive-type x #'log-derive-type-aux-1 #'log)))
929 (defun atan-derive-type-aux-1 (y)
930 (elfun-derive-type-simple y #'atan nil nil (- (/ pi 2)) (/ pi 2)))
932 (defun atan-derive-type-aux-2 (y x same-arg)
933 (declare (ignore same-arg))
934 ;; The hard case with two args. We just return the max bounds.
935 (let ((result-type (numeric-contagion y x)))
936 (cond ((and (numeric-type-real-p x)
937 (numeric-type-real-p y))
938 (let* (;; FIXME: This expression for FORMAT seems to
939 ;; appear multiple times, and should be factored out.
940 (format (case (numeric-type-class result-type)
941 ((integer rational) 'single-float)
942 (t (numeric-type-format result-type))))
943 (bound-format (or format 'float)))
944 (make-numeric-type :class 'float
947 :low (coerce (- pi) bound-format)
948 :high (coerce pi bound-format))))
950 ;; The result is a float or a complex number
951 (float-or-complex-float-type result-type)))))
953 (defoptimizer (atan derive-type) ((y &optional x))
955 (two-arg-derive-type y x #'atan-derive-type-aux-2 #'atan)
956 (one-arg-derive-type y #'atan-derive-type-aux-1 #'atan)))
958 (defun cosh-derive-type-aux (x)
959 ;; We note that cosh x = cosh |x| for all real x.
960 (elfun-derive-type-simple
961 (if (numeric-type-real-p x)
962 (abs-derive-type-aux x)
964 #'cosh nil nil 0 nil))
966 (defoptimizer (cosh derive-type) ((num))
967 (one-arg-derive-type num #'cosh-derive-type-aux #'cosh))
969 (defun phase-derive-type-aux (arg)
970 (let* ((format (case (numeric-type-class arg)
971 ((integer rational) 'single-float)
972 (t (numeric-type-format arg))))
973 (bound-type (or format 'float)))
974 (cond ((numeric-type-real-p arg)
975 (case (interval-range-info (numeric-type->interval arg) 0.0)
977 ;; The number is positive, so the phase is 0.
978 (make-numeric-type :class 'float
981 :low (coerce 0 bound-type)
982 :high (coerce 0 bound-type)))
984 ;; The number is always negative, so the phase is pi.
985 (make-numeric-type :class 'float
988 :low (coerce pi bound-type)
989 :high (coerce pi bound-type)))
991 ;; We can't tell. The result is 0 or pi. Use a union
994 (make-numeric-type :class 'float
997 :low (coerce 0 bound-type)
998 :high (coerce 0 bound-type))
999 (make-numeric-type :class 'float
1002 :low (coerce pi bound-type)
1003 :high (coerce pi bound-type))))))
1005 ;; We have a complex number. The answer is the range -pi
1006 ;; to pi. (-pi is included because we have -0.)
1007 (make-numeric-type :class 'float
1010 :low (coerce (- pi) bound-type)
1011 :high (coerce pi bound-type))))))
1013 (defoptimizer (phase derive-type) ((num))
1014 (one-arg-derive-type num #'phase-derive-type-aux #'phase))
1018 (deftransform realpart ((x) ((complex rational)) *)
1019 '(sb!kernel:%realpart x))
1020 (deftransform imagpart ((x) ((complex rational)) *)
1021 '(sb!kernel:%imagpart x))
1023 ;;; Make REALPART and IMAGPART return the appropriate types. This
1024 ;;; should help a lot in optimized code.
1025 (defun realpart-derive-type-aux (type)
1026 (let ((class (numeric-type-class type))
1027 (format (numeric-type-format type)))
1028 (cond ((numeric-type-real-p type)
1029 ;; The realpart of a real has the same type and range as
1031 (make-numeric-type :class class
1034 :low (numeric-type-low type)
1035 :high (numeric-type-high type)))
1037 ;; We have a complex number. The result has the same type
1038 ;; as the real part, except that it's real, not complex,
1040 (make-numeric-type :class class
1043 :low (numeric-type-low type)
1044 :high (numeric-type-high type))))))
1045 #-sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
1046 (defoptimizer (realpart derive-type) ((num))
1047 (one-arg-derive-type num #'realpart-derive-type-aux #'realpart))
1048 (defun imagpart-derive-type-aux (type)
1049 (let ((class (numeric-type-class type))
1050 (format (numeric-type-format type)))
1051 (cond ((numeric-type-real-p type)
1052 ;; The imagpart of a real has the same type as the input,
1053 ;; except that it's zero.
1054 (let ((bound-format (or format class 'real)))
1055 (make-numeric-type :class class
1058 :low (coerce 0 bound-format)
1059 :high (coerce 0 bound-format))))
1061 ;; We have a complex number. The result has the same type as
1062 ;; the imaginary part, except that it's real, not complex,
1064 (make-numeric-type :class class
1067 :low (numeric-type-low type)
1068 :high (numeric-type-high type))))))
1069 #-sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
1070 (defoptimizer (imagpart derive-type) ((num))
1071 (one-arg-derive-type num #'imagpart-derive-type-aux #'imagpart))
1073 (defun complex-derive-type-aux-1 (re-type)
1074 (if (numeric-type-p re-type)
1075 (make-numeric-type :class (numeric-type-class re-type)
1076 :format (numeric-type-format re-type)
1077 :complexp (if (csubtypep re-type
1078 (specifier-type 'rational))
1081 :low (numeric-type-low re-type)
1082 :high (numeric-type-high re-type))
1083 (specifier-type 'complex)))
1085 (defun complex-derive-type-aux-2 (re-type im-type same-arg)
1086 (declare (ignore same-arg))
1087 (if (and (numeric-type-p re-type)
1088 (numeric-type-p im-type))
1089 ;; Need to check to make sure numeric-contagion returns the
1090 ;; right type for what we want here.
1092 ;; Also, what about rational canonicalization, like (complex 5 0)
1093 ;; is 5? So, if the result must be complex, we make it so.
1094 ;; If the result might be complex, which happens only if the
1095 ;; arguments are rational, we make it a union type of (or
1096 ;; rational (complex rational)).
1097 (let* ((element-type (numeric-contagion re-type im-type))
1098 (rat-result-p (csubtypep element-type
1099 (specifier-type 'rational))))
1101 (type-union element-type
1103 `(complex ,(numeric-type-class element-type))))
1104 (make-numeric-type :class (numeric-type-class element-type)
1105 :format (numeric-type-format element-type)
1106 :complexp (if rat-result-p
1109 (specifier-type 'complex)))
1111 #-sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
1112 (defoptimizer (complex derive-type) ((re &optional im))
1114 (two-arg-derive-type re im #'complex-derive-type-aux-2 #'complex)
1115 (one-arg-derive-type re #'complex-derive-type-aux-1 #'complex)))
1117 ;;; Define some transforms for complex operations. We do this in lieu
1118 ;;; of complex operation VOPs.
1119 (macrolet ((frob (type)
1122 (deftransform %negate ((z) ((complex ,type)) *)
1123 '(complex (%negate (realpart z)) (%negate (imagpart z))))
1124 ;; complex addition and subtraction
1125 (deftransform + ((w z) ((complex ,type) (complex ,type)) *)
1126 '(complex (+ (realpart w) (realpart z))
1127 (+ (imagpart w) (imagpart z))))
1128 (deftransform - ((w z) ((complex ,type) (complex ,type)) *)
1129 '(complex (- (realpart w) (realpart z))
1130 (- (imagpart w) (imagpart z))))
1131 ;; Add and subtract a complex and a real.
1132 (deftransform + ((w z) ((complex ,type) real) *)
1133 '(complex (+ (realpart w) z) (imagpart w)))
1134 (deftransform + ((z w) (real (complex ,type)) *)
1135 '(complex (+ (realpart w) z) (imagpart w)))
1136 ;; Add and subtract a real and a complex number.
1137 (deftransform - ((w z) ((complex ,type) real) *)
1138 '(complex (- (realpart w) z) (imagpart w)))
1139 (deftransform - ((z w) (real (complex ,type)) *)
1140 '(complex (- z (realpart w)) (- (imagpart w))))
1141 ;; Multiply and divide two complex numbers.
1142 (deftransform * ((x y) ((complex ,type) (complex ,type)) *)
1143 '(let* ((rx (realpart x))
1147 (complex (- (* rx ry) (* ix iy))
1148 (+ (* rx iy) (* ix ry)))))
1149 (deftransform / ((x y) ((complex ,type) (complex ,type)) *)
1150 '(let* ((rx (realpart x))
1154 (if (> (abs ry) (abs iy))
1155 (let* ((r (/ iy ry))
1156 (dn (* ry (+ 1 (* r r)))))
1157 (complex (/ (+ rx (* ix r)) dn)
1158 (/ (- ix (* rx r)) dn)))
1159 (let* ((r (/ ry iy))
1160 (dn (* iy (+ 1 (* r r)))))
1161 (complex (/ (+ (* rx r) ix) dn)
1162 (/ (- (* ix r) rx) dn))))))
1163 ;; Multiply a complex by a real or vice versa.
1164 (deftransform * ((w z) ((complex ,type) real) *)
1165 '(complex (* (realpart w) z) (* (imagpart w) z)))
1166 (deftransform * ((z w) (real (complex ,type)) *)
1167 '(complex (* (realpart w) z) (* (imagpart w) z)))
1168 ;; Divide a complex by a real.
1169 (deftransform / ((w z) ((complex ,type) real) *)
1170 '(complex (/ (realpart w) z) (/ (imagpart w) z)))
1171 ;; conjugate of complex number
1172 (deftransform conjugate ((z) ((complex ,type)) *)
1173 '(complex (realpart z) (- (imagpart z))))
1175 (deftransform cis ((z) ((,type)) *)
1176 '(complex (cos z) (sin z)))
1178 (deftransform = ((w z) ((complex ,type) (complex ,type)) *)
1179 '(and (= (realpart w) (realpart z))
1180 (= (imagpart w) (imagpart z))))
1181 (deftransform = ((w z) ((complex ,type) real) *)
1182 '(and (= (realpart w) z) (zerop (imagpart w))))
1183 (deftransform = ((w z) (real (complex ,type)) *)
1184 '(and (= (realpart z) w) (zerop (imagpart z)))))))
1187 (frob double-float))
1189 ;;; Here are simple optimizers for SIN, COS, and TAN. They do not
1190 ;;; produce a minimal range for the result; the result is the widest
1191 ;;; possible answer. This gets around the problem of doing range
1192 ;;; reduction correctly but still provides useful results when the
1193 ;;; inputs are union types.
1194 #-sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
1196 (defun trig-derive-type-aux (arg domain fcn
1197 &optional def-lo def-hi (increasingp t))
1200 (cond ((eq (numeric-type-complexp arg) :complex)
1201 (make-numeric-type :class (numeric-type-class arg)
1202 :format (numeric-type-format arg)
1203 :complexp :complex))
1204 ((numeric-type-real-p arg)
1205 (let* ((format (case (numeric-type-class arg)
1206 ((integer rational) 'single-float)
1207 (t (numeric-type-format arg))))
1208 (bound-type (or format 'float)))
1209 ;; If the argument is a subset of the "principal" domain
1210 ;; of the function, we can compute the bounds because
1211 ;; the function is monotonic. We can't do this in
1212 ;; general for these periodic functions because we can't
1213 ;; (and don't want to) do the argument reduction in
1214 ;; exactly the same way as the functions themselves do
1216 (if (csubtypep arg domain)
1217 (let ((res-lo (bound-func fcn (numeric-type-low arg)))
1218 (res-hi (bound-func fcn (numeric-type-high arg))))
1220 (rotatef res-lo res-hi))
1224 :low (coerce-numeric-bound res-lo bound-type)
1225 :high (coerce-numeric-bound res-hi bound-type)))
1229 :low (and def-lo (coerce def-lo bound-type))
1230 :high (and def-hi (coerce def-hi bound-type))))))
1232 (float-or-complex-float-type arg def-lo def-hi))))))
1234 (defoptimizer (sin derive-type) ((num))
1235 (one-arg-derive-type
1238 ;; Derive the bounds if the arg is in [-pi/2, pi/2].
1239 (trig-derive-type-aux
1241 (specifier-type `(float ,(- (/ pi 2)) ,(/ pi 2)))
1246 (defoptimizer (cos derive-type) ((num))
1247 (one-arg-derive-type
1250 ;; Derive the bounds if the arg is in [0, pi].
1251 (trig-derive-type-aux arg
1252 (specifier-type `(float 0d0 ,pi))
1258 (defoptimizer (tan derive-type) ((num))
1259 (one-arg-derive-type
1262 ;; Derive the bounds if the arg is in [-pi/2, pi/2].
1263 (trig-derive-type-aux arg
1264 (specifier-type `(float ,(- (/ pi 2)) ,(/ pi 2)))
1269 ;;; CONJUGATE always returns the same type as the input type.
1271 ;;; FIXME: ANSI allows any subtype of REAL for the components of COMPLEX.
1272 ;;; So what if the input type is (COMPLEX (SINGLE-FLOAT 0 1))?
1273 (defoptimizer (conjugate derive-type) ((num))
1274 (continuation-type num))
1276 (defoptimizer (cis derive-type) ((num))
1277 (one-arg-derive-type num
1279 (sb!c::specifier-type
1280 `(complex ,(or (numeric-type-format arg) 'float))))
1285 ;;;; TRUNCATE, FLOOR, CEILING, and ROUND
1287 (macrolet ((define-frobs (fun ufun)
1289 (defknown ,ufun (real) integer (movable foldable flushable))
1290 (deftransform ,fun ((x &optional by)
1292 (constant-arg (member 1))))
1293 '(let ((res (,ufun x)))
1294 (values res (- x res)))))))
1295 (define-frobs truncate %unary-truncate)
1296 (define-frobs round %unary-round))
1298 ;;; Convert (TRUNCATE x y) to the obvious implementation. We only want
1299 ;;; this when under certain conditions and let the generic TRUNCATE
1300 ;;; handle the rest. (Note: if Y = 1, the divide and multiply by Y
1301 ;;; should be removed by other DEFTRANSFORMs.)
1302 (deftransform truncate ((x &optional y)
1303 (float &optional (or float integer)))
1304 (let ((defaulted-y (if y 'y 1)))
1305 `(let ((res (%unary-truncate (/ x ,defaulted-y))))
1306 (values res (- x (* ,defaulted-y res))))))
1308 (deftransform floor ((number &optional divisor)
1309 (float &optional (or integer float)))
1310 (let ((defaulted-divisor (if divisor 'divisor 1)))
1311 `(multiple-value-bind (tru rem) (truncate number ,defaulted-divisor)
1312 (if (and (not (zerop rem))
1313 (if (minusp ,defaulted-divisor)
1316 (values (1- tru) (+ rem ,defaulted-divisor))
1317 (values tru rem)))))
1319 (deftransform ceiling ((number &optional divisor)
1320 (float &optional (or integer float)))
1321 (let ((defaulted-divisor (if divisor 'divisor 1)))
1322 `(multiple-value-bind (tru rem) (truncate number ,defaulted-divisor)
1323 (if (and (not (zerop rem))
1324 (if (minusp ,defaulted-divisor)
1327 (values (1+ tru) (- rem ,defaulted-divisor))
1328 (values tru rem)))))