Debian Codegraph @

src/PushButtonSynthesis/InvertHighLow.v

raw
1 (** * Push-Button Synthesis fancy argument definitions *)
2 From Coq Require Import ZArith.
3 From Coq Require Import List.
4 Local Open Scope Z_scope.
5
6 Section with_wordmax.
7 Context (log2wordmax : Z) (consts : list Z).
8 Let wordmax := 2 ^ log2wordmax.
9 Let half_bits := log2wordmax / 2.
10 Let wordmax_half_bits := 2 ^ half_bits.
11
12 Inductive kind_of_constant := upper_half (c : BinInt.Z) | lower_half (c : BinInt.Z).
13
14 Definition constant_to_scalar_single (const x : BinInt.Z) : option kind_of_constant :=
15 if x =? (BinInt.Z.shiftr const half_bits)
16 then Some (upper_half const)
17 else if x =? (BinInt.Z.land const (wordmax_half_bits - 1))
18 then Some (lower_half const)
19 else None.
20
21 Definition constant_to_scalar (x : BinInt.Z)
22 : option kind_of_constant :=
23 fold_right (fun c res => match res with
24 | Some s => Some s
25 | None => constant_to_scalar_single c x
26 end) None consts.
27
28 Definition invert_low (v : BinInt.Z) : option BinInt.Z
29 := match constant_to_scalar v with
30 | Some (lower_half v) => Some v
31 | _ => None
32 end.
33
34 Definition invert_high (v : BinInt.Z) : option BinInt.Z
35 := match constant_to_scalar v with
36 | Some (upper_half v) => Some v
37 | _ => None
38 end.
39 End with_wordmax.