Debian Codegraph @

src/Util/ZUtil/Definitions.v

raw
1 From Coq Require Import ZArith.
2 Require Import Crypto.Util.Decidable.
3 Require Import Crypto.Util.ZUtil.Notations.
4 Require Import Crypto.Util.LetIn.
5 Local Open Scope Z_scope.
6
7 Module Z.
8 Definition divideb (x y : Z) : bool
9 := if x =? 0
10 then y =? 0
11 else y mod x =? 0.
12
13 Definition pow2_mod n i := (n &' (Z.ones i)).
14
15 Definition zselect (cond zero_case nonzero_case : Z) :=
16 if cond =? 0 then zero_case else nonzero_case.
17
18 Definition add_modulo x y modulus :=
19 if (modulus <=? x + y) then (x + y) - modulus else (x + y).
20
21 (** Logical negation, modulo a number *)
22 Definition lnot_modulo (v : Z) (modulus : Z) : Z
23 := Z.lnot v mod modulus.
24
25 (** Boolean negation *)
26 Definition bneg (v : Z) : Z
27 := if dec (v = 0) then 1 else 0.
28
29 (* most significant bit *)
30 Definition cc_m s x := if dec (2 ^ (Z.log2 s) = s) then x >> (Z.log2 s - 1) else x / (s / 2).
31
32 (* least significant bit *)
33 Definition cc_l x := x mod 2.
34
35 (* two-register right shift *)
36 Definition rshi s hi lo n :=
37 let k := Z.log2 s in
38 if dec (2 ^ k = s)
39 then ((lo + (hi << k)) >> n) &' (Z.ones k)
40 else ((lo + hi * s) >> n) mod s.
41
42 (** left-shift that truncates *)
43 Definition truncating_shiftl bw x n := (x << n) mod (2^bw).
44
45 Definition get_carry (bitwidth : Z) (v : Z) : Z * Z
46 := (v mod 2^bitwidth, v / 2^bitwidth).
47 Definition add_with_carry (c : Z) (x y : Z) : Z
48 := c + x + y.
49 Definition add_with_get_carry (bitwidth : Z) (c : Z) (x y : Z) : Z * Z
50 := dlet v := add_with_carry c x y in get_carry bitwidth v.
51 Definition add_get_carry (bitwidth : Z) (x y : Z) : Z * Z
52 := add_with_get_carry bitwidth 0 x y.
53
54 Definition get_borrow (bitwidth : Z) (v : Z) : Z * Z
55 := let '(v, c) := get_carry bitwidth v in
56 (v, -c).
57 Definition sub_with_borrow (c : Z) (x y : Z) : Z
58 := add_with_carry (-c) x (-y).
59 Definition sub_with_get_borrow (bitwidth : Z) (c : Z) (x y : Z) : Z * Z
60 := dlet v := sub_with_borrow c x y in get_borrow bitwidth v.
61 Definition sub_get_borrow (bitwidth : Z) (x y : Z) : Z * Z
62 := sub_with_get_borrow bitwidth 0 x y.
63
64 (* splits at [bound], not [2^bitwidth]; wrapper to make add_getcarry
65 work if input is not known to be a power of 2 *)
66 Definition add_get_carry_full (bound : Z) (x y : Z) : Z * Z
67 := if 2 ^ (Z.log2 bound) =? bound
68 then add_get_carry (Z.log2 bound) x y
69 else ((x + y) mod bound, (x + y) / bound).
70 Definition add_with_get_carry_full (bound : Z) (c x y : Z) : Z * Z
71 := if 2 ^ (Z.log2 bound) =? bound
72 then add_with_get_carry (Z.log2 bound) c x y
73 else ((c + x + y) mod bound, (c + x + y) / bound).
74 Definition sub_get_borrow_full (bound : Z) (x y : Z) : Z * Z
75 := if 2 ^ (Z.log2 bound) =? bound
76 then sub_get_borrow (Z.log2 bound) x y
77 else ((x - y) mod bound, -((x - y) / bound)).
78 Definition sub_with_get_borrow_full (bound : Z) (c x y : Z) : Z * Z
79 := if 2 ^ (Z.log2 bound) =? bound
80 then sub_with_get_borrow (Z.log2 bound) c x y
81 else ((x - y - c) mod bound, -((x - y - c) / bound)).
82
83 Definition add_split (s x y : Z) : Z * Z
84 := dlet sum := Z.add x y in (sum mod s, sum / s).
85
86 Definition mul_split_at_bitwidth (bitwidth : Z) (x y : Z) : Z * Z
87 := dlet xy := x * y in
88 (if Z.geb bitwidth 0
89 then xy &' Z.ones bitwidth
90 else xy mod 2^bitwidth,
91 if Z.geb bitwidth 0
92 then xy >> bitwidth
93 else xy / 2^bitwidth).
94 Definition mul_split (s x y : Z) : Z * Z
95 := if s =? 2^Z.log2 s
96 then mul_split_at_bitwidth (Z.log2 s) x y
97 else ((x * y) mod s, (x * y) / s).
98
99 Definition mul_high (s x y : Z) : Z
100 := snd (mul_split s x y).
101
102 (** returns [1] iff [x < y] *)
103 Definition ltz (x y : Z) : Z
104 := if x <? y then 1 else 0.
105
106 Definition combine_at_bitwidth (bitwidth lo hi : Z) : Z
107 := lo + (hi << bitwidth).
108
109 (** if positive, round up to 2^k-1 (0b11111....); if negative, round down to -2^k (0b...111000000...) *)
110 Definition round_lor_land_bound (x : Z) : Z
111 := if (0 <=? x)%Z
112 then 2^(Z.log2_up (x+1))-1
113 else -2^(Z.log2_up (-x)).
114
115 Fixpoint log10_fuel (fuel : nat) (v : Z) :=
116 match fuel with
117 | O => 0
118 | S fuel
119 => if v >? 1
120 then 1 + log10_fuel fuel (v / 10)
121 else 0
122 end.
123 Definition log10 (v : Z) : Z := log10_fuel (Z.to_nat (Z.log2 v)) v.
124
125 (** Special identity function for constant-time cmov *)
126 Definition value_barrier (x : Z) := x.
127
128 (* arithmetic right shift *)
129 Definition arithmetic_shiftr1 (m a : Z) :=
130 (a &' 2^(m - 1)) |' (a >> 1).
131
132 Definition sign_bit m a := a >> (m - 1).
133
134 Definition ones_from m k := (Z.ones k) << (m - k).
135 Definition ones_at m k := (Z.ones k) << m.
136
137 Definition sign_extend old_m new_m a :=
138 dlet q := Z.zselect (sign_bit old_m a) 0 (ones_at old_m (new_m - old_m)) in
139 q |' a.
140
141 Definition arithmetic_shiftr m a k :=
142 dlet q := Z.zselect (sign_bit m a) 0 (ones_from m k) in
143 q |' (a >> k).
144
145 (** Note that the following definition may be inconvenient to reason about,
146 and [(a + 2^(m-1)) mod 2^m - 2^(m-1)] may prove simpler to reason about arithmetically.
147 See also https://github.com/mit-plv/coqutil/blob/c8006ceca816076b117c31d7feaefb5bbb850754/src/coqutil/Word/Naive.v#L15
148 and https://github.com/mit-plv/coqutil/blob/c8006ceca816076b117c31d7feaefb5bbb850754/src/coqutil/Word/Properties.v#L190 *)
149
150 Definition twos_complement m a :=
151 (if ((a mod 2 ^ m) <? 2 ^ (m - 1)) then a mod 2 ^ m else a mod 2 ^ m - 2 ^ m).
152
153 (* Negation in twos complement *)
154 Definition twos_complement_opp m a :=
155 ((Z.lnot_modulo a (2 ^ m)) + 1) mod (2 ^ m).
156
157 (* Check if a number considered in twos complement of bitwidth m is negative *)
158 Definition twos_complement_neg m a := a >> (m - 1).
159
160 (* note the corner case condition: when f is exactly 2 to the mw-1'th power, then -f = f and
161 so checking that -f is negative does not work in that case.
162 This is not really an issue, since it just requires that our integers are small (which they are)
163 Long term, we would like to add comparison operators to the supported C language *)
164 Definition twos_complement_pos m a :=
165 dlet b := twos_complement_opp m a in sign_bit m b.
166
167 Definition twos_complement_mul ma mb a b :=
168 (sign_extend ma (ma + mb) a) * (sign_extend mb (ma + mb) b).
169
170 Definition signed (s : N) (n : Z) : Z :=
171 Z.land (Z.shiftl 1 (Z.of_N s-1) + n) (Z.ones (Z.of_N s)) - Z.shiftl 1 (Z.of_N s-1).
172 End Z.
173
174 Module Export Notations.
175 Notation "( x | ? y )" := (Z.divideb x y) : Z_scope.
176 End Notations.