Changeset 11141
 Timestamp:
 Oct 18, 2008, 1:01:33 AM (11 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

trunk/source/level0/l0bignum64.lisp
r11111 r11141 142 142 ;;; a) it's not that bad 143 143 ;;; b) it does some bounds/sanity checking, which isn't a bad idea. 144 (evalwhen (:compiletoplevel :execute) 145 (declaim (inline bignumref bignumset))) 146 147 (defun bignumref (b i) 148 (%typedmiscref :bignum b i)) 149 150 (defun bignumset (b i val) 151 (declare (fixnum val)) 152 (%typedmiscset :bignum b i (logand val allonesdigit))) 144 145 (defmacro bignumref (b i) 146 `(%typedmiscref :bignum ,b ,i)) 147 148 (defmacro bignumset (b i val) 149 `(%typedmiscset :bignum ,b ,i ,val)) 153 150 154 151 … … 178 175 0)) 179 176 177 180 178 (defun %addwithcarry (adigit bdigit carryin) 181 179 (declare (fixnum adigit bdigit carryin)) … … 184 182 (let* ((sum (+ carryin (the fixnum (+ adigit bdigit))))) 185 183 (declare (fixnum sum)) 186 (values (logand allonesdigit sum) (logand 1 ( ash sum 32)))))184 (values (logand allonesdigit sum) (logand 1 (the fixnum (ash sum 32)))))) 187 185 188 186 (defun %subtractwithborrow (adigit bdigit borrowin) … … 214 212 (let* ((lena (%bignumlength a)) 215 213 (lenb (%bignumlength b))) 216 (declare (bignumindex lena lenb)) 214 (declare (bignumindex lena lenb) 215 (optimize (speed 3) (safety 0))) 217 216 (when (> lenb lena) 218 217 (rotatef a b) … … 223 222 (signb (%bignumsign b))) 224 223 (dotimes (i lenb) 225 (multiplevaluebind (resultdigit carryout) 226 (%addwithcarry (bignumref a i) (bignumref b i) carry) 227 (setf (bignumref res i) resultdigit 228 carry carryout))) 224 (let* ((sum (+ 225 (the fixnum (+ (the digittype (bignumref a i)) 226 (the digittype (bignumref b i)))) 227 carry))) 228 (declare (fixnum sum)) 229 (setf (bignumref res i) sum) 230 (setq carry (logand 1 (the fixnum (ash sum 32)))))) 229 231 (if (/= lena lenb) 230 232 (finishbignumadd res carry a signb lenb lena) 231 233 (setf (bignumref res lena) 232 (%addwithcarry (%bignumsign a) signb carry))) 234 (+ (the fixnum carry) 235 (the fixnum (+ (the digittype (%bignumsign a)) 236 signb))))) 233 237 (%normalizebignummacro res)))) 238 239 (defun addbignumandfixnum (bignum fixnum) 240 (declare (bignumtype bignum) 241 (fixnum fixnum) 242 (optimize (speed 3) (safety 0))) 243 (let* ((lenbignum (%bignumlength bignum)) 244 (lenres (1+ lenbignum)) 245 (res (%allocatebignum lenres)) 246 (low (logand allonesdigit fixnum)) 247 (high (logand allonesdigit (the fixnum (ash fixnum 32))))) 248 (declare (bignumindex lenbignum) 249 (bignumtype res) 250 (digittype low high)) 251 (let* ((sum0 (+ (the digittype (bignumref bignum 0)) low)) 252 (sum1 (+ (the fixnum (+ (the digittype (bignumref bignum 1)) 253 high)) 254 (the fixnum (logand 1 (ash sum0 32))))) 255 (carry (logand 1 (ash sum1 32)))) 256 (declare (fixnum sum0 sum1) (digittype carry)) 257 (setf (bignumref res 0) sum0 258 (bignumref res 1) sum1) 259 (if (> lenbignum 2) 260 (finishbignumadd res carry bignum (ash fixnum ( ( target::nbitsinword target::fixnumshift))) 2 lenbignum) 261 (setf (bignumref res 2) 262 (+ (the fixnum carry) 263 (the fixnum (+ (the digittype (%bignumsign bignum)) 264 (the fixnum (ash fixnum ( ( target::nbitsinword target::fixnumshift))))))))) 265 (%normalizebignummacro res)))) 266 267 268 234 269 235 270 … … 237 272 ;;; digit of A, propagating the carry. 238 273 (defun finishbignumadd (result carry a signb start end) 239 (declare (type bignumindex start end)) 240 (do* ((i start (1+ i))) 274 (declare (type bignumindex start end) 275 (digittype signb carry) 276 (optimize (speed 3) (safety 0))) 277 (do* ((i start (1+ i)) 278 (signb (logand allonesdigit signb))) 241 279 ((= i end) 242 280 (setf (bignumref result end) 243 (%addwithcarry (%signdigit a end) signb carry))) 244 (multiplevaluebind (resultdigit carryout) 245 (%addwithcarry (bignumref a i) signb carry) 246 (setf (bignumref result i) resultdigit 247 carry carryout)))) 281 (the fixnum (+ 282 (the fixnum (+ (the fixnum 283 (logand allonesdigit 284 (the fixnum 285 (%signdigit a end)))) 286 signb)) 287 carry)))) 288 (declare (fixnum i) (digittype signb)) 289 (let* ((sum (the fixnum (+ (the fixnum (+ (bignumref a i) 290 signb)) 291 carry)))) 292 (declare (fixnum sum)) 293 (setf (bignumref result i) sum) 294 (setq carry (logand 1 (the fixnum (ash sum 32))))))) 295 296 248 297 249 298 … … 260 309 261 310 (defun bignumsubtractloop (a lena b lenb res) 262 (declare (bignumindex lena lenb )) 311 (declare (bignumindex lena lenb ) 312 (optimize (speed 3) (safety 0))) 263 313 (let* ((lenres (%bignumlength res))) 264 314 (declare (bignumindex lenres)) … … 266 316 (signa (%bignumsign a)) 267 317 (signb (%bignumsign b))) 318 (declare (digittype borrow signa signb)) 268 319 (dotimes (i (the bignumindex lenres)) 269 320 (multiplevaluebind (resultdigit borrowout) … … 943 994 (defun bignumashiftright (bignum x) 944 995 (declare (type bignumtype bignum) 945 (fixnum x)) 996 (fixnum x) 997 (optimize (speed 3) (safety 0))) 946 998 (let ((bignumlen (%bignumlength bignum))) 947 999 (declare (type bignumindex bignumlen)) … … 1048 1100 (reslen1 (1 reslen)) 1049 1101 (res (or res (%allocatebignum reslen)))) 1050 (declare (type bignumindex reslen reslen1)) 1102 (declare (type bignumindex reslen reslen1) 1103 (optimize (speed 3) (safety 0))) 1051 1104 (do ((i 0 i+1) 1052 1105 (i+1 1 (1+ i+1))
Note: See TracChangeset
for help on using the changeset viewer.