| 1 |
(** Definitions for use in pre-reified rewriter rules *) |
| 2 |
From Coq Require Import BinInt. |
| 3 |
Require Import Crypto.Util.ZRange. |
| 4 |
Require Import Crypto.Util.ZRange.Operations. |
| 5 |
Require Import Crypto.Util.ZUtil.Definitions. |
| 6 |
Require Import Crypto.Util.ZUtil.Notations. |
| 7 |
Require Import Crypto.Util.Notations. |
| 8 |
Require Rewriter.Util.Bool. |
| 9 |
Require Rewriter.Util.Option. |
| 10 |
Require Rewriter.Util.Prod. |
| 11 |
Require Rewriter.Util.NatUtil. |
| 12 |
Require Rewriter.Util.ListUtil. |
| 13 |
Require Export Rewriter.Language.Pre. |
| 14 |
Require Crypto.Util.PrimitiveHList. |
| 15 |
Local Open Scope bool_scope. |
| 16 |
Local Open Scope Z_scope. |
| 17 |
|
| 18 |
Module ident. |
| 19 |
Section cast. |
| 20 |
Definition is_more_pos_than_neg (r : zrange) (v : BinInt.Z) : bool |
| 21 |
:= ((Z.abs (lower r) <? Z.abs (upper r)) (* if more of the range is above 0 than below 0 *) |
| 22 |
|| ((lower r =? upper r) && (0 <=? lower r)) |
| 23 |
|| ((Z.abs (lower r) =? Z.abs (upper r)) && (0 <=? v))). |
| 24 |
|
| 25 |
(** We ensure that [ident.cast] is symmetric under [Z.opp], as |
| 26 |
this makes some rewrite rules much, much easier to prove. *) |
| 27 |
(** XXX TODO: Come up with a good way of proving boundedness for |
| 28 |
the abstract interpreter, now that we no longer can even |
| 29 |
guarantee that we have the same behavior independent of |
| 30 |
overflow behavior. *) |
| 31 |
Let cast_outside_of_range (r : zrange) (v : BinInt.Z) : BinInt.Z |
| 32 |
:= ((v - lower r) mod (upper r - lower r + 1)) + lower r. |
| 33 |
|
| 34 |
Definition cast (r : zrange) (x : BinInt.Z) |
| 35 |
:= let r := ZRange.normalize r in |
| 36 |
if (lower r <=? x) && (x <=? upper r) |
| 37 |
then x |
| 38 |
else if is_more_pos_than_neg r x |
| 39 |
then cast_outside_of_range r x |
| 40 |
else -cast_outside_of_range (-r) (-x). |
| 41 |
Definition cast2 (r : zrange * zrange) (x : BinInt.Z * BinInt.Z) |
| 42 |
:= (cast (Datatypes.fst r) (Datatypes.fst x), |
| 43 |
cast (Datatypes.snd r) (Datatypes.snd x)). |
| 44 |
End cast. |
| 45 |
|
| 46 |
Section comment. |
| 47 |
Definition comment {A} (x : A) := tt. |
| 48 |
(** Version of [comment] which does not keep its arguments alive in the final output *) |
| 49 |
Definition comment_no_keep {A} (x : A) := tt. |
| 50 |
End comment. |
| 51 |
|
| 52 |
Module fancy. |
| 53 |
Module with_wordmax. |
| 54 |
Section with_wordmax. |
| 55 |
Context (log2wordmax : BinInt.Z). |
| 56 |
Let wordmax := 2 ^ log2wordmax. |
| 57 |
Let half_bits := log2wordmax / 2. |
| 58 |
Let wordmax_half_bits := 2 ^ half_bits. |
| 59 |
|
| 60 |
Local Notation low x := (Z.land x (wordmax_half_bits - 1)). |
| 61 |
Local Notation high x := (x >> half_bits). |
| 62 |
Local Notation shift x imm := ((x << imm) mod wordmax). |
| 63 |
|
| 64 |
Definition add (imm : Z) : Z * Z -> Z * Z |
| 65 |
:= fun x => Z.add_get_carry_full wordmax (fst x) (shift (snd x) imm). |
| 66 |
Definition addc (imm : Z) : Z * Z * Z -> Z * Z |
| 67 |
:= fun x => Z.add_with_get_carry_full wordmax (fst (fst x)) (snd (fst x)) (shift (snd x) imm). |
| 68 |
Definition sub (imm : Z) : Z * Z -> Z * Z |
| 69 |
:= fun x => Z.sub_get_borrow_full wordmax (fst x) (shift (snd x) imm). |
| 70 |
Definition subb (imm : Z) : Z * Z * Z -> Z * Z |
| 71 |
:= fun x => Z.sub_with_get_borrow_full wordmax (fst (fst x)) (snd (fst x)) (shift (snd x) imm). |
| 72 |
Definition mulll : Z * Z -> Z |
| 73 |
:= fun x => low (fst x) * low (snd x). |
| 74 |
Definition mullh : Z * Z -> Z |
| 75 |
:= fun x => low (fst x) * high (snd x). |
| 76 |
Definition mulhl : Z * Z -> Z |
| 77 |
:= fun x => high (fst x) * low (snd x). |
| 78 |
Definition mulhh : Z * Z -> Z |
| 79 |
:= fun x => high (fst x) * high (snd x). |
| 80 |
Definition selm : Z * Z * Z -> Z |
| 81 |
:= fun x => Z.zselect (Z.cc_m wordmax (fst (fst x))) (snd (fst x)) (snd x). |
| 82 |
Definition rshi (n : Z) : Z * Z -> Z |
| 83 |
:= fun x => Z.rshi wordmax (fst x) (snd x) n. |
| 84 |
End with_wordmax. |
| 85 |
End with_wordmax. |
| 86 |
|
| 87 |
|
| 88 |
Definition add : (Z * Z) * (Z * Z) -> Z * Z |
| 89 |
:= Eval cbv [with_wordmax.add] in fun x => with_wordmax.add (fst (fst x)) (snd (fst x)) (snd x). |
| 90 |
Definition addc : (Z * Z) * (Z * Z * Z) -> Z * Z |
| 91 |
:= Eval cbv [with_wordmax.addc] in fun x => with_wordmax.addc (fst (fst x)) (snd (fst x)) (snd x). |
| 92 |
Definition sub : (Z * Z) * (Z * Z) -> Z * Z |
| 93 |
:= Eval cbv [with_wordmax.sub] in fun x => with_wordmax.sub (fst (fst x)) (snd (fst x)) (snd x). |
| 94 |
Definition subb : (Z * Z) * (Z * Z * Z) -> Z * Z |
| 95 |
:= Eval cbv [with_wordmax.subb] in fun x => with_wordmax.subb (fst (fst x)) (snd (fst x)) (snd x). |
| 96 |
Definition mulll : Z * (Z * Z) -> Z |
| 97 |
:= Eval cbv [with_wordmax.mulll] in fun x => with_wordmax.mulll (fst x) (snd x). |
| 98 |
Definition mullh : Z * (Z * Z) -> Z |
| 99 |
:= Eval cbv [with_wordmax.mullh] in fun x => with_wordmax.mullh (fst x) (snd x). |
| 100 |
Definition mulhl : Z * (Z * Z) -> Z |
| 101 |
:= Eval cbv [with_wordmax.mulhl] in fun x => with_wordmax.mulhl (fst x) (snd x). |
| 102 |
Definition mulhh : Z * (Z * Z) -> Z |
| 103 |
:= Eval cbv [with_wordmax.mulhh] in fun x => with_wordmax.mulhh (fst x) (snd x). |
| 104 |
Definition selm : Z * (Z * Z * Z) -> Z |
| 105 |
:= Eval cbv [with_wordmax.selm] in fun x => with_wordmax.selm (fst x) (snd x). |
| 106 |
Definition rshi : (Z * Z) * (Z * Z) -> Z |
| 107 |
:= Eval cbv [with_wordmax.rshi] in fun x => with_wordmax.rshi (fst (fst x)) (snd (fst x)) (snd x). |
| 108 |
|
| 109 |
Definition selc : Z * Z * Z -> Z |
| 110 |
:= fun x => Z.zselect (fst (fst x)) (snd (fst x)) (snd x). |
| 111 |
Definition sell : Z * Z * Z -> Z |
| 112 |
:= fun x => Z.zselect (Z.land (fst (fst x)) 1) (snd (fst x)) (snd x). |
| 113 |
Definition addm : Z * Z * Z -> Z |
| 114 |
:= fun x => Z.add_modulo (fst (fst x)) (snd (fst x)) (snd x). |
| 115 |
|
| 116 |
Declare Reduction cbv_fancy := cbv [ident.fancy.add ident.fancy.addc ident.fancy.addm ident.fancy.mulhh ident.fancy.mulhl ident.fancy.mullh ident.fancy.mulll ident.fancy.rshi ident.fancy.selc ident.fancy.sell ident.fancy.selm ident.fancy.sub ident.fancy.subb]. |
| 117 |
Ltac cbv_fancy := cbv [ident.fancy.add ident.fancy.addc ident.fancy.addm ident.fancy.mulhh ident.fancy.mulhl ident.fancy.mullh ident.fancy.mulll ident.fancy.rshi ident.fancy.selc ident.fancy.sell ident.fancy.selm ident.fancy.sub ident.fancy.subb]. |
| 118 |
Ltac cbv_fancy_in_all := cbv [ident.fancy.add ident.fancy.addc ident.fancy.addm ident.fancy.mulhh ident.fancy.mulhl ident.fancy.mullh ident.fancy.mulll ident.fancy.rshi ident.fancy.selc ident.fancy.sell ident.fancy.selm ident.fancy.sub ident.fancy.subb] in *. |
| 119 |
End fancy. |
| 120 |
End ident. |
| 121 |
|
| 122 |
Global Opaque ident.cast. (* This should never be unfolded except in [Language.Wf] *) |
| 123 |
Global Opaque ident.comment. |
| 124 |
Global Opaque ident.comment_no_keep. |
| 125 |
|
| 126 |
Notation prod_rect_nodep := Rewriter.Util.Prod.prod_rect_nodep (only parsing). |
| 127 |
Notation nat_rect_arrow_nodep := Rewriter.Util.NatUtil.nat_rect_arrow_nodep (only parsing). |
| 128 |
Notation list_rect_arrow_nodep := Rewriter.Util.ListUtil.list_rect_arrow_nodep (only parsing). |
| 129 |
Notation bool_rect_nodep := Rewriter.Util.Bool.bool_rect_nodep (only parsing). |
| 130 |
|
| 131 |
Module Thunked. |
| 132 |
Notation bool_rect := Rewriter.Util.Bool.Thunked.bool_rect (only parsing). |
| 133 |
Notation list_rect := Rewriter.Util.ListUtil.Thunked.list_rect (only parsing). |
| 134 |
Notation list_case := Rewriter.Util.ListUtil.Thunked.list_case (only parsing). |
| 135 |
Notation nat_rect := Rewriter.Util.NatUtil.Thunked.nat_rect (only parsing). |
| 136 |
Notation option_rect := Rewriter.Util.Option.Thunked.option_rect (only parsing). |
| 137 |
End Thunked. |
| 138 |
|
| 139 |
Definition nat_rect_fbb_b {A B C} := |
| 140 |
@Coq.Init.Datatypes.nat_rect (fun _ => (A -> B) -> C). |
| 141 |
Definition nat_rect_fbb_b_b {A B C D} := |
| 142 |
@Coq.Init.Datatypes.nat_rect (fun _ => (A -> B) -> C -> D). |
| 143 |
|
| 144 |
Definition list_rect_fbb_b {T A B C} := |
| 145 |
@Coq.Init.Datatypes.list_rect T (fun _ => (A -> B) -> C). |
| 146 |
Definition list_rect_fbb_b_b {T A B C D} := |
| 147 |
@Coq.Init.Datatypes.list_rect T (fun _ => (A -> B) -> C -> D). |
| 148 |
Definition list_rect_fbb_b_b_b {T A B C D E} := |
| 149 |
@Coq.Init.Datatypes.list_rect T (fun _ => (A -> B) -> C -> D -> E). |
| 150 |
Definition list_rect_fbb_b_b_b_b {T A B C D E F} := |
| 151 |
@Coq.Init.Datatypes.list_rect T (fun _ => (A -> B) -> C -> D -> E -> F). |
| 152 |
Definition list_rect_fbb_b_b_b_b_b {T A B C D E F G} := |
| 153 |
@Coq.Init.Datatypes.list_rect T (fun _ => (A -> B) -> C -> D -> E -> F -> G). |
| 154 |
|
| 155 |
Lemma unfold1_nat_rect_fbb_b {A B C} fO fS n k : |
| 156 |
@nat_rect_fbb_b A B C fO fS n k = |
| 157 |
if Nat.eqb 0 n then fO k else fS (pred n) (nat_rect_fbb_b fO fS (pred n)) k. |
| 158 |
Proof. case n; trivial. Qed. |
| 159 |
|
| 160 |
Lemma unfold1_nat_rect_fbb_b_b {A B C D} fO fS n k x : |
| 161 |
@nat_rect_fbb_b_b A B C D fO fS n k x = |
| 162 |
if Nat.eqb 0 n then fO k x else fS (pred n) (nat_rect_fbb_b_b fO fS (pred n)) k x. |
| 163 |
Proof. case n; trivial. Qed. |
| 164 |
|
| 165 |
Lemma unfold1_list_rect_fbb_b {T A B C} fnil fcons l k : |
| 166 |
@list_rect_fbb_b T A B C fnil fcons l k = |
| 167 |
match l with |
| 168 |
| nil => fnil k |
| 169 |
| cons x l => fcons x l (list_rect_fbb_b fnil fcons l) k |
| 170 |
end. |
| 171 |
Proof. case l; trivial. Qed. |
| 172 |
|
| 173 |
Lemma unfold1_list_rect_fbb_b_b {T A B C D} fnil fcons l k c : |
| 174 |
@list_rect_fbb_b_b T A B C D fnil fcons l k c = |
| 175 |
match l with |
| 176 |
| nil => fnil k c |
| 177 |
| cons x l => fcons x l (list_rect_fbb_b_b fnil fcons l) k c |
| 178 |
end. |
| 179 |
Proof. case l; trivial. Qed. |
| 180 |
|
| 181 |
Lemma unfold1_list_rect_fbb_b_b_b {T A B C D E} fnil fcons l k c d : |
| 182 |
@list_rect_fbb_b_b_b T A B C D E fnil fcons l k c d = |
| 183 |
match l with |
| 184 |
| nil => fnil k c d |
| 185 |
| cons x l => fcons x l (list_rect_fbb_b_b_b fnil fcons l) k c d |
| 186 |
end. |
| 187 |
Proof. case l; trivial. Qed. |
| 188 |
|
| 189 |
Lemma unfold1_list_rect_fbb_b_b_b_b {T A B C D E F} fnil fcons l k c d e : |
| 190 |
@list_rect_fbb_b_b_b_b T A B C D E F fnil fcons l k c d e = |
| 191 |
match l with |
| 192 |
| nil => fnil k c d e |
| 193 |
| cons x l => fcons x l (list_rect_fbb_b_b_b_b fnil fcons l) k c d e |
| 194 |
end. |
| 195 |
Proof. case l; trivial. Qed. |
| 196 |
|
| 197 |
Lemma unfold1_list_rect_fbb_b_b_b_b_b {T A B C D E F G} fnil fcons l k c d e f : |
| 198 |
@list_rect_fbb_b_b_b_b_b T A B C D E F G fnil fcons l k c d e f = |
| 199 |
match l with |
| 200 |
| nil => fnil k c d e f |
| 201 |
| cons x l => fcons x l (list_rect_fbb_b_b_b_b_b fnil fcons l) k c d e f |
| 202 |
end. |
| 203 |
Proof. case l; trivial. Qed. |
| 204 |
|
| 205 |
Import Coq.Classes.Morphisms. |
| 206 |
|
| 207 |
Global Instance Proper_nat_rect_fbb_b {A B C} : |
| 208 |
Proper (((eq ==> eq) ==> eq) ==> (eq ==> ((eq ==> eq) ==> eq) ==> (eq ==> eq) ==> eq) ==> eq ==> (eq ==> eq) ==> eq) (@nat_rect_fbb_b A B C) | 10. |
| 209 |
Proof. cbv -[nat_rect]; intros ??? ??? n m ?; subst m. induction n; cbn; eauto. Qed. |