calc-bin.el 25.5 KB
Newer Older
1 2
;;; calc-bin.el --- binary functions for Calc

Jay Belanger's avatar
Jay Belanger committed
3
;; Copyright (C) 1990, 1991, 1992, 1993, 2001, 2002, 2003, 2004,
Glenn Morris's avatar
Glenn Morris committed
4
;;   2005, 2006, 2007 Free Software Foundation, Inc.
5 6

;; Author: David Gillespie <daveg@synaptics.com>
Jay Belanger's avatar
Jay Belanger committed
7
;; Maintainer: Jay Belanger <jay.p.belanger@gmail.com>
Eli Zaretskii's avatar
Eli Zaretskii committed
8 9 10

;; This file is part of GNU Emacs.

11 12 13 14 15
;; GNU Emacs is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.

Eli Zaretskii's avatar
Eli Zaretskii committed
16
;; GNU Emacs is distributed in the hope that it will be useful,
17 18 19 20 21 22 23 24
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
Eli Zaretskii's avatar
Eli Zaretskii committed
25

26
;;; Commentary:
Eli Zaretskii's avatar
Eli Zaretskii committed
27

28
;;; Code:
Eli Zaretskii's avatar
Eli Zaretskii committed
29 30 31

;; This file is autoloaded from calc-ext.el.

