comparison lisp/calc/calc-bin.el @ 90979:988f1edc9674

Merge from emacs--devo--0 Patches applied: * emacs--devo--0 (patch 803-805) - Update from CVS Revision: emacs@sv.gnu.org/emacs--unicode--0--patch-227
author Miles Bader <miles@gnu.org>
date Mon, 09 Jul 2007 08:00:55 +0000
parents e6fdae9180d4 95cbb77aca81
children f55f9811f5d7
comparison
equal deleted inserted replaced
90978:f866074aedc4 90979:988f1edc9674
29 29
30 ;; This file is autoloaded from calc-ext.el. 30 ;; This file is autoloaded from calc-ext.el.
31 31
32 (require 'calc-ext) 32 (require 'calc-ext)
33 (require 'calc-macs) 33 (require 'calc-macs)
34
35 ;;; Some useful numbers
36 (defconst math-bignum-logb-digit-size
37 (eval-when-compile (logb math-bignum-digit-size))
38 "The logb of the size of a bignum digit.
39 This is the largest value of B such that 2^B is less than
40 the size of a Calc bignum digit.")
41
42 (defconst math-bignum-digit-power-of-two
43 (eval-when-compile (expt 2 (logb math-bignum-digit-size)))
44 "The largest power of 2 less than the size of a Calc bignum digit.")
34 45
35 ;;; b-prefix binary commands. 46 ;;; b-prefix binary commands.
36 47
37 (defun calc-and (n) 48 (defun calc-and (n)
38 (interactive "P") 49 (interactive "P")
295 (funcall f a w)) 306 (funcall f a w))
296 mod)))) 307 mod))))
297 308
298 (defun math-and-bignum (a b) ; [l l l] 309 (defun math-and-bignum (a b) ; [l l l]
299 (and a b 310 (and a b
300 (let ((qa (math-div-bignum-digit a 512)) 311 (let ((qa (math-div-bignum-digit a math-bignum-digit-power-of-two))
301 (qb (math-div-bignum-digit b 512))) 312 (qb (math-div-bignum-digit b math-bignum-digit-power-of-two)))
302 (math-mul-bignum-digit (math-and-bignum (math-norm-bignum (car qa)) 313 (math-mul-bignum-digit (math-and-bignum (math-norm-bignum (car qa))
303 (math-norm-bignum (car qb))) 314 (math-norm-bignum (car qb)))
304 512 315 math-bignum-digit-power-of-two
305 (logand (cdr qa) (cdr qb)))))) 316 (logand (cdr qa) (cdr qb))))))
306 317
307 (defun calcFunc-or (a b &optional w) ; [I I I] [Public] 318 (defun calcFunc-or (a b &optional w) ; [I I I] [Public]
308 (cond ((Math-messy-integerp w) 319 (cond ((Math-messy-integerp w)
309 (calcFunc-or a b (math-trunc w))) 320 (calcFunc-or a b (math-trunc w)))
322 (math-binary-arg b w))) 333 (math-binary-arg b w)))
323 w)))) 334 w))))
324 335
325 (defun math-or-bignum (a b) ; [l l l] 336 (defun math-or-bignum (a b) ; [l l l]
326 (and (or a b) 337 (and (or a b)
327 (let ((qa (math-div-bignum-digit a 512)) 338 (let ((qa (math-div-bignum-digit a math-bignum-digit-power-of-two))
328 (qb (math-div-bignum-digit b 512))) 339 (qb (math-div-bignum-digit b math-bignum-digit-power-of-two)))
329 (math-mul-bignum-digit (math-or-bignum (math-norm-bignum (car qa)) 340 (math-mul-bignum-digit (math-or-bignum (math-norm-bignum (car qa))
330 (math-norm-bignum (car qb))) 341 (math-norm-bignum (car qb)))
331 512 342 math-bignum-digit-power-of-two
332 (logior (cdr qa) (cdr qb)))))) 343 (logior (cdr qa) (cdr qb))))))
333 344
334 (defun calcFunc-xor (a b &optional w) ; [I I I] [Public] 345 (defun calcFunc-xor (a b &optional w) ; [I I I] [Public]
335 (cond ((Math-messy-integerp w) 346 (cond ((Math-messy-integerp w)
336 (calcFunc-xor a b (math-trunc w))) 347 (calcFunc-xor a b (math-trunc w)))
349 (math-binary-arg b w))) 360 (math-binary-arg b w)))
350 w)))) 361 w))))
351 362
352 (defun math-xor-bignum (a b) ; [l l l] 363 (defun math-xor-bignum (a b) ; [l l l]
353 (and (or a b) 364 (and (or a b)
354 (let ((qa (math-div-bignum-digit a 512)) 365 (let ((qa (math-div-bignum-digit a math-bignum-digit-power-of-two))
355 (qb (math-div-bignum-digit b 512))) 366 (qb (math-div-bignum-digit b math-bignum-digit-power-of-two)))
356 (math-mul-bignum-digit (math-xor-bignum (math-norm-bignum (car qa)) 367 (math-mul-bignum-digit (math-xor-bignum (math-norm-bignum (car qa))
357 (math-norm-bignum (car qb))) 368 (math-norm-bignum (car qb)))
358 512 369 math-bignum-digit-power-of-two
359 (logxor (cdr qa) (cdr qb)))))) 370 (logxor (cdr qa) (cdr qb))))))
360 371
361 (defun calcFunc-diff (a b &optional w) ; [I I I] [Public] 372 (defun calcFunc-diff (a b &optional w) ; [I I I] [Public]
362 (cond ((Math-messy-integerp w) 373 (cond ((Math-messy-integerp w)
363 (calcFunc-diff a b (math-trunc w))) 374 (calcFunc-diff a b (math-trunc w)))
376 (math-binary-arg b w))) 387 (math-binary-arg b w)))
377 w)))) 388 w))))
378 389
379 (defun math-diff-bignum (a b) ; [l l l] 390 (defun math-diff-bignum (a b) ; [l l l]
380 (and a 391 (and a
381 (let ((qa (math-div-bignum-digit a 512)) 392 (let ((qa (math-div-bignum-digit a math-bignum-digit-power-of-two))
382 (qb (math-div-bignum-digit b 512))) 393 (qb (math-div-bignum-digit b math-bignum-digit-power-of-two)))
383 (math-mul-bignum-digit (math-diff-bignum (math-norm-bignum (car qa)) 394 (math-mul-bignum-digit (math-diff-bignum (math-norm-bignum (car qa))
384 (math-norm-bignum (car qb))) 395 (math-norm-bignum (car qb)))
385 512 396 math-bignum-digit-power-of-two
386 (logand (cdr qa) (lognot (cdr qb))))))) 397 (logand (cdr qa) (lognot (cdr qb)))))))
387 398
388 (defun calcFunc-not (a &optional w) ; [I I] [Public] 399 (defun calcFunc-not (a &optional w) ; [I I] [Public]
389 (cond ((Math-messy-integerp w) 400 (cond ((Math-messy-integerp w)
390 (calcFunc-not a (math-trunc w))) 401 (calcFunc-not a (math-trunc w)))
400 (cons 'bigpos 411 (cons 'bigpos
401 (math-not-bignum (math-binary-arg a w) 412 (math-not-bignum (math-binary-arg a w)
402 w)))))) 413 w))))))
403 414
404 (defun math-not-bignum (a w) ; [l l] 415 (defun math-not-bignum (a w) ; [l l]
405 (let ((q (math-div-bignum-digit a 512))) 416 (let ((q (math-div-bignum-digit a math-bignum-digit-power-of-two)))
406 (if (<= w 9) 417 (if (<= w math-bignum-logb-digit-size)
407 (list (logand (lognot (cdr q)) 418 (list (logand (lognot (cdr q))
408 (1- (lsh 1 w)))) 419 (1- (lsh 1 w))))
409 (math-mul-bignum-digit (math-not-bignum (math-norm-bignum (car q)) 420 (math-mul-bignum-digit (math-not-bignum (math-norm-bignum (car q))
410 (- w 9)) 421 (- w math-bignum-logb-digit-size))
411 512 422 math-bignum-digit-power-of-two
412 (logxor (cdr q) 511))))) 423 (logxor (cdr q)
424 (1- math-bignum-digit-power-of-two))))))
413 425
414 (defun calcFunc-lsh (a &optional n w) ; [I I] [Public] 426 (defun calcFunc-lsh (a &optional n w) ; [I I] [Public]
415 (setq a (math-trunc a) 427 (setq a (math-trunc a)
416 n (if n (math-trunc n) 1)) 428 n (if n (math-trunc n) 1))
417 (if (eq (car-safe a) 'mod) 429 (if (eq (car-safe a) 'mod)
508 (if (Math-natnum-lessp a (math-power-of-2 (- -1 w))) 520 (if (Math-natnum-lessp a (math-power-of-2 (- -1 w)))
509 a 521 a
510 (math-sub a (math-power-of-2 (- w))))) 522 (math-sub a (math-power-of-2 (- w)))))
511 ((Math-negp a) 523 ((Math-negp a)
512 (math-normalize (cons 'bigpos (math-binary-arg a w)))) 524 (math-normalize (cons 'bigpos (math-binary-arg a w))))
513 ((and (integerp a) (< a 1000000)) 525 ((and (integerp a) (< a math-small-integer-size))
514 (if (>= w 20) 526 (if (> w (logb math-small-integer-size))
515 a 527 a
516 (logand a (1- (lsh 1 w))))) 528 (logand a (1- (lsh 1 w)))))
517 (t 529 (t
518 (math-normalize 530 (math-normalize
519 (cons 'bigpos 531 (cons 'bigpos
521 w)))))) 533 w))))))
522 534
523 (defalias 'calcFunc-clip 'math-clip) 535 (defalias 'calcFunc-clip 'math-clip)
524 536
525 (defun math-clip-bignum (a w) ; [l l] 537 (defun math-clip-bignum (a w) ; [l l]
526 (let ((q (math-div-bignum-digit a 512))) 538 (let ((q (math-div-bignum-digit a math-bignum-digit-power-of-two)))
527 (if (<= w 9) 539 (if (<= w math-bignum-logb-digit-size)
528 (list (logand (cdr q) 540 (list (logand (cdr q)
529 (1- (lsh 1 w)))) 541 (1- (lsh 1 w))))
530 (math-mul-bignum-digit (math-clip-bignum (math-norm-bignum (car q)) 542 (math-mul-bignum-digit (math-clip-bignum (math-norm-bignum (car q))
531 (- w 9)) 543 (- w math-bignum-logb-digit-size))
532 512 544 math-bignum-digit-power-of-two
533 (cdr q))))) 545 (cdr q)))))
534 546
535 (defvar math-max-digits-cache nil) 547 (defvar math-max-digits-cache nil)
536 (defun math-compute-max-digits (w r) 548 (defun math-compute-max-digits (w r)
537 (let* ((pair (+ (* r 100000) w)) 549 (let* ((pair (+ (* r 100000) w))