diff options
| author | Paul Eggert | 2017-09-30 15:36:52 -0700 |
|---|---|---|
| committer | Paul Eggert | 2017-09-30 15:38:14 -0700 |
| commit | 8136df6a8cbf071266eb38f5baef005f4e9241a3 (patch) | |
| tree | 4ade7520c5be858be40b7939a0338e4a48288c85 /test/src/data-tests.el | |
| parent | d247e1d30abcb77665f829ca98a5bdef69ff4bc3 (diff) | |
| download | emacs-8136df6a8cbf071266eb38f5baef005f4e9241a3.tar.gz emacs-8136df6a8cbf071266eb38f5baef005f4e9241a3.zip | |
Make logcount act like CL on negative arg
Behaving like Common Lisp is less likely to lead to surprises,
as it yields the same answers on 32- vs 64-bit machines.
* doc/lispref/numbers.texi (Bitwise Operations):
Document behavior on negative integers.
* src/data.c (Flogcount):
Behave like Common Lisp for negative arguments.
* test/src/data-tests.el (data-tests-popcnt)
(data-tests-logcount): Test negative args too.
Diffstat (limited to 'test/src/data-tests.el')
| -rw-r--r-- | test/src/data-tests.el | 4 |
1 files changed, 3 insertions, 1 deletions
diff --git a/test/src/data-tests.el b/test/src/data-tests.el index d1154cc5c44..374d1689b9e 100644 --- a/test/src/data-tests.el +++ b/test/src/data-tests.el | |||
| @@ -109,12 +109,14 @@ | |||
| 109 | 109 | ||
| 110 | (defun data-tests-popcnt (byte) | 110 | (defun data-tests-popcnt (byte) |
| 111 | "Calculate the Hamming weight of BYTE." | 111 | "Calculate the Hamming weight of BYTE." |
| 112 | (if (< byte 0) | ||
| 113 | (setq byte (lognot byte))) | ||
| 112 | (setq byte (- byte (logand (lsh byte -1) #x55555555))) | 114 | (setq byte (- byte (logand (lsh byte -1) #x55555555))) |
| 113 | (setq byte (+ (logand byte #x33333333) (logand (lsh byte -2) #x33333333))) | 115 | (setq byte (+ (logand byte #x33333333) (logand (lsh byte -2) #x33333333))) |
| 114 | (lsh (* (logand (+ byte (lsh byte -4)) #x0f0f0f0f) #x01010101) -24)) | 116 | (lsh (* (logand (+ byte (lsh byte -4)) #x0f0f0f0f) #x01010101) -24)) |
| 115 | 117 | ||
| 116 | (ert-deftest data-tests-logcount () | 118 | (ert-deftest data-tests-logcount () |
| 117 | (should (cl-loop for n in (number-sequence 0 255) | 119 | (should (cl-loop for n in (number-sequence -255 255) |
| 118 | always (= (logcount n) (data-tests-popcnt n)))) | 120 | always (= (logcount n) (data-tests-popcnt n)))) |
| 119 | ;; https://oeis.org/A000120 | 121 | ;; https://oeis.org/A000120 |
| 120 | (should (= 11 (logcount 9727))) | 122 | (should (= 11 (logcount 9727))) |