Jay Belanger's avatar
Jay Belanger committed
32
(require 'calc-ext)
Eli Zaretskii's avatar
Eli Zaretskii committed
33 34
(require 'calc-macs)

35 36
;;; Some useful numbers
(defconst math-bignum-logb-digit-size
37
  (eval-when-compile (logb math-bignum-digit-size))
38 39 40 41 42
  "The logb of the size of a bignum digit.
This is the largest value of B such that 2^B is less than 
the size of a Calc bignum digit.")

(defconst math-bignum-digit-power-of-two
43
  (eval-when-compile (expt 2 (logb math-bignum-digit-size)))
44 45
  "The largest power of 2 less than the size of a Calc bignum digit.")

Eli Zaretskii's avatar
Eli Zaretskii committed
46 47 48 49 50 51 52 53
;;; b-prefix binary commands.

(defun calc-and (n)
  (interactive "P")
  (calc-slow-wrapper
   (calc-enter-result 2 "and"
		      (append '(calcFunc-and)
			      (calc-top-list-n 2)
54
			      (and n (list (prefix-numeric-value n)))))))
Eli Zaretskii's avatar
Eli Zaretskii committed
55 56 57 58 59 60 61

(defun calc-or (n)
  (interactive "P")
  (calc-slow-wrapper
   (calc-enter-result 2 "or"
		      (append '(calcFunc-or)
			      (calc-top-list-n 2)
62
			      (and n (list (prefix-numeric-value n)))))))
Eli Zaretskii's avatar
Eli Zaretskii committed
63 64 65 66 67 68 69

(defun calc-xor (n)
  (interactive "P")
  (calc-slow-wrapper
   (calc-enter-result 2 "xor"
		      (append '(calcFunc-xor)
			      (calc-top-list-n 2)
70
			      (and n (list (prefix-numeric-value n)))))))
Eli Zaretskii's avatar
Eli Zaretskii committed
71 72 73 74 75 76 77

(defun calc-diff (n)
  (interactive "P")
  (calc-slow-wrapper
   (calc-enter-result 2 "diff"
		      (append '(calcFunc-diff)
			      (calc-top-list-n 2)
78
			      (and n (list (prefix-numeric-value n)))))))
Eli Zaretskii's avatar
Eli Zaretskii committed
79 80 81 82 83 84 85

(defun calc-not (n)
  (interactive "P")
  (calc-slow-wrapper
   (calc-enter-result 1 "not"
		      (append '(calcFunc-not)
			      (calc-top-list-n 1)
86
			      (and n (list (prefix-numeric-value n)))))))
Eli Zaretskii's avatar
Eli Zaretskii committed
87 88 89 90 91 92 93 94

(defun calc-lshift-binary (n)
  (interactive "P")
  (calc-slow-wrapper
   (let ((hyp (if (calc-is-hyperbolic) 2 1)))
     (calc-enter-result hyp "lsh"
			(append '(calcFunc-lsh)
				(calc-top-list-n hyp)
95
				(and n (list (prefix-numeric-value n))))))))
Eli Zaretskii's avatar
Eli Zaretskii committed
96 97 98 99 100 101 102 103

(defun calc-rshift-binary (n)
  (interactive "P")
  (calc-slow-wrapper
   (let ((hyp (if (calc-is-hyperbolic) 2 1)))
     (calc-enter-result hyp "rsh"
			(append '(calcFunc-rsh)
				(calc-top-list-n hyp)
104
				(and n (list (prefix-numeric-value n))))))))
Eli Zaretskii's avatar
Eli Zaretskii committed
105 106 107 108 109 110 111 112

(defun calc-lshift-arith (n)
  (interactive "P")
  (calc-slow-wrapper
   (let ((hyp (if (calc-is-hyperbolic) 2 1)))
     (calc-enter-result hyp "ash"
			(append '(calcFunc-ash)
				(calc-top-list-n hyp)
113
				(and n (list (prefix-numeric-value n))))))))
Eli Zaretskii's avatar
Eli Zaretskii committed
114 115 116 117 118 119 120 121

(defun calc-rshift-arith (n)
  (interactive "P")
  (calc-slow-wrapper
   (let ((hyp (if (calc-is-hyperbolic) 2 1)))
     (calc-enter-result hyp "rash"
			(append '(calcFunc-rash)
				(calc-top-list-n hyp)
122
				(and n (list (prefix-numeric-value n))))))))
Eli Zaretskii's avatar
Eli Zaretskii committed
123 124 125 126 127 128 129 130

(defun calc-rotate-binary (n)
  (interactive "P")
  (calc-slow-wrapper
   (let ((hyp (if (calc-is-hyperbolic) 2 1)))
     (calc-enter-result hyp "rot"
			(append '(calcFunc-rot)
				(calc-top-list-n hyp)
131
				(and n (list (prefix-numeric-value n))))))))
Eli Zaretskii's avatar
Eli Zaretskii committed
132 133 134 135 136 137 138

(defun calc-clip (n)
  (interactive "P")
  (calc-slow-wrapper
   (calc-enter-result 1 "clip"
		      (append '(calcFunc-clip)
			      (calc-top-list-n 1)
139
			      (and n (list (prefix-numeric-value n)))))))
Eli Zaretskii's avatar
Eli Zaretskii committed
140 141 142 143 144 145 146 147 148 149

(defun calc-word-size (n)
  (interactive "P")
  (calc-wrapper
   (or n (setq n (read-string (format "Binary word size: (default %d) "
				      calc-word-size))))
   (setq n (if (stringp n)
	       (if (equal n "")
		   calc-word-size
		 (if (string-match "\\`[-+]?[0-9]+\\'" n)
150
		     (string-to-number n)
Eli Zaretskii's avatar
Eli Zaretskii committed
151 152 153 154 155 156 157 158 159
		   (error "Expected an integer")))
	     (prefix-numeric-value n)))
   (or (= n calc-word-size)
       (if (> (math-abs n) 100)
	   (calc-change-mode 'calc-word-size n calc-leading-zeros)
	 (calc-change-mode '(calc-word-size calc-previous-modulo)
			   (list n (math-power-of-2 (math-abs n)))
			   calc-leading-zeros)))
   (if (< n 0)
160 161
       (message "Binary word size is %d bits (2's complement)" (- n))
     (message "Binary word size is %d bits" n))))
Eli Zaretskii's avatar
Eli Zaretskii committed
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177





;;; d-prefix mode commands.

(defun calc-radix (n)
  (interactive "NDisplay radix (2-36): ")
  (calc-wrapper
   (if (and (>= n 2) (<= n 36))
       (progn
	 (calc-change-mode 'calc-number-radix n t)
	 ;; also change global value so minibuffer sees it
	 (setq-default calc-number-radix calc-number-radix))
     (setq n calc-number-radix))
178
   (message "Number radix is %d" n)))
Eli Zaretskii's avatar
Eli Zaretskii committed
179 180 181

(defun calc-decimal-radix ()
  (interactive)
182
  (calc-radix 10))
Eli Zaretskii's avatar
Eli Zaretskii committed
183 184 185

(defun calc-binary-radix ()
  (interactive)
186
  (calc-radix 2))
Eli Zaretskii's avatar
Eli Zaretskii committed
187 188 189

(defun calc-octal-radix ()
  (interactive)
190
  (calc-radix 8))
Eli Zaretskii's avatar
Eli Zaretskii committed
191 192 193

(defun calc-hex-radix ()
  (interactive)
194
  (calc-radix 16))
Eli Zaretskii's avatar
Eli Zaretskii committed
195 196 197 198 199

(defun calc-leading-zeros (n)
  (interactive "P")
  (calc-wrapper
   (if (calc-change-mode 'calc-leading-zeros n t t)
200
       (message "Zero-padding integers to %d digits (assuming radix %d)"
Eli Zaretskii's avatar
Eli Zaretskii committed
201 202 203 204
		(let* ((calc-internal-prec 6))
		  (math-compute-max-digits (math-abs calc-word-size)
					   calc-number-radix))
		calc-number-radix)
205
     (message "Omitting leading zeros on integers"))))
Eli Zaretskii's avatar
Eli Zaretskii committed
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226


(defvar math-power-of-2-cache (list 1 2 4 8 16 32 64 128 256 512 1024))
(defvar math-big-power-of-2-cache nil)
(defun math-power-of-2 (n)    ;  [I I] [Public]
  (if (and (natnump n) (<= n 100))
      (or (nth n math-power-of-2-cache)
	  (let* ((i (length math-power-of-2-cache))
		 (val (nth (1- i) math-power-of-2-cache)))
	    (while (<= i n)
	      (setq val (math-mul val 2)
		    math-power-of-2-cache (nconc math-power-of-2-cache
						 (list val))
		    i (1+ i)))
	    val))
    (let ((found (assq n math-big-power-of-2-cache)))
      (if found
	  (cdr found)
	(let ((po2 (math-ipow 2 n)))
	  (setq math-big-power-of-2-cache
		(cons (cons n po2) math-big-power-of-2-cache))
227
	  po2)))))
Eli Zaretskii's avatar
Eli Zaretskii committed
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246

(defun math-integer-log2 (n)    ; [I I] [Public]
  (let ((i 0)
	(p math-power-of-2-cache)
	val)
    (while (and p (Math-natnum-lessp (setq val (car p)) n))
      (setq p (cdr p)
	    i (1+ i)))
    (if p
	(and (equal val n)
	     i)
      (while (Math-natnum-lessp
	      (prog1
		  (setq val (math-mul val 2))
		(setq math-power-of-2-cache (nconc math-power-of-2-cache
						   (list val))))
	      n)
	(setq i (1+ i)))
      (and (equal val n)
247
	   i))))
Eli Zaretskii's avatar
Eli Zaretskii committed
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269




;;; Bitwise operations.

(defun calcFunc-and (a b &optional w)   ; [I I I] [Public]
  (cond ((Math-messy-integerp w)
	 (calcFunc-and a b (math-trunc w)))
	((and w (not (integerp w)))
	 (math-reject-arg w 'fixnump))
	((and (integerp a) (integerp b))
	 (math-clip (logand a b) w))
	((or (eq (car-safe a) 'mod) (eq (car-safe b) 'mod))
	 (math-binary-modulo-args 'calcFunc-and a b w))
	((not (Math-num-integerp a))
	 (math-reject-arg a 'integerp))
	((not (Math-num-integerp b))
	 (math-reject-arg b 'integerp))
	(t (math-clip (cons 'bigpos
			    (math-and-bignum (math-binary-arg a w)
					     (math-binary-arg b w)))
270
		      w))))
Eli Zaretskii's avatar
Eli Zaretskii committed
271 272 273 274 275 276 277

(defun math-binary-arg (a w)
  (if (not (Math-integerp a))
      (setq a (math-trunc a)))
  (if (Math-integer-negp a)
      (math-not-bignum (cdr (math-bignum-test (math-sub -1 a)))
		       (math-abs (if w (math-trunc w) calc-word-size)))
278
    (cdr (Math-bignum-test a))))
Eli Zaretskii's avatar
Eli Zaretskii committed
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306

(defun math-binary-modulo-args (f a b w)
  (let (mod)
    (if (eq (car-safe a) 'mod)
	(progn
	  (setq mod (nth 2 a)
		a (nth 1 a))
	  (if (eq (car-safe b) 'mod)
	      (if (equal mod (nth 2 b))
		  (setq b (nth 1 b))
		(math-reject-arg b "*Inconsistent modulos"))))
      (setq mod (nth 2 b)
	    b (nth 1 b)))
    (if (Math-messy-integerp mod)
	(setq mod (math-trunc mod))
      (or (Math-integerp mod)
	  (math-reject-arg mod 'integerp)))
    (let ((bits (math-integer-log2 mod)))
      (if bits
	  (if w
	      (if (/= w bits)
		  (calc-record-why
		   "*Warning: Modulo inconsistent with word size"))
	    (setq w bits))
	(calc-record-why "*Warning: Modulo is not a power of 2"))
      (math-make-mod (if b
			 (funcall f a b w)
		       (funcall f a w))
307
		     mod))))
Eli Zaretskii's avatar
Eli Zaretskii committed
308 309 310

(defun math-and-bignum (a b)   ; [l l l]
  (and a b
311 312
       (let ((qa (math-div-bignum-digit a math-bignum-digit-power-of-two))
	     (qb (math-div-bignum-digit b math-bignum-digit-power-of-two)))
Eli Zaretskii's avatar
Eli Zaretskii committed
313 314
	 (math-mul-bignum-digit (math-and-bignum (math-norm-bignum (car qa))
						  (math-norm-bignum (car qb)))
315
				 math-bignum-digit-power-of-two
316
				 (logand (cdr qa) (cdr qb))))))
Eli Zaretskii's avatar
Eli Zaretskii committed
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333

(defun calcFunc-or (a b &optional w)   ; [I I I] [Public]
  (cond ((Math-messy-integerp w)
	 (calcFunc-or a b (math-trunc w)))
	((and w (not (integerp w)))
	 (math-reject-arg w 'fixnump))
	((and (integerp a) (integerp b))
	 (math-clip (logior a b) w))
	((or (eq (car-safe a) 'mod) (eq (car-safe b) 'mod))
	 (math-binary-modulo-args 'calcFunc-or a b w))
	((not (Math-num-integerp a))
	 (math-reject-arg a 'integerp))
	((not (Math-num-integerp b))
	 (math-reject-arg b 'integerp))
	(t (math-clip (cons 'bigpos
			    (math-or-bignum (math-binary-arg a w)
					    (math-binary-arg b w)))
334
		      w))))
Eli Zaretskii's avatar
Eli Zaretskii committed
335 336 337

(defun math-or-bignum (a b)   ; [l l l]
  (and (or a b)
338 339
       (let ((qa (math-div-bignum-digit a math-bignum-digit-power-of-two))
	     (qb (math-div-bignum-digit b math-bignum-digit-power-of-two)))
Eli Zaretskii's avatar
Eli Zaretskii committed
340 341
	 (math-mul-bignum-digit (math-or-bignum (math-norm-bignum (car qa))
						 (math-norm-bignum (car qb)))
342
				 math-bignum-digit-power-of-two
343
				 (logior (cdr qa) (cdr qb))))))
Eli Zaretskii's avatar
Eli Zaretskii committed
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360

(defun calcFunc-xor (a b &optional w)   ; [I I I] [Public]
  (cond ((Math-messy-integerp w)
	 (calcFunc-xor a b (math-trunc w)))
	((and w (not (integerp w)))
	 (math-reject-arg w 'fixnump))
	((and (integerp a) (integerp b))
	 (math-clip (logxor a b) w))
	((or (eq (car-safe a) 'mod) (eq (car-safe b) 'mod))
	 (math-binary-modulo-args 'calcFunc-xor a b w))
	((not (Math-num-integerp a))
	 (math-reject-arg a 'integerp))
	((not (Math-num-integerp b))
	 (math-reject-arg b 'integerp))
	(t (math-clip (cons 'bigpos
			    (math-xor-bignum (math-binary-arg a w)
					     (math-binary-arg b w)))
361
		      w))))
Eli Zaretskii's avatar
Eli Zaretskii committed
362 363 364

(defun math-xor-bignum (a b)   ; [l l l]
  (and (or a b)
365 366
       (let ((qa (math-div-bignum-digit a math-bignum-digit-power-of-two))
	     (qb (math-div-bignum-digit b math-bignum-digit-power-of-two)))
Eli Zaretskii's avatar
Eli Zaretskii committed
367 368
	 (math-mul-bignum-digit (math-xor-bignum (math-norm-bignum (car qa))
						  (math-norm-bignum (car qb)))
369
				 math-bignum-digit-power-of-two
370
				 (logxor (cdr qa) (cdr qb))))))
Eli Zaretskii's avatar
Eli Zaretskii committed
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387

(defun calcFunc-diff (a b &optional w)   ; [I I I] [Public]
  (cond ((Math-messy-integerp w)
	 (calcFunc-diff a b (math-trunc w)))
	((and w (not (integerp w)))
	 (math-reject-arg w 'fixnump))
	((and (integerp a) (integerp b))
	 (math-clip (logand a (lognot b)) w))
	((or (eq (car-safe a) 'mod) (eq (car-safe b) 'mod))
	 (math-binary-modulo-args 'calcFunc-diff a b w))
	((not (Math-num-integerp a))
	 (math-reject-arg a 'integerp))
	((not (Math-num-integerp b))
	 (math-reject-arg b 'integerp))
	(t (math-clip (cons 'bigpos
			    (math-diff-bignum (math-binary-arg a w)
					      (math-binary-arg b w)))
388
		      w))))
Eli Zaretskii's avatar
Eli Zaretskii committed
389 390 391

(defun math-diff-bignum (a b)   ; [l l l]
  (and a
392 393
       (let ((qa (math-div-bignum-digit a math-bignum-digit-power-of-two))
	     (qb (math-div-bignum-digit b math-bignum-digit-power-of-two)))
Eli Zaretskii's avatar
Eli Zaretskii committed
394 395
	 (math-mul-bignum-digit (math-diff-bignum (math-norm-bignum (car qa))
						   (math-norm-bignum (car qb)))
396
				 math-bignum-digit-power-of-two
397
				 (logand (cdr qa) (lognot (cdr qb)))))))
Eli Zaretskii's avatar
Eli Zaretskii committed
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412

(defun calcFunc-not (a &optional w)   ; [I I] [Public]
  (cond ((Math-messy-integerp w)
	 (calcFunc-not a (math-trunc w)))
	((eq (car-safe a) 'mod)
	 (math-binary-modulo-args 'calcFunc-not a nil w))
	((and w (not (integerp w)))
	 (math-reject-arg w 'fixnump))
	((not (Math-num-integerp a))
	 (math-reject-arg a 'integerp))
	((< (or w (setq w calc-word-size)) 0)
	 (math-clip (calcFunc-not a (- w)) w))
	(t (math-normalize
	    (cons 'bigpos
		  (math-not-bignum (math-binary-arg a w)
413
				   w))))))
Eli Zaretskii's avatar
Eli Zaretskii committed
414 415

(defun math-not-bignum (a w)   ; [l l]
416 417
  (let ((q (math-div-bignum-digit a math-bignum-digit-power-of-two)))
    (if (<= w math-bignum-logb-digit-size)
Eli Zaretskii's avatar
Eli Zaretskii committed
418 419 420
	(list (logand (lognot (cdr q))
		      (1- (lsh 1 w))))
      (math-mul-bignum-digit (math-not-bignum (math-norm-bignum (car q))
421 422 423 424
					       (- w math-bignum-logb-digit-size))
			      math-bignum-digit-power-of-two
			      (logxor (cdr q) 
                                      (1- math-bignum-digit-power-of-two))))))
Eli Zaretskii's avatar
Eli Zaretskii committed
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447

(defun calcFunc-lsh (a &optional n w)   ; [I I] [Public]
  (setq a (math-trunc a)
	n (if n (math-trunc n) 1))
  (if (eq (car-safe a) 'mod)
      (math-binary-modulo-args 'calcFunc-lsh a n w)
    (setq w (if w (math-trunc w) calc-word-size))
    (or (integerp w)
	(math-reject-arg w 'fixnump))
    (or (Math-integerp a)
	(math-reject-arg a 'integerp))
    (or (Math-integerp n)
	(math-reject-arg n 'integerp))
    (if (< w 0)
	(math-clip (calcFunc-lsh a n (- w)) w)
      (if (Math-integer-negp a)
	  (setq a (math-clip a w)))
      (cond ((or (Math-lessp n (- w))
		 (Math-lessp w n))
	     0)
	    ((< n 0)
	     (math-quotient (math-clip a w) (math-power-of-2 (- n))))
	    (t
448
	     (math-clip (math-mul a (math-power-of-2 n)) w))))))
Eli Zaretskii's avatar
Eli Zaretskii committed
449 450

(defun calcFunc-rsh (a &optional n w)   ; [I I] [Public]
451
  (calcFunc-lsh a (math-neg (or n 1)) w))
Eli Zaretskii's avatar
Eli Zaretskii committed
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480

(defun calcFunc-ash (a &optional n w)   ; [I I] [Public]
  (if (or (null n)
	  (not (Math-negp n)))
      (calcFunc-lsh a n w)
    (setq a (math-trunc a)
	  n (if n (math-trunc n) 1))
    (if (eq (car-safe a) 'mod)
	(math-binary-modulo-args 'calcFunc-ash a n w)
      (setq w (if w (math-trunc w) calc-word-size))
      (or (integerp w)
	  (math-reject-arg w 'fixnump))
      (or (Math-integerp a)
	  (math-reject-arg a 'integerp))
      (or (Math-integerp n)
	  (math-reject-arg n 'integerp))
      (if (< w 0)
	  (math-clip (calcFunc-ash a n (- w)) w)
	(if (Math-integer-negp a)
	    (setq a (math-clip a w)))
	(let ((two-to-sizem1 (math-power-of-2 (1- w)))
	      (sh (calcFunc-lsh a n w)))
	  (cond ((Math-natnum-lessp a two-to-sizem1)
		 sh)
		((Math-lessp n (- 1 w))
		 (math-add (math-mul two-to-sizem1 2) -1))
		(t (let ((two-to-n (math-power-of-2 (- n))))
		     (math-add (calcFunc-lsh (math-add two-to-n -1)
					     (+ w n) w)
481
			       sh)))))))))
Eli Zaretskii's avatar
Eli Zaretskii committed
482 483

(defun calcFunc-rash (a &optional n w)   ; [I I] [Public]
484
  (calcFunc-ash a (math-neg (or n 1)) w))
Eli Zaretskii's avatar
Eli Zaretskii committed
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506

(defun calcFunc-rot (a &optional n w)   ; [I I] [Public]
  (setq a (math-trunc a)
	n (if n (math-trunc n) 1))
  (if (eq (car-safe a) 'mod)
      (math-binary-modulo-args 'calcFunc-rot a n w)
    (setq w (if w (math-trunc w) calc-word-size))
    (or (integerp w)
	(math-reject-arg w 'fixnump))
    (or (Math-integerp a)
	(math-reject-arg a 'integerp))
    (or (Math-integerp n)
	(math-reject-arg n 'integerp))
    (if (< w 0)
	(math-clip (calcFunc-rot a n (- w)) w)
      (if (Math-integer-negp a)
	  (setq a (math-clip a w)))
      (cond ((or (Math-integer-negp n)
		 (not (Math-natnum-lessp n w)))
	     (calcFunc-rot a (math-mod n w) w))
	    (t
	     (math-add (calcFunc-lsh a (- n w) w)
507
		       (calcFunc-lsh a n w)))))))
Eli Zaretskii's avatar
Eli Zaretskii committed
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524

(defun math-clip (a &optional w)   ; [I I] [Public]
  (cond ((Math-messy-integerp w)
	 (math-clip a (math-trunc w)))
	((eq (car-safe a) 'mod)
	 (math-binary-modulo-args 'math-clip a nil w))
	((and w (not (integerp w)))
	 (math-reject-arg w 'fixnump))
	((not (Math-num-integerp a))
	 (math-reject-arg a 'integerp))
	((< (or w (setq w calc-word-size)) 0)
	 (setq a (math-clip a (- w)))
	 (if (Math-natnum-lessp a (math-power-of-2 (- -1 w)))
	     a
	   (math-sub a (math-power-of-2 (- w)))))
	((Math-negp a)
	 (math-normalize (cons 'bigpos (math-binary-arg a w))))
525 526
	((and (integerp a) (< a math-small-integer-size))
	 (if (> w (logb math-small-integer-size))
Eli Zaretskii's avatar
Eli Zaretskii committed
527 528 529 530 531 532
	     a
	   (logand a (1- (lsh 1 w)))))
	(t
	 (math-normalize
	  (cons 'bigpos
		(math-clip-bignum (cdr (math-bignum-test (math-trunc a)))
533 534 535
				  w))))))

(defalias 'calcFunc-clip 'math-clip)
Eli Zaretskii's avatar
Eli Zaretskii committed
536 537

(defun math-clip-bignum (a w)   ; [l l]
538 539
  (let ((q (math-div-bignum-digit a math-bignum-digit-power-of-two)))
    (if (<= w math-bignum-logb-digit-size)
Eli Zaretskii's avatar
Eli Zaretskii committed
540 541 542
	(list (logand (cdr q)
		      (1- (lsh 1 w))))
      (math-mul-bignum-digit (math-clip-bignum (math-norm-bignum (car q))
543 544
						(- w math-bignum-logb-digit-size))
			      math-bignum-digit-power-of-two
545
			      (cdr q)))))
Eli Zaretskii's avatar
Eli Zaretskii committed
546 547 548 549 550 551 552 553 554 555 556

(defvar math-max-digits-cache nil)
(defun math-compute-max-digits (w r)
  (let* ((pair (+ (* r 100000) w))
	 (res (assq pair math-max-digits-cache)))
    (if res
	(cdr res)
      (let* ((calc-command-flags nil)
	     (digs (math-ceiling (math-div w (math-real-log2 r)))))
	(setq math-max-digits-cache (cons (cons pair digs)
					  math-max-digits-cache))
557
	digs))))
Eli Zaretskii's avatar
Eli Zaretskii committed
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572

(defvar math-log2-cache (list '(2 . 1)
			      '(4 . 2)
			      '(8 . 3)
			      '(10 . (float 332193 -5))
			      '(16 . 4)
			      '(32 . 5)))
(defun math-real-log2 (x)   ;;; calc-internal-prec must be 6
  (let ((res (assq x math-log2-cache)))
    (if res
	(cdr res)
      (let* ((calc-symbolic-mode nil)
	     (calc-display-working-message nil)
	     (log (calcFunc-log x 2)))
	(setq math-log2-cache (cons (cons x log) math-log2-cache))
573
	log))))
Eli Zaretskii's avatar
Eli Zaretskii committed
574 575 576 577 578 579

(defconst math-radix-digits ["0" "1" "2" "3" "4" "5" "6" "7" "8" "9"
			     "A" "B" "C" "D" "E" "F" "G" "H" "I" "J"
			     "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T"
			     "U" "V" "W" "X" "Y" "Z"])

580 581 582
(defsubst math-format-radix-digit (a)   ; [X D]
  (aref math-radix-digits a))

Eli Zaretskii's avatar
Eli Zaretskii committed
583 584 585 586 587 588 589 590 591
(defun math-format-radix (a)   ; [X S]
  (if (< a calc-number-radix)
      (if (< a 0)
	  (concat "-" (math-format-radix (- a)))
	(math-format-radix-digit a))
    (let ((s ""))
      (while (> a 0)
	(setq s (concat (math-format-radix-digit (% a calc-number-radix)) s)
	      a (/ a calc-number-radix)))
592
      s)))
Eli Zaretskii's avatar
Eli Zaretskii committed
593 594 595 596 597 598 599 600 601 602 603 604

(defconst math-binary-digits ["000" "001" "010" "011"
			      "100" "101" "110" "111"])
(defun math-format-binary (a)   ; [X S]
  (if (< a 8)
      (if (< a 0)
	  (concat "-" (math-format-binary (- a)))
	(math-format-radix a))
    (let ((s ""))
      (while (> a 7)
	(setq s (concat (aref math-binary-digits (% a 8)) s)
	      a (/ a 8)))
605
      (concat (math-format-radix a) s))))
Eli Zaretskii's avatar
Eli Zaretskii committed
606 607 608 609 610 611 612 613 614

(defun math-format-bignum-radix (a)   ; [X L]
  (cond ((null a) "0")
	((and (null (cdr a))
	      (< (car a) calc-number-radix))
	 (math-format-radix-digit (car a)))
	(t
	 (let ((q (math-div-bignum-digit a calc-number-radix)))
	   (concat (math-format-bignum-radix (math-norm-bignum (car q)))
615
		   (math-format-radix-digit (cdr q)))))))
Eli Zaretskii's avatar
Eli Zaretskii committed
616 617 618 619 620 621 622 623 624 625

(defun math-format-bignum-binary (a)   ; [X L]
  (cond ((null a) "0")
	((null (cdr a))
	 (math-format-binary (car a)))
	(t
	 (let ((q (math-div-bignum-digit a 512)))
	   (concat (math-format-bignum-binary (math-norm-bignum (car q)))
		   (aref math-binary-digits (/ (cdr q) 64))
		   (aref math-binary-digits (% (/ (cdr q) 8) 8))
626
		   (aref math-binary-digits (% (cdr q) 8)))))))
Eli Zaretskii's avatar
Eli Zaretskii committed
627 628 629 630 631 632 633 634 635 636

(defun math-format-bignum-octal (a)   ; [X L]
  (cond ((null a) "0")
	((null (cdr a))
	 (math-format-radix (car a)))
	(t
	 (let ((q (math-div-bignum-digit a 512)))
	   (concat (math-format-bignum-octal (math-norm-bignum (car q)))
		   (math-format-radix-digit (/ (cdr q) 64))
		   (math-format-radix-digit (% (/ (cdr q) 8) 8))
637
		   (math-format-radix-digit (% (cdr q) 8)))))))
Eli Zaretskii's avatar
Eli Zaretskii committed
638 639 640 641 642 643 644 645 646

(defun math-format-bignum-hex (a)   ; [X L]
  (cond ((null a) "0")
	((null (cdr a))
	 (math-format-radix (car a)))
	(t
	 (let ((q (math-div-bignum-digit a 256)))
	   (concat (math-format-bignum-hex (math-norm-bignum (car q)))
		   (math-format-radix-digit (/ (cdr q) 16))
647
		   (math-format-radix-digit (% (cdr q) 16)))))))
Eli Zaretskii's avatar
Eli Zaretskii committed
648 649 650 651 652 653 654 655 656 657 658 659 660 661

;;; Decompose into integer and fractional parts, without depending
;;; on calc-internal-prec.
(defun math-float-parts (a need-frac)    ; returns ( int frac fracdigs )
  (if (>= (nth 2 a) 0)
      (list (math-scale-rounding (nth 1 a) (nth 2 a)) '(float 0 0) 0)
    (let* ((d (math-numdigs (nth 1 a)))
	   (n (- (nth 2 a))))
      (if need-frac
	  (if (>= n d)
	      (list 0 a n)
	    (let ((qr (math-idivmod (nth 1 a) (math-scale-int 1 n))))
	      (list (car qr) (math-make-float (cdr qr) (- n)) n)))
	(list (math-scale-rounding (nth 1 a) (nth 2 a))
662
	      '(float 0 0) 0)))))
Eli Zaretskii's avatar
Eli Zaretskii committed
663 664 665 666 667

(defun math-format-radix-float (a prec)
  (let ((fmt (car calc-float-format))
	(figs (nth 1 calc-float-format))
	(point calc-point-char)
668 669
	(str nil)
        pos)
Eli Zaretskii's avatar
Eli Zaretskii committed
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689
    (if (eq fmt 'fix)
	(let* ((afigs (math-abs figs))
	       (fp (math-float-parts a (> afigs 0)))
	       (calc-internal-prec (+ 3 (max (nth 2 fp)
					     (math-convert-radix-digits
					      afigs t))))
	       (int (car fp))
	       (frac (math-round (math-mul (math-normalize (nth 1 fp))
					   (math-radix-float-power afigs)))))
	  (if (not (and (math-zerop frac) (math-zerop int) (< figs 0)))
	      (let ((math-radix-explicit-format nil))
		(let ((calc-group-digits nil))
		  (setq str (if (> afigs 0) (math-format-number frac) ""))
		  (if (< (length str) afigs)
		      (setq str (concat (make-string (- afigs (length str)) ?0)
					str))
		    (if (> (length str) afigs)
			(setq str (substring str 1)
			      int (math-add int 1))))
		  (setq str (concat (math-format-number int) point str)))
690 691
		(when calc-group-digits
		  (setq str (math-group-float str))))
Eli Zaretskii's avatar
Eli Zaretskii committed
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758
	    (setq figs 0))))
    (or str
	(let* ((prec calc-internal-prec)
	       (afigs (if (> figs 0)
			  figs
			(max 1 (+ figs
				  (1- (math-convert-radix-digits
				       (max prec
					    (math-numdigs (nth 1 a)))))))))
	       (calc-internal-prec (+ 3 (math-convert-radix-digits afigs t)))
	       (explo -1) (vlo (math-radix-float-power explo))
	       (exphi 1) (vhi (math-radix-float-power exphi))
	       expmid vmid eadj)
	  (setq a (math-normalize a))
	  (if (Math-zerop a)
	      (setq explo 0)
	    (if (math-lessp-float '(float 1 0) a)
		(while (not (math-lessp-float a vhi))
		  (setq explo exphi vlo vhi
			exphi (math-mul exphi 2)
			vhi (math-radix-float-power exphi)))
	      (while (math-lessp-float a vlo)
		(setq exphi explo vhi vlo
		      explo (math-mul explo 2)
		      vlo (math-radix-float-power explo))))
	    (while (not (eq (math-sub exphi explo) 1))
	      (setq expmid (math-div2 (math-add explo exphi))
		    vmid (math-radix-float-power expmid))
	      (if (math-lessp-float a vmid)
		  (setq exphi expmid vhi vmid)
		(setq explo expmid vlo vmid)))
	    (setq a (math-div-float a vlo)))
	  (let* ((sc (math-round (math-mul a (math-radix-float-power
					      (1- afigs)))))
		 (math-radix-explicit-format nil))
	    (let ((calc-group-digits nil))
	      (setq str (math-format-number sc))))
	  (if (> (length str) afigs)
	      (setq str (substring str 0 -1)
		    explo (1+ explo)))
	  (if (and (eq fmt 'float)
		   (math-lessp explo (+ (if (= figs 0)
					    (1- (math-convert-radix-digits
						 prec))
					  afigs)
					calc-display-sci-high 1))
		   (math-lessp calc-display-sci-low explo))
	      (let ((dpos (1+ explo)))
		(cond ((<= dpos 0)
		       (setq str (concat "0" point (make-string (- dpos) ?0)
					 str)))
		      ((> dpos (length str))
		       (setq str (concat str (make-string (- dpos (length str))
							  ?0) point)))
		      (t
		       (setq str (concat (substring str 0 dpos) point
					 (substring str dpos)))))
		(setq explo nil))
	    (setq eadj (if (eq fmt 'eng)
			   (min (math-mod explo 3) (length str))
			 0)
		  str (concat (substring str 0 (1+ eadj)) point
			      (substring str (1+ eadj)))))
	  (setq pos (length str))
	  (while (eq (aref str (1- pos)) ?0) (setq pos (1- pos)))
	  (and explo (eq (aref str (1- pos)) ?.) (setq pos (1- pos)))
	  (setq str (substring str 0 pos))
759 760
	  (when calc-group-digits
	    (setq str (math-group-float str)))
Eli Zaretskii's avatar
Eli Zaretskii committed
761 762 763
	  (if explo
	      (let ((estr (let ((calc-number-radix 10)
				(calc-group-digits nil))
764 765
                            (math-format-number
                             (math-sub explo eadj)))))
Eli Zaretskii's avatar
Eli Zaretskii committed
766 767 768 769
		(setq str (if (or (memq calc-language '(math maple))
				  (> calc-number-radix 14))
			      (format "%s*%d.^%s" str calc-number-radix estr)
			    (format "%se%s" str estr)))))))
770
    str))
Eli Zaretskii's avatar
Eli Zaretskii committed
771

772 773
(defvar math-radix-digits-cache nil)

Eli Zaretskii's avatar
Eli Zaretskii committed
774 775 776 777 778 779 780 781 782 783
(defun math-convert-radix-digits (n &optional to-dec)
  (let ((key (cons n (cons to-dec calc-number-radix))))
    (or (cdr (assoc key math-radix-digits-cache))
	(let* ((calc-internal-prec 6)
	       (log (math-div (math-real-log2 calc-number-radix)
			      '(float 332193 -5))))
	  (cdr (car (setq math-radix-digits-cache
			  (cons (cons key (math-ceiling (if to-dec
							    (math-mul n log)
							  (math-div n log))))
784 785
				math-radix-digits-cache))))))))

786
(defvar math-radix-float-cache-tag nil)
787
(defvar math-radix-float-cache)
Eli Zaretskii's avatar
Eli Zaretskii committed
788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814

(defun math-radix-float-power (n)
  (if (eq n 0)
      '(float 1 0)
    (or (and (eq calc-number-radix (car math-radix-float-cache-tag))
	     (<= calc-internal-prec (cdr math-radix-float-cache-tag)))
	(setq math-radix-float-cache-tag (cons calc-number-radix
					       calc-internal-prec)
	      math-radix-float-cache nil))
    (math-normalize
     (or (cdr (assoc n math-radix-float-cache))
	 (cdr (car (setq math-radix-float-cache
			 (cons (cons
				n
				(let ((calc-internal-prec
				       (cdr math-radix-float-cache-tag)))
				  (if (math-negp n)
				      (math-div-float '(float 1 0)
						      (math-radix-float-power
						       (math-neg n)))
				    (math-mul-float (math-sqr-float
						     (math-radix-float-power
						      (math-div2 n)))
						    (if (math-evenp n)
							'(float 1 0)
						      (math-float
						       calc-number-radix))))))
815 816
			       math-radix-float-cache))))))))

Jay Belanger's avatar
Jay Belanger committed
817
(provide 'calc-bin)
Eli Zaretskii's avatar
Eli Zaretskii committed
818

Miles Bader's avatar
Miles Bader committed
819
;;; arch-tag: f6dba7bc-53b2-41ae-919c-c266ab0ca8b3
820
;;; calc-bin.el ends here