Fix 'bitwise-bit-count' for negative arguments.
authorMark H Weaver <mhw@netris.org>
Sun, 14 Jul 2013 18:08:33 +0000 (14:08 -0400)
committerMark H Weaver <mhw@netris.org>
Sun, 14 Jul 2013 18:08:33 +0000 (14:08 -0400)
Fixes <http://bugs.gnu.org/14864>.
Reported by Göran Weinholt <goran@weinholt.se>.

* module/rnrs/arithmetic/bitwise.scm (bitwise-bit-count): If the
  argument is negative, return the 'bitwise-not' of the result of
  'logcount', as per R6RS.  Previously, 'bitwise-bit-count' was
  identical to 'logcount'.

module/rnrs/arithmetic/bitwise.scm
test-suite/tests/r6rs-arithmetic-bitwise.test

index bb3a207..ac870ff 100644 (file)
                  (logand bitwise-and) 
                  (logior bitwise-ior) 
                  (logxor bitwise-xor)
-                 (logcount bitwise-bit-count)
                  (ash bitwise-arithmetic-shift)))
 
+  (define (bitwise-bit-count ei)
+    (if (negative? ei)
+        (bitwise-not (logcount ei))
+        (logcount ei)))
+
   (define (bitwise-if ei1 ei2 ei3)
     (bitwise-ior (bitwise-and ei1 ei2) (bitwise-and (bitwise-not ei1) ei3)))
   
index a61fef8..c864f3b 100644 (file)
@@ -43,7 +43,9 @@
 
 (with-test-prefix "bitwise-bit-count"
   (pass-if "bitwise-bit-count simple"
-    (eqv? (bitwise-bit-count #b101) 2)))
+    (eqv? (bitwise-bit-count #b101) 2))
+  (pass-if "bitwise-bit-count negative"
+    (eqv? (bitwise-bit-count #b-101) -2)))
 
 (with-test-prefix "bitwise-length"
   (pass-if "bitwise-length simple"