Skip to content

Commit 484aa3a

Browse files
authored
Merge pull request #394 from c-cube/ccbv_bytes2
(continued) use bytes for CCBV
2 parents 69f2805 + b7d19e9 commit 484aa3a

File tree

12 files changed

+976
-255
lines changed

12 files changed

+976
-255
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ build:
66
dune build @install -p $(PACKAGES)
77

88
test: build
9-
dune runtest --cache=disabled --no-buffer --force
9+
dune runtest --display=quiet --cache=disabled --no-buffer --force
1010

1111
clean:
1212
dune clean

src/core/CCInt32.ml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,20 @@ let pow a b =
2727
raise (Invalid_argument "pow: can't raise int to negative power")
2828
| b -> aux a b
2929

30+
(* see {!CCInt.popcount} for more details *)
31+
let[@inline] popcount (b : t) : int =
32+
let m1 = 0x55555555l in
33+
let m2 = 0x33333333l in
34+
let m4 = 0x0f0f0f0fl in
35+
36+
let b = sub b (logand (shift_right_logical b 1) m1) in
37+
let b = add (logand b m2) (logand (shift_right_logical b 2) m2) in
38+
let b = logand (add b (shift_right_logical b 4)) m4 in
39+
let b = add b (shift_right_logical b 8) in
40+
let b = add b (shift_right_logical b 16) in
41+
let b = logand b 0x7fl in
42+
to_int b
43+
3044
let floor_div a n =
3145
if compare a 0l < 0 && compare n 0l >= 0 then
3246
sub (div (add a 1l) n) 1l

src/core/CCInt32.mli

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ val pow : t -> t -> t
4141
Raises [Invalid_argument] if [x = y = 0] or [y] < 0.
4242
@since 0.11 *)
4343

44+
val popcount : t -> int
45+
(** Number of bits set to 1.
46+
@since NEXT_RELEASE *)
47+
4448
val floor_div : t -> t -> t
4549
(** [floor_div x n] is integer division rounding towards negative infinity.
4650
It satisfies [x = m * floor_div x n + rem x n].

src/core/CCInt64.ml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,21 @@ let max : t -> t -> t = Stdlib.max
88
let hash x = Stdlib.abs (to_int x)
99
let sign i = compare i zero
1010

11+
(* see {!CCInt.popcount} for more details *)
12+
let[@inline] popcount (b : t) : int =
13+
let m1 = 0x5555555555555555L in
14+
let m2 = 0x3333333333333333L in
15+
let m4 = 0x0f0f0f0f0f0f0f0fL in
16+
17+
let b = sub b (logand (shift_right_logical b 1) m1) in
18+
let b = add (logand b m2) (logand (shift_right_logical b 2) m2) in
19+
let b = logand (add b (shift_right_logical b 4)) m4 in
20+
let b = add b (shift_right_logical b 8) in
21+
let b = add b (shift_right_logical b 16) in
22+
let b = add b (shift_right_logical b 32) in
23+
let b = logand b 0x7fL in
24+
to_int b
25+
1126
let pow a b =
1227
let rec aux acc = function
1328
| 1L -> acc

src/core/CCInt64.mli

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ val hash : t -> int
3030
(** [hash x] computes the hash of [x].
3131
Like {!Stdlib.abs (to_int x)}. *)
3232

33+
val popcount : t -> int
34+
(** Number of bits set to 1.
35+
@since NEXT_RELEASE *)
36+
3337
val sign : t -> int
3438
(** [sign x] return [0] if [x = 0], [-1] if [x < 0] and [1] if [x > 0].
3539
Same as [compare x zero].

0 commit comments

Comments
 (0)