calc-bin.el 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  1. ;;; calc-bin.el --- binary functions for Calc
  2. ;; Copyright (C) 1990-1993, 2001-2015 Free Software Foundation, Inc.
  3. ;; Author: David Gillespie <daveg@synaptics.com>
  4. ;; This file is part of GNU Emacs.
  5. ;; GNU Emacs is free software: you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; GNU Emacs is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;;; Code:
  17. ;; This file is autoloaded from calc-ext.el.
  18. (require 'calc-ext)
  19. (require 'calc-macs)
  20. ;;; Some useful numbers
  21. (defconst math-bignum-logb-digit-size
  22. (logb math-bignum-digit-size)
  23. "The logb of the size of a bignum digit.
  24. This is the largest value of B such that 2^B is less than
  25. the size of a Calc bignum digit.")
  26. (defconst math-bignum-digit-power-of-two
  27. (expt 2 (logb math-bignum-digit-size))
  28. "The largest power of 2 less than the size of a Calc bignum digit.")
  29. ;;; b-prefix binary commands.
  30. (defun calc-and (n)
  31. (interactive "P")
  32. (calc-slow-wrapper
  33. (calc-enter-result 2 "and"
  34. (append '(calcFunc-and)
  35. (calc-top-list-n 2)
  36. (and n (list (prefix-numeric-value n)))))))
  37. (defun calc-or (n)
  38. (interactive "P")
  39. (calc-slow-wrapper
  40. (calc-enter-result 2 "or"
  41. (append '(calcFunc-or)
  42. (calc-top-list-n 2)
  43. (and n (list (prefix-numeric-value n)))))))
  44. (defun calc-xor (n)
  45. (interactive "P")
  46. (calc-slow-wrapper
  47. (calc-enter-result 2 "xor"
  48. (append '(calcFunc-xor)
  49. (calc-top-list-n 2)
  50. (and n (list (prefix-numeric-value n)))))))
  51. (defun calc-diff (n)
  52. (interactive "P")
  53. (calc-slow-wrapper
  54. (calc-enter-result 2 "diff"
  55. (append '(calcFunc-diff)
  56. (calc-top-list-n 2)
  57. (and n (list (prefix-numeric-value n)))))))
  58. (defun calc-not (n)
  59. (interactive "P")
  60. (calc-slow-wrapper
  61. (calc-enter-result 1 "not"
  62. (append '(calcFunc-not)
  63. (calc-top-list-n 1)
  64. (and n (list (prefix-numeric-value n)))))))
  65. (defun calc-lshift-binary (n)
  66. (interactive "P")
  67. (calc-slow-wrapper
  68. (let ((hyp (if (calc-is-hyperbolic) 2 1)))
  69. (calc-enter-result hyp "lsh"
  70. (append '(calcFunc-lsh)
  71. (calc-top-list-n hyp)
  72. (and n (list (prefix-numeric-value n))))))))
  73. (defun calc-rshift-binary (n)
  74. (interactive "P")
  75. (calc-slow-wrapper
  76. (let ((hyp (if (calc-is-hyperbolic) 2 1)))
  77. (calc-enter-result hyp "rsh"
  78. (append '(calcFunc-rsh)
  79. (calc-top-list-n hyp)
  80. (and n (list (prefix-numeric-value n))))))))
  81. (defun calc-lshift-arith (n)
  82. (interactive "P")
  83. (calc-slow-wrapper
  84. (let ((hyp (if (calc-is-hyperbolic) 2 1)))
  85. (calc-enter-result hyp "ash"
  86. (append '(calcFunc-ash)
  87. (calc-top-list-n hyp)
  88. (and n (list (prefix-numeric-value n))))))))
  89. (defun calc-rshift-arith (n)
  90. (interactive "P")
  91. (calc-slow-wrapper
  92. (let ((hyp (if (calc-is-hyperbolic) 2 1)))
  93. (calc-enter-result hyp "rash"
  94. (append '(calcFunc-rash)
  95. (calc-top-list-n hyp)
  96. (and n (list (prefix-numeric-value n))))))))
  97. (defun calc-rotate-binary (n)
  98. (interactive "P")
  99. (calc-slow-wrapper
  100. (let ((hyp (if (calc-is-hyperbolic) 2 1)))
  101. (calc-enter-result hyp "rot"
  102. (append '(calcFunc-rot)
  103. (calc-top-list-n hyp)
  104. (and n (list (prefix-numeric-value n))))))))
  105. (defun calc-clip (n)
  106. (interactive "P")
  107. (calc-slow-wrapper
  108. (calc-enter-result 1 "clip"
  109. (append '(calcFunc-clip)
  110. (calc-top-list-n 1)
  111. (and n (list (prefix-numeric-value n)))))))
  112. (defun calc-word-size (n)
  113. (interactive "P")
  114. (calc-wrapper
  115. (or n (setq n (read-string (format "Binary word size: (default %d) "
  116. calc-word-size))))
  117. (setq n (if (stringp n)
  118. (if (equal n "")
  119. calc-word-size
  120. (if (string-match "\\`[-+]?[0-9]+\\'" n)
  121. (string-to-number n)
  122. (error "Expected an integer")))
  123. (prefix-numeric-value n)))
  124. (or (= n calc-word-size)
  125. (if (> (math-abs n) 100)
  126. (calc-change-mode 'calc-word-size n calc-leading-zeros)
  127. (calc-change-mode '(calc-word-size calc-previous-modulo)
  128. (list n (math-power-of-2 (math-abs n)))
  129. calc-leading-zeros)))
  130. (setq math-2-word-size (math-power-of-2 (math-abs n)))
  131. (setq math-half-2-word-size (math-power-of-2 (1- (math-abs n))))
  132. (calc-do-refresh)
  133. (calc-refresh-evaltos)
  134. (if (< n 0)
  135. (message "Binary word size is %d bits (two's complement)" (- n))
  136. (message "Binary word size is %d bits" n))))
  137. ;;; d-prefix mode commands.
  138. (defun calc-radix (n &optional arg)
  139. (interactive "NDisplay radix (2-36): ")
  140. (calc-wrapper
  141. (if (and (>= n 2) (<= n 36))
  142. (progn
  143. (calc-change-mode
  144. (list 'calc-number-radix 'calc-twos-complement-mode)
  145. (list n (or arg (calc-is-option))) t)
  146. ;; also change global value so minibuffer sees it
  147. (setq-default calc-number-radix calc-number-radix))
  148. (setq n calc-number-radix))
  149. (if calc-twos-complement-mode
  150. (message "Number radix is %d, two's complement mode is on." n)
  151. (message "Number radix is %d" n))))
  152. (defun calc-decimal-radix ()
  153. (interactive)
  154. (calc-radix 10))
  155. (defun calc-binary-radix (&optional arg)
  156. (interactive "P")
  157. (calc-radix 2 arg))
  158. (defun calc-octal-radix (&optional arg)
  159. (interactive "P")
  160. (calc-radix 8 arg))
  161. (defun calc-hex-radix (&optional arg)
  162. (interactive "P")
  163. (calc-radix 16 arg))
  164. (defun calc-leading-zeros (n)
  165. (interactive "P")
  166. (calc-wrapper
  167. (if (calc-change-mode 'calc-leading-zeros n t t)
  168. (message "Zero-padding integers to %d digits (assuming radix %d)"
  169. (let* ((calc-internal-prec 6))
  170. (math-compute-max-digits (math-abs calc-word-size)
  171. calc-number-radix))
  172. calc-number-radix)
  173. (message "Omitting leading zeros on integers"))))
  174. (defvar math-power-of-2-cache (list 1 2 4 8 16 32 64 128 256 512 1024))
  175. (defvar math-big-power-of-2-cache nil)
  176. (defun math-power-of-2 (n) ; [I I] [Public]
  177. (if (and (natnump n) (<= n 100))
  178. (or (nth n math-power-of-2-cache)
  179. (let* ((i (length math-power-of-2-cache))
  180. (val (nth (1- i) math-power-of-2-cache)))
  181. (while (<= i n)
  182. (setq val (math-mul val 2)
  183. math-power-of-2-cache (nconc math-power-of-2-cache
  184. (list val))
  185. i (1+ i)))
  186. val))
  187. (let ((found (assq n math-big-power-of-2-cache)))
  188. (if found
  189. (cdr found)
  190. (let ((po2 (math-ipow 2 n)))
  191. (setq math-big-power-of-2-cache
  192. (cons (cons n po2) math-big-power-of-2-cache))
  193. po2)))))
  194. (defun math-integer-log2 (n) ; [I I] [Public]
  195. (let ((i 0)
  196. (p math-power-of-2-cache)
  197. val)
  198. (while (and p (Math-natnum-lessp (setq val (car p)) n))
  199. (setq p (cdr p)
  200. i (1+ i)))
  201. (if p
  202. (and (equal val n)
  203. i)
  204. (while (Math-natnum-lessp
  205. (prog1
  206. (setq val (math-mul val 2))
  207. (setq math-power-of-2-cache (nconc math-power-of-2-cache
  208. (list val))))
  209. n)
  210. (setq i (1+ i)))
  211. (and (equal val n)
  212. i))))
  213. ;;; Bitwise operations.
  214. (defun calcFunc-and (a b &optional w) ; [I I I] [Public]
  215. (cond ((Math-messy-integerp w)
  216. (calcFunc-and a b (math-trunc w)))
  217. ((and w (not (integerp w)))
  218. (math-reject-arg w 'fixnump))
  219. ((and (integerp a) (integerp b))
  220. (math-clip (logand a b) w))
  221. ((or (eq (car-safe a) 'mod) (eq (car-safe b) 'mod))
  222. (math-binary-modulo-args 'calcFunc-and a b w))
  223. ((not (Math-num-integerp a))
  224. (math-reject-arg a 'integerp))
  225. ((not (Math-num-integerp b))
  226. (math-reject-arg b 'integerp))
  227. (t (math-clip (cons 'bigpos
  228. (math-and-bignum (math-binary-arg a w)
  229. (math-binary-arg b w)))
  230. w))))
  231. (defun math-binary-arg (a w)
  232. (if (not (Math-integerp a))
  233. (setq a (math-trunc a)))
  234. (if (Math-integer-negp a)
  235. (math-not-bignum (cdr (math-bignum-test (math-sub -1 a)))
  236. (math-abs (if w (math-trunc w) calc-word-size)))
  237. (cdr (Math-bignum-test a))))
  238. (defun math-binary-modulo-args (f a b w)
  239. (let (mod)
  240. (if (eq (car-safe a) 'mod)
  241. (progn
  242. (setq mod (nth 2 a)
  243. a (nth 1 a))
  244. (if (eq (car-safe b) 'mod)
  245. (if (equal mod (nth 2 b))
  246. (setq b (nth 1 b))
  247. (math-reject-arg b "*Inconsistent modulus"))))
  248. (setq mod (nth 2 b)
  249. b (nth 1 b)))
  250. (if (Math-messy-integerp mod)
  251. (setq mod (math-trunc mod))
  252. (or (Math-integerp mod)
  253. (math-reject-arg mod 'integerp)))
  254. (let ((bits (math-integer-log2 mod)))
  255. (if bits
  256. (if w
  257. (if (/= w bits)
  258. (calc-record-why
  259. "*Warning: Modulus inconsistent with word size"))
  260. (setq w bits))
  261. (calc-record-why "*Warning: Modulus is not a power of 2"))
  262. (math-make-mod (if b
  263. (funcall f a b w)
  264. (funcall f a w))
  265. mod))))
  266. (defun math-and-bignum (a b) ; [l l l]
  267. (and a b
  268. (let ((qa (math-div-bignum-digit a math-bignum-digit-power-of-two))
  269. (qb (math-div-bignum-digit b math-bignum-digit-power-of-two)))
  270. (math-mul-bignum-digit (math-and-bignum (math-norm-bignum (car qa))
  271. (math-norm-bignum (car qb)))
  272. math-bignum-digit-power-of-two
  273. (logand (cdr qa) (cdr qb))))))
  274. (defun calcFunc-or (a b &optional w) ; [I I I] [Public]
  275. (cond ((Math-messy-integerp w)
  276. (calcFunc-or a b (math-trunc w)))
  277. ((and w (not (integerp w)))
  278. (math-reject-arg w 'fixnump))
  279. ((and (integerp a) (integerp b))
  280. (math-clip (logior a b) w))
  281. ((or (eq (car-safe a) 'mod) (eq (car-safe b) 'mod))
  282. (math-binary-modulo-args 'calcFunc-or a b w))
  283. ((not (Math-num-integerp a))
  284. (math-reject-arg a 'integerp))
  285. ((not (Math-num-integerp b))
  286. (math-reject-arg b 'integerp))
  287. (t (math-clip (cons 'bigpos
  288. (math-or-bignum (math-binary-arg a w)
  289. (math-binary-arg b w)))
  290. w))))
  291. (defun math-or-bignum (a b) ; [l l l]
  292. (and (or a b)
  293. (let ((qa (math-div-bignum-digit a math-bignum-digit-power-of-two))
  294. (qb (math-div-bignum-digit b math-bignum-digit-power-of-two)))
  295. (math-mul-bignum-digit (math-or-bignum (math-norm-bignum (car qa))
  296. (math-norm-bignum (car qb)))
  297. math-bignum-digit-power-of-two
  298. (logior (cdr qa) (cdr qb))))))
  299. (defun calcFunc-xor (a b &optional w) ; [I I I] [Public]
  300. (cond ((Math-messy-integerp w)
  301. (calcFunc-xor a b (math-trunc w)))
  302. ((and w (not (integerp w)))
  303. (math-reject-arg w 'fixnump))
  304. ((and (integerp a) (integerp b))
  305. (math-clip (logxor a b) w))
  306. ((or (eq (car-safe a) 'mod) (eq (car-safe b) 'mod))
  307. (math-binary-modulo-args 'calcFunc-xor a b w))
  308. ((not (Math-num-integerp a))
  309. (math-reject-arg a 'integerp))
  310. ((not (Math-num-integerp b))
  311. (math-reject-arg b 'integerp))
  312. (t (math-clip (cons 'bigpos
  313. (math-xor-bignum (math-binary-arg a w)
  314. (math-binary-arg b w)))
  315. w))))
  316. (defun math-xor-bignum (a b) ; [l l l]
  317. (and (or a b)
  318. (let ((qa (math-div-bignum-digit a math-bignum-digit-power-of-two))
  319. (qb (math-div-bignum-digit b math-bignum-digit-power-of-two)))
  320. (math-mul-bignum-digit (math-xor-bignum (math-norm-bignum (car qa))
  321. (math-norm-bignum (car qb)))
  322. math-bignum-digit-power-of-two
  323. (logxor (cdr qa) (cdr qb))))))
  324. (defun calcFunc-diff (a b &optional w) ; [I I I] [Public]
  325. (cond ((Math-messy-integerp w)
  326. (calcFunc-diff a b (math-trunc w)))
  327. ((and w (not (integerp w)))
  328. (math-reject-arg w 'fixnump))
  329. ((and (integerp a) (integerp b))
  330. (math-clip (logand a (lognot b)) w))
  331. ((or (eq (car-safe a) 'mod) (eq (car-safe b) 'mod))
  332. (math-binary-modulo-args 'calcFunc-diff a b w))
  333. ((not (Math-num-integerp a))
  334. (math-reject-arg a 'integerp))
  335. ((not (Math-num-integerp b))
  336. (math-reject-arg b 'integerp))
  337. (t (math-clip (cons 'bigpos
  338. (math-diff-bignum (math-binary-arg a w)
  339. (math-binary-arg b w)))
  340. w))))
  341. (defun math-diff-bignum (a b) ; [l l l]
  342. (and a
  343. (let ((qa (math-div-bignum-digit a math-bignum-digit-power-of-two))
  344. (qb (math-div-bignum-digit b math-bignum-digit-power-of-two)))
  345. (math-mul-bignum-digit (math-diff-bignum (math-norm-bignum (car qa))
  346. (math-norm-bignum (car qb)))
  347. math-bignum-digit-power-of-two
  348. (logand (cdr qa) (lognot (cdr qb)))))))
  349. (defun calcFunc-not (a &optional w) ; [I I] [Public]
  350. (cond ((Math-messy-integerp w)
  351. (calcFunc-not a (math-trunc w)))
  352. ((eq (car-safe a) 'mod)
  353. (math-binary-modulo-args 'calcFunc-not a nil w))
  354. ((and w (not (integerp w)))
  355. (math-reject-arg w 'fixnump))
  356. ((not (Math-num-integerp a))
  357. (math-reject-arg a 'integerp))
  358. ((< (or w (setq w calc-word-size)) 0)
  359. (math-clip (calcFunc-not a (- w)) w))
  360. (t (math-normalize
  361. (cons 'bigpos
  362. (math-not-bignum (math-binary-arg a w)
  363. w))))))
  364. (defun math-not-bignum (a w) ; [l l]
  365. (let ((q (math-div-bignum-digit a math-bignum-digit-power-of-two)))
  366. (if (<= w math-bignum-logb-digit-size)
  367. (list (logand (lognot (cdr q))
  368. (1- (lsh 1 w))))
  369. (math-mul-bignum-digit (math-not-bignum (math-norm-bignum (car q))
  370. (- w math-bignum-logb-digit-size))
  371. math-bignum-digit-power-of-two
  372. (logxor (cdr q)
  373. (1- math-bignum-digit-power-of-two))))))
  374. (defun calcFunc-lsh (a &optional n w) ; [I I] [Public]
  375. (setq a (math-trunc a)
  376. n (if n (math-trunc n) 1))
  377. (if (eq (car-safe a) 'mod)
  378. (math-binary-modulo-args 'calcFunc-lsh a n w)
  379. (setq w (if w (math-trunc w) calc-word-size))
  380. (or (integerp w)
  381. (math-reject-arg w 'fixnump))
  382. (or (Math-integerp a)
  383. (math-reject-arg a 'integerp))
  384. (or (Math-integerp n)
  385. (math-reject-arg n 'integerp))
  386. (if (< w 0)
  387. (math-clip (calcFunc-lsh a n (- w)) w)
  388. (if (Math-integer-negp a)
  389. (setq a (math-clip a w)))
  390. (cond ((or (Math-lessp n (- w))
  391. (Math-lessp w n))
  392. 0)
  393. ((< n 0)
  394. (math-quotient (math-clip a w) (math-power-of-2 (- n))))
  395. (t
  396. (math-clip (math-mul a (math-power-of-2 n)) w))))))
  397. (defun calcFunc-rsh (a &optional n w) ; [I I] [Public]
  398. (calcFunc-lsh a (math-neg (or n 1)) w))
  399. (defun calcFunc-ash (a &optional n w) ; [I I] [Public]
  400. (if (or (null n)
  401. (not (Math-negp n)))
  402. (calcFunc-lsh a n w)
  403. (setq a (math-trunc a)
  404. n (if n (math-trunc n) 1))
  405. (if (eq (car-safe a) 'mod)
  406. (math-binary-modulo-args 'calcFunc-ash a n w)
  407. (setq w (if w (math-trunc w) calc-word-size))
  408. (or (integerp w)
  409. (math-reject-arg w 'fixnump))
  410. (or (Math-integerp a)
  411. (math-reject-arg a 'integerp))
  412. (or (Math-integerp n)
  413. (math-reject-arg n 'integerp))
  414. (if (< w 0)
  415. (math-clip (calcFunc-ash a n (- w)) w)
  416. (if (Math-integer-negp a)
  417. (setq a (math-clip a w)))
  418. (let ((two-to-sizem1 (math-power-of-2 (1- w)))
  419. (sh (calcFunc-lsh a n w)))
  420. (cond ((Math-natnum-lessp a two-to-sizem1)
  421. sh)
  422. ((Math-lessp n (- 1 w))
  423. (math-add (math-mul two-to-sizem1 2) -1))
  424. (t (let ((two-to-n (math-power-of-2 (- n))))
  425. (math-add (calcFunc-lsh (math-add two-to-n -1)
  426. (+ w n) w)
  427. sh)))))))))
  428. (defun calcFunc-rash (a &optional n w) ; [I I] [Public]
  429. (calcFunc-ash a (math-neg (or n 1)) w))
  430. (defun calcFunc-rot (a &optional n w) ; [I I] [Public]
  431. (setq a (math-trunc a)
  432. n (if n (math-trunc n) 1))
  433. (if (eq (car-safe a) 'mod)
  434. (math-binary-modulo-args 'calcFunc-rot a n w)
  435. (setq w (if w (math-trunc w) calc-word-size))
  436. (or (integerp w)
  437. (math-reject-arg w 'fixnump))
  438. (or (Math-integerp a)
  439. (math-reject-arg a 'integerp))
  440. (or (Math-integerp n)
  441. (math-reject-arg n 'integerp))
  442. (if (< w 0)
  443. (math-clip (calcFunc-rot a n (- w)) w)
  444. (if (Math-integer-negp a)
  445. (setq a (math-clip a w)))
  446. (cond ((or (Math-integer-negp n)
  447. (not (Math-natnum-lessp n w)))
  448. (calcFunc-rot a (math-mod n w) w))
  449. (t
  450. (math-add (calcFunc-lsh a (- n w) w)
  451. (calcFunc-lsh a n w)))))))
  452. (defun math-clip (a &optional w) ; [I I] [Public]
  453. (cond ((Math-messy-integerp w)
  454. (math-clip a (math-trunc w)))
  455. ((eq (car-safe a) 'mod)
  456. (math-binary-modulo-args 'math-clip a nil w))
  457. ((and w (not (integerp w)))
  458. (math-reject-arg w 'fixnump))
  459. ((not (Math-num-integerp a))
  460. (math-reject-arg a 'integerp))
  461. ((< (or w (setq w calc-word-size)) 0)
  462. (setq a (math-clip a (- w)))
  463. (if (Math-natnum-lessp a (math-power-of-2 (- -1 w)))
  464. a
  465. (math-sub a (math-power-of-2 (- w)))))
  466. ((Math-negp a)
  467. (math-normalize (cons 'bigpos (math-binary-arg a w))))
  468. ((and (integerp a) (< a math-small-integer-size))
  469. (if (> w (logb math-small-integer-size))
  470. a
  471. (logand a (1- (lsh 1 w)))))
  472. (t
  473. (math-normalize
  474. (cons 'bigpos
  475. (math-clip-bignum (cdr (math-bignum-test (math-trunc a)))
  476. w))))))
  477. (defalias 'calcFunc-clip 'math-clip)
  478. (defun math-clip-bignum (a w) ; [l l]
  479. (let ((q (math-div-bignum-digit a math-bignum-digit-power-of-two)))
  480. (if (<= w math-bignum-logb-digit-size)
  481. (list (logand (cdr q)
  482. (1- (lsh 1 w))))
  483. (math-mul-bignum-digit (math-clip-bignum (math-norm-bignum (car q))
  484. (- w math-bignum-logb-digit-size))
  485. math-bignum-digit-power-of-two
  486. (cdr q)))))
  487. (defvar math-max-digits-cache nil)
  488. (defun math-compute-max-digits (w r)
  489. (let* ((pair (+ (* r 100000) w))
  490. (res (assq pair math-max-digits-cache)))
  491. (if res
  492. (cdr res)
  493. (let* ((calc-command-flags nil)
  494. (digs (math-ceiling (math-div w (math-real-log2 r)))))
  495. (setq math-max-digits-cache (cons (cons pair digs)
  496. math-max-digits-cache))
  497. digs))))
  498. (defvar math-log2-cache (list '(2 . 1)
  499. '(4 . 2)
  500. '(8 . 3)
  501. '(10 . (float 332193 -5))
  502. '(16 . 4)
  503. '(32 . 5)))
  504. (defun math-real-log2 (x) ;;; calc-internal-prec must be 6
  505. (let ((res (assq x math-log2-cache)))
  506. (if res
  507. (cdr res)
  508. (let* ((calc-symbolic-mode nil)
  509. (calc-display-working-message nil)
  510. (log (calcFunc-log x 2)))
  511. (setq math-log2-cache (cons (cons x log) math-log2-cache))
  512. log))))
  513. (defconst math-radix-digits ["0" "1" "2" "3" "4" "5" "6" "7" "8" "9"
  514. "A" "B" "C" "D" "E" "F" "G" "H" "I" "J"
  515. "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T"
  516. "U" "V" "W" "X" "Y" "Z"])
  517. (defsubst math-format-radix-digit (a) ; [X D]
  518. (aref math-radix-digits a))
  519. (defun math-format-radix (a) ; [X S]
  520. (if (< a calc-number-radix)
  521. (if (< a 0)
  522. (concat "-" (math-format-radix (- a)))
  523. (math-format-radix-digit a))
  524. (let ((s ""))
  525. (while (> a 0)
  526. (setq s (concat (math-format-radix-digit (% a calc-number-radix)) s)
  527. a (/ a calc-number-radix)))
  528. s)))
  529. (defconst math-binary-digits ["000" "001" "010" "011"
  530. "100" "101" "110" "111"])
  531. (defun math-format-binary (a) ; [X S]
  532. (if (< a 8)
  533. (if (< a 0)
  534. (concat "-" (math-format-binary (- a)))
  535. (math-format-radix a))
  536. (let ((s ""))
  537. (while (> a 7)
  538. (setq s (concat (aref math-binary-digits (% a 8)) s)
  539. a (/ a 8)))
  540. (concat (math-format-radix a) s))))
  541. (defun math-format-bignum-radix (a) ; [X L]
  542. (cond ((null a) "0")
  543. ((and (null (cdr a))
  544. (< (car a) calc-number-radix))
  545. (math-format-radix-digit (car a)))
  546. (t
  547. (let ((q (math-div-bignum-digit a calc-number-radix)))
  548. (concat (math-format-bignum-radix (math-norm-bignum (car q)))
  549. (math-format-radix-digit (cdr q)))))))
  550. (defun math-format-bignum-binary (a) ; [X L]
  551. (cond ((null a) "0")
  552. ((null (cdr a))
  553. (math-format-binary (car a)))
  554. (t
  555. (let ((q (math-div-bignum-digit a 512)))
  556. (concat (math-format-bignum-binary (math-norm-bignum (car q)))
  557. (aref math-binary-digits (/ (cdr q) 64))
  558. (aref math-binary-digits (% (/ (cdr q) 8) 8))
  559. (aref math-binary-digits (% (cdr q) 8)))))))
  560. (defun math-format-bignum-octal (a) ; [X L]
  561. (cond ((null a) "0")
  562. ((null (cdr a))
  563. (math-format-radix (car a)))
  564. (t
  565. (let ((q (math-div-bignum-digit a 512)))
  566. (concat (math-format-bignum-octal (math-norm-bignum (car q)))
  567. (math-format-radix-digit (/ (cdr q) 64))
  568. (math-format-radix-digit (% (/ (cdr q) 8) 8))
  569. (math-format-radix-digit (% (cdr q) 8)))))))
  570. (defun math-format-bignum-hex (a) ; [X L]
  571. (cond ((null a) "0")
  572. ((null (cdr a))
  573. (math-format-radix (car a)))
  574. (t
  575. (let ((q (math-div-bignum-digit a 256)))
  576. (concat (math-format-bignum-hex (math-norm-bignum (car q)))
  577. (math-format-radix-digit (/ (cdr q) 16))
  578. (math-format-radix-digit (% (cdr q) 16)))))))
  579. ;;; Decompose into integer and fractional parts, without depending
  580. ;;; on calc-internal-prec.
  581. (defun math-float-parts (a need-frac) ; returns ( int frac fracdigs )
  582. (if (>= (nth 2 a) 0)
  583. (list (math-scale-rounding (nth 1 a) (nth 2 a)) '(float 0 0) 0)
  584. (let* ((d (math-numdigs (nth 1 a)))
  585. (n (- (nth 2 a))))
  586. (if need-frac
  587. (if (>= n d)
  588. (list 0 a n)
  589. (let ((qr (math-idivmod (nth 1 a) (math-scale-int 1 n))))
  590. (list (car qr) (math-make-float (cdr qr) (- n)) n)))
  591. (list (math-scale-rounding (nth 1 a) (nth 2 a))
  592. '(float 0 0) 0)))))
  593. (defun math-format-radix-float (a prec)
  594. (let ((fmt (car calc-float-format))
  595. (figs (nth 1 calc-float-format))
  596. (point calc-point-char)
  597. (str nil)
  598. pos)
  599. (if (eq fmt 'fix)
  600. (let* ((afigs (math-abs figs))
  601. (fp (math-float-parts a (> afigs 0)))
  602. (calc-internal-prec (+ 3 (max (nth 2 fp)
  603. (math-convert-radix-digits
  604. afigs t))))
  605. (int (car fp))
  606. (frac (math-round (math-mul (math-normalize (nth 1 fp))
  607. (math-radix-float-power afigs)))))
  608. (if (not (and (math-zerop frac) (math-zerop int) (< figs 0)))
  609. (let ((math-radix-explicit-format nil))
  610. (let ((calc-group-digits nil))
  611. (setq str (if (> afigs 0) (math-format-number frac) ""))
  612. (if (< (length str) afigs)
  613. (setq str (concat (make-string (- afigs (length str)) ?0)
  614. str))
  615. (if (> (length str) afigs)
  616. (setq str (substring str 1)
  617. int (math-add int 1))))
  618. (setq str (concat (math-format-number int) point str)))
  619. (when calc-group-digits
  620. (setq str (math-group-float str))))
  621. (setq figs 0))))
  622. (or str
  623. (let* ((prec calc-internal-prec)
  624. (afigs (if (> figs 0)
  625. figs
  626. (max 1 (+ figs
  627. (1- (math-convert-radix-digits
  628. (max prec
  629. (math-numdigs (nth 1 a)))))))))
  630. (calc-internal-prec (+ 3 (math-convert-radix-digits afigs t)))
  631. (explo -1) (vlo (math-radix-float-power explo))
  632. (exphi 1) (vhi (math-radix-float-power exphi))
  633. expmid vmid eadj)
  634. (setq a (math-normalize a))
  635. (if (Math-zerop a)
  636. (setq explo 0)
  637. (if (math-lessp-float '(float 1 0) a)
  638. (while (not (math-lessp-float a vhi))
  639. (setq explo exphi vlo vhi
  640. exphi (math-mul exphi 2)
  641. vhi (math-radix-float-power exphi)))
  642. (while (math-lessp-float a vlo)
  643. (setq exphi explo vhi vlo
  644. explo (math-mul explo 2)
  645. vlo (math-radix-float-power explo))))
  646. (while (not (eq (math-sub exphi explo) 1))
  647. (setq expmid (math-div2 (math-add explo exphi))
  648. vmid (math-radix-float-power expmid))
  649. (if (math-lessp-float a vmid)
  650. (setq exphi expmid vhi vmid)
  651. (setq explo expmid vlo vmid)))
  652. (setq a (math-div-float a vlo)))
  653. (let* ((sc (math-round (math-mul a (math-radix-float-power
  654. (1- afigs)))))
  655. (math-radix-explicit-format nil))
  656. (let ((calc-group-digits nil))
  657. (setq str (math-format-number sc))))
  658. (if (> (length str) afigs)
  659. (setq str (substring str 0 -1)
  660. explo (1+ explo)))
  661. (if (and (eq fmt 'float)
  662. (math-lessp explo (+ (if (= figs 0)
  663. (1- (math-convert-radix-digits
  664. prec))
  665. afigs)
  666. calc-display-sci-high 1))
  667. (math-lessp calc-display-sci-low explo))
  668. (let ((dpos (1+ explo)))
  669. (cond ((<= dpos 0)
  670. (setq str (concat "0" point (make-string (- dpos) ?0)
  671. str)))
  672. ((> dpos (length str))
  673. (setq str (concat str (make-string (- dpos (length str))
  674. ?0) point)))
  675. (t
  676. (setq str (concat (substring str 0 dpos) point
  677. (substring str dpos)))))
  678. (setq explo nil))
  679. (setq eadj (if (eq fmt 'eng)
  680. (min (math-mod explo 3) (length str))
  681. 0)
  682. str (concat (substring str 0 (1+ eadj)) point
  683. (substring str (1+ eadj)))))
  684. (setq pos (length str))
  685. (while (eq (aref str (1- pos)) ?0) (setq pos (1- pos)))
  686. (and explo (eq (aref str (1- pos)) ?.) (setq pos (1- pos)))
  687. (setq str (substring str 0 pos))
  688. (when calc-group-digits
  689. (setq str (math-group-float str)))
  690. (if explo
  691. (let ((estr (let ((calc-number-radix 10)
  692. (calc-group-digits nil))
  693. (math-format-number
  694. (math-sub explo eadj)))))
  695. (setq str (if (or (memq calc-language '(math maple))
  696. (> calc-number-radix 14))
  697. (format "%s*%d.^%s" str calc-number-radix estr)
  698. (format "%se%s" str estr)))))))
  699. str))
  700. (defvar math-radix-digits-cache nil)
  701. (defun math-convert-radix-digits (n &optional to-dec)
  702. (let ((key (cons n (cons to-dec calc-number-radix))))
  703. (or (cdr (assoc key math-radix-digits-cache))
  704. (let* ((calc-internal-prec 6)
  705. (log (math-div (math-real-log2 calc-number-radix)
  706. '(float 332193 -5))))
  707. (cdr (car (setq math-radix-digits-cache
  708. (cons (cons key (math-ceiling (if to-dec
  709. (math-mul n log)
  710. (math-div n log))))
  711. math-radix-digits-cache))))))))
  712. (defvar math-radix-float-cache-tag nil)
  713. (defvar math-radix-float-cache)
  714. (defun math-radix-float-power (n)
  715. (if (eq n 0)
  716. '(float 1 0)
  717. (or (and (eq calc-number-radix (car math-radix-float-cache-tag))
  718. (<= calc-internal-prec (cdr math-radix-float-cache-tag)))
  719. (setq math-radix-float-cache-tag (cons calc-number-radix
  720. calc-internal-prec)
  721. math-radix-float-cache nil))
  722. (math-normalize
  723. (or (cdr (assoc n math-radix-float-cache))
  724. (cdr (car (setq math-radix-float-cache
  725. (cons (cons
  726. n
  727. (let ((calc-internal-prec
  728. (cdr math-radix-float-cache-tag)))
  729. (if (math-negp n)
  730. (math-div-float '(float 1 0)
  731. (math-radix-float-power
  732. (math-neg n)))
  733. (math-mul-float (math-sqr-float
  734. (math-radix-float-power
  735. (math-div2 n)))
  736. (if (math-evenp n)
  737. '(float 1 0)
  738. (math-float
  739. calc-number-radix))))))
  740. math-radix-float-cache))))))))
  741. ;;; Two's complement mode
  742. (defun math-format-twos-complement (a)
  743. "Format an integer in two's complement mode."
  744. (let* (;(calc-leading-zeros t)
  745. (overflow nil)
  746. (negative nil)
  747. (num
  748. (cond
  749. ((or (eq a 0)
  750. (and (Math-integer-posp a)))
  751. (if (integerp a)
  752. (math-format-radix a)
  753. (math-format-bignum-radix (cdr a))))
  754. ((Math-integer-negp a)
  755. (let ((newa (math-add a math-2-word-size)))
  756. (if (integerp newa)
  757. (math-format-radix newa)
  758. (math-format-bignum-radix (cdr newa))))))))
  759. (let* ((calc-internal-prec 6)
  760. (digs (math-compute-max-digits (math-abs calc-word-size)
  761. calc-number-radix))
  762. (len (length num)))
  763. (if (< len digs)
  764. (setq num (concat (make-string (- digs len) ?0) num))))
  765. (when calc-group-digits
  766. (setq num (math-group-float num)))
  767. (concat
  768. (number-to-string calc-number-radix)
  769. "##"
  770. num)))
  771. (provide 'calc-bin)
  772. ;;; calc-bin.el ends here