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) *)
24 (deftransform float ((n f) (* double-float) *)
27 (deftransform %single-float ((n) (single-float) *)
30 (deftransform %double-float ((n) (double-float) *)
34 (macrolet ((frob (fun type)
35 `(deftransform random ((num &optional state)
36 (,type &optional *) *)
37 "Use inline float operations."
38 '(,fun num (or state *random-state*)))))
39 (frob %random-single-float single-float)
40 (frob %random-double-float double-float))
42 ;;; Mersenne Twister RNG
44 ;;; FIXME: It's unpleasant to have RANDOM functionality scattered
45 ;;; through the code this way. It would be nice to move this into the
46 ;;; same file as the other RANDOM definitions.
47 (deftransform random ((num &optional state)
48 ((integer 1 #.(expt 2 32)) &optional *))
49 ;; FIXME: I almost conditionalized this as #!+sb-doc. Find some way
50 ;; of automatically finding #!+sb-doc in proximity to DEFTRANSFORM
51 ;; to let me scan for places that I made this mistake and didn't
53 "use inline (UNSIGNED-BYTE 32) operations"
54 (let ((num-high (numeric-type-high (continuation-type num))))
56 (give-up-ir1-transform))
57 (cond ((constant-continuation-p num)
58 ;; Check the worst case sum absolute error for the random number
60 (let ((rem (rem (expt 2 32) num-high)))
61 (unless (< (/ (* 2 rem (- num-high rem)) num-high (expt 2 32))
62 (expt 2 (- sb!kernel::random-integer-extra-bits)))
63 (give-up-ir1-transform
64 "The random number expectations are inaccurate."))
65 (if (= num-high (expt 2 32))
66 '(random-chunk (or state *random-state*))
67 #!-x86 '(rem (random-chunk (or state *random-state*)) num)
69 ;; Use multiplication, which is faster.
70 '(values (sb!bignum::%multiply
71 (random-chunk (or state *random-state*))
73 ((> num-high random-fixnum-max)
74 (give-up-ir1-transform
75 "The range is too large to ensure an accurate result."))
77 ((< num-high (expt 2 32))
78 '(values (sb!bignum::%multiply (random-chunk (or state
82 '(rem (random-chunk (or state *random-state*)) num)))))
86 (defknown make-single-float ((signed-byte 32)) single-float
87 (movable foldable flushable))
89 (defknown make-double-float ((signed-byte 32) (unsigned-byte 32)) double-float
90 (movable foldable flushable))
92 (defknown single-float-bits (single-float) (signed-byte 32)
93 (movable foldable flushable))
95 (defknown double-float-high-bits (double-float) (signed-byte 32)
96 (movable foldable flushable))
98 (defknown double-float-low-bits (double-float) (unsigned-byte 32)
99 (movable foldable flushable))
101 (deftransform float-sign ((float &optional float2)
102 (single-float &optional single-float) *)
104 (let ((temp (gensym)))
105 `(let ((,temp (abs float2)))
106 (if (minusp (single-float-bits float)) (- ,temp) ,temp)))
107 '(if (minusp (single-float-bits float)) -1f0 1f0)))
109 (deftransform float-sign ((float &optional float2)
110 (double-float &optional double-float) *)
112 (let ((temp (gensym)))
113 `(let ((,temp (abs float2)))
114 (if (minusp (double-float-high-bits float)) (- ,temp) ,temp)))
115 '(if (minusp (double-float-high-bits float)) -1d0 1d0)))
117 ;;;; DECODE-FLOAT, INTEGER-DECODE-FLOAT, and SCALE-FLOAT
119 (defknown decode-single-float (single-float)
120 (values single-float single-float-exponent (single-float -1f0 1f0))
121 (movable foldable flushable))
123 (defknown decode-double-float (double-float)
124 (values double-float double-float-exponent (double-float -1d0 1d0))
125 (movable foldable flushable))
127 (defknown integer-decode-single-float (single-float)
128 (values single-float-significand single-float-int-exponent (integer -1 1))
129 (movable foldable flushable))
131 (defknown integer-decode-double-float (double-float)
132 (values double-float-significand double-float-int-exponent (integer -1 1))
133 (movable foldable flushable))
135 (defknown scale-single-float (single-float fixnum) single-float
136 (movable foldable flushable))
138 (defknown scale-double-float (double-float fixnum) double-float
139 (movable foldable flushable))
141 (deftransform decode-float ((x) (single-float) *)
142 '(decode-single-float x))
144 (deftransform decode-float ((x) (double-float) *)
145 '(decode-double-float x))
147 (deftransform integer-decode-float ((x) (single-float) *)
148 '(integer-decode-single-float x))
150 (deftransform integer-decode-float ((x) (double-float) *)
151 '(integer-decode-double-float x))
153 (deftransform scale-float ((f ex) (single-float *) *)
154 (if (and #!+x86 t #!-x86 nil
155 (csubtypep (continuation-type ex)
156 (specifier-type '(signed-byte 32))))
157 '(coerce (%scalbn (coerce f 'double-float) ex) 'single-float)
158 '(scale-single-float f ex)))
160 (deftransform scale-float ((f ex) (double-float *) *)
161 (if (and #!+x86 t #!-x86 nil
162 (csubtypep (continuation-type ex)
163 (specifier-type '(signed-byte 32))))
165 '(scale-double-float f ex)))
167 ;;; What is the CROSS-FLOAT-INFINITY-KLUDGE?
169 ;;; SBCL's own implementation of floating point supports floating
170 ;;; point infinities. Some of the old CMU CL :PROPAGATE-FLOAT-TYPE and
171 ;;; :PROPAGATE-FUN-TYPE code, like the DEFOPTIMIZERs below, uses this
172 ;;; floating point support. Thus, we have to avoid running it on the
173 ;;; cross-compilation host, since we're not guaranteed that the
174 ;;; cross-compilation host will support floating point infinities.
176 ;;; If we wanted to live dangerously, we could conditionalize the code
177 ;;; with #+(OR SBCL SB-XC) instead. That way, if the cross-compilation
178 ;;; host happened to be SBCL, we'd be able to run the infinity-using
180 ;;; * SBCL itself gets built with more complete optimization.
182 ;;; * You get a different SBCL depending on what your cross-compilation
184 ;;; So far the pros and cons seem seem to be mostly academic, since
185 ;;; AFAIK (WHN 2001-08-28) the propagate-foo-type optimizations aren't
186 ;;; actually important in compiling SBCL itself. If this changes, then
187 ;;; we have to decide:
188 ;;; * Go for simplicity, leaving things as they are.
189 ;;; * Go for performance at the expense of conceptual clarity,
190 ;;; using #+(OR SBCL SB-XC) and otherwise leaving the build
192 ;;; * Go for performance at the expense of build time, using
193 ;;; #+(OR SBCL SB-XC) and also making SBCL do not just
194 ;;; make-host-1.sh and make-host-2.sh, but a third step
195 ;;; make-host-3.sh where it builds itself under itself. (Such a
196 ;;; 3-step build process could also help with other things, e.g.
197 ;;; using specialized arrays to represent debug information.)
198 ;;; * Rewrite the code so that it doesn't depend on unportable
199 ;;; floating point infinities.
201 ;;; optimizers for SCALE-FLOAT. If the float has bounds, new bounds
202 ;;; are computed for the result, if possible.
203 #-sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
206 (defun scale-float-derive-type-aux (f ex same-arg)
207 (declare (ignore same-arg))
208 (flet ((scale-bound (x n)
209 ;; We need to be a bit careful here and catch any overflows
210 ;; that might occur. We can ignore underflows which become
214 (scale-float (type-bound-number x) n)
215 (floating-point-overflow ()
218 (when (and (numeric-type-p f) (numeric-type-p ex))
219 (let ((f-lo (numeric-type-low f))
220 (f-hi (numeric-type-high f))
221 (ex-lo (numeric-type-low ex))
222 (ex-hi (numeric-type-high ex))
225 (when (and f-hi ex-hi)
226 (setf new-hi (scale-bound f-hi ex-hi)))
227 (when (and f-lo ex-lo)
228 (setf new-lo (scale-bound f-lo ex-lo)))
229 (make-numeric-type :class (numeric-type-class f)
230 :format (numeric-type-format f)
234 (defoptimizer (scale-single-float derive-type) ((f ex))
235 (two-arg-derive-type f ex #'scale-float-derive-type-aux
236 #'scale-single-float t))
237 (defoptimizer (scale-double-float derive-type) ((f ex))
238 (two-arg-derive-type f ex #'scale-float-derive-type-aux
239 #'scale-double-float t))
241 ;;; DEFOPTIMIZERs for %SINGLE-FLOAT and %DOUBLE-FLOAT. This makes the
242 ;;; FLOAT function return the correct ranges if the input has some
243 ;;; defined range. Quite useful if we want to convert some type of
244 ;;; bounded integer into a float.
247 (let ((aux-name (symbolicate fun "-DERIVE-TYPE-AUX")))
249 (defun ,aux-name (num)
250 ;; When converting a number to a float, the limits are
252 (let* ((lo (bound-func (lambda (x)
254 (numeric-type-low num)))
255 (hi (bound-func (lambda (x)
257 (numeric-type-high num))))
258 (specifier-type `(,',type ,(or lo '*) ,(or hi '*)))))
260 (defoptimizer (,fun derive-type) ((num))
261 (one-arg-derive-type num #',aux-name #',fun))))))
262 (frob %single-float single-float)
263 (frob %double-float double-float))
268 ;;; Do some stuff to recognize when the loser is doing mixed float and
269 ;;; rational arithmetic, or different float types, and fix it up. If
270 ;;; we don't, he won't even get so much as an efficiency note.
271 (deftransform float-contagion-arg1 ((x y) * * :defun-only t :node node)
272 `(,(continuation-fun-name (basic-combination-fun node))
274 (deftransform float-contagion-arg2 ((x y) * * :defun-only t :node node)
275 `(,(continuation-fun-name (basic-combination-fun node))
278 (dolist (x '(+ * / -))
279 (%deftransform x '(function (rational float) *) #'float-contagion-arg1)
280 (%deftransform x '(function (float rational) *) #'float-contagion-arg2))
282 (dolist (x '(= < > + * / -))
283 (%deftransform x '(function (single-float double-float) *)
284 #'float-contagion-arg1)
285 (%deftransform x '(function (double-float single-float) *)
286 #'float-contagion-arg2))
288 ;;; Prevent ZEROP, PLUSP, and MINUSP from losing horribly. We can't in
289 ;;; general float rational args to comparison, since Common Lisp
290 ;;; semantics says we are supposed to compare as rationals, but we can
291 ;;; do it for any rational that has a precise representation as a
292 ;;; float (such as 0).
293 (macrolet ((frob (op)
294 `(deftransform ,op ((x y) (float rational) *)
295 "open-code FLOAT to RATIONAL comparison"
296 (unless (constant-continuation-p y)
297 (give-up-ir1-transform
298 "The RATIONAL value isn't known at compile time."))
299 (let ((val (continuation-value y)))
300 (unless (eql (rational (float val)) val)
301 (give-up-ir1-transform
302 "~S doesn't have a precise float representation."
304 `(,',op x (float y x)))))
309 ;;;; irrational derive-type methods
311 ;;; Derive the result to be float for argument types in the
312 ;;; appropriate domain.
313 #+sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
314 (dolist (stuff '((asin (real -1.0 1.0))
315 (acos (real -1.0 1.0))
317 (atanh (real -1.0 1.0))
319 (destructuring-bind (name type) stuff
320 (let ((type (specifier-type type)))
321 (setf (fun-info-derive-type (fun-info-or-lose name))
323 (declare (type combination call))
324 (when (csubtypep (continuation-type
325 (first (combination-args call)))
327 (specifier-type 'float)))))))
329 #+sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
330 (defoptimizer (log derive-type) ((x &optional y))
331 (when (and (csubtypep (continuation-type x)
332 (specifier-type '(real 0.0)))
334 (csubtypep (continuation-type y)
335 (specifier-type '(real 0.0)))))
336 (specifier-type 'float)))
338 ;;;; irrational transforms
340 (defknown (%tan %sinh %asinh %atanh %log %logb %log10 %tan-quick)
341 (double-float) double-float
342 (movable foldable flushable))
344 (defknown (%sin %cos %tanh %sin-quick %cos-quick)
345 (double-float) (double-float -1.0d0 1.0d0)
346 (movable foldable flushable))
348 (defknown (%asin %atan)
350 (double-float #.(coerce (- (/ pi 2)) 'double-float)
351 #.(coerce (/ pi 2) 'double-float))
352 (movable foldable flushable))
355 (double-float) (double-float 0.0d0 #.(coerce pi 'double-float))
356 (movable foldable flushable))
359 (double-float) (double-float 1.0d0)
360 (movable foldable flushable))
362 (defknown (%acosh %exp %sqrt)
363 (double-float) (double-float 0.0d0)
364 (movable foldable flushable))
367 (double-float) (double-float -1d0)
368 (movable foldable flushable))
371 (double-float double-float) (double-float 0d0)
372 (movable foldable flushable))
375 (double-float double-float) double-float
376 (movable foldable flushable))
379 (double-float double-float)
380 (double-float #.(coerce (- pi) 'double-float)
381 #.(coerce pi 'double-float))
382 (movable foldable flushable))
385 (double-float double-float) double-float
386 (movable foldable flushable))
389 (double-float (signed-byte 32)) double-float
390 (movable foldable flushable))
393 (double-float) double-float
394 (movable foldable flushable))
396 (macrolet ((def (name prim rtype)
398 (deftransform ,name ((x) (single-float) ,rtype)
399 `(coerce (,',prim (coerce x 'double-float)) 'single-float))
400 (deftransform ,name ((x) (double-float) ,rtype)
404 (def sqrt %sqrt float)
405 (def asin %asin float)
406 (def acos %acos float)
412 (def acosh %acosh float)
413 (def atanh %atanh float))
415 ;;; The argument range is limited on the x86 FP trig. functions. A
416 ;;; post-test can detect a failure (and load a suitable result), but
417 ;;; this test is avoided if possible.
418 (macrolet ((def (name prim prim-quick)
419 (declare (ignorable prim-quick))
421 (deftransform ,name ((x) (single-float) *)
422 #!+x86 (cond ((csubtypep (continuation-type x)
423 (specifier-type '(single-float
424 (#.(- (expt 2f0 64)))
426 `(coerce (,',prim-quick (coerce x 'double-float))
430 "unable to avoid inline argument range check~@
431 because the argument range (~S) was not within 2^64"
432 (type-specifier (continuation-type x)))
433 `(coerce (,',prim (coerce x 'double-float)) 'single-float)))
434 #!-x86 `(coerce (,',prim (coerce x 'double-float)) 'single-float))
435 (deftransform ,name ((x) (double-float) *)
436 #!+x86 (cond ((csubtypep (continuation-type x)
437 (specifier-type '(double-float
438 (#.(- (expt 2d0 64)))
443 "unable to avoid inline argument range check~@
444 because the argument range (~S) was not within 2^64"
445 (type-specifier (continuation-type x)))
447 #!-x86 `(,',prim x)))))
448 (def sin %sin %sin-quick)
449 (def cos %cos %cos-quick)
450 (def tan %tan %tan-quick))
452 (deftransform atan ((x y) (single-float single-float) *)
453 `(coerce (%atan2 (coerce x 'double-float) (coerce y 'double-float))
455 (deftransform atan ((x y) (double-float double-float) *)
458 (deftransform expt ((x y) ((single-float 0f0) single-float) *)
459 `(coerce (%pow (coerce x 'double-float) (coerce y 'double-float))
461 (deftransform expt ((x y) ((double-float 0d0) double-float) *)
463 (deftransform expt ((x y) ((single-float 0f0) (signed-byte 32)) *)
464 `(coerce (%pow (coerce x 'double-float) (coerce y 'double-float))
466 (deftransform expt ((x y) ((double-float 0d0) (signed-byte 32)) *)
467 `(%pow x (coerce y 'double-float)))
469 ;;; ANSI says log with base zero returns zero.
470 (deftransform log ((x y) (float float) float)
471 '(if (zerop y) y (/ (log x) (log y))))
473 ;;; Handle some simple transformations.
475 (deftransform abs ((x) ((complex double-float)) double-float)
476 '(%hypot (realpart x) (imagpart x)))
478 (deftransform abs ((x) ((complex single-float)) single-float)
479 '(coerce (%hypot (coerce (realpart x) 'double-float)
480 (coerce (imagpart x) 'double-float))
483 (deftransform phase ((x) ((complex double-float)) double-float)
484 '(%atan2 (imagpart x) (realpart x)))
486 (deftransform phase ((x) ((complex single-float)) single-float)
487 '(coerce (%atan2 (coerce (imagpart x) 'double-float)
488 (coerce (realpart x) 'double-float))
491 (deftransform phase ((x) ((float)) float)
492 '(if (minusp (float-sign x))
496 ;;; The number is of type REAL.
497 (defun numeric-type-real-p (type)
498 (and (numeric-type-p type)
499 (eq (numeric-type-complexp type) :real)))
501 ;;; Coerce a numeric type bound to the given type while handling
502 ;;; exclusive bounds.
503 (defun coerce-numeric-bound (bound type)
506 (list (coerce (car bound) type))
507 (coerce bound type))))
509 #-sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
512 ;;;; optimizers for elementary functions
514 ;;;; These optimizers compute the output range of the elementary
515 ;;;; function, based on the domain of the input.
517 ;;; Generate a specifier for a complex type specialized to the same
518 ;;; type as the argument.
519 (defun complex-float-type (arg)
520 (declare (type numeric-type arg))
521 (let* ((format (case (numeric-type-class arg)
522 ((integer rational) 'single-float)
523 (t (numeric-type-format arg))))
524 (float-type (or format 'float)))
525 (specifier-type `(complex ,float-type))))
527 ;;; Compute a specifier like '(OR FLOAT (COMPLEX FLOAT)), except float
528 ;;; should be the right kind of float. Allow bounds for the float
530 (defun float-or-complex-float-type (arg &optional lo hi)
531 (declare (type numeric-type arg))
532 (let* ((format (case (numeric-type-class arg)
533 ((integer rational) 'single-float)
534 (t (numeric-type-format arg))))
535 (float-type (or format 'float))
536 (lo (coerce-numeric-bound lo float-type))
537 (hi (coerce-numeric-bound hi float-type)))
538 (specifier-type `(or (,float-type ,(or lo '*) ,(or hi '*))
539 (complex ,float-type)))))
541 ;;; Test whether the numeric-type ARG is within in domain specified by
542 ;;; DOMAIN-LOW and DOMAIN-HIGH, consider negative and positive zero to
543 ;;; be distinct as for the :NEGATIVE-ZERO-IS-NOT-ZERO feature. With
544 ;;; the :NEGATIVE-ZERO-IS-NOT-ZERO feature this could be handled by
545 ;;; the numeric subtype code in type.lisp.
546 (defun domain-subtypep (arg domain-low domain-high)
547 (declare (type numeric-type arg)
548 (type (or real null) domain-low domain-high))
549 (let* ((arg-lo (numeric-type-low arg))
550 (arg-lo-val (type-bound-number arg-lo))
551 (arg-hi (numeric-type-high arg))
552 (arg-hi-val (type-bound-number arg-hi)))
553 ;; Check that the ARG bounds are correctly canonicalized.
554 (when (and arg-lo (floatp arg-lo-val) (zerop arg-lo-val) (consp arg-lo)
555 (minusp (float-sign arg-lo-val)))
556 (compiler-note "float zero bound ~S not correctly canonicalized?" arg-lo)
557 (setq arg-lo '(0l0) arg-lo-val 0l0))
558 (when (and arg-hi (zerop arg-hi-val) (floatp arg-hi-val) (consp arg-hi)
559 (plusp (float-sign arg-hi-val)))
560 (compiler-note "float zero bound ~S not correctly canonicalized?" arg-hi)
561 (setq arg-hi '(-0l0) arg-hi-val -0l0))
562 (and (or (null domain-low)
563 (and arg-lo (>= arg-lo-val domain-low)
564 (not (and (zerop domain-low) (floatp domain-low)
565 (plusp (float-sign domain-low))
566 (zerop arg-lo-val) (floatp arg-lo-val)
568 (plusp (float-sign arg-lo-val))
569 (minusp (float-sign arg-lo-val)))))))
570 (or (null domain-high)
571 (and arg-hi (<= arg-hi-val domain-high)
572 (not (and (zerop domain-high) (floatp domain-high)
573 (minusp (float-sign domain-high))
574 (zerop arg-hi-val) (floatp arg-hi-val)
576 (minusp (float-sign arg-hi-val))
577 (plusp (float-sign arg-hi-val))))))))))
579 ;;; Handle monotonic functions of a single variable whose domain is
580 ;;; possibly part of the real line. ARG is the variable, FCN is the
581 ;;; function, and DOMAIN is a specifier that gives the (real) domain
582 ;;; of the function. If ARG is a subset of the DOMAIN, we compute the
583 ;;; bounds directly. Otherwise, we compute the bounds for the
584 ;;; intersection between ARG and DOMAIN, and then append a complex
585 ;;; result, which occurs for the parts of ARG not in the DOMAIN.
587 ;;; Negative and positive zero are considered distinct within
588 ;;; DOMAIN-LOW and DOMAIN-HIGH, as for the :negative-zero-is-not-zero
591 ;;; DEFAULT-LOW and DEFAULT-HIGH are the lower and upper bounds if we
592 ;;; can't compute the bounds using FCN.
593 (defun elfun-derive-type-simple (arg fcn domain-low domain-high
594 default-low default-high
595 &optional (increasingp t))
596 (declare (type (or null real) domain-low domain-high))
599 (cond ((eq (numeric-type-complexp arg) :complex)
600 (make-numeric-type :class (numeric-type-class arg)
601 :format (numeric-type-format arg)
603 ((numeric-type-real-p arg)
604 ;; The argument is real, so let's find the intersection
605 ;; between the argument and the domain of the function.
606 ;; We compute the bounds on the intersection, and for
607 ;; everything else, we return a complex number of the
609 (multiple-value-bind (intersection difference)
610 (interval-intersection/difference (numeric-type->interval arg)
616 ;; Process the intersection.
617 (let* ((low (interval-low intersection))
618 (high (interval-high intersection))
619 (res-lo (or (bound-func fcn (if increasingp low high))
621 (res-hi (or (bound-func fcn (if increasingp high low))
623 (format (case (numeric-type-class arg)
624 ((integer rational) 'single-float)
625 (t (numeric-type-format arg))))
626 (bound-type (or format 'float))
631 :low (coerce-numeric-bound res-lo bound-type)
632 :high (coerce-numeric-bound res-hi bound-type))))
633 ;; If the ARG is a subset of the domain, we don't
634 ;; have to worry about the difference, because that
636 (if (or (null difference)
637 ;; Check whether the arg is within the domain.
638 (domain-subtypep arg domain-low domain-high))
641 (specifier-type `(complex ,bound-type))))))
643 ;; No intersection so the result must be purely complex.
644 (complex-float-type arg)))))
646 (float-or-complex-float-type arg default-low default-high))))))
649 ((frob (name domain-low domain-high def-low-bnd def-high-bnd
650 &key (increasingp t))
651 (let ((num (gensym)))
652 `(defoptimizer (,name derive-type) ((,num))
656 (elfun-derive-type-simple arg #',name
657 ,domain-low ,domain-high
658 ,def-low-bnd ,def-high-bnd
661 ;; These functions are easy because they are defined for the whole
663 (frob exp nil nil 0 nil)
664 (frob sinh nil nil nil nil)
665 (frob tanh nil nil -1 1)
666 (frob asinh nil nil nil nil)
668 ;; These functions are only defined for part of the real line. The
669 ;; condition selects the desired part of the line.
670 (frob asin -1d0 1d0 (- (/ pi 2)) (/ pi 2))
671 ;; Acos is monotonic decreasing, so we need to swap the function
672 ;; values at the lower and upper bounds of the input domain.
673 (frob acos -1d0 1d0 0 pi :increasingp nil)
674 (frob acosh 1d0 nil nil nil)
675 (frob atanh -1d0 1d0 -1 1)
676 ;; Kahan says that (sqrt -0.0) is -0.0, so use a specifier that
678 (frob sqrt -0d0 nil 0 nil))
680 ;;; Compute bounds for (expt x y). This should be easy since (expt x
681 ;;; y) = (exp (* y (log x))). However, computations done this way
682 ;;; have too much roundoff. Thus we have to do it the hard way.
683 (defun safe-expt (x y)
689 ;;; Handle the case when x >= 1.
690 (defun interval-expt-> (x y)
691 (case (sb!c::interval-range-info y 0d0)
693 ;; Y is positive and log X >= 0. The range of exp(y * log(x)) is
694 ;; obviously non-negative. We just have to be careful for
695 ;; infinite bounds (given by nil).
696 (let ((lo (safe-expt (type-bound-number (sb!c::interval-low x))
697 (type-bound-number (sb!c::interval-low y))))
698 (hi (safe-expt (type-bound-number (sb!c::interval-high x))
699 (type-bound-number (sb!c::interval-high y)))))
700 (list (sb!c::make-interval :low (or lo 1) :high hi))))
702 ;; Y is negative and log x >= 0. The range of exp(y * log(x)) is
703 ;; obviously [0, 1]. However, underflow (nil) means 0 is the
705 (let ((lo (safe-expt (type-bound-number (sb!c::interval-high x))
706 (type-bound-number (sb!c::interval-low y))))
707 (hi (safe-expt (type-bound-number (sb!c::interval-low x))
708 (type-bound-number (sb!c::interval-high y)))))
709 (list (sb!c::make-interval :low (or lo 0) :high (or hi 1)))))
711 ;; Split the interval in half.
712 (destructuring-bind (y- y+)
713 (sb!c::interval-split 0 y t)
714 (list (interval-expt-> x y-)
715 (interval-expt-> x y+))))))
717 ;;; Handle the case when x <= 1
718 (defun interval-expt-< (x y)
719 (case (sb!c::interval-range-info x 0d0)
721 ;; The case of 0 <= x <= 1 is easy
722 (case (sb!c::interval-range-info y)
724 ;; Y is positive and log X <= 0. The range of exp(y * log(x)) is
725 ;; obviously [0, 1]. We just have to be careful for infinite bounds
727 (let ((lo (safe-expt (type-bound-number (sb!c::interval-low x))
728 (type-bound-number (sb!c::interval-high y))))
729 (hi (safe-expt (type-bound-number (sb!c::interval-high x))
730 (type-bound-number (sb!c::interval-low y)))))
731 (list (sb!c::make-interval :low (or lo 0) :high (or hi 1)))))
733 ;; Y is negative and log x <= 0. The range of exp(y * log(x)) is
734 ;; obviously [1, inf].
735 (let ((hi (safe-expt (type-bound-number (sb!c::interval-low x))
736 (type-bound-number (sb!c::interval-low y))))
737 (lo (safe-expt (type-bound-number (sb!c::interval-high x))
738 (type-bound-number (sb!c::interval-high y)))))
739 (list (sb!c::make-interval :low (or lo 1) :high hi))))
741 ;; Split the interval in half
742 (destructuring-bind (y- y+)
743 (sb!c::interval-split 0 y t)
744 (list (interval-expt-< x y-)
745 (interval-expt-< x y+))))))
747 ;; The case where x <= 0. Y MUST be an INTEGER for this to work!
748 ;; The calling function must insure this! For now we'll just
749 ;; return the appropriate unbounded float type.
750 (list (sb!c::make-interval :low nil :high nil)))
752 (destructuring-bind (neg pos)
753 (interval-split 0 x t t)
754 (list (interval-expt-< neg y)
755 (interval-expt-< pos y))))))
757 ;;; Compute bounds for (expt x y).
758 (defun interval-expt (x y)
759 (case (interval-range-info x 1)
762 (interval-expt-> x y))
765 (interval-expt-< x y))
767 (destructuring-bind (left right)
768 (interval-split 1 x t t)
769 (list (interval-expt left y)
770 (interval-expt right y))))))
772 (defun fixup-interval-expt (bnd x-int y-int x-type y-type)
773 (declare (ignore x-int))
774 ;; Figure out what the return type should be, given the argument
775 ;; types and bounds and the result type and bounds.
776 (cond ((csubtypep x-type (specifier-type 'integer))
777 ;; an integer to some power
778 (case (numeric-type-class y-type)
780 ;; Positive integer to an integer power is either an
781 ;; integer or a rational.
782 (let ((lo (or (interval-low bnd) '*))
783 (hi (or (interval-high bnd) '*)))
784 (if (and (interval-low y-int)
785 (>= (type-bound-number (interval-low y-int)) 0))
786 (specifier-type `(integer ,lo ,hi))
787 (specifier-type `(rational ,lo ,hi)))))
789 ;; Positive integer to rational power is either a rational
790 ;; or a single-float.
791 (let* ((lo (interval-low bnd))
792 (hi (interval-high bnd))
794 (floor (type-bound-number lo))
797 (ceiling (type-bound-number hi))
800 (bound-func #'float lo)
803 (bound-func #'float hi)
805 (specifier-type `(or (rational ,int-lo ,int-hi)
806 (single-float ,f-lo, f-hi)))))
808 ;; A positive integer to a float power is a float.
809 (modified-numeric-type y-type
810 :low (interval-low bnd)
811 :high (interval-high bnd)))
813 ;; A positive integer to a number is a number (for now).
814 (specifier-type 'number))))
815 ((csubtypep x-type (specifier-type 'rational))
816 ;; a rational to some power
817 (case (numeric-type-class y-type)
819 ;; A positive rational to an integer power is always a rational.
820 (specifier-type `(rational ,(or (interval-low bnd) '*)
821 ,(or (interval-high bnd) '*))))
823 ;; A positive rational to rational power is either a rational
824 ;; or a single-float.
825 (let* ((lo (interval-low bnd))
826 (hi (interval-high bnd))
828 (floor (type-bound-number lo))
831 (ceiling (type-bound-number hi))
834 (bound-func #'float lo)
837 (bound-func #'float hi)
839 (specifier-type `(or (rational ,int-lo ,int-hi)
840 (single-float ,f-lo, f-hi)))))
842 ;; A positive rational to a float power is a float.
843 (modified-numeric-type y-type
844 :low (interval-low bnd)
845 :high (interval-high bnd)))
847 ;; A positive rational to a number is a number (for now).
848 (specifier-type 'number))))
849 ((csubtypep x-type (specifier-type 'float))
850 ;; a float to some power
851 (case (numeric-type-class y-type)
852 ((or integer rational)
853 ;; A positive float to an integer or rational power is
857 :format (numeric-type-format x-type)
858 :low (interval-low bnd)
859 :high (interval-high bnd)))
861 ;; A positive float to a float power is a float of the
865 :format (float-format-max (numeric-type-format x-type)
866 (numeric-type-format y-type))
867 :low (interval-low bnd)
868 :high (interval-high bnd)))
870 ;; A positive float to a number is a number (for now)
871 (specifier-type 'number))))
873 ;; A number to some power is a number.
874 (specifier-type 'number))))
876 (defun merged-interval-expt (x y)
877 (let* ((x-int (numeric-type->interval x))
878 (y-int (numeric-type->interval y)))
879 (mapcar (lambda (type)
880 (fixup-interval-expt type x-int y-int x y))
881 (flatten-list (interval-expt x-int y-int)))))
883 (defun expt-derive-type-aux (x y same-arg)
884 (declare (ignore same-arg))
885 (cond ((or (not (numeric-type-real-p x))
886 (not (numeric-type-real-p y)))
887 ;; Use numeric contagion if either is not real.
888 (numeric-contagion x y))
889 ((csubtypep y (specifier-type 'integer))
890 ;; A real raised to an integer power is well-defined.
891 (merged-interval-expt x y))
893 ;; A real raised to a non-integral power can be a float or a
895 (cond ((or (csubtypep x (specifier-type '(rational 0)))
896 (csubtypep x (specifier-type '(float (0d0)))))
897 ;; But a positive real to any power is well-defined.
898 (merged-interval-expt x y))
900 ;; a real to some power. The result could be a real
902 (float-or-complex-float-type (numeric-contagion x y)))))))
904 (defoptimizer (expt derive-type) ((x y))
905 (two-arg-derive-type x y #'expt-derive-type-aux #'expt))
907 ;;; Note we must assume that a type including 0.0 may also include
908 ;;; -0.0 and thus the result may be complex -infinity + i*pi.
909 (defun log-derive-type-aux-1 (x)
910 (elfun-derive-type-simple x #'log 0d0 nil nil nil))
912 (defun log-derive-type-aux-2 (x y same-arg)
913 (let ((log-x (log-derive-type-aux-1 x))
914 (log-y (log-derive-type-aux-1 y))
915 (accumulated-list nil))
916 ;; LOG-X or LOG-Y might be union types. We need to run through
917 ;; the union types ourselves because /-DERIVE-TYPE-AUX doesn't.
918 (dolist (x-type (prepare-arg-for-derive-type log-x))
919 (dolist (y-type (prepare-arg-for-derive-type log-y))
920 (push (/-derive-type-aux x-type y-type same-arg) accumulated-list)))
921 (apply #'type-union (flatten-list accumulated-list))))
923 (defoptimizer (log derive-type) ((x &optional y))
925 (two-arg-derive-type x y #'log-derive-type-aux-2 #'log)
926 (one-arg-derive-type x #'log-derive-type-aux-1 #'log)))
928 (defun atan-derive-type-aux-1 (y)
929 (elfun-derive-type-simple y #'atan nil nil (- (/ pi 2)) (/ pi 2)))
931 (defun atan-derive-type-aux-2 (y x same-arg)
932 (declare (ignore same-arg))
933 ;; The hard case with two args. We just return the max bounds.
934 (let ((result-type (numeric-contagion y x)))
935 (cond ((and (numeric-type-real-p x)
936 (numeric-type-real-p y))
937 (let* (;; FIXME: This expression for FORMAT seems to
938 ;; appear multiple times, and should be factored out.
939 (format (case (numeric-type-class result-type)
940 ((integer rational) 'single-float)
941 (t (numeric-type-format result-type))))
942 (bound-format (or format 'float)))
943 (make-numeric-type :class 'float
946 :low (coerce (- pi) bound-format)
947 :high (coerce pi bound-format))))
949 ;; The result is a float or a complex number
950 (float-or-complex-float-type result-type)))))
952 (defoptimizer (atan derive-type) ((y &optional x))
954 (two-arg-derive-type y x #'atan-derive-type-aux-2 #'atan)
955 (one-arg-derive-type y #'atan-derive-type-aux-1 #'atan)))
957 (defun cosh-derive-type-aux (x)
958 ;; We note that cosh x = cosh |x| for all real x.
959 (elfun-derive-type-simple
960 (if (numeric-type-real-p x)
961 (abs-derive-type-aux x)
963 #'cosh nil nil 0 nil))
965 (defoptimizer (cosh derive-type) ((num))
966 (one-arg-derive-type num #'cosh-derive-type-aux #'cosh))
968 (defun phase-derive-type-aux (arg)
969 (let* ((format (case (numeric-type-class arg)
970 ((integer rational) 'single-float)
971 (t (numeric-type-format arg))))
972 (bound-type (or format 'float)))
973 (cond ((numeric-type-real-p arg)
974 (case (interval-range-info (numeric-type->interval arg) 0.0)
976 ;; The number is positive, so the phase is 0.
977 (make-numeric-type :class 'float
980 :low (coerce 0 bound-type)
981 :high (coerce 0 bound-type)))
983 ;; The number is always negative, so the phase is pi.
984 (make-numeric-type :class 'float
987 :low (coerce pi bound-type)
988 :high (coerce pi bound-type)))
990 ;; We can't tell. The result is 0 or pi. Use a union
993 (make-numeric-type :class 'float
996 :low (coerce 0 bound-type)
997 :high (coerce 0 bound-type))
998 (make-numeric-type :class 'float
1001 :low (coerce pi bound-type)
1002 :high (coerce pi bound-type))))))
1004 ;; We have a complex number. The answer is the range -pi
1005 ;; to pi. (-pi is included because we have -0.)
1006 (make-numeric-type :class 'float
1009 :low (coerce (- pi) bound-type)
1010 :high (coerce pi bound-type))))))
1012 (defoptimizer (phase derive-type) ((num))
1013 (one-arg-derive-type num #'phase-derive-type-aux #'phase))
1017 (deftransform realpart ((x) ((complex rational)) *)
1018 '(sb!kernel:%realpart x))
1019 (deftransform imagpart ((x) ((complex rational)) *)
1020 '(sb!kernel:%imagpart x))
1022 ;;; Make REALPART and IMAGPART return the appropriate types. This
1023 ;;; should help a lot in optimized code.
1024 (defun realpart-derive-type-aux (type)
1025 (let ((class (numeric-type-class type))
1026 (format (numeric-type-format type)))
1027 (cond ((numeric-type-real-p type)
1028 ;; The realpart of a real has the same type and range as
1030 (make-numeric-type :class class
1033 :low (numeric-type-low type)
1034 :high (numeric-type-high type)))
1036 ;; We have a complex number. The result has the same type
1037 ;; as the real part, except that it's real, not complex,
1039 (make-numeric-type :class class
1042 :low (numeric-type-low type)
1043 :high (numeric-type-high type))))))
1044 #-sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
1045 (defoptimizer (realpart derive-type) ((num))
1046 (one-arg-derive-type num #'realpart-derive-type-aux #'realpart))
1047 (defun imagpart-derive-type-aux (type)
1048 (let ((class (numeric-type-class type))
1049 (format (numeric-type-format type)))
1050 (cond ((numeric-type-real-p type)
1051 ;; The imagpart of a real has the same type as the input,
1052 ;; except that it's zero.
1053 (let ((bound-format (or format class 'real)))
1054 (make-numeric-type :class class
1057 :low (coerce 0 bound-format)
1058 :high (coerce 0 bound-format))))
1060 ;; We have a complex number. The result has the same type as
1061 ;; the imaginary part, except that it's real, not complex,
1063 (make-numeric-type :class class
1066 :low (numeric-type-low type)
1067 :high (numeric-type-high type))))))
1068 #-sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
1069 (defoptimizer (imagpart derive-type) ((num))
1070 (one-arg-derive-type num #'imagpart-derive-type-aux #'imagpart))
1072 (defun complex-derive-type-aux-1 (re-type)
1073 (if (numeric-type-p re-type)
1074 (make-numeric-type :class (numeric-type-class re-type)
1075 :format (numeric-type-format re-type)
1076 :complexp (if (csubtypep re-type
1077 (specifier-type 'rational))
1080 :low (numeric-type-low re-type)
1081 :high (numeric-type-high re-type))
1082 (specifier-type 'complex)))
1084 (defun complex-derive-type-aux-2 (re-type im-type same-arg)
1085 (declare (ignore same-arg))
1086 (if (and (numeric-type-p re-type)
1087 (numeric-type-p im-type))
1088 ;; Need to check to make sure numeric-contagion returns the
1089 ;; right type for what we want here.
1091 ;; Also, what about rational canonicalization, like (complex 5 0)
1092 ;; is 5? So, if the result must be complex, we make it so.
1093 ;; If the result might be complex, which happens only if the
1094 ;; arguments are rational, we make it a union type of (or
1095 ;; rational (complex rational)).
1096 (let* ((element-type (numeric-contagion re-type im-type))
1097 (rat-result-p (csubtypep element-type
1098 (specifier-type 'rational))))
1100 (type-union element-type
1102 `(complex ,(numeric-type-class element-type))))
1103 (make-numeric-type :class (numeric-type-class element-type)
1104 :format (numeric-type-format element-type)
1105 :complexp (if rat-result-p
1108 (specifier-type 'complex)))
1110 #-sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
1111 (defoptimizer (complex derive-type) ((re &optional im))
1113 (two-arg-derive-type re im #'complex-derive-type-aux-2 #'complex)
1114 (one-arg-derive-type re #'complex-derive-type-aux-1 #'complex)))
1116 ;;; Define some transforms for complex operations. We do this in lieu
1117 ;;; of complex operation VOPs.
1118 (macrolet ((frob (type)
1121 (deftransform %negate ((z) ((complex ,type)) *)
1122 '(complex (%negate (realpart z)) (%negate (imagpart z))))
1123 ;; complex addition and subtraction
1124 (deftransform + ((w z) ((complex ,type) (complex ,type)) *)
1125 '(complex (+ (realpart w) (realpart z))
1126 (+ (imagpart w) (imagpart z))))
1127 (deftransform - ((w z) ((complex ,type) (complex ,type)) *)
1128 '(complex (- (realpart w) (realpart z))
1129 (- (imagpart w) (imagpart z))))
1130 ;; Add and subtract a complex and a real.
1131 (deftransform + ((w z) ((complex ,type) real) *)
1132 '(complex (+ (realpart w) z) (imagpart w)))
1133 (deftransform + ((z w) (real (complex ,type)) *)
1134 '(complex (+ (realpart w) z) (imagpart w)))
1135 ;; Add and subtract a real and a complex number.
1136 (deftransform - ((w z) ((complex ,type) real) *)
1137 '(complex (- (realpart w) z) (imagpart w)))
1138 (deftransform - ((z w) (real (complex ,type)) *)
1139 '(complex (- z (realpart w)) (- (imagpart w))))
1140 ;; Multiply and divide two complex numbers.
1141 (deftransform * ((x y) ((complex ,type) (complex ,type)) *)
1142 '(let* ((rx (realpart x))
1146 (complex (- (* rx ry) (* ix iy))
1147 (+ (* rx iy) (* ix ry)))))
1148 (deftransform / ((x y) ((complex ,type) (complex ,type)) *)
1149 '(let* ((rx (realpart x))
1153 (if (> (abs ry) (abs iy))
1154 (let* ((r (/ iy ry))
1155 (dn (* ry (+ 1 (* r r)))))
1156 (complex (/ (+ rx (* ix r)) dn)
1157 (/ (- ix (* rx r)) dn)))
1158 (let* ((r (/ ry iy))
1159 (dn (* iy (+ 1 (* r r)))))
1160 (complex (/ (+ (* rx r) ix) dn)
1161 (/ (- (* ix r) rx) dn))))))
1162 ;; Multiply a complex by a real or vice versa.
1163 (deftransform * ((w z) ((complex ,type) real) *)
1164 '(complex (* (realpart w) z) (* (imagpart w) z)))
1165 (deftransform * ((z w) (real (complex ,type)) *)
1166 '(complex (* (realpart w) z) (* (imagpart w) z)))
1167 ;; Divide a complex by a real.
1168 (deftransform / ((w z) ((complex ,type) real) *)
1169 '(complex (/ (realpart w) z) (/ (imagpart w) z)))
1170 ;; conjugate of complex number
1171 (deftransform conjugate ((z) ((complex ,type)) *)
1172 '(complex (realpart z) (- (imagpart z))))
1174 (deftransform cis ((z) ((,type)) *)
1175 '(complex (cos z) (sin z)))
1177 (deftransform = ((w z) ((complex ,type) (complex ,type)) *)
1178 '(and (= (realpart w) (realpart z))
1179 (= (imagpart w) (imagpart z))))
1180 (deftransform = ((w z) ((complex ,type) real) *)
1181 '(and (= (realpart w) z) (zerop (imagpart w))))
1182 (deftransform = ((w z) (real (complex ,type)) *)
1183 '(and (= (realpart z) w) (zerop (imagpart z)))))))
1186 (frob double-float))
1188 ;;; Here are simple optimizers for SIN, COS, and TAN. They do not
1189 ;;; produce a minimal range for the result; the result is the widest
1190 ;;; possible answer. This gets around the problem of doing range
1191 ;;; reduction correctly but still provides useful results when the
1192 ;;; inputs are union types.
1193 #-sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
1195 (defun trig-derive-type-aux (arg domain fcn
1196 &optional def-lo def-hi (increasingp t))
1199 (cond ((eq (numeric-type-complexp arg) :complex)
1200 (make-numeric-type :class (numeric-type-class arg)
1201 :format (numeric-type-format arg)
1202 :complexp :complex))
1203 ((numeric-type-real-p arg)
1204 (let* ((format (case (numeric-type-class arg)
1205 ((integer rational) 'single-float)
1206 (t (numeric-type-format arg))))
1207 (bound-type (or format 'float)))
1208 ;; If the argument is a subset of the "principal" domain
1209 ;; of the function, we can compute the bounds because
1210 ;; the function is monotonic. We can't do this in
1211 ;; general for these periodic functions because we can't
1212 ;; (and don't want to) do the argument reduction in
1213 ;; exactly the same way as the functions themselves do
1215 (if (csubtypep arg domain)
1216 (let ((res-lo (bound-func fcn (numeric-type-low arg)))
1217 (res-hi (bound-func fcn (numeric-type-high arg))))
1219 (rotatef res-lo res-hi))
1223 :low (coerce-numeric-bound res-lo bound-type)
1224 :high (coerce-numeric-bound res-hi bound-type)))
1228 :low (and def-lo (coerce def-lo bound-type))
1229 :high (and def-hi (coerce def-hi bound-type))))))
1231 (float-or-complex-float-type arg def-lo def-hi))))))
1233 (defoptimizer (sin derive-type) ((num))
1234 (one-arg-derive-type
1237 ;; Derive the bounds if the arg is in [-pi/2, pi/2].
1238 (trig-derive-type-aux
1240 (specifier-type `(float ,(- (/ pi 2)) ,(/ pi 2)))
1245 (defoptimizer (cos derive-type) ((num))
1246 (one-arg-derive-type
1249 ;; Derive the bounds if the arg is in [0, pi].
1250 (trig-derive-type-aux arg
1251 (specifier-type `(float 0d0 ,pi))
1257 (defoptimizer (tan derive-type) ((num))
1258 (one-arg-derive-type
1261 ;; Derive the bounds if the arg is in [-pi/2, pi/2].
1262 (trig-derive-type-aux arg
1263 (specifier-type `(float ,(- (/ pi 2)) ,(/ pi 2)))
1268 ;;; CONJUGATE always returns the same type as the input type.
1270 ;;; FIXME: ANSI allows any subtype of REAL for the components of COMPLEX.
1271 ;;; So what if the input type is (COMPLEX (SINGLE-FLOAT 0 1))?
1272 (defoptimizer (conjugate derive-type) ((num))
1273 (continuation-type num))
1275 (defoptimizer (cis derive-type) ((num))
1276 (one-arg-derive-type num
1278 (sb!c::specifier-type
1279 `(complex ,(or (numeric-type-format arg) 'float))))
1284 ;;;; TRUNCATE, FLOOR, CEILING, and ROUND
1286 (macrolet ((define-frobs (fun ufun)
1288 (defknown ,ufun (real) integer (movable foldable flushable))
1289 (deftransform ,fun ((x &optional by)
1291 (constant-arg (member 1))))
1292 '(let ((res (,ufun x)))
1293 (values res (- x res)))))))
1294 (define-frobs truncate %unary-truncate)
1295 (define-frobs round %unary-round))
1297 ;;; Convert (TRUNCATE x y) to the obvious implementation. We only want
1298 ;;; this when under certain conditions and let the generic TRUNCATE
1299 ;;; handle the rest. (Note: if Y = 1, the divide and multiply by Y
1300 ;;; should be removed by other DEFTRANSFORMs.)
1301 (deftransform truncate ((x &optional y)
1302 (float &optional (or float integer)))
1303 (let ((defaulted-y (if y 'y 1)))
1304 `(let ((res (%unary-truncate (/ x ,defaulted-y))))
1305 (values res (- x (* ,defaulted-y res))))))
1307 (deftransform floor ((number &optional divisor)
1308 (float &optional (or integer float)))
1309 (let ((defaulted-divisor (if divisor 'divisor 1)))
1310 `(multiple-value-bind (tru rem) (truncate number ,defaulted-divisor)
1311 (if (and (not (zerop rem))
1312 (if (minusp ,defaulted-divisor)
1315 (values (1- tru) (+ rem ,defaulted-divisor))
1316 (values tru rem)))))
1318 (deftransform ceiling ((number &optional divisor)
1319 (float &optional (or integer float)))
1320 (let ((defaulted-divisor (if divisor 'divisor 1)))
1321 `(multiple-value-bind (tru rem) (truncate number ,defaulted-divisor)
1322 (if (and (not (zerop rem))
1323 (if (minusp ,defaulted-divisor)
1326 (values (1+ tru) (- rem ,defaulted-divisor))
1327 (values tru rem)))))