10
1
mirror of https://gitlab.com/scemama/QCaml.git synced 2024-07-11 22:03:37 +02:00
QCaml/common/lib/bitstring.ml

236 lines
6.5 KiB
OCaml
Raw Normal View History

2020-12-26 01:47:55 +01:00
(* Single-integer implementation *)
2019-03-26 01:20:17 +01:00
2020-12-26 01:47:55 +01:00
(* [[file:../bitstring.org::*Single-integer implementation][Single-integer implementation:1]] *)
module One = struct
2019-03-26 01:20:17 +01:00
let of_int x =
assert (x > 0); x
let numbits _ = 63
let zero = 0
let is_zero x = x = 0
let shift_left x i = x lsl i
let shift_right x i = x lsr i
let shift_left_one i = 1 lsl i
let testbit x i = ( (x lsr i) land 1 ) = 1
let logor a b = a lor b
2019-12-03 12:25:31 +01:00
let neg a = - a
2019-03-26 01:20:17 +01:00
let logxor a b = a lxor b
let logand a b = a land b
let lognot a = lnot a
let minus_one a = a - 1
let plus_one a = a + 1
let popcount = function
| 0 -> 0
| r -> Util.popcnt (Int64.of_int r)
let trailing_zeros r =
Util.trailz (Int64.of_int r)
let hamdist a b =
a lxor b
2019-04-03 22:17:20 +02:00
|> popcount
2019-03-26 01:20:17 +01:00
let pp ppf s =
2019-12-03 12:25:31 +01:00
Format.fprintf ppf "@[@[%a@]@]" (Util.pp_bitstring 64)
2019-03-26 01:20:17 +01:00
(Z.of_int s)
end
2020-12-26 01:47:55 +01:00
(* Single-integer implementation:1 ends here *)
2019-03-26 01:20:17 +01:00
2020-12-26 01:47:55 +01:00
(* Zarith implementation *)
2019-03-26 01:20:17 +01:00
2020-12-26 01:47:55 +01:00
(* [[file:../bitstring.org::*Zarith implementation][Zarith implementation:1]] *)
2019-03-26 01:20:17 +01:00
module Many = struct
let of_z x = x
let zero = Z.zero
let is_zero x = x = Z.zero
let shift_left = Z.shift_left
let shift_right = Z.shift_right
let shift_left_one i = Z.shift_left Z.one i
let testbit = Z.testbit
let logor = Z.logor
let logxor = Z.logxor
let logand = Z.logand
let lognot = Z.lognot
2019-12-03 12:25:31 +01:00
let neg = Z.neg
2019-03-26 01:20:17 +01:00
let minus_one = Z.pred
let plus_one = Z.succ
let trailing_zeros = Z.trailing_zeros
let hamdist = Z.hamdist
let numbits i = max (Z.numbits i) 64
let popcount z =
if z = Z.zero then 0 else Z.popcount z
let pp ppf s =
2019-12-03 12:25:31 +01:00
Format.fprintf ppf "@[@[%a@]@]" (Util.pp_bitstring (Z.numbits s)) s
2019-03-26 01:20:17 +01:00
end
2020-12-26 01:47:55 +01:00
(* Zarith implementation:1 ends here *)
2019-03-26 01:20:17 +01:00
2020-12-26 01:47:55 +01:00
(* [[file:../bitstring.org::*Type][Type:2]] *)
type t =
| One of int
| Many of Z.t
(* Type:2 ends here *)
2019-03-26 01:20:17 +01:00
2020-12-26 01:47:55 +01:00
(* [[file:../bitstring.org::*~of_int~][~of_int~:2]] *)
2019-03-26 01:20:17 +01:00
let of_int x =
One (One.of_int x)
2020-12-26 01:47:55 +01:00
(* ~of_int~:2 ends here *)
2019-03-25 19:28:38 +01:00
2020-12-26 01:47:55 +01:00
(* [[file:../bitstring.org::*~of_z~][~of_z~:2]] *)
2019-03-26 01:20:17 +01:00
let of_z x =
if Z.numbits x < 64 then One (Z.to_int x) else Many (Many.of_z x)
2020-12-26 01:47:55 +01:00
(* ~of_z~:2 ends here *)
2019-03-25 19:28:38 +01:00
2020-12-26 01:47:55 +01:00
(* [[file:../bitstring.org::*~zero~][~zero~:2]] *)
2019-03-26 01:20:17 +01:00
let zero = function
| n when n < 64 -> One (One.zero)
| _ -> Many (Many.zero)
2020-12-26 01:47:55 +01:00
(* ~zero~:2 ends here *)
2019-03-25 19:28:38 +01:00
2020-12-26 01:47:55 +01:00
(* [[file:../bitstring.org::*~numbits~][~numbits~:2]] *)
2019-03-26 01:20:17 +01:00
let numbits = function
| One x -> One.numbits x
| Many x -> Many.numbits x
2020-12-26 01:47:55 +01:00
(* ~numbits~:2 ends here *)
2019-03-25 19:28:38 +01:00
2020-12-26 01:47:55 +01:00
(* [[file:../bitstring.org::*~is_zero~][~is_zero~:2]] *)
2019-03-25 19:28:38 +01:00
let is_zero = function
2020-12-26 01:47:55 +01:00
| One x -> One.is_zero x
| Many x -> Many.is_zero x
(* ~is_zero~:2 ends here *)
2019-03-25 19:28:38 +01:00
2020-12-26 01:47:55 +01:00
(* [[file:../bitstring.org::*~neg~][~neg~:2]] *)
2019-12-03 12:25:31 +01:00
let neg = function
2020-12-26 01:47:55 +01:00
| One x -> One (One.neg x)
| Many x -> Many (Many.neg x)
(* ~neg~:2 ends here *)
2019-12-03 12:25:31 +01:00
2020-12-26 01:47:55 +01:00
(* [[file:../bitstring.org::*~shift_left~][~shift_left~:2]] *)
2019-03-26 01:20:17 +01:00
let shift_left x i = match x with
| One x -> One (One.shift_left x i)
| Many x -> Many (Many.shift_left x i)
2020-12-26 01:47:55 +01:00
(* ~shift_left~:2 ends here *)
2019-03-25 19:28:38 +01:00
2020-12-26 01:47:55 +01:00
(* [[file:../bitstring.org::*~shift_right~][~shift_right~:2]] *)
2019-03-26 01:20:17 +01:00
let shift_right x i = match x with
| One x -> One (One.shift_right x i)
| Many x -> Many (Many.shift_right x i)
2020-12-26 01:47:55 +01:00
(* ~shift_right~:2 ends here *)
2019-03-25 19:28:38 +01:00
2020-12-26 01:47:55 +01:00
(* [[file:../bitstring.org::*~shift_left_one~][~shift_left_one~:2]] *)
2019-03-26 01:20:17 +01:00
let shift_left_one = function
| n when n < 64 -> fun i -> One (One.shift_left_one i)
| _ -> fun i -> Many (Many.shift_left_one i)
2020-12-26 01:47:55 +01:00
(* ~shift_left_one~:2 ends here *)
2019-03-25 19:28:38 +01:00
2020-12-26 01:47:55 +01:00
(* [[file:../bitstring.org::*~testbit~][~testbit~:2]] *)
2019-03-26 01:20:17 +01:00
let testbit = function
| One x -> One.testbit x
| Many x -> Many.testbit x
2020-12-26 01:47:55 +01:00
(* ~testbit~:2 ends here *)
2019-03-25 19:28:38 +01:00
2020-12-26 01:47:55 +01:00
(* [[file:../bitstring.org::*~logor~][~logor~:2]] *)
2019-03-25 19:28:38 +01:00
let logor a b =
match a,b with
2019-03-26 01:20:17 +01:00
| One a, One b -> One (One.logor a b)
| Many a, Many b -> Many (Many.logor a b)
| _ -> invalid_arg "Bitstring.logor"
2020-12-26 01:47:55 +01:00
(* ~logor~:2 ends here *)
2019-03-25 19:28:38 +01:00
2020-12-26 01:47:55 +01:00
(* [[file:../bitstring.org::*~logxor~][~logxor~:2]] *)
2019-03-25 19:28:38 +01:00
let logxor a b =
match a,b with
2019-03-26 01:20:17 +01:00
| One a, One b -> One (One.logxor a b)
| Many a, Many b -> Many (Many.logxor a b)
| _ -> invalid_arg "Bitstring.logxor"
2020-12-26 01:47:55 +01:00
(* ~logxor~:2 ends here *)
2019-03-25 19:28:38 +01:00
2020-12-26 01:47:55 +01:00
(* [[file:../bitstring.org::*~logand~][~logand~:2]] *)
2019-03-25 19:28:38 +01:00
let logand a b =
match a,b with
2019-03-26 01:20:17 +01:00
| One a, One b -> One (One.logand a b)
| Many a, Many b -> Many (Many.logand a b)
| _ -> invalid_arg "Bitstring.logand"
2020-12-26 01:47:55 +01:00
(* ~logand~:2 ends here *)
2019-03-25 19:28:38 +01:00
2020-12-26 01:47:55 +01:00
(* [[file:../bitstring.org::*~lognot~][~lognot~:2]] *)
2019-03-25 19:28:38 +01:00
let lognot = function
2019-03-26 01:20:17 +01:00
| One x -> One (One.lognot x)
| Many x -> Many (Many.lognot x)
2020-12-26 01:47:55 +01:00
(* ~lognot~:2 ends here *)
2019-03-25 19:28:38 +01:00
2020-12-26 01:47:55 +01:00
(* [[file:../bitstring.org::*~minus_one~][~minus_one~:2]] *)
2019-03-25 19:28:38 +01:00
let minus_one = function
2019-03-26 01:20:17 +01:00
| One x -> One (One.minus_one x)
| Many x -> Many (Many.minus_one x)
2020-12-26 01:47:55 +01:00
(* ~minus_one~:2 ends here *)
2019-03-25 19:28:38 +01:00
2020-12-26 01:47:55 +01:00
(* [[file:../bitstring.org::*~plus_one~][~plus_one~:2]] *)
2019-03-25 19:28:38 +01:00
let plus_one = function
2019-03-26 01:20:17 +01:00
| One x -> One (One.plus_one x)
| Many x -> Many (Many.plus_one x)
2020-12-26 01:47:55 +01:00
(* ~plus_one~:2 ends here *)
2019-03-25 19:28:38 +01:00
2020-12-26 01:47:55 +01:00
(* [[file:../bitstring.org::*~trailing_zeros~][~trailing_zeros~:2]] *)
2019-03-25 19:28:38 +01:00
let trailing_zeros = function
2019-03-26 01:20:17 +01:00
| One x -> One.trailing_zeros x
| Many x -> Many.trailing_zeros x
2020-12-26 01:47:55 +01:00
(* ~trailing_zeros~:2 ends here *)
2019-03-25 19:28:38 +01:00
2020-12-26 01:47:55 +01:00
(* [[file:../bitstring.org::*~hamdist~][~hamdist~:2]] *)
2019-03-26 01:20:17 +01:00
let hamdist a b = match a, b with
| One a, One b -> One.hamdist a b
| Many a, Many b -> Many.hamdist a b
| _ -> invalid_arg "Bitstring.hamdist"
2020-12-26 01:47:55 +01:00
(* ~hamdist~:2 ends here *)
2019-03-25 19:28:38 +01:00
2020-12-26 01:47:55 +01:00
(* [[file:../bitstring.org::*~popcount~][~popcount~:2]] *)
2019-03-26 01:20:17 +01:00
let popcount = function
| One x -> One.popcount x
| Many x -> Many.popcount x
2020-12-26 01:47:55 +01:00
(* ~popcount~:2 ends here *)
2019-03-26 01:20:17 +01:00
2020-12-26 01:47:55 +01:00
(* [[file:../bitstring.org::*~to_list~][~to_list~:2]] *)
2019-03-26 01:20:17 +01:00
let rec to_list ?(accu=[]) = function
| t when (is_zero t) -> List.rev accu
2019-03-25 19:28:38 +01:00
| t -> let newlist =
(trailing_zeros t + 1)::accu
in
2019-09-10 18:39:14 +02:00
logand t @@ minus_one t
|> (to_list [@tailcall]) ~accu:newlist
2020-12-26 01:47:55 +01:00
(* ~to_list~:2 ends here *)
2019-03-25 19:28:38 +01:00
2020-12-26 01:54:39 +01:00
(* [[file:../bitstring.org::*~permutations~][~permutations~:2]] *)
let permutations m n =
2019-03-25 19:28:38 +01:00
let rec aux k u rest =
if k=1 then
List.rev (u :: rest)
else
2019-12-03 12:25:31 +01:00
let t = logor u @@ minus_one u in
2019-03-25 19:28:38 +01:00
let t' = plus_one t in
2019-12-03 12:25:31 +01:00
let not_t = lognot t in
let neg_not_t = neg not_t in
let t'' = shift_right (minus_one @@ logand not_t neg_not_t) (trailing_zeros u + 1) in
(*
2019-03-25 19:28:38 +01:00
let t'' = shift_right (minus_one (logand (lognot t) t')) (trailing_zeros u + 1) in
2019-12-03 12:25:31 +01:00
*)
2019-09-10 18:39:14 +02:00
(aux [@tailcall]) (k-1) (logor t' t'') (u :: rest)
2019-03-25 19:28:38 +01:00
in
2019-03-26 18:02:22 +01:00
aux (Util.binom n m) (minus_one (shift_left_one n m)) []
2020-12-26 01:54:39 +01:00
(* ~permutations~:2 ends here *)
2019-03-25 19:28:38 +01:00
2020-12-26 01:47:55 +01:00
(* [[file:../bitstring.org::*Printers][Printers:2]] *)
let pp ppf = function
| One x -> One.pp ppf x
| Many x -> Many.pp ppf x
(* Printers:2 ends here *)