package rfsm

  1. Overview
  2. Docs
A toolset for describing and simulating StateChart-like state diagrams

Install

dune-project
 Dependency

Authors

Maintainers

Sources

2.2.tar.gz
md5=ea1b496f0aa758933ae23921ee55a531
sha512=4fa72747bb2e32f91d64e4b8c24f60d6f0bdad297cc40f36d5c687ed1de900ab8441fa8a12aecf2523928833cddd5391fa87c11a1af2162ac8001467e8f485a5

doc/src/rfsm/bits.ml.html

Source file bits.ml

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
35
36
37
38
(**********************************************************************)
(*                                                                    *)
(*              This file is part of the RFSM package                 *)
(*                                                                    *)
(*  Copyright (c) 2018-present, Jocelyn SEROT.  All rights reserved.  *)
(*                                                                    *)
(*  This source code is licensed under the license found in the       *)
(*  LICENSE file in the root directory of this source tree.           *)
(*                                                                    *)
(**********************************************************************)

(**{1 Bit-level manipulation of integers} *)

let of_uint s n = 
  let b = Bytes.make s '0' in
  let rec h n i =
    if i >= 0 then begin
      Bytes.set b i (if n mod 2 = 1 then '1' else '0');
      h (n/2) (i-1)
      end in
  h n (s-1);
  Bytes.to_string b

let cpl2 n x = Ext.Base.pow2 n - x

let of_int s v = 
  if v < 0 
  then of_uint s (cpl2 s (-v))
  else of_uint s v

let get_bits ~hi ~lo n = (n lsr lo) mod (1 lsl (hi-lo+1))

let set_bits ~hi ~lo ~dst v =
  let v' = v mod (1 lsl (hi-lo+1)) in
  let msk = let r = ref 0 in for i=lo to hi do r := !r lor (1 lsl i) done; !r in
  (dst land (lnot msk)) lor (v' lsl lo)

let rec bit_size n = if n=0 then 0 else 1 + bit_size (n/2)