aboutsummaryrefslogtreecommitdiffstats
path: root/test/src
diff options
context:
space:
mode:
authorMark Oteiza2017-09-30 14:14:12 -0400
committerMark Oteiza2017-09-30 14:16:18 -0400
commit185f33340680d918a95ff704a8f7e2d9e1a6f0ca (patch)
treeaf80fe1f744c467325c54a0361d6f1309bbe2855 /test/src
parent20a09de953f437109a098fa8c4d380663d921481 (diff)
downloademacs-185f33340680d918a95ff704a8f7e2d9e1a6f0ca.tar.gz
emacs-185f33340680d918a95ff704a8f7e2d9e1a6f0ca.zip
Add logcount (Bug#22689)
* doc/lispref/numbers.texi (Bitwise Operations): Add documentation. * etc/NEWS: Mention. * src/data.c (logcount32, logcount64): New functions. (logcount): New Lisp function. (syms_of_data): Declare it. * test/src/data-tests.el (data-tests-popcnt, data-tests-logcount): New test.
Diffstat (limited to 'test/src')
-rw-r--r--test/src/data-tests.el13
1 files changed, 13 insertions, 0 deletions
diff --git a/test/src/data-tests.el b/test/src/data-tests.el
index 8de8c145d40..d1154cc5c44 100644
--- a/test/src/data-tests.el
+++ b/test/src/data-tests.el
@@ -107,6 +107,19 @@
107 (should (isnan (min 1.0 0.0e+NaN))) 107 (should (isnan (min 1.0 0.0e+NaN)))
108 (should (isnan (min 1.0 0.0e+NaN 1.1)))) 108 (should (isnan (min 1.0 0.0e+NaN 1.1))))
109 109
110(defun data-tests-popcnt (byte)
111 "Calculate the Hamming weight of BYTE."
112 (setq byte (- byte (logand (lsh byte -1) #x55555555)))
113 (setq byte (+ (logand byte #x33333333) (logand (lsh byte -2) #x33333333)))
114 (lsh (* (logand (+ byte (lsh byte -4)) #x0f0f0f0f) #x01010101) -24))
115
116(ert-deftest data-tests-logcount ()
117 (should (cl-loop for n in (number-sequence 0 255)
118 always (= (logcount n) (data-tests-popcnt n))))
119 ;; https://oeis.org/A000120
120 (should (= 11 (logcount 9727)))
121 (should (= 8 (logcount 9999))))
122
110;; Bool vector tests. Compactly represent bool vectors as hex 123;; Bool vector tests. Compactly represent bool vectors as hex
111;; strings. 124;; strings.
112 125