File tree Expand file tree Collapse file tree 12 files changed +976
-255
lines changed Expand file tree Collapse file tree 12 files changed +976
-255
lines changed Original file line number Diff line number Diff line change 66 dune build @install -p $(PACKAGES )
77
88test : build
9- dune runtest --cache=disabled --no-buffer --force
9+ dune runtest --display=quiet -- cache=disabled --no-buffer --force
1010
1111clean :
1212 dune clean
Original file line number Diff line number Diff 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+
3044let floor_div a n =
3145 if compare a 0l < 0 && compare n 0l > = 0 then
3246 sub (div (add a 1l ) n) 1l
Original file line number Diff line number Diff 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+
4448val 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].
Original file line number Diff line number Diff line change @@ -8,6 +8,21 @@ let max : t -> t -> t = Stdlib.max
88let hash x = Stdlib. abs (to_int x)
99let 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+
1126let pow a b =
1227 let rec aux acc = function
1328 | 1L -> acc
Original file line number Diff line number Diff 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+
3337val 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].
You can’t perform that action at this time.
0 commit comments