Skip to content

Commit 3cbd661

Browse files
committed
Support larger bignum restore and a bit less cons-y
1 parent 8f00e48 commit 3cbd661

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

src/numbers.lisp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,21 @@
144144

145145
(defun restore-bignum (storage)
146146
(declare (optimize (speed 3) (safety 1)))
147-
(let ((count (restore-tagged-fixnum storage)))
147+
(let* ((count (restore-tagged-fixnum storage))
148+
(num-words (abs count)))
148149
(declare (type fixnum count))
149-
(ensure-enough-data storage (the fixnum (* 4 (abs count))))
150-
(* (if (< count 0) -1 1)
151-
(bits->num
152-
(loop repeat (the fixnum (abs count))
153-
collect (restore-ub32 storage))))))
150+
(unless (<= num-words (ash most-positive-fixnum -2))
151+
(unexpected-data "number of words in bignum" num-words))
152+
(check-if-too-much-data (read-storage-max-to-read storage)
153+
(truly-the fixnum (* 4 num-words)))
154+
(let ((sum 0))
155+
(loop
156+
repeat num-words
157+
for pos from 0 by 32
158+
do
159+
(ensure-enough-data storage 4)
160+
(incf sum (* (restore-ub32 storage) (expt 2 pos))))
161+
(* (if (< count 0) -1 1) sum))))
154162

155163
(defun store-bignum (bignum storage)
156164
(when storage

0 commit comments

Comments
 (0)