Debian Codegraph @

kernel/uint63_63.ml

raw symbols SCIP
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
11 type t = int
12
13 let _ = assert (Sys.word_size = 64)
14
15 let uint_size = 63
16
17 let maxuint63 = Int64.of_string "0x7FFFFFFFFFFFFFFF"
18 let maxuint31 = 0x7FFFFFFF
19
20 (* conversion from an int *)
21 let to_uint64 i = Int64.logand (Int64.of_int i) maxuint63
22
23 let of_int i = i
24 [@@ocaml.inline always]
25
26 let to_int2 i = (0,i)
27
28 let of_int64 = Int64.to_int
29 let to_int64 = to_uint64
30
31 let of_float = int_of_float
32
33 external to_float : int -> (float [@unboxed])
34 = "rocq_uint63_to_float_byte" "rocq_uint63_to_float"
35 [@@noalloc]
36
37 let hash i = i
38 [@@ocaml.inline always]
39
40 (* conversion of an uint63 to a string *)
41 let to_string i = Int64.to_string (to_uint64 i)
42
43 (* Compiles an unsigned int to OCaml code *)
44 let compile i = Printf.sprintf "Uint63.of_int (%i)" i
45
46 let zero = 0
47 let one = 1
48
49 (* logical shift *)
50 let l_sl x y =
51 if 0 <= y && y < 63 then x lsl y else 0
52
53 let l_sr x y =
54 if 0 <= y && y < 63 then x lsr y else 0
55
56 (* arithmetic shift (for sint63) *)
57 let a_sr x y =
58 if 0 <= y && y < 63 then x asr y else 0
59
60 let l_and x y = x land y
61 [@@ocaml.inline always]
62
63 let l_or x y = x lor y
64 [@@ocaml.inline always]
65
66 let l_xor x y = x lxor y
67 [@@ocaml.inline always]
68
69 (* addition of int63 *)
70 let add x y = x + y
71 [@@ocaml.inline always]
72
73 (* subtraction *)
74 let sub x y = x - y
75 [@@ocaml.inline always]
76
77 (* multiplication *)
78 let mul x y = x * y
79 [@@ocaml.inline always]
80
81 (* division *)
82 let div (x : int) (y : int) =
83 if y = 0 then 0 else Int64.to_int (Int64.div (to_uint64 x) (to_uint64 y))
84
85 (* modulo *)
86 let rem (x : int) (y : int) =
87 if y = 0 then x else Int64.to_int (Int64.rem (to_uint64 x) (to_uint64 y))
88
89 let diveucl x y = (div x y, rem x y)
90
91 (* signed division *)
92 let divs (x : int) (y : int) =
93 if y = 0 then 0 else x / y
94
95 (* modulo *)
96 let rems (x : int) (y : int) =
97 if y = 0 then x else x mod y
98
99 let addmuldiv p x y =
100 l_or (l_sl x p) (l_sr y (uint_size - p))
101
102 (* comparison *)
103 let lt (x : int) (y : int) =
104 (x lxor 0x4000000000000000) < (y lxor 0x4000000000000000)
105 [@@ocaml.inline always]
106
107 let le (x : int) (y : int) =
108 (x lxor 0x4000000000000000) <= (y lxor 0x4000000000000000)
109 [@@ocaml.inline always]
110
111 (* signed comparison *)
112 let lts (x : int) (y : int) =
113 x < y
114 [@@ocaml.inline always]
115
116 let les (x : int) (y : int) =
117 x <= y
118 [@@ocaml.inline always]
119
120 let to_int_min n m =
121 if lt n m then n else m
122 [@@ocaml.inline always]
123
124 (* division of two numbers by one *)
125 (* precondition: xh < y *)
126 (* outputs: q, r s.t. x = q * y + r, r < y *)
127 let div21 xh xl y =
128 (* nh might temporarily grow as large as 2*y - 1 in the loop body,
129 so we store it as a 64-bit unsigned integer *)
130 let nh = ref xh in
131 let nl = ref xl in
132 let q = ref 0 in
133 for _i = 0 to 62 do
134 (* invariants: 0 <= nh < y, nl = (xl*2^i) % 2^63,
135 (q*y + nh) * 2^(63-i) + (xl % 2^(63-i)) = (xh%y) * 2^63 + xl *)
136 nh := Int64.logor (Int64.shift_left !nh 1) (Int64.of_int (!nl lsr 62));
137 nl := !nl lsl 1;
138 q := !q lsl 1;
139 if Int64.unsigned_compare !nh y >= 0 then
140 begin q := !q lor 1; nh := Int64.sub !nh y; end
141 done;
142 !q, Int64.to_int !nh
143
144 let div21 xh xl y =
145 let xh = to_uint64 xh in
146 let y = to_uint64 y in
147 if Int64.compare y xh <= 0 then 0, 0 else div21 xh xl y
148
149 (* exact multiplication *)
150 (* TODO: check that none of these additions could be a logical or *)
151
152
153 (* size = 32 + 31
154 (hx << 31 + lx) * (hy << 31 + ly) =
155 hxhy << 62 + (hxly + lxhy) << 31 + lxly
156 *)
157
158 (* precondition : (x lsr 62 = 0 || y lsr 62 = 0) *)
159 let mulc_aux x y =
160 let lx = x land maxuint31 in
161 let ly = y land maxuint31 in
162 let hx = x lsr 31 in
163 let hy = y lsr 31 in
164 (* hx and hy are 32 bits value but at most one is 32 *)
165 let hxy = hx * hy in (* 63 bits *)
166 let hxly = hx * ly in (* 63 bits *)
167 let lxhy = lx * hy in (* 63 bits *)
168 let lxy = lx * ly in (* 62 bits *)
169 let l = lxy lor (hxy lsl 62) (* 63 bits *) in
170 let h = hxy lsr 1 in (* 62 bits *)
171 let hl = hxly + lxhy in (* We can have a carry *)
172 let h = if lt hl hxly then h + (1 lsl 31) else h in
173 let hl'= hl lsl 31 in
174 let l = l + hl' in
175 let h = if lt l hl' then h + 1 else h in
176 let h = h + (hl lsr 32) in
177 (h,l)
178
179 let mulc x y =
180 if (x lsr 62 = 0 || y lsr 62 = 0) then mulc_aux x y
181 else
182 let yl = y lxor (1 lsl 62) in
183 let (h,l) = mulc_aux x yl in
184 (* h << 63 + l = x * yl
185 x * y = x * (1 << 62 + yl) =
186 x << 62 + x*yl = x << 62 + h << 63 + l *)
187 let l' = l + (x lsl 62) in
188 let h = if lt l' l then h + 1 else h in
189 (h + (x lsr 1), l')
190
191 let equal (x : int) (y : int) = x = y
192 [@@ocaml.inline always]
193
194 let compare (x:int) (y:int) =
195 let x = x lxor 0x4000000000000000 in
196 let y = y lxor 0x4000000000000000 in
197 Int.compare x y
198
199 let compares (x : int) (y : int) =
200 Int.compare x y
201
202 (* head tail *)
203
204 let head0 x =
205 let r = ref 0 in
206 let x = ref x in
207 if !x land 0x7FFFFFFF00000000 = 0 then r := !r + 31
208 else x := !x lsr 31;
209 if !x land 0xFFFF0000 = 0 then (x := !x lsl 16; r := !r + 16);
210 if !x land 0xFF000000 = 0 then (x := !x lsl 8; r := !r + 8);
211 if !x land 0xF0000000 = 0 then (x := !x lsl 4; r := !r + 4);
212 if !x land 0xC0000000 = 0 then (x := !x lsl 2; r := !r + 2);
213 if !x land 0x80000000 = 0 then (x := !x lsl 1; r := !r + 1);
214 if !x land 0x80000000 = 0 then ( r := !r + 1);
215 !r;;
216
217 let tail0 x =
218 let r = ref 0 in
219 let x = ref x in
220 if !x land 0xFFFFFFFF = 0 then (x := !x lsr 32; r := !r + 32);
221 if !x land 0xFFFF = 0 then (x := !x lsr 16; r := !r + 16);
222 if !x land 0xFF = 0 then (x := !x lsr 8; r := !r + 8);
223 if !x land 0xF = 0 then (x := !x lsr 4; r := !r + 4);
224 if !x land 0x3 = 0 then (x := !x lsr 2; r := !r + 2);
225 if !x land 0x1 = 0 then ( r := !r + 1);
226 !r
227
228 let is_uint63 t =
229 Obj.is_int t
230 [@@ocaml.inline always]
231
232 (* Arithmetic with explicit carries *)
233
234 (* Analog of Numbers.Abstract.Cyclic.carry *)
235 type 'a carry = C0 of 'a | C1 of 'a
236
237 let addc x y =
238 let r = x + y in
239 if lt r x then C1 r else C0 r
240
241 let addcarryc x y =
242 let r = x + y + 1 in
243 if le r x then C1 r else C0 r
244
245 let subc x y =
246 let r = x - y in
247 if le y x then C0 r else C1 r
248
249 let subcarryc x y =
250 let r = x - y - 1 in
251 if lt y x then C0 r else C1 r