1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
From: =?utf-8?q?St=C3=A9phane_Glondu?= <glondu@debian.org>
Date: Sun, 14 Sep 2025 20:13:19 +0200
Subject: Fix CCInt.popcount on 32-bit architectures
---
src/core/CCInt.ml | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)
diff --git a/src/core/CCInt.ml b/src/core/CCInt.ml
index a2efe7b..d5b0122 100644
--- a/src/core/CCInt.ml
+++ b/src/core/CCInt.ml
@@ -210,6 +210,12 @@ let range_by ~step i j yield =
else
range i (((j - i) / step * step) + i) yield
+let adjust =
+ match Sys.word_size with
+ | 64 -> Fun.id
+ | 32 -> (fun x -> if x >= 32 then x - 32 else x)
+ | _ -> failwith "adjust"
+
(*
from https://en.wikipedia.org/wiki/Hamming_weight
@@ -247,7 +253,7 @@ let popcount (b : int) : int =
let b = add b (shift_right_logical b 16) in
let b = add b (shift_right_logical b 32) in
let b = logand b 0x7fL in
- to_int b
+ adjust (to_int b)
let logand = ( land )
let logor = ( lor )
|