Debian Codegraph @

theories/Corelib/Numbers/Cyclic/Int63/Uint63Axioms.v

raw
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10 (* Benjamin Gregoire, Laurent Thery, INRIA, 2007 *)
11 (************************************************************************)
12
13 From Corelib Require Import BinNums PosDef IntDef.
14 From Corelib Require Export PrimInt63.
15
16 Local Open Scope Z_scope.
17
18 Local Notation "0" := Z0 : Z_scope.
19 Local Notation "1" := (Zpos 1) : Z_scope.
20 Local Notation "2" := (Zpos 2) : Z_scope.
21 Local Infix "+" := Z.add : Z_scope.
22 Local Infix "-" := Z.sub : Z_scope.
23 Local Infix "*" := Z.mul : Z_scope.
24 Local Infix "^" := Z.pow : Z_scope.
25 Local Infix "<=" := Z.le : Z_scope.
26 Local Infix "<" := Z.lt : Z_scope.
27 Local Notation "x <= y < z" := (x <= y /\ y < z) : Z_scope.
28
29 Definition size := 63%nat.
30
31 (** The number of digits as an int *)
32 Definition digits := 63%uint63.
33
34 (** The biggest int *)
35 Definition max_int := Eval vm_compute in sub 0 1.
36
37 (** Access to the nth digits *)
38 Definition get_digit x p := ltb 0 (land x (lsl 1 p)).
39
40 Definition set_digit x p (b:bool) :=
41 if if leb 0 p then ltb p digits else false then
42 if b then lor x (lsl 1 p)
43 else land x (lxor max_int (lsl 1 p))
44 else x.
45
46 (** Translation to Z *)
47 Definition is_zero (i:int) := eqb i 0.
48 Definition is_even (i:int) := is_zero (land i 1).
49 Fixpoint to_Z_rec (n:nat) (i:int) :=
50 match n with
51 | O => 0
52 | S n =>
53 (if is_even i then Z.double else Z.succ_double) (to_Z_rec n (lsr i 1))
54 end.
55
56 Definition to_Z := to_Z_rec size.
57
58 Fixpoint of_pos_rec (n:nat) (p:positive) {struct p} :=
59 match n, p with
60 | O, _ => 0%uint63
61 | S n, xH => 1%uint63
62 | S n, xO p => lsl (of_pos_rec n p) 1
63 | S n, xI p => lor (lsl (of_pos_rec n p) 1) 1
64 end.
65
66 Definition of_pos := of_pos_rec size.
67
68 Definition of_Z z :=
69 match z with
70 | Zpos p => of_pos p
71 | 0 => 0%uint63
72 | Zneg p => sub 0 (of_pos p)
73 end.
74
75 Definition wB := 2 ^ (Z.of_nat size).
76
77 (* Bijection : uint63 <-> Bvector size *)
78
79 Axiom of_to_Z : forall x, of_Z (to_Z x) = x.
80
81 (** Specification of logical operations *)
82
83 Axiom lsl_spec : forall x p, to_Z (lsl x p) = Z.modulo (to_Z x * 2 ^ to_Z p) wB.
84
85 Axiom lsr_spec : forall x p, to_Z (lsr x p) = Z.div (to_Z x) (2 ^ to_Z p).
86
87 Definition bit i n := negb (is_zero (lsl (lsr i n) (sub digits 1))).
88
89 Axiom land_spec : forall x y i, bit (land x y) i = andb (bit x i) (bit y i).
90
91 Axiom lor_spec : forall x y i, bit (lor x y) i = orb (bit x i) (bit y i).
92
93 Axiom lxor_spec : forall x y i, bit (lxor x y) i = xorb (bit x i) (bit y i).
94
95 (** Specification of basic opetations *)
96
97 (* Arithmetic modulo operations *)
98
99 (* Note: axioms would be simpler if we were using of_Z instead:
100 example: add_spec : forall x y, of_Z (x + y) = of_Z x + of_Z y. *)
101
102 Axiom add_spec : forall x y, to_Z (add x y) = Z.modulo (to_Z x + to_Z y) wB.
103
104 Axiom sub_spec : forall x y, to_Z (sub x y) = Z.modulo (to_Z x - to_Z y) wB.
105
106 Axiom mul_spec : forall x y, to_Z (mul x y) = Z.modulo (to_Z x * to_Z y) wB.
107
108 Axiom mulc_spec : forall x y,
109 to_Z x * to_Z y = to_Z (fst (mulc x y)) * wB + to_Z (snd (mulc x y)).
110
111 Axiom div_spec : forall x y, to_Z (div x y) = Z.div (to_Z x) (to_Z y).
112
113 Axiom mod_spec : forall x y,
114 to_Z (PrimInt63.mod x y) = Z.modulo (to_Z x) (to_Z y).
115
116 (* Comparisons *)
117 Axiom eqb_correct : forall i j, eqb i j = true -> i = j.
118
119 Axiom eqb_refl : forall x, eqb x x = true.
120
121 Axiom ltb_spec : forall x y, ltb x y = true <-> to_Z x < to_Z y.
122
123 Axiom leb_spec : forall x y, leb x y = true <-> to_Z x <= to_Z y.
124
125 (** Exotic operations *)
126
127 (** Axioms on operations which are just short cut *)
128
129 Definition compare_def x y :=
130 if ltb x y then Lt else if eqb x y then Eq else Gt.
131
132 Axiom compare_def_spec : forall x y, compare x y = compare_def x y.
133
134 Axiom head0_spec : forall x, 0 < to_Z x ->
135 Z.div wB 2 <= 2 ^ (to_Z (head0 x)) * to_Z x < wB.
136
137 Axiom tail0_spec : forall x, 0 < to_Z x ->
138 exists y, Z0 <= y /\ to_Z x = (2 * y + 1) * (2 ^ to_Z (tail0 x)).
139
140 Definition addc_def x y :=
141 let r := add x y in
142 if ltb r x then C1 r else C0 r.
143
144 Axiom addc_def_spec : forall x y, addc x y = addc_def x y.
145
146 Definition addcarry i j := add (add i j) 1.
147 Definition addcarryc_def x y :=
148 let r := addcarry x y in
149 if leb r x then C1 r else C0 r.
150
151 Axiom addcarryc_def_spec : forall x y, addcarryc x y = addcarryc_def x y.
152
153 Definition subc_def x y := if leb y x then C0 (sub x y) else C1 (sub x y).
154
155 Axiom subc_def_spec : forall x y, subc x y = subc_def x y.
156
157 Definition subcarryc_def x y :=
158 if ltb y x then C0 (sub (sub x y) 1) else C1 (sub (sub x y) 1).
159
160 Axiom subcarryc_def_spec : forall x y, subcarryc x y = subcarryc_def x y.
161
162 Definition diveucl_def x y := (div x y, PrimInt63.mod x y).
163
164 Axiom diveucl_def_spec : forall x y, diveucl x y = diveucl_def x y.
165
166 Axiom diveucl_21_spec : forall a1 a2 b,
167 let (q,r) := diveucl_21 a1 a2 b in
168 let (q',r') := Z.div_eucl (to_Z a1 * wB + to_Z a2) (to_Z b) in
169 to_Z a1 < to_Z b -> to_Z q = q' /\ to_Z r = r'.
170
171 Definition addmuldiv_def p x y :=
172 lor (lsl x p) (lsr y (sub digits p)).
173
174 Axiom addmuldiv_def_spec : forall p x y,
175 addmuldiv p x y = addmuldiv_def p x y.