1 ;;;; code to implement bignum support
3 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
12 (in-package "SB!BIGNUM")
16 ;;; comments from CMU CL:
17 ;;; These symbols define the interface to the number code:
18 ;;; add-bignums multiply-bignums negate-bignum subtract-bignum
19 ;;; multiply-bignum-and-fixnum multiply-fixnums
20 ;;; bignum-ashift-right bignum-ashift-left bignum-gcd
21 ;;; bignum-to-float bignum-integer-length
22 ;;; bignum-logical-and bignum-logical-ior bignum-logical-xor
23 ;;; bignum-logical-not bignum-load-byte
24 ;;; bignum-truncate bignum-plus-p bignum-compare make-small-bignum
25 ;;; bignum-logbitp bignum-logcount
26 ;;; These symbols define the interface to the compiler:
27 ;;; bignum-type bignum-element-type bignum-index %allocate-bignum
28 ;;; %bignum-length %bignum-set-length %bignum-ref %bignum-set
29 ;;; %digit-0-or-plusp %add-with-carry %subtract-with-borrow
30 ;;; %multiply-and-add %multiply %lognot %logand %logior %logxor
31 ;;; %fixnum-to-digit %bigfloor %fixnum-digit-with-correct-sign %ashl
32 ;;; %ashr %digit-logical-shift-right))
34 ;;; The following interfaces will either be assembler routines or code
35 ;;; sequences expanded into the code as basic bignum operations:
41 ;;; %BIGNUM-SET-LENGTH
42 ;;; %FIXNUM-DIGIT-WITH-CORRECT-SIGN
46 ;;; %BIGNUM-0-OR-PLUSP
47 ;;; %DIGIT-LOGICAL-SHIFT-RIGHT
48 ;;; General (May not exist when done due to sole use in %-routines.)
53 ;;; %SUBTRACT-WITH-BORROW
58 ;;; Shifting (in place)
59 ;;; %NORMALIZE-BIGNUM-BUFFER
60 ;;; GCD/Relational operators:
63 ;;; Relational operators:
72 ;;; Note: The floating routines know about the float representation.
75 ;;; There might be a problem with various LET's and parameters that take a
76 ;;; digit value. We need to write these so those things stay in machine
77 ;;; registers and number stack slots. I bind locals to these values, and I
78 ;;; use function on them -- ZEROP, ASH, etc.
81 ;;; In shifting and byte operations, I use masks and logical operations that
82 ;;; could result in intermediate bignums. This is hidden by the current system,
83 ;;; but I may need to write these in a way that keeps these masks and logical
84 ;;; operations from diving into the Lisp level bignum code.
88 ;;; logior, logxor, logand
89 ;;; depending on relationals, < (twice) and <= (twice)
90 ;;; or write compare thing (twice).
91 ;;; LDB on fixnum with bignum result.
92 ;;; DPB on fixnum with bignum result.
93 ;;; TRUNCATE returns zero or one as one value and fixnum or minus fixnum
94 ;;; for the other value when given (truncate fixnum bignum).
95 ;;; Returns (truncate bignum fixnum) otherwise.
97 ;;; subtraction (twice)
100 ;;; Write MASK-FIELD and DEPOSIT-FIELD in terms of logical operations.
102 ;;; IF (/ x y) with bignums:
103 ;;; do the truncate, and if rem is 0, return quotient.
106 ;;; "truncate" each by gcd, ignoring remainder 0.
107 ;;; form ratio of each result, bottom is positive.
109 ;;;; What's a bignum?
111 (defconstant digit-size sb
!vm
:n-word-bits
)
113 (defconstant maximum-bignum-length
(1- (ash 1 (- sb
!vm
:n-word-bits
114 sb
!vm
:n-widetag-bits
))))
116 (defconstant all-ones-digit
(1- (ash 1 sb
!vm
:n-word-bits
)))
118 ;;;; internal inline routines
120 ;;; %ALLOCATE-BIGNUM must zero all elements.
121 (defun %allocate-bignum
(length)
122 (declare (type bignum-length length
))
123 (%allocate-bignum length
))
125 ;;; Extract the length of the bignum.
126 (defun %bignum-length
(bignum)
127 (declare (type bignum-type bignum
))
128 (%bignum-length bignum
))
130 ;;; %BIGNUM-REF needs to access bignums as obviously as possible, and it needs
131 ;;; to be able to return the digit somewhere no one looks for real objects.
132 (defun %bignum-ref
(bignum i
)
133 (declare (type bignum-type bignum
)
134 (type bignum-index i
))
135 (%bignum-ref bignum i
))
136 (defun %bignum-set
(bignum i value
)
137 (declare (type bignum-type bignum
)
138 (type bignum-index i
)
139 (type bignum-element-type value
))
140 (%bignum-set bignum i value
))
142 ;;; Return T if digit is positive, or NIL if negative.
143 (defun %digit-0-or-plusp
(digit)
144 (declare (type bignum-element-type digit
))
145 (not (logbitp (1- digit-size
) digit
)))
147 #!-sb-fluid
(declaim (inline %bignum-0-or-plusp
))
148 (defun %bignum-0-or-plusp
(bignum len
)
149 (declare (type bignum-type bignum
)
150 (type bignum-length len
))
151 (%digit-0-or-plusp
(%bignum-ref bignum
(1- len
))))
153 ;;; This should be in assembler, and should not cons intermediate
154 ;;; results. It returns a bignum digit and a carry resulting from adding
155 ;;; together a, b, and an incoming carry.
156 (defun %add-with-carry
(a b carry
)
157 (declare (type bignum-element-type a b
)
158 (type (mod 2) carry
))
159 (%add-with-carry a b carry
))
161 ;;; This should be in assembler, and should not cons intermediate
162 ;;; results. It returns a bignum digit and a borrow resulting from
163 ;;; subtracting b from a, and subtracting a possible incoming borrow.
165 ;;; We really do: a - b - 1 + borrow, where borrow is either 0 or 1.
166 (defun %subtract-with-borrow
(a b borrow
)
167 (declare (type bignum-element-type a b
)
168 (type (mod 2) borrow
))
169 (%subtract-with-borrow a b borrow
))
171 ;;; Multiply two digit-size numbers, returning a 2*digit-size result
172 ;;; split into two digit-size quantities.
173 (defun %multiply
(x y
)
174 (declare (type bignum-element-type x y
))
177 ;;; This multiplies x-digit and y-digit, producing high and low digits
178 ;;; manifesting the result. Then it adds the low digit, res-digit, and
179 ;;; carry-in-digit. Any carries (note, you still have to add two digits
180 ;;; at a time possibly producing two carries) from adding these three
181 ;;; digits get added to the high digit from the multiply, producing the
182 ;;; next carry digit. Res-digit is optional since two uses of this
183 ;;; primitive multiplies a single digit bignum by a multiple digit
184 ;;; bignum, and in this situation there is no need for a result buffer
185 ;;; accumulating partial results which is where the res-digit comes
187 (defun %multiply-and-add
(x-digit y-digit carry-in-digit
188 &optional
(res-digit 0))
189 (declare (type bignum-element-type x-digit y-digit res-digit carry-in-digit
))
190 (%multiply-and-add x-digit y-digit carry-in-digit res-digit
))
192 (defun %lognot
(digit)
193 (declare (type bignum-element-type digit
))
196 ;;; Each of these does the digit-size unsigned op.
197 (declaim (inline %logand %logior %logxor
))
199 (declare (type bignum-element-type a b
))
202 (declare (type bignum-element-type a b
))
205 (declare (type bignum-element-type a b
))
208 ;;; This takes a fixnum and sets it up as an unsigned digit-size
210 (defun %fixnum-to-digit
(x)
212 (logand x
(1- (ash 1 digit-size
))))
215 ;;; This takes three digits and returns the FLOOR'ed result of
216 ;;; dividing the first two as a 2*digit-size integer by the third.
218 ;;; Do weird LET and SETQ stuff to bamboozle the compiler into allowing
219 ;;; the %BIGFLOOR transform to expand into pseudo-assembler for which the
220 ;;; compiler can later correctly allocate registers.
221 (defun %bigfloor
(a b c
)
222 (let ((a a
) (b b
) (c c
))
223 (declare (type bignum-element-type a b c
))
227 ;;; Convert the digit to a regular integer assuming that the digit is signed.
228 (defun %fixnum-digit-with-correct-sign
(digit)
229 (declare (type bignum-element-type digit
))
230 (if (logbitp (1- digit-size
) digit
)
231 (logior digit
(ash -
1 digit-size
))
234 ;;; Do an arithmetic shift right of data even though bignum-element-type is
236 (defun %ashr
(data count
)
237 (declare (type bignum-element-type data
)
238 (type (mod #.sb
!vm
:n-word-bits
) count
))
241 ;;; This takes a digit-size quantity and shifts it to the left,
242 ;;; returning a digit-size quantity.
243 (defun %ashl
(data count
)
244 (declare (type bignum-element-type data
)
245 (type (mod #.sb
!vm
:n-word-bits
) count
))
248 ;;; Do an unsigned (logical) right shift of a digit by Count.
249 (defun %digit-logical-shift-right
(data count
)
250 (declare (type bignum-element-type data
)
251 (type (mod #.sb
!vm
:n-word-bits
) count
))
252 (%digit-logical-shift-right data count
))
254 ;;; Change the length of bignum to be newlen. Newlen must be the same or
255 ;;; smaller than the old length, and any elements beyond newlen must be zeroed.
256 (defun %bignum-set-length
(bignum newlen
)
257 (declare (type bignum-type bignum
)
258 (type bignum-length newlen
))
259 (%bignum-set-length bignum newlen
))
261 ;;; This returns 0 or "-1" depending on whether the bignum is positive. This
262 ;;; is suitable for infinite sign extension to complete additions,
263 ;;; subtractions, negations, etc. This cannot return a -1 represented as
264 ;;; a negative fixnum since it would then have to low zeros.
265 #!-sb-fluid
(declaim (inline %sign-digit
))
266 (defun %sign-digit
(bignum len
)
267 (declare (type bignum-type bignum
)
268 (type bignum-length len
))
269 (%ashr
(%bignum-ref bignum
(1- len
)) (1- digit-size
)))
271 ;;; These take two digit-size quantities and compare or contrast them
272 ;;; without wasting time with incorrect type checking.
273 (declaim (inline %digit-compare %digit-greater
))
274 (defun %digit-compare
(x y
)
276 (defun %digit-greater
(x y
)
279 (declaim (optimize (speed 3) (safety 0)))
283 (defun add-bignums (a b
)
284 (declare (type bignum-type a b
))
285 (declare (muffle-conditions compiler-note
)) ; returns lispobj, so what.
286 (let ((len-a (%bignum-length a
))
287 (len-b (%bignum-length b
)))
288 (multiple-value-bind (a len-a b len-b
)
290 (values a len-a b len-b
)
291 (values b len-b a len-a
))
292 (declare (type bignum-type a b
)
293 (type bignum-length len-a len-b
))
294 (let* ((len-res (1+ len-a
))
295 (res (%allocate-bignum len-res
))
297 (declare (type bignum-length len-res
)
298 (type bignum-type res
)
299 (type (mod 2) carry
))
301 (declare (type bignum-index i
))
302 (multiple-value-bind (v k
)
303 (%add-with-carry
(%bignum-ref a i
) (%bignum-ref b i
) carry
)
304 (declare (type bignum-element-type v
)
306 (setf (%bignum-ref res i
) v
)
309 (finish-add a res carry
(%sign-digit b len-b
) len-b len-a
)
310 (setf (%bignum-ref res len-a
)
311 (%add-with-carry
(%sign-digit a len-a
)
312 (%sign-digit b len-b
)
314 (%normalize-bignum res len-res
)))))
316 ;;; This takes the longer of two bignums and propagates the carry through its
317 ;;; remaining high order digits.
318 (defun finish-add (a res carry sign-digit-b start end
)
319 (declare (type bignum-type a res
)
321 (type bignum-element-type sign-digit-b
)
322 (type bignum-index start
)
323 (type bignum-length end
))
324 (do ((i start
(1+ i
)))
326 (setf (%bignum-ref res end
)
327 (%add-with-carry
(%sign-digit a end
) sign-digit-b carry
)))
328 (declare (type bignum-index i
))
329 (multiple-value-bind (v k
)
330 (%add-with-carry
(%bignum-ref a i
) sign-digit-b carry
)
331 (setf (%bignum-ref res i
) v
)
337 (eval-when (:compile-toplevel
:execute
)
339 ;;; This subtracts b from a plugging result into res. Return-fun is the
340 ;;; function to call that fixes up the result returning any useful values, such
341 ;;; as the result. This macro may evaluate its arguments more than once.
342 (sb!xc
:defmacro subtract-bignum-loop
(a len-a b len-b res len-res return-fun
)
343 (with-unique-names (borrow a-digit a-sign b-digit b-sign i v k
)
345 (,a-sign
(%sign-digit
,a
,len-a
))
346 (,b-sign
(%sign-digit
,b
,len-b
)))
347 (declare (type bignum-element-type
,a-sign
,b-sign
))
348 (dotimes (,i
,len-res
)
349 (declare (type bignum-index
,i
))
350 (let ((,a-digit
(if (< ,i
,len-a
) (%bignum-ref
,a
,i
) ,a-sign
))
351 (,b-digit
(if (< ,i
,len-b
) (%bignum-ref
,b
,i
) ,b-sign
)))
352 (declare (type bignum-element-type
,a-digit
,b-digit
))
353 (multiple-value-bind (,v
,k
)
354 (%subtract-with-borrow
,a-digit
,b-digit
,borrow
)
355 (setf (%bignum-ref
,res
,i
) ,v
)
357 (,return-fun
,res
,len-res
))))
361 (defun subtract-bignum (a b
)
362 (declare (type bignum-type a b
))
363 (let* ((len-a (%bignum-length a
))
364 (len-b (%bignum-length b
))
365 (len-res (1+ (max len-a len-b
)))
366 (res (%allocate-bignum len-res
)))
367 (declare (type bignum-length len-a len-b len-res
)) ;Test len-res for bounds?
368 (subtract-bignum-loop a len-a b len-b res len-res %normalize-bignum
)))
370 ;;; Operations requiring a subtraction without the overhead of intermediate
371 ;;; results, such as GCD, use this. It assumes Result is big enough for the
373 (defun subtract-bignum-buffers-with-len (a len-a b len-b result len-res
)
374 (declare (type bignum-type a b result
)
375 (type bignum-length len-a len-b len-res
))
376 (subtract-bignum-loop a len-a b len-b result len-res
377 %normalize-bignum-buffer
))
379 (defun subtract-bignum-buffers (a len-a b len-b result
)
380 (declare (type bignum-type a b result
)
381 (type bignum-length len-a len-b
))
382 (subtract-bignum-loop a len-a b len-b result
(max len-a len-b
)
383 %normalize-bignum-buffer
))
387 (defun multiply-bignums (a b
)
388 (declare (type bignum-type a b
))
389 (let* ((a-plusp (%bignum-0-or-plusp a
(%bignum-length a
)))
390 (b-plusp (%bignum-0-or-plusp b
(%bignum-length b
)))
391 (a (if a-plusp a
(negate-bignum a
)))
392 (b (if b-plusp b
(negate-bignum b
)))
393 (len-a (%bignum-length a
))
394 (len-b (%bignum-length b
))
395 (len-res (+ len-a len-b
))
396 (res (%allocate-bignum len-res
))
397 (negate-res (not (eq a-plusp b-plusp
))))
398 (declare (type bignum-length len-a len-b len-res
))
400 (declare (type bignum-index i
))
401 (let ((carry-digit 0)
402 (x (%bignum-ref a i
))
404 (declare (type bignum-index k
)
405 (type bignum-element-type carry-digit x
))
407 (multiple-value-bind (big-carry res-digit
)
412 (declare (type bignum-element-type big-carry res-digit
))
413 (setf (%bignum-ref res k
) res-digit
)
414 (setf carry-digit big-carry
)
416 (setf (%bignum-ref res k
) carry-digit
)))
417 (when negate-res
(negate-bignum-in-place res
))
418 (%normalize-bignum res len-res
)))
420 (defun multiply-bignum-and-fixnum (bignum fixnum
)
421 (declare (type bignum-type bignum
) (type fixnum fixnum
))
422 (let* ((bignum-plus-p (%bignum-0-or-plusp bignum
(%bignum-length bignum
)))
423 (fixnum-plus-p (not (minusp fixnum
)))
424 (bignum (if bignum-plus-p bignum
(negate-bignum bignum
)))
425 (bignum-len (%bignum-length bignum
))
426 (fixnum (if fixnum-plus-p fixnum
(- fixnum
)))
427 (result (%allocate-bignum
(1+ bignum-len
)))
429 (declare (type bignum-type bignum result
)
430 (type bignum-element-type fixnum carry-digit
))
431 (dotimes (index bignum-len
)
432 (declare (type bignum-index index
))
433 (multiple-value-bind (next-digit low
)
434 (%multiply-and-add
(%bignum-ref bignum index
) fixnum carry-digit
)
435 (declare (type bignum-element-type next-digit low
))
436 (setf carry-digit next-digit
)
437 (setf (%bignum-ref result index
) low
)))
438 (setf (%bignum-ref result bignum-len
) carry-digit
)
439 (unless (eq bignum-plus-p fixnum-plus-p
)
440 (negate-bignum-in-place result
))
441 (%normalize-bignum result
(1+ bignum-len
))))
443 (defun multiply-fixnums (a b
)
444 (declare (fixnum a b
))
445 (declare (muffle-conditions compiler-note
)) ; returns lispobj, so what.
446 (let* ((a-minusp (minusp a
))
447 (b-minusp (minusp b
)))
448 (multiple-value-bind (high low
)
449 (%multiply
(if a-minusp
(- a
) a
)
450 (if b-minusp
(- b
) b
))
451 (declare (type bignum-element-type high low
))
452 (if (and (zerop high
)
453 (%digit-0-or-plusp low
))
454 (let ((low (truly-the (unsigned-byte #.
(1- sb
!vm
:n-word-bits
))
455 (%fixnum-digit-with-correct-sign low
))))
456 (if (eq a-minusp b-minusp
)
459 (let ((res (%allocate-bignum
2)))
460 (%bignum-set res
0 low
)
461 (%bignum-set res
1 high
)
462 (unless (eq a-minusp b-minusp
) (negate-bignum-in-place res
))
463 (%normalize-bignum res
2))))))
465 ;;;; BIGNUM-REPLACE and WITH-BIGNUM-BUFFERS
467 (eval-when (:compile-toplevel
:execute
)
469 (sb!xc
:defmacro bignum-replace
(dest
477 (sb!int
:once-only
((n-dest dest
)
479 (with-unique-names (n-start1 n-end1 n-start2 n-end2 i1 i2
)
480 (let ((end1 (or end1
`(%bignum-length
,n-dest
)))
481 (end2 (or end2
`(%bignum-length
,n-src
))))
483 `(let ((,n-start1
,start1
)
485 (do ((,i1
(1- ,end1
) (1- ,i1
))
486 (,i2
(1- ,end2
) (1- ,i2
)))
487 ((or (< ,i1
,n-start1
) (< ,i2
,n-start2
)))
488 (declare (fixnum ,i1
,i2
))
489 (%bignum-set
,n-dest
,i1
(%bignum-ref
,n-src
,i2
))))
490 (if (eql start1 start2
)
491 `(let ((,n-end1
(min ,end1
,end2
)))
492 (do ((,i1
,start1
(1+ ,i1
)))
494 (declare (type bignum-index
,i1
))
495 (%bignum-set
,n-dest
,i1
(%bignum-ref
,n-src
,i1
))))
496 `(let ((,n-end1
,end1
)
498 (do ((,i1
,start1
(1+ ,i1
))
499 (,i2
,start2
(1+ ,i2
)))
500 ((or (>= ,i1
,n-end1
) (>= ,i2
,n-end2
)))
501 (declare (type bignum-index
,i1
,i2
))
502 (%bignum-set
,n-dest
,i1
(%bignum-ref
,n-src
,i2
))))))))))
504 (sb!xc
:defmacro with-bignum-buffers
(specs &body body
)
506 "WITH-BIGNUM-BUFFERS ({(var size [init])}*) Form*"
507 (sb!int
:collect
((binds)
510 (let ((name (first spec
))
511 (size (second spec
)))
512 (binds `(,name
(%allocate-bignum
,size
)))
513 (let ((init (third spec
)))
515 (inits `(bignum-replace ,name
,init
))))))
524 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
525 ;; The asserts in the GCD implementation are way too expensive to
526 ;; check in normal use, and are disabled here.
527 (sb!xc
:defmacro gcd-assert
(&rest args
)
530 ;; We'll be doing a lot of modular arithmetic.
531 (sb!xc
:defmacro modularly
(form)
532 `(logand all-ones-digit
,form
)))
534 ;;; I'm not sure why I need this FTYPE declaration. Compiled by the
535 ;;; target compiler, it can deduce the return type fine, but without
536 ;;; it, we pay a heavy price in BIGNUM-GCD when compiled by the
537 ;;; cross-compiler. -- CSR, 2004-07-19
538 (declaim (ftype (sfunction (bignum-type bignum-length bignum-type bignum-length
)
539 (and unsigned-byte fixnum
))
540 bignum-factors-of-two
))
541 (defun bignum-factors-of-two (a len-a b len-b
)
542 (declare (type bignum-length len-a len-b
) (type bignum-type a b
))
544 (end (min len-a len-b
)))
545 ((= i end
) (error "Unexpected zero bignums?"))
546 (declare (type bignum-index i
)
547 (type bignum-length end
))
548 (let ((or-digits (%logior
(%bignum-ref a i
) (%bignum-ref b i
))))
549 (unless (zerop or-digits
)
550 (return (do ((j 0 (1+ j
))
551 (or-digits or-digits
(%ashr or-digits
1)))
552 ((oddp or-digits
) (+ (* i digit-size
) j
))
553 (declare (type (mod #.sb
!vm
:n-word-bits
) j
))))))))
555 ;;; Multiply a bignum buffer with a fixnum or a digit, storing the
556 ;;; result in another bignum buffer, and without using any
557 ;;; temporaries. Inlined to avoid boxing smallnum if it's actually a
558 ;;; digit. Needed by GCD, should possibly OAOO with
559 ;;; MULTIPLY-BIGNUM-AND-FIXNUM.
560 (declaim (inline multiply-bignum-buffer-and-smallnum-to-buffer
))
561 (defun multiply-bignum-buffer-and-smallnum-to-buffer (bignum bignum-len
563 (declare (type bignum-type bignum
))
564 (let* ((bignum-plus-p (%bignum-0-or-plusp bignum bignum-len
))
565 (smallnum-plus-p (not (minusp smallnum
)))
566 (smallnum (if smallnum-plus-p smallnum
(- smallnum
)))
568 (declare (type bignum-type bignum res
)
569 (type bignum-length bignum-len
)
570 (type bignum-element-type smallnum carry-digit
))
571 (unless bignum-plus-p
572 (negate-bignum-buffer-in-place bignum bignum-len
))
573 (dotimes (index bignum-len
)
574 (declare (type bignum-index index
))
575 (multiple-value-bind (next-digit low
)
576 (%multiply-and-add
(%bignum-ref bignum index
)
579 (declare (type bignum-element-type next-digit low
))
580 (setf carry-digit next-digit
)
581 (setf (%bignum-ref res index
) low
)))
582 (setf (%bignum-ref res bignum-len
) carry-digit
)
583 (unless bignum-plus-p
584 (negate-bignum-buffer-in-place bignum bignum-len
))
585 (let ((res-len (%normalize-bignum-buffer res
(1+ bignum-len
))))
586 (unless (eq bignum-plus-p smallnum-plus-p
)
587 (negate-bignum-buffer-in-place res res-len
))
590 ;;; Given U and V, return U / V mod 2^32. Implements the algorithm in the
591 ;;; paper, but uses some clever bit-twiddling nicked from Nickle to do it.
592 (declaim (inline bmod
))
594 (declare (muffle-conditions compiler-note
)) ; returns lispobj, so what.
595 (let ((ud (%bignum-ref u
0))
596 (vd (%bignum-ref v
0))
600 (declare (type (unsigned-byte #.sb
!vm
:n-word-bits
) ud vd umask imask m
))
601 (dotimes (i digit-size
)
602 (setf umask
(logior umask imask
))
603 (when (logtest ud umask
)
604 (setf ud
(modularly (- ud vd
)))
605 (setf m
(modularly (logior m imask
))))
606 (setf imask
(modularly (ash imask
1)))
607 (setf vd
(modularly (ash vd
1))))
610 (defun dmod (u u-len v v-len tmp1
)
611 (loop while
(> (bignum-buffer-integer-length u u-len
)
612 (+ (bignum-buffer-integer-length v v-len
)
615 (unless (zerop (%bignum-ref u
0))
616 (let* ((bmod (bmod u v
))
617 (tmp1-len (multiply-bignum-buffer-and-smallnum-to-buffer v v-len
620 (setf u-len
(subtract-bignum-buffers u u-len
623 (bignum-abs-buffer u u-len
)))
624 (gcd-assert (zerop (%bignum-ref u
0)))
625 (setf u-len
(bignum-buffer-ashift-right u u-len digit-size
)))
626 (let* ((d (+ 1 (- (bignum-buffer-integer-length u u-len
)
627 (bignum-buffer-integer-length v v-len
))))
629 (declare (type (unsigned-byte #.
(integer-length #.sb
!vm
:n-word-bits
)) d
)
630 (type (unsigned-byte #.sb
!vm
:n-word-bits
) n
))
631 (gcd-assert (>= d
0))
632 (when (logtest (%bignum-ref u
0) n
)
634 (multiply-bignum-buffer-and-smallnum-to-buffer v v-len
638 (setf u-len
(subtract-bignum-buffers u u-len
641 (bignum-abs-buffer u u-len
)))
644 (defconstant lower-ones-digit
(1- (ash 1 (truncate sb
!vm
:n-word-bits
2))))
646 ;;; Find D and N such that (LOGAND ALL-ONES-DIGIT (- (* D X) (* N Y))) is 0,
647 ;;; (< 0 N LOWER-ONES-DIGIT) and (< 0 (ABS D) LOWER-ONES-DIGIT).
648 (defun reduced-ratio-mod (x y
)
649 (let* ((c (bmod x y
))
652 (n2 (modularly (1+ (modularly (lognot n1
)))))
654 (declare (type (unsigned-byte #.sb
!vm
:n-word-bits
) n1 d1 n2 d2
))
655 (loop while
(> n2
(expt 2 (truncate digit-size
2))) do
656 (loop for i of-type
(mod #.sb
!vm
:n-word-bits
)
657 downfrom
(- (integer-length n1
) (integer-length n2
))
659 (when (>= n1
(modularly (ash n2 i
)))
660 (psetf n1
(modularly (- n1
(modularly (ash n2 i
))))
661 d1
(modularly (- d1
(modularly (ash d2 i
)))))))
666 (values n2
(if (>= d2
(expt 2 (1- digit-size
)))
667 (lognot (logand most-positive-fixnum
(lognot d2
)))
668 (logand lower-ones-digit d2
)))))
671 (defun copy-bignum (a &optional
(len (%bignum-length a
)))
672 (let ((b (%allocate-bignum len
)))
674 (%bignum-set-length b len
)
677 ;;; Allocate a single word bignum that holds fixnum. This is useful when
678 ;;; we are trying to mix fixnum and bignum operands.
679 #!-sb-fluid
(declaim (inline make-small-bignum
))
680 (defun make-small-bignum (fixnum)
681 (let ((res (%allocate-bignum
1)))
682 (setf (%bignum-ref res
0) (%fixnum-to-digit fixnum
))
685 ;; When the larger number is less than this many bignum digits long, revert
687 (defparameter *accelerated-gcd-cutoff
* 3)
689 ;;; Alternate between k-ary reduction with the help of
690 ;;; REDUCED-RATIO-MOD and digit modulus reduction via DMOD. Once the
691 ;;; arguments get small enough, drop through to BIGNUM-MOD-GCD (since
692 ;;; k-ary reduction can introduce spurious factors, which need to be
693 ;;; filtered out). Reference: Kenneth Weber, "The accelerated integer
694 ;;; GCD algorithm", ACM Transactions on Mathematical Software, volume
695 ;;; 21, number 1, March 1995, epp. 111-122.
696 (defun bignum-gcd (u0 v0
)
697 (declare (type bignum-type u0 v0
))
698 (let* ((u1 (if (%bignum-0-or-plusp u0
(%bignum-length u0
))
700 (negate-bignum u0 nil
)))
701 (v1 (if (%bignum-0-or-plusp v0
(%bignum-length v0
))
703 (negate-bignum v0 nil
))))
705 (return-from bignum-gcd u1
))
708 (let ((n (mod v1 u1
)))
709 (setf v1
(if (fixnump n
)
710 (make-small-bignum n
)
712 (if (and (= 1 (%bignum-length v1
))
713 (zerop (%bignum-ref v1
0)))
714 (return-from bignum-gcd
(%normalize-bignum u1
715 (%bignum-length u1
))))
716 (let* ((buffer-len (+ 2 (%bignum-length u1
)))
717 (u (%allocate-bignum buffer-len
))
718 (u-len (%bignum-length u1
))
719 (v (%allocate-bignum buffer-len
))
720 (v-len (%bignum-length v1
))
721 (tmp1 (%allocate-bignum buffer-len
))
723 (tmp2 (%allocate-bignum buffer-len
))
726 (bignum-factors-of-two u1
(%bignum-length u1
)
727 v1
(%bignum-length v1
))))
728 (declare (type (or null bignum-length
)
729 buffer-len u-len v-len tmp1-len tmp2-len
))
730 (bignum-replace u u1
)
731 (bignum-replace v v1
)
733 (make-gcd-bignum-odd u
734 (bignum-buffer-ashift-right u u-len
737 (make-gcd-bignum-odd v
738 (bignum-buffer-ashift-right v v-len
740 (loop until
(or (< u-len
*accelerated-gcd-cutoff
*)
744 (zerop (%bignum-ref v
0))))
746 (gcd-assert (= buffer-len
(%bignum-length u
)
748 (%bignum-length tmp1
)
749 (%bignum-length tmp2
)))
750 (if (> (bignum-buffer-integer-length u u-len
)
751 (+ #.
(truncate sb
!vm
:n-word-bits
4)
752 (bignum-buffer-integer-length v v-len
)))
753 (setf u-len
(dmod u u-len
756 (multiple-value-bind (n d
) (reduced-ratio-mod u v
)
758 (multiply-bignum-buffer-and-smallnum-to-buffer v v-len
761 (multiply-bignum-buffer-and-smallnum-to-buffer u u-len
763 (gcd-assert (= (copy-bignum tmp2 tmp2-len
)
764 (* (copy-bignum u u-len
) d
)))
765 (gcd-assert (= (copy-bignum tmp1 tmp1-len
)
766 (* (copy-bignum v v-len
) n
)))
768 (subtract-bignum-buffers-with-len tmp1 tmp1-len
773 (gcd-assert (or (zerop (- (copy-bignum tmp1 tmp1-len
)
774 (copy-bignum tmp2 tmp2-len
)))
775 (= (copy-bignum u u-len
)
776 (- (copy-bignum tmp1 tmp1-len
)
777 (copy-bignum tmp2 tmp2-len
)))))
778 (bignum-abs-buffer u u-len
)
779 (gcd-assert (zerop (modularly u
)))))
780 (setf u-len
(make-gcd-bignum-odd u u-len
))
782 (rotatef u-len v-len
))
783 (bignum-abs-buffer u u-len
)
784 (setf u
(copy-bignum u u-len
))
785 (let ((n (bignum-mod-gcd v1 u
)))
786 (ash (bignum-mod-gcd u1
(if (fixnump n
)
787 (make-small-bignum n
)
791 (defun bignum-mod-gcd (a b
)
792 (declare (type bignum-type a b
))
795 ;; While the length difference of A and B is sufficiently large,
796 ;; reduce using MOD (slowish, but it should equalize the sizes of
797 ;; A and B pretty quickly). After that, use the binary GCD
798 ;; algorithm to handle the rest.
799 (loop until
(and (= (%bignum-length b
) 1) (zerop (%bignum-ref b
0))) do
800 (when (<= (%bignum-length a
) (1+ (%bignum-length b
)))
801 (return-from bignum-mod-gcd
(bignum-binary-gcd a b
)))
802 (let ((rem (mod a b
)))
804 (setf a
(make-small-bignum rem
))
807 (if (= (%bignum-length a
) 1)
808 (%normalize-bignum a
1)
811 (defun bignum-binary-gcd (a b
)
812 (declare (type bignum-type a b
))
813 (let* ((len-a (%bignum-length a
))
814 (len-b (%bignum-length b
)))
815 (with-bignum-buffers ((a-buffer len-a a
)
817 (res-buffer (max len-a len-b
)))
818 (let* ((factors-of-two
819 (bignum-factors-of-two a-buffer len-a
821 (len-a (make-gcd-bignum-odd
823 (bignum-buffer-ashift-right a-buffer len-a
825 (len-b (make-gcd-bignum-odd
827 (bignum-buffer-ashift-right b-buffer len-b
829 (declare (type bignum-length len-a len-b
))
836 (multiple-value-bind (u v len-v r len-r
)
837 (bignum-gcd-order-and-subtract x len-x y len-y z
)
838 (declare (type bignum-length len-v len-r
))
839 (when (and (= len-r
1) (zerop (%bignum-ref r
0)))
840 (if (zerop factors-of-two
)
841 (let ((ret (%allocate-bignum len-v
)))
843 (setf (%bignum-ref ret i
) (%bignum-ref v i
)))
844 (return (%normalize-bignum ret len-v
)))
845 (return (bignum-ashift-left v factors-of-two len-v
))))
846 (setf x v len-x len-v
)
847 (setf y r len-y
(make-gcd-bignum-odd r len-r
))
850 (defun bignum-gcd-order-and-subtract (a len-a b len-b res
)
851 (declare (type bignum-length len-a len-b
) (type bignum-type a b
))
852 (cond ((= len-a len-b
)
853 (do ((i (1- len-a
) (1- i
)))
855 (setf (%bignum-ref res
0) 0)
856 (values a b len-b res
1))
857 (let ((a-digit (%bignum-ref a i
))
858 (b-digit (%bignum-ref b i
)))
859 (cond ((%digit-compare a-digit b-digit
))
860 ((%digit-greater a-digit b-digit
)
862 (values a b len-b res
863 (subtract-bignum-buffers a len-a b len-b
867 (values b a len-a res
868 (subtract-bignum-buffers b len-b
872 (values a b len-b res
873 (subtract-bignum-buffers a len-a b len-b res
)))
875 (values b a len-a res
876 (subtract-bignum-buffers b len-b a len-a res
)))))
878 (defun make-gcd-bignum-odd (a len-a
)
879 (declare (type bignum-type a
) (type bignum-length len-a
))
880 (dotimes (index len-a
)
881 (declare (type bignum-index index
))
882 (do ((digit (%bignum-ref a index
) (%ashr digit
1))
883 (increment 0 (1+ increment
)))
885 (declare (type (mod #.sb
!vm
:n-word-bits
) increment
))
887 (return-from make-gcd-bignum-odd
888 (bignum-buffer-ashift-right a len-a
889 (+ (* index digit-size
)
895 (eval-when (:compile-toplevel
:execute
)
897 ;;; This negates bignum-len digits of bignum, storing the resulting digits into
898 ;;; result (possibly EQ to bignum) and returning whatever end-carry there is.
899 (sb!xc
:defmacro bignum-negate-loop
900 (bignum bignum-len
&optional
(result nil resultp
))
901 (with-unique-names (carry end value last
)
902 `(let* (,@(if (not resultp
) `(,last
))
904 (multiple-value-bind (,value
,carry
)
905 (%add-with-carry
(%lognot
(%bignum-ref
,bignum
0)) 1 0)
907 `(setf (%bignum-ref
,result
0) ,value
)
908 `(setf ,last
,value
))
912 (declare (type bit
,carry
)
913 (type bignum-index i
)
914 (type bignum-length
,end
))
916 (when (= i
,end
) (return))
917 (multiple-value-bind (,value temp
)
918 (%add-with-carry
(%lognot
(%bignum-ref
,bignum i
)) 0 ,carry
)
920 `(setf (%bignum-ref
,result i
) ,value
)
921 `(setf ,last
,value
))
924 ,(if resultp carry
`(values ,carry
,last
)))))
928 ;;; Fully-normalize is an internal optional. It cause this to always return
929 ;;; a bignum, without any extraneous digits, and it never returns a fixnum.
930 (defun negate-bignum (x &optional
(fully-normalize t
))
931 (declare (type bignum-type x
))
932 (let* ((len-x (%bignum-length x
))
934 (res (%allocate-bignum len-res
)))
935 (declare (type bignum-length len-x len-res
)) ;Test len-res for range?
936 (let ((carry (bignum-negate-loop x len-x res
)))
937 (setf (%bignum-ref res len-x
)
938 (%add-with-carry
(%lognot
(%sign-digit x len-x
)) 0 carry
)))
940 (%normalize-bignum res len-res
)
941 (%mostly-normalize-bignum res len-res
))))
943 ;;; This assumes bignum is positive; that is, the result of negating it will
944 ;;; stay in the provided allocated bignum.
945 (declaim (maybe-inline negate-bignum-buffer-in-place
))
946 (defun negate-bignum-buffer-in-place (bignum bignum-len
)
947 (bignum-negate-loop bignum bignum-len bignum
)
950 (defun negate-bignum-in-place (bignum)
951 (declare (inline negate-bignum-buffer-in-place
))
952 (negate-bignum-buffer-in-place bignum
(%bignum-length bignum
)))
954 (defun bignum-abs-buffer (bignum len
)
955 (unless (%bignum-0-or-plusp bignum len
)
956 (negate-bignum-buffer-in-place bignum len
)))
960 (eval-when (:compile-toplevel
:execute
)
962 ;;; This macro is used by BIGNUM-ASHIFT-RIGHT, BIGNUM-BUFFER-ASHIFT-RIGHT, and
963 ;;; BIGNUM-LDB-BIGNUM-RES. They supply a termination form that references
964 ;;; locals established by this form. Source is the source bignum. Start-digit
965 ;;; is the first digit in source from which we pull bits. Start-pos is the
966 ;;; first bit we want. Res-len-form is the form that computes the length of
967 ;;; the resulting bignum. Termination is a DO termination form with a test and
968 ;;; body. When result is supplied, it is the variable to which this binds a
969 ;;; newly allocated bignum.
971 ;;; Given start-pos, 1-31 inclusively, of shift, we form the j'th resulting
972 ;;; digit from high bits of the i'th source digit and the start-pos number of
973 ;;; bits from the i+1'th source digit.
974 (sb!xc
:defmacro shift-right-unaligned
(source
980 `(let* ((high-bits-in-first-digit (- digit-size
,start-pos
))
981 (res-len ,res-len-form
)
982 (res-len-1 (1- res-len
))
983 ,@(if result
`((,result
(%allocate-bignum res-len
)))))
984 (declare (type bignum-length res-len res-len-1
))
985 (do ((i ,start-digit
(1+ i
))
988 (declare (type bignum-index i j
))
989 (setf (%bignum-ref
,(if result result source
) j
)
990 (%logior
(%digit-logical-shift-right
(%bignum-ref
,source i
)
992 (%ashl
(%bignum-ref
,source
(1+ i
))
993 high-bits-in-first-digit
))))))
997 ;;; First compute the number of whole digits to shift, shifting them by
998 ;;; skipping them when we start to pick up bits, and the number of bits to
999 ;;; shift the remaining digits into place. If the number of digits is greater
1000 ;;; than the length of the bignum, then the result is either 0 or -1. If we
1001 ;;; shift on a digit boundary (that is, n-bits is zero), then we just copy
1002 ;;; digits. The last branch handles the general case which uses a macro that a
1003 ;;; couple other routines use. The fifth argument to the macro references
1004 ;;; locals established by the macro.
1005 (defun bignum-ashift-right (bignum count
)
1006 (declare (type bignum-type bignum
)
1007 (type unsigned-byte count
))
1008 (let ((bignum-len (%bignum-length bignum
)))
1009 (cond ((fixnump count
)
1010 (multiple-value-bind (digits n-bits
) (truncate count digit-size
)
1011 (declare (type bignum-length digits
))
1013 ((>= digits bignum-len
)
1014 (if (%bignum-0-or-plusp bignum bignum-len
) 0 -
1))
1016 (bignum-ashift-right-digits bignum digits
))
1018 (shift-right-unaligned bignum digits n-bits
(- bignum-len digits
)
1020 (setf (%bignum-ref res j
)
1021 (%ashr
(%bignum-ref bignum i
) n-bits
))
1022 (%normalize-bignum res res-len
))
1024 ((> count bignum-len
)
1025 (if (%bignum-0-or-plusp bignum bignum-len
) 0 -
1))
1026 ;; Since a FIXNUM should be big enough to address anything in
1027 ;; memory, including arrays of bits, and since arrays of bits
1028 ;; take up about the same space as corresponding fixnums, there
1029 ;; should be no way that we fall through to this case: any shift
1030 ;; right by a bignum should give zero. But let's check anyway:
1031 (t (error "bignum overflow: can't shift right by ~S" count
)))))
1033 (defun bignum-ashift-right-digits (bignum digits
)
1034 (declare (type bignum-type bignum
)
1035 (type bignum-length digits
))
1036 (let* ((res-len (- (%bignum-length bignum
) digits
))
1037 (res (%allocate-bignum res-len
)))
1038 (declare (type bignum-length res-len
)
1039 (type bignum-type res
))
1040 (bignum-replace res bignum
:start2 digits
)
1041 (%normalize-bignum res res-len
)))
1043 ;;; GCD uses this for an in-place shifting operation. This is different enough
1044 ;;; from BIGNUM-ASHIFT-RIGHT that it isn't worth folding the bodies into a
1045 ;;; macro, but they share the basic algorithm. This routine foregoes a first
1046 ;;; test for digits being greater than or equal to bignum-len since that will
1047 ;;; never happen for its uses in GCD. We did fold the last branch into a macro
1048 ;;; since it was duplicated a few times, and the fifth argument to it
1049 ;;; references locals established by the macro.
1050 (defun bignum-buffer-ashift-right (bignum bignum-len x
)
1051 (declare (type bignum-length bignum-len
) (fixnum x
))
1052 (multiple-value-bind (digits n-bits
) (truncate x digit-size
)
1053 (declare (type bignum-length digits
))
1056 (let ((new-end (- bignum-len digits
)))
1057 (bignum-replace bignum bignum
:end1 new-end
:start2 digits
1059 (%normalize-bignum-buffer bignum new-end
)))
1061 (shift-right-unaligned bignum digits n-bits
(- bignum-len digits
)
1063 (setf (%bignum-ref bignum j
)
1064 (%ashr
(%bignum-ref bignum i
) n-bits
))
1065 (%normalize-bignum-buffer bignum res-len
)))))))
1067 ;;; This handles shifting a bignum buffer to provide fresh bignum data for some
1068 ;;; internal routines. We know bignum is safe when called with bignum-len.
1069 ;;; First we compute the number of whole digits to shift, shifting them
1070 ;;; starting to store farther along the result bignum. If we shift on a digit
1071 ;;; boundary (that is, n-bits is zero), then we just copy digits. The last
1072 ;;; branch handles the general case.
1073 (defun bignum-ashift-left (bignum x
&optional bignum-len
)
1074 (declare (type bignum-type bignum
)
1075 (type unsigned-byte x
)
1076 (type (or null bignum-length
) bignum-len
))
1078 (multiple-value-bind (digits n-bits
) (truncate x digit-size
)
1079 (let* ((bignum-len (or bignum-len
(%bignum-length bignum
)))
1080 (res-len (+ digits bignum-len
1)))
1081 (when (> res-len maximum-bignum-length
)
1082 (error "can't represent result of left shift"))
1084 (bignum-ashift-left-digits bignum bignum-len digits
)
1085 (bignum-ashift-left-unaligned bignum digits n-bits res-len
))))
1086 ;; Left shift by a number too big to be represented as a fixnum
1087 ;; would exceed our memory capacity, since a fixnum is big enough
1088 ;; to index any array, including a bit array.
1089 (error "can't represent result of left shift")))
1091 (defun bignum-ashift-left-digits (bignum bignum-len digits
)
1092 (declare (type bignum-length bignum-len digits
))
1093 (let* ((res-len (+ bignum-len digits
))
1094 (res (%allocate-bignum res-len
)))
1095 (declare (type bignum-length res-len
))
1096 (bignum-replace res bignum
:start1 digits
:end1 res-len
:end2 bignum-len
1100 ;;; BIGNUM-TRUNCATE uses this to store into a bignum buffer by supplying res.
1101 ;;; When res comes in non-nil, then this foregoes allocating a result, and it
1102 ;;; normalizes the buffer instead of the would-be allocated result.
1104 ;;; We start storing into one digit higher than digits, storing a whole result
1105 ;;; digit from parts of two contiguous digits from bignum. When the loop
1106 ;;; finishes, we store the remaining bits from bignum's first digit in the
1107 ;;; first non-zero result digit, digits. We also grab some left over high
1108 ;;; bits from the last digit of bignum.
1109 (defun bignum-ashift-left-unaligned (bignum digits n-bits res-len
1110 &optional
(res nil resp
))
1111 (declare (type bignum-length digits res-len
)
1112 (type (mod #.digit-size
) n-bits
))
1113 (let* ((remaining-bits (- digit-size n-bits
))
1114 (res-len-1 (1- res-len
))
1115 (res (or res
(%allocate-bignum res-len
))))
1116 (declare (type bignum-length res-len res-len-1
))
1118 (j (1+ digits
) (1+ j
)))
1120 (setf (%bignum-ref res digits
)
1121 (%ashl
(%bignum-ref bignum
0) n-bits
))
1122 (setf (%bignum-ref res j
)
1123 (%ashr
(%bignum-ref bignum i
) remaining-bits
))
1125 (%normalize-bignum-buffer res res-len
)
1126 (%normalize-bignum res res-len
)))
1127 (declare (type bignum-index i j
))
1128 (setf (%bignum-ref res j
)
1129 (%logior
(%digit-logical-shift-right
(%bignum-ref bignum i
)
1131 (%ashl
(%bignum-ref bignum
(1+ i
)) n-bits
))))))
1133 ;;;; relational operators
1135 ;;; Return T iff bignum is positive.
1136 (defun bignum-plus-p (bignum)
1137 (declare (type bignum-type bignum
))
1138 (%bignum-0-or-plusp bignum
(%bignum-length bignum
)))
1140 ;;; This compares two bignums returning -1, 0, or 1, depending on
1141 ;;; whether a is less than, equal to, or greater than b.
1142 (declaim (ftype (function (bignum bignum
) (integer -
1 1)) bignum-compare
))
1143 (defun bignum-compare (a b
)
1144 (declare (type bignum-type a b
))
1145 (let* ((len-a (%bignum-length a
))
1146 (len-b (%bignum-length b
))
1147 (a-plusp (%bignum-0-or-plusp a len-a
))
1148 (b-plusp (%bignum-0-or-plusp b len-b
)))
1149 (declare (type bignum-length len-a len-b
))
1150 (cond ((not (eq a-plusp b-plusp
))
1153 (do ((i (1- len-a
) (1- i
)))
1155 (declare (type bignum-index i
))
1156 (let ((a-digit (%bignum-ref a i
))
1157 (b-digit (%bignum-ref b i
)))
1158 (declare (type bignum-element-type a-digit b-digit
))
1159 (when (%digit-greater a-digit b-digit
)
1161 (when (%digit-greater b-digit a-digit
)
1163 (when (zerop i
) (return 0))))
1166 (t (if a-plusp -
1 1)))))
1168 ;;;; float conversion
1170 ;;; Make a single or double float with the specified significand,
1171 ;;; exponent and sign.
1172 (defun single-float-from-bits (bits exp plusp
)
1173 (declare (fixnum exp
))
1174 (declare (optimize #-sb-xc-host
(inhibit-warnings 3)))
1176 sb
!vm
:single-float-exponent-byte
1177 (logandc2 (logand #xffffffff
1178 (%bignum-ref bits
1))
1179 sb
!vm
:single-float-hidden-bit
))))
1183 (logior res
(ash -
1 sb
!vm
:float-sign-shift
))))))
1184 (defun double-float-from-bits (bits exp plusp
)
1185 (declare (fixnum exp
))
1186 (declare (optimize #-sb-xc-host
(inhibit-warnings 3)))
1188 sb
!vm
:double-float-exponent-byte
1189 (logandc2 (ecase sb
!vm
::n-word-bits
1190 (32 (%bignum-ref bits
2))
1191 (64 (ash (%bignum-ref bits
1) -
32)))
1192 sb
!vm
:double-float-hidden-bit
)))
1193 (lo (logand #xffffffff
(%bignum-ref bits
1))))
1194 (make-double-float (if plusp
1196 (logior hi
(ash -
1 sb
!vm
:float-sign-shift
)))
1198 #!+(and long-float x86
)
1199 (defun long-float-from-bits (bits exp plusp
)
1200 (declare (fixnum exp
))
1201 (declare (optimize #-sb-xc-host
(inhibit-warnings 3)))
1205 (logior exp
(ash 1 15)))
1206 (%bignum-ref bits
2)
1207 (%bignum-ref bits
1)))
1209 ;;; Convert Bignum to a float in the specified Format, rounding to the best
1211 (defun bignum-to-float (bignum format
)
1212 (let* ((plusp (bignum-plus-p bignum
))
1213 (x (if plusp bignum
(negate-bignum bignum
)))
1214 (len (bignum-integer-length x
))
1215 (digits (float-format-digits format
))
1216 (keep (+ digits digit-size
))
1217 (shift (- keep len
))
1218 (shifted (if (minusp shift
)
1219 (bignum-ashift-right x
(- shift
))
1220 (bignum-ashift-left x shift
)))
1221 (low (%bignum-ref shifted
0))
1222 (round-bit (ash 1 (1- digit-size
))))
1223 (declare (type bignum-length len digits keep
) (fixnum shift
))
1224 (labels ((round-up ()
1225 (let ((rounded (add-bignums shifted round-bit
)))
1226 (if (> (integer-length rounded
) keep
)
1227 (float-from-bits (bignum-ashift-right rounded
1)
1229 (float-from-bits rounded len
))))
1230 (float-from-bits (bits len
)
1231 (declare (type bignum-length len
))
1234 (single-float-from-bits
1236 (check-exponent len sb
!vm
:single-float-bias
1237 sb
!vm
:single-float-normal-exponent-max
)
1240 (double-float-from-bits
1242 (check-exponent len sb
!vm
:double-float-bias
1243 sb
!vm
:double-float-normal-exponent-max
)
1247 (long-float-from-bits
1249 (check-exponent len sb
!vm
:long-float-bias
1250 sb
!vm
:long-float-normal-exponent-max
)
1252 (check-exponent (exp bias max
)
1253 (declare (type bignum-length len
))
1254 (let ((exp (+ exp bias
)))
1256 ;; Why a SIMPLE-TYPE-ERROR? Well, this is mainly
1257 ;; called by COERCE, which requires an error of
1258 ;; TYPE-ERROR if the conversion can't happen
1259 ;; (except in certain circumstances when we are
1260 ;; coercing to a FUNCTION) -- CSR, 2002-09-18
1261 (error 'simple-type-error
1262 :format-control
"Too large to be represented as a ~S:~% ~S"
1263 :format-arguments
(list format x
)
1264 :expected-type format
1269 ;; Round down if round bit is 0.
1270 ((not (logtest round-bit low
))
1271 (float-from-bits shifted len
))
1272 ;; If only round bit is set, then round to even.
1273 ((and (= low round-bit
)
1274 (dotimes (i (- (%bignum-length x
) (ceiling keep digit-size
))
1276 (unless (zerop (%bignum-ref x i
)) (return nil
))))
1277 (let ((next (%bignum-ref shifted
1)))
1280 (float-from-bits shifted len
))))
1281 ;; Otherwise, round up.
1285 ;;;; integer length and logbitp/logcount
1287 (defun bignum-buffer-integer-length (bignum len
)
1288 (declare (type bignum-type bignum
))
1289 (let* ((len-1 (1- len
))
1290 (digit (%bignum-ref bignum len-1
)))
1291 (declare (type bignum-length len len-1
)
1292 (type bignum-element-type digit
))
1293 (+ (integer-length (%fixnum-digit-with-correct-sign digit
))
1294 (* len-1 digit-size
))))
1296 (defun bignum-integer-length (bignum)
1297 (declare (type bignum-type bignum
))
1298 (bignum-buffer-integer-length bignum
(%bignum-length bignum
)))
1300 (defun bignum-logbitp (index bignum
)
1301 (declare (type bignum-type bignum
)
1302 (type bignum-index index
))
1303 (let ((len (%bignum-length bignum
)))
1304 (declare (type bignum-length len
))
1305 (multiple-value-bind (word-index bit-index
)
1306 (floor index digit-size
)
1307 (if (>= word-index len
)
1308 (not (bignum-plus-p bignum
))
1309 (logbitp bit-index
(%bignum-ref bignum word-index
))))))
1311 (defun bignum-logcount (bignum)
1312 (declare (type bignum-type bignum
)
1314 (declare (muffle-conditions compiler-note
)) ; returns lispobj, so what.
1315 (let ((length (%bignum-length bignum
))
1317 (declare (type bignum-length length
)
1319 (do ((index 0 (1+ index
)))
1321 (if (%bignum-0-or-plusp bignum length
)
1323 (- (* length digit-size
) result
)))
1324 (let ((digit (%bignum-ref bignum index
)))
1325 (declare (type bignum-element-type digit
))
1326 (incf result
(logcount digit
))))))
1328 ;;;; logical operations
1332 (defun bignum-logical-not (a)
1333 (declare (type bignum-type a
))
1334 (let* ((len (%bignum-length a
))
1335 (res (%allocate-bignum len
)))
1336 (declare (type bignum-length len
))
1337 (dotimes (i len res
)
1338 (declare (type bignum-index i
))
1339 (setf (%bignum-ref res i
) (%lognot
(%bignum-ref a i
))))))
1343 (defun bignum-logical-and (a b
)
1344 (declare (type bignum-type a b
))
1345 (let* ((len-a (%bignum-length a
))
1346 (len-b (%bignum-length b
))
1347 (a-plusp (%bignum-0-or-plusp a len-a
))
1348 (b-plusp (%bignum-0-or-plusp b len-b
)))
1349 (declare (type bignum-length len-a len-b
))
1353 (logand-shorter-positive a len-a b
(%allocate-bignum len-a
))
1354 (logand-shorter-negative a len-a b len-b
(%allocate-bignum len-b
))))
1357 (logand-shorter-positive b len-b a
(%allocate-bignum len-b
))
1358 (logand-shorter-negative b len-b a len-a
(%allocate-bignum len-a
))))
1359 (t (logand-shorter-positive a len-a b
(%allocate-bignum len-a
))))))
1361 ;;; This takes a shorter bignum, a and len-a, that is positive. Because this
1362 ;;; is AND, we don't care about any bits longer than a's since its infinite 0
1363 ;;; sign bits will mask the other bits out of b. The result is len-a big.
1364 (defun logand-shorter-positive (a len-a b res
)
1365 (declare (type bignum-type a b res
)
1366 (type bignum-length len-a
))
1368 (declare (type bignum-index i
))
1369 (setf (%bignum-ref res i
)
1370 (%logand
(%bignum-ref a i
) (%bignum-ref b i
))))
1371 (%normalize-bignum res len-a
))
1373 ;;; This takes a shorter bignum, a and len-a, that is negative. Because this
1374 ;;; is AND, we just copy any bits longer than a's since its infinite 1 sign
1375 ;;; bits will include any bits from b. The result is len-b big.
1376 (defun logand-shorter-negative (a len-a b len-b res
)
1377 (declare (type bignum-type a b res
)
1378 (type bignum-length len-a len-b
))
1380 (declare (type bignum-index i
))
1381 (setf (%bignum-ref res i
)
1382 (%logand
(%bignum-ref a i
) (%bignum-ref b i
))))
1383 (do ((i len-a
(1+ i
)))
1385 (declare (type bignum-index i
))
1386 (setf (%bignum-ref res i
) (%bignum-ref b i
)))
1387 (%normalize-bignum res len-b
))
1391 (defun bignum-logical-ior (a b
)
1392 (declare (type bignum-type a b
))
1393 (let* ((len-a (%bignum-length a
))
1394 (len-b (%bignum-length b
))
1395 (a-plusp (%bignum-0-or-plusp a len-a
))
1396 (b-plusp (%bignum-0-or-plusp b len-b
)))
1397 (declare (type bignum-length len-a len-b
))
1401 (logior-shorter-positive a len-a b len-b
(%allocate-bignum len-b
))
1402 (logior-shorter-negative a len-a b len-b
(%allocate-bignum len-b
))))
1405 (logior-shorter-positive b len-b a len-a
(%allocate-bignum len-a
))
1406 (logior-shorter-negative b len-b a len-a
(%allocate-bignum len-a
))))
1407 (t (logior-shorter-positive a len-a b len-b
(%allocate-bignum len-a
))))))
1409 ;;; This takes a shorter bignum, a and len-a, that is positive. Because this
1410 ;;; is IOR, we don't care about any bits longer than a's since its infinite
1411 ;;; 0 sign bits will mask the other bits out of b out to len-b. The result
1413 (defun logior-shorter-positive (a len-a b len-b res
)
1414 (declare (type bignum-type a b res
)
1415 (type bignum-length len-a len-b
))
1417 (declare (type bignum-index i
))
1418 (setf (%bignum-ref res i
)
1419 (%logior
(%bignum-ref a i
) (%bignum-ref b i
))))
1420 (do ((i len-a
(1+ i
)))
1422 (declare (type bignum-index i
))
1423 (setf (%bignum-ref res i
) (%bignum-ref b i
)))
1424 (%normalize-bignum res len-b
))
1426 ;;; This takes a shorter bignum, a and len-a, that is negative. Because this
1427 ;;; is IOR, we just copy any bits longer than a's since its infinite 1 sign
1428 ;;; bits will include any bits from b. The result is len-b long.
1429 (defun logior-shorter-negative (a len-a b len-b res
)
1430 (declare (type bignum-type a b res
)
1431 (type bignum-length len-a len-b
))
1433 (declare (type bignum-index i
))
1434 (setf (%bignum-ref res i
)
1435 (%logior
(%bignum-ref a i
) (%bignum-ref b i
))))
1436 (do ((i len-a
(1+ i
))
1437 (sign (%sign-digit a len-a
)))
1439 (declare (type bignum-index i
))
1440 (setf (%bignum-ref res i
) sign
))
1441 (%normalize-bignum res len-b
))
1445 (defun bignum-logical-xor (a b
)
1446 (declare (type bignum-type a b
))
1447 (let ((len-a (%bignum-length a
))
1448 (len-b (%bignum-length b
)))
1449 (declare (type bignum-length len-a len-b
))
1451 (bignum-logical-xor-aux a len-a b len-b
(%allocate-bignum len-b
))
1452 (bignum-logical-xor-aux b len-b a len-a
(%allocate-bignum len-a
)))))
1454 ;;; This takes the shorter of two bignums in a and len-a. Res is len-b
1455 ;;; long. Do the XOR.
1456 (defun bignum-logical-xor-aux (a len-a b len-b res
)
1457 (declare (type bignum-type a b res
)
1458 (type bignum-length len-a len-b
))
1460 (declare (type bignum-index i
))
1461 (setf (%bignum-ref res i
)
1462 (%logxor
(%bignum-ref a i
) (%bignum-ref b i
))))
1463 (do ((i len-a
(1+ i
))
1464 (sign (%sign-digit a len-a
)))
1466 (declare (type bignum-index i
))
1467 (setf (%bignum-ref res i
) (%logxor sign
(%bignum-ref b i
))))
1468 (%normalize-bignum res len-b
))
1470 ;;;; There used to be a bunch of code to implement "efficient" versions of LDB
1471 ;;;; and DPB here. But it apparently was never used, so it's been deleted.
1472 ;;;; --njf, 2007-02-04
1476 ;;; This is the original sketch of the algorithm from which I implemented this
1477 ;;; TRUNCATE, assuming both operands are bignums. I should modify this to work
1478 ;;; with the documentation on my functions, as a general introduction. I've
1479 ;;; left this here just in case someone needs it in the future. Don't look at
1480 ;;; this unless reading the functions' comments leaves you at a loss. Remember
1481 ;;; this comes from Knuth, so the book might give you the right general
1486 ;;; If X's magnitude is less than Y's, then result is 0 with remainder X.
1488 ;;; Make x and y positive, copying x if it is already positive.
1490 ;;; Shift y left until there's a 1 in the 30'th bit (most significant, non-sign
1492 ;;; Just do most sig digit to determine how much to shift whole number.
1493 ;;; Shift x this much too.
1494 ;;; Remember this initial shift count.
1496 ;;; Allocate q to be len-x minus len-y quantity plus 1.
1498 ;;; i = last digit of x.
1499 ;;; k = last digit of q.
1503 ;;; j = last digit of y.
1506 ;;; if x[i] = y[j] then g = (1- (ash 1 digit-size))
1507 ;;; else g = x[i]x[i-1]/y[j].
1510 ;;; %UNSIGNED-MULTIPLY returns b and c defined below.
1511 ;;; a = x[i-1] - (logand (* g y[j]) #xFFFFFFFF).
1512 ;;; Use %UNSIGNED-MULTIPLY taking low-order result.
1513 ;;; b = (logand (ash (* g y[j-1]) (- digit-size)) (1- (ash 1 digit-size))).
1514 ;;; c = (logand (* g y[j-1]) (1- (ash 1 digit-size))).
1516 ;;; if a > b, guess is too high
1517 ;;; g = g - 1; go back to "check guess".
1518 ;;; if a = b and c > x[i-2], guess is too high
1519 ;;; g = g - 1; go back to "check guess".
1520 ;;; GUESS IS 32-BIT NUMBER, SO USE THING TO KEEP IN SPECIAL REGISTER
1521 ;;; SAME FOR A, B, AND C.
1523 ;;; Subtract g * y from x[i - len-y+1]..x[i]. See paper for doing this in step.
1524 ;;; If x[i] < 0, guess is screwed up.
1525 ;;; negative g, then add 1
1526 ;;; zero or positive g, then subtract 1
1527 ;;; AND add y back into x[len-y+1..i].
1533 ;;; If k>=0, goto LOOP.
1535 ;;; Now quotient is good, but remainder is not.
1536 ;;; Shift x right by saved initial left shifting count.
1538 ;;; Check quotient and remainder signs.
1539 ;;; x pos y pos --> q pos r pos
1540 ;;; x pos y neg --> q neg r pos
1541 ;;; x neg y pos --> q neg r neg
1542 ;;; x neg y neg --> q pos r neg
1544 ;;; Normalize quotient and remainder. Cons result if necessary.
1547 ;;; This used to be split into multiple functions, which shared state
1548 ;;; in special variables *TRUNCATE-X* and *TRUNCATE-Y*. Having so many
1549 ;;; special variable accesses in tight inner loops was having a large
1550 ;;; effect on performance, so the helper functions have now been
1551 ;;; refactored into local functions and the special variables into
1552 ;;; lexicals. There was also a lot of boxing and unboxing of
1553 ;;; (UNSIGNED-BYTE 32)'s going on, which this refactoring
1554 ;;; eliminated. This improves the performance on some CL-BENCH tests
1555 ;;; by up to 50%, which is probably signigicant enough to justify the
1556 ;;; reduction in readability that was introduced. --JES, 2004-08-07
1557 (defun bignum-truncate (x y
)
1558 (declare (type bignum-type x y
))
1559 (declare (muffle-conditions compiler-note
)) ; returns lispobj, so what.
1560 (let (truncate-x truncate-y
)
1562 ;;; Divide X by Y when Y is a single bignum digit. BIGNUM-TRUNCATE
1563 ;;; fixes up the quotient and remainder with respect to sign and
1566 ;;; We don't have to worry about shifting Y to make its most
1567 ;;; significant digit sufficiently large for %BIGFLOOR to return
1568 ;;; digit-size quantities for the q-digit and r-digit. If Y is
1569 ;;; a single digit bignum, it is already large enough for
1570 ;;; %BIGFLOOR. That is, it has some bits on pretty high in the
1572 ((bignum-truncate-single-digit (x len-x y
)
1573 (declare (type bignum-length len-x
))
1574 (let ((y (%bignum-ref y
0)))
1575 (declare (type bignum-element-type y
))
1576 (if (not (logtest y
(1- y
)))
1577 ;; Y is a power of two.
1578 ;; SHIFT-RIGHT-UNALIGNED won't do the right thing
1579 ;; with a shift count of 0 or -1, so special case this.
1581 (error 'division-by-zero
:operation
'truncate
1582 :operands
(list x y
)))
1584 ;; We could probably get away with (VALUES X 0)
1585 ;; here, but it's not clear that some of the
1586 ;; normalization logic further down would avoid
1587 ;; mutilating X. Just go ahead and cons, consing's
1589 (values (copy-bignum x len-x
) 0))
1591 (let ((n-bits (1- (integer-length y
))))
1593 (shift-right-unaligned x
0 n-bits len-x
1595 (setf (%bignum-ref res j
)
1596 (%ashr
(%bignum-ref x i
) n-bits
))
1599 (logand (%bignum-ref x
0) (1- y
))))))
1600 (do ((i (1- len-x
) (1- i
))
1601 (q (%allocate-bignum len-x
))
1604 (let ((rem (%allocate-bignum
1)))
1605 (setf (%bignum-ref rem
0) r
)
1607 (declare (type bignum-element-type r
))
1608 (multiple-value-bind (q-digit r-digit
)
1609 (%bigfloor r
(%bignum-ref x i
) y
)
1610 (declare (type bignum-element-type q-digit r-digit
))
1611 (setf (%bignum-ref q i
) q-digit
)
1612 (setf r r-digit
))))))
1613 ;;; This returns a guess for the next division step. Y1 is the
1614 ;;; highest y digit, and y2 is the second to highest y
1615 ;;; digit. The x... variables are the three highest x digits
1616 ;;; for the next division step.
1618 ;;; From Knuth, our guess is either all ones or x-i and x-i-1
1619 ;;; divided by y1, depending on whether x-i and y1 are the
1620 ;;; same. We test this guess by determining whether guess*y2
1621 ;;; is greater than the three high digits of x minus guess*y1
1622 ;;; shifted left one digit:
1623 ;;; ------------------------------
1624 ;;; | x-i | x-i-1 | x-i-2 |
1625 ;;; ------------------------------
1626 ;;; ------------------------------
1627 ;;; - | g*y1 high | g*y1 low | 0 |
1628 ;;; ------------------------------
1629 ;;; ... < guess*y2 ???
1630 ;;; If guess*y2 is greater, then we decrement our guess by one
1631 ;;; and try again. This returns a guess that is either
1632 ;;; correct or one too large.
1633 (bignum-truncate-guess (y1 y2 x-i x-i-1 x-i-2
)
1634 (declare (type bignum-element-type y1 y2 x-i x-i-1 x-i-2
))
1635 (let ((guess (if (%digit-compare x-i y1
)
1637 (%bigfloor x-i x-i-1 y1
))))
1638 (declare (type bignum-element-type guess
))
1640 (multiple-value-bind (high-guess*y1 low-guess
*y1
)
1641 (%multiply guess y1
)
1642 (declare (type bignum-element-type low-guess
*y1
1644 (multiple-value-bind (high-guess*y2 low-guess
*y2
)
1645 (%multiply guess y2
)
1646 (declare (type bignum-element-type high-guess
*y2
1648 (multiple-value-bind (middle-digit borrow
)
1649 (%subtract-with-borrow x-i-1 low-guess
*y1
1)
1650 (declare (type bignum-element-type middle-digit
)
1652 ;; Supplying borrow of 1 means there was no
1653 ;; borrow, and we know x-i-2 minus 0 requires
1655 (let ((high-digit (%subtract-with-borrow x-i
1658 (declare (type bignum-element-type high-digit
))
1659 (if (and (%digit-compare high-digit
0)
1660 (or (%digit-greater high-guess
*y2
1662 (and (%digit-compare middle-digit
1664 (%digit-greater low-guess
*y2
1666 (setf guess
(%subtract-with-borrow guess
1 1))
1667 (return guess
)))))))))
1668 ;;; Divide TRUNCATE-X by TRUNCATE-Y, returning the quotient
1669 ;;; and destructively modifying TRUNCATE-X so that it holds
1672 ;;; LEN-X and LEN-Y tell us how much of the buffers we care about.
1674 ;;; TRUNCATE-X definitely has at least three digits, and it has one
1675 ;;; more than TRUNCATE-Y. This keeps i, i-1, i-2, and low-x-digit
1676 ;;; happy. Thanks to SHIFT-AND-STORE-TRUNCATE-BUFFERS.
1677 (return-quotient-leaving-remainder (len-x len-y
)
1678 (declare (type bignum-length len-x len-y
))
1679 (let* ((len-q (- len-x len-y
))
1680 ;; Add one for extra sign digit in case high bit is on.
1681 (q (%allocate-bignum
(1+ len-q
)))
1683 (y1 (%bignum-ref truncate-y
(1- len-y
)))
1684 (y2 (%bignum-ref truncate-y
(- len-y
2)))
1688 (low-x-digit (- i len-y
)))
1689 (declare (type bignum-length len-q
)
1690 (type bignum-index k i i-1 i-2 low-x-digit
)
1691 (type bignum-element-type y1 y2
))
1693 (setf (%bignum-ref q k
)
1694 (try-bignum-truncate-guess
1695 ;; This modifies TRUNCATE-X. Must access
1696 ;; elements each pass.
1697 (bignum-truncate-guess y1 y2
1698 (%bignum-ref truncate-x i
)
1699 (%bignum-ref truncate-x i-1
)
1700 (%bignum-ref truncate-x i-2
))
1702 (cond ((zerop k
) (return))
1705 (shiftf i i-1 i-2
(1- i-2
)))))
1707 ;;; This takes a digit guess, multiplies it by TRUNCATE-Y for a
1708 ;;; result one greater in length than LEN-Y, and subtracts this result
1709 ;;; from TRUNCATE-X. LOW-X-DIGIT is the first digit of X to start
1710 ;;; the subtraction, and we know X is long enough to subtract a LEN-Y
1711 ;;; plus one length bignum from it. Next we check the result of the
1712 ;;; subtraction, and if the high digit in X became negative, then our
1713 ;;; guess was one too big. In this case, return one less than GUESS
1714 ;;; passed in, and add one value of Y back into X to account for
1715 ;;; subtracting one too many. Knuth shows that the guess is wrong on
1716 ;;; the order of 3/b, where b is the base (2 to the digit-size power)
1717 ;;; -- pretty rarely.
1718 (try-bignum-truncate-guess (guess len-y low-x-digit
)
1719 (declare (type bignum-index low-x-digit
)
1720 (type bignum-length len-y
)
1721 (type bignum-element-type guess
))
1722 (let ((carry-digit 0)
1725 (declare (type bignum-element-type carry-digit
)
1726 (type bignum-index i
)
1728 ;; Multiply guess and divisor, subtracting from dividend
1731 (multiple-value-bind (high-digit low-digit
)
1732 (%multiply-and-add guess
1733 (%bignum-ref truncate-y j
)
1735 (declare (type bignum-element-type high-digit low-digit
))
1736 (setf carry-digit high-digit
)
1737 (multiple-value-bind (x temp-borrow
)
1738 (%subtract-with-borrow
(%bignum-ref truncate-x i
)
1741 (declare (type bignum-element-type x
)
1742 (fixnum temp-borrow
))
1743 (setf (%bignum-ref truncate-x i
) x
)
1744 (setf borrow temp-borrow
)))
1746 (setf (%bignum-ref truncate-x i
)
1747 (%subtract-with-borrow
(%bignum-ref truncate-x i
)
1748 carry-digit borrow
))
1749 ;; See whether guess is off by one, adding one
1750 ;; Y back in if necessary.
1751 (cond ((%digit-0-or-plusp
(%bignum-ref truncate-x i
))
1754 ;; If subtraction has negative result, add one
1755 ;; divisor value back in. The guess was one too
1756 ;; large in magnitude.
1757 (let ((i low-x-digit
)
1760 (multiple-value-bind (v k
)
1761 (%add-with-carry
(%bignum-ref truncate-y j
)
1762 (%bignum-ref truncate-x i
)
1764 (declare (type bignum-element-type v
))
1765 (setf (%bignum-ref truncate-x i
) v
)
1768 (setf (%bignum-ref truncate-x i
)
1769 (%add-with-carry
(%bignum-ref truncate-x i
)
1771 (%subtract-with-borrow guess
1 1)))))
1772 ;;; This returns the amount to shift y to place a one in the
1773 ;;; second highest bit. Y must be positive. If the last digit
1774 ;;; of y is zero, then y has a one in the previous digit's
1775 ;;; sign bit, so we know it will take one less than digit-size
1776 ;;; to get a one where we want. Otherwise, we count how many
1777 ;;; right shifts it takes to get zero; subtracting this value
1778 ;;; from digit-size tells us how many high zeros there are
1779 ;;; which is one more than the shift amount sought.
1781 ;;; Note: This is exactly the same as one less than the
1782 ;;; integer-length of the last digit subtracted from the
1785 ;;; We shift y to make it sufficiently large that doing the
1786 ;;; 2*digit-size by digit-size %BIGFLOOR calls ensures the quotient and
1787 ;;; remainder fit in digit-size.
1788 (shift-y-for-truncate (y)
1789 (let* ((len (%bignum-length y
))
1790 (last (%bignum-ref y
(1- len
))))
1791 (declare (type bignum-length len
)
1792 (type bignum-element-type last
))
1793 (- digit-size
(integer-length last
) 1)))
1794 ;;; Stores two bignums into the truncation bignum buffers,
1795 ;;; shifting them on the way in. This assumes x and y are
1796 ;;; positive and at least two in length, and it assumes
1797 ;;; truncate-x and truncate-y are one digit longer than x and
1799 (shift-and-store-truncate-buffers (x len-x y len-y shift
)
1800 (declare (type bignum-length len-x len-y
)
1801 (type (integer 0 (#.digit-size
)) shift
))
1802 (cond ((zerop shift
)
1803 (bignum-replace truncate-x x
:end1 len-x
)
1804 (bignum-replace truncate-y y
:end1 len-y
))
1806 (bignum-ashift-left-unaligned x
0 shift
(1+ len-x
)
1808 (bignum-ashift-left-unaligned y
0 shift
(1+ len-y
)
1809 truncate-y
))))) ;; LABELS
1810 ;;; Divide X by Y returning the quotient and remainder. In the
1811 ;;; general case, we shift Y to set up for the algorithm, and we
1812 ;;; use two buffers to save consing intermediate values. X gets
1813 ;;; destructively modified to become the remainder, and we have
1814 ;;; to shift it to account for the initial Y shift. After we
1815 ;;; multiple bind q and r, we first fix up the signs and then
1816 ;;; return the normalized results.
1817 (let* ((x-plusp (%bignum-0-or-plusp x
(%bignum-length x
)))
1818 (y-plusp (%bignum-0-or-plusp y
(%bignum-length y
)))
1819 (x (if x-plusp x
(negate-bignum x nil
)))
1820 (y (if y-plusp y
(negate-bignum y nil
)))
1821 (len-x (%bignum-length x
))
1822 (len-y (%bignum-length y
)))
1823 (multiple-value-bind (q r
)
1825 (bignum-truncate-single-digit x len-x y
))
1826 ((plusp (bignum-compare y x
))
1827 (let ((res (%allocate-bignum len-x
)))
1829 (setf (%bignum-ref res i
) (%bignum-ref x i
)))
1832 (let ((len-x+1 (1+ len-x
)))
1833 (setf truncate-x
(%allocate-bignum len-x
+1))
1834 (setf truncate-y
(%allocate-bignum
(1+ len-y
)))
1835 (let ((y-shift (shift-y-for-truncate y
)))
1836 (shift-and-store-truncate-buffers x len-x y
1838 (values (return-quotient-leaving-remainder len-x
+1
1840 ;; Now that RETURN-QUOTIENT-LEAVING-REMAINDER
1841 ;; has executed, we just tidy up the remainder
1842 ;; (in TRUNCATE-X) and return it.
1845 (let ((res (%allocate-bignum len-y
)))
1846 (declare (type bignum-type res
))
1847 (bignum-replace res truncate-x
:end2 len-y
)
1848 (%normalize-bignum res len-y
)))
1850 (shift-right-unaligned
1851 truncate-x
0 y-shift len-y
1853 (setf (%bignum-ref res j
)
1854 (%ashr
(%bignum-ref truncate-x i
)
1856 (%normalize-bignum res res-len
))
1858 (let ((quotient (cond ((eq x-plusp y-plusp
) q
)
1859 ((typep q
'fixnum
) (the fixnum
(- q
)))
1860 (t (negate-bignum-in-place q
))))
1861 (rem (cond (x-plusp r
)
1862 ((typep r
'fixnum
) (the fixnum
(- r
)))
1863 (t (negate-bignum-in-place r
)))))
1864 (values (if (typep quotient
'fixnum
)
1866 (%normalize-bignum quotient
(%bignum-length quotient
)))
1867 (if (typep rem
'fixnum
)
1869 (%normalize-bignum rem
(%bignum-length rem
))))))))))
1872 ;;;; There used to be a pile of code for implementing division for bignum digits
1873 ;;;; for machines that don't have a 2*digit-size by digit-size divide instruction.
1874 ;;;; This happens to be most machines, but all the SBCL ports seem to be content
1875 ;;;; to implement SB-BIGNUM:%BIGFLOOR as a VOP rather than using the code here.
1876 ;;;; So it's been deleted. --njf, 2007-02-04
1878 ;;;; general utilities
1880 ;;; Internal in-place operations use this to fixup remaining digits in the
1881 ;;; incoming data, such as in-place shifting. This is basically the same as
1882 ;;; the first form in %NORMALIZE-BIGNUM, but we return the length of the buffer
1883 ;;; instead of shrinking the bignum.
1884 #!-sb-fluid
(declaim (maybe-inline %normalize-bignum-buffer
))
1885 (defun %normalize-bignum-buffer
(result len
)
1886 (declare (type bignum-type result
)
1887 (type bignum-length len
))
1889 (do ((next-digit (%bignum-ref result
(- len
2))
1890 (%bignum-ref result
(- len
2)))
1891 (sign-digit (%bignum-ref result
(1- len
)) next-digit
))
1892 ((not (zerop (logxor sign-digit
(%ashr next-digit
(1- digit-size
))))))
1894 (setf (%bignum-ref result len
) 0)
1899 ;;; This drops the last digit if it is unnecessary sign information. It repeats
1900 ;;; this as needed, possibly ending with a fixnum. If the resulting length from
1901 ;;; shrinking is one, see whether our one word is a fixnum. Shift the possible
1902 ;;; fixnum bits completely out of the word, and compare this with shifting the
1903 ;;; sign bit all the way through. If the bits are all 1's or 0's in both words,
1904 ;;; then there are just sign bits between the fixnum bits and the sign bit. If
1905 ;;; we do have a fixnum, shift it over for the two low-tag bits.
1906 (defun %normalize-bignum
(result len
)
1907 (declare (type bignum-type result
)
1908 (type bignum-length len
)
1909 (muffle-conditions compiler-note
)
1910 (inline %normalize-bignum-buffer
))
1911 (let ((newlen (%normalize-bignum-buffer result len
)))
1912 (declare (type bignum-length newlen
))
1913 (unless (= newlen len
)
1914 (%bignum-set-length result newlen
))
1916 (let ((digit (%bignum-ref result
0)))
1917 (if (= (%ashr digit sb
!vm
:n-positive-fixnum-bits
)
1918 (%ashr digit
(1- digit-size
)))
1919 (%fixnum-digit-with-correct-sign digit
)
1923 ;;; This drops the last digit if it is unnecessary sign information. It
1924 ;;; repeats this as needed, possibly ending with a fixnum magnitude but never
1925 ;;; returning a fixnum.
1926 (defun %mostly-normalize-bignum
(result len
)
1927 (declare (type bignum-type result
)
1928 (type bignum-length len
)
1929 (inline %normalize-bignum-buffer
))
1930 (let ((newlen (%normalize-bignum-buffer result len
)))
1931 (declare (type bignum-length newlen
))
1932 (unless (= newlen len
)
1933 (%bignum-set-length result newlen
))
1938 ;;; the bignum case of the SXHASH function
1939 (defun sxhash-bignum (x)
1940 (let ((result 316495330))
1941 (declare (type fixnum result
))
1942 (dotimes (i (%bignum-length x
))
1943 (declare (type index i
))
1944 (let ((xi (%bignum-ref x i
)))
1946 (logand most-positive-fixnum