| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
type t = Int64.t |
| 13 |
|
| 14 |
let _ = assert (Sys.word_size = 32) |
| 15 |
|
| 16 |
let uint_size = 63 |
| 17 |
|
| 18 |
let maxuint63 = 0x7FFF_FFFF_FFFF_FFFFL |
| 19 |
let maxuint31 = 0x7FFF_FFFFL |
| 20 |
|
| 21 |
let zero = Int64.zero |
| 22 |
let one = Int64.one |
| 23 |
|
| 24 |
|
| 25 |
let mask63 i = Int64.logand i maxuint63 |
| 26 |
let of_int i = mask63 (Int64.of_int i) |
| 27 |
let to_int2 i = (Int64.to_int (Int64.shift_right_logical i 31), Int64.to_int i) |
| 28 |
let of_int64 = mask63 |
| 29 |
let to_int64 i = i |
| 30 |
|
| 31 |
let to_int_min n m = |
| 32 |
if Int64.(compare n (of_int m)) < 0 then Int64.to_int n else m |
| 33 |
|
| 34 |
let of_float f = mask63 (Int64.of_float f) |
| 35 |
let to_float = Int64.to_float |
| 36 |
|
| 37 |
let hash i = |
| 38 |
let (h,l) = to_int2 i in |
| 39 |
|
| 40 |
h * 65599 + l |
| 41 |
|
| 42 |
|
| 43 |
let to_string i = Int64.to_string i |
| 44 |
|
| 45 |
|
| 46 |
let compile i = Printf.sprintf "Uint63.of_int64 (%LiL)" i |
| 47 |
|
| 48 |
|
| 49 |
let lt x y = |
| 50 |
Int64.compare x y < 0 |
| 51 |
|
| 52 |
let le x y = |
| 53 |
Int64.compare x y <= 0 |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
let lts x y = |
| 59 |
Int64.(compare (shift_left x 1) (shift_left y 1)) < 0 |
| 60 |
|
| 61 |
let les x y = |
| 62 |
Int64.(compare (shift_left x 1) (shift_left y 1)) <= 0 |
| 63 |
|
| 64 |
|
| 65 |
let l_sl x y = |
| 66 |
if le 0L y && lt y 63L then mask63 (Int64.shift_left x (Int64.to_int y)) else 0L |
| 67 |
|
| 68 |
let l_sr x y = |
| 69 |
if le 0L y && lt y 63L then Int64.shift_right x (Int64.to_int y) else 0L |
| 70 |
|
| 71 |
|
| 72 |
let a_sr x y = |
| 73 |
if les 0L y && lts y 63L then |
| 74 |
mask63 (Int64.shift_right (Int64.shift_left x 1) ((Int64.to_int y) + 1)) |
| 75 |
else 0L |
| 76 |
|
| 77 |
let l_and x y = Int64.logand x y |
| 78 |
let l_or x y = Int64.logor x y |
| 79 |
let l_xor x y = Int64.logxor x y |
| 80 |
|
| 81 |
|
| 82 |
let add x y = mask63 (Int64.add x y) |
| 83 |
|
| 84 |
let addcarry x y = mask63 Int64.(add (add x y) one) |
| 85 |
|
| 86 |
|
| 87 |
let sub x y = mask63 (Int64.sub x y) |
| 88 |
|
| 89 |
let subcarry x y = mask63 Int64.(sub (sub x y) one) |
| 90 |
|
| 91 |
|
| 92 |
let mul x y = mask63 (Int64.mul x y) |
| 93 |
|
| 94 |
|
| 95 |
let div x y = |
| 96 |
if y = 0L then 0L else Int64.div x y |
| 97 |
|
| 98 |
|
| 99 |
let rem x y = |
| 100 |
if y = 0L then x else Int64.rem x y |
| 101 |
|
| 102 |
let diveucl x y = (div x y, rem x y) |
| 103 |
|
| 104 |
|
| 105 |
let divs x y = |
| 106 |
if y = 0L then 0L else mask63 Int64.(div (shift_left x 1) (shift_left y 1)) |
| 107 |
|
| 108 |
|
| 109 |
let rems x y = |
| 110 |
if y = 0L then x else |
| 111 |
Int64.shift_right_logical (Int64.(rem (shift_left x 1) (shift_left y 1))) 1 |
| 112 |
|
| 113 |
let addmuldiv p x y = |
| 114 |
l_or (l_sl x p) (l_sr y Int64.(sub (of_int uint_size) p)) |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
let div21 xh xl y = |
| 120 |
let nh = ref xh in |
| 121 |
let nl = ref xl in |
| 122 |
let q = ref 0L in |
| 123 |
for _i = 0 to 62 do |
| 124 |
|
| 125 |
|
| 126 |
nl := Int64.shift_left !nl 1; |
| 127 |
nh := Int64.logor (Int64.shift_left !nh 1) (Int64.shift_right_logical !nl 63); |
| 128 |
q := Int64.shift_left !q 1; |
| 129 |
if Int64.unsigned_compare !nh y >= 0 then |
| 130 |
begin q := Int64.logor !q 1L; nh := Int64.sub !nh y; end |
| 131 |
done; |
| 132 |
!q, !nh |
| 133 |
|
| 134 |
let div21 xh xl y = |
| 135 |
if Int64.compare y xh <= 0 then zero, zero else div21 xh xl y |
| 136 |
|
| 137 |
|
| 138 |
let mulc x y = |
| 139 |
let lx = Int64.logand x maxuint31 in |
| 140 |
let ly = Int64.logand y maxuint31 in |
| 141 |
let hx = Int64.shift_right x 31 in |
| 142 |
let hy = Int64.shift_right y 31 in |
| 143 |
|
| 144 |
let s = Int64.add (Int64.mul lx hy) (Int64.mul hx ly) in |
| 145 |
|
| 146 |
let lr = Int64.shift_left (Int64.logand s maxuint31) 31 in |
| 147 |
let hr = Int64.shift_right_logical s 31 in |
| 148 |
|
| 149 |
let lr = Int64.add (Int64.mul lx ly) lr in |
| 150 |
let hr = Int64.add (Int64.mul hx hy) hr in |
| 151 |
|
| 152 |
|
| 153 |
let lr = Int64.add lr (Int64.shift_left (Int64.logand hr 1L) 62) in |
| 154 |
let hr = Int64.shift_right_logical hr 1 in |
| 155 |
|
| 156 |
if Int64.logand lr Int64.min_int <> 0L |
| 157 |
then Int64.add hr 1L, mask63 lr |
| 158 |
else hr, lr |
| 159 |
|
| 160 |
let equal (x : t) y = x = y |
| 161 |
|
| 162 |
let compare x y = Int64.compare x y |
| 163 |
|
| 164 |
let compares x y = Int64.(compare (shift_left x 1) (shift_left y 1)) |
| 165 |
|
| 166 |
|
| 167 |
let head0 x = |
| 168 |
let r = ref 0 in |
| 169 |
let x = ref x in |
| 170 |
if Int64.logand !x 0x7FFFFFFF00000000L = 0L then r := !r + 31 |
| 171 |
else x := Int64.shift_right !x 31; |
| 172 |
if Int64.logand !x 0xFFFF0000L = 0L then (x := Int64.shift_left !x 16; r := !r + 16); |
| 173 |
if Int64.logand !x 0xFF000000L = 0L then (x := Int64.shift_left !x 8; r := !r + 8); |
| 174 |
if Int64.logand !x 0xF0000000L = 0L then (x := Int64.shift_left !x 4; r := !r + 4); |
| 175 |
if Int64.logand !x 0xC0000000L = 0L then (x := Int64.shift_left !x 2; r := !r + 2); |
| 176 |
if Int64.logand !x 0x80000000L = 0L then (x := Int64.shift_left !x 1; r := !r + 1); |
| 177 |
if Int64.logand !x 0x80000000L = 0L then (r := !r + 1); |
| 178 |
Int64.of_int !r |
| 179 |
|
| 180 |
|
| 181 |
let tail0 x = |
| 182 |
let r = ref 0 in |
| 183 |
let x = ref x in |
| 184 |
if Int64.logand !x 0xFFFFFFFFL = 0L then (x := Int64.shift_right !x 32; r := !r + 32); |
| 185 |
if Int64.logand !x 0xFFFFL = 0L then (x := Int64.shift_right !x 16; r := !r + 16); |
| 186 |
if Int64.logand !x 0xFFL = 0L then (x := Int64.shift_right !x 8; r := !r + 8); |
| 187 |
if Int64.logand !x 0xFL = 0L then (x := Int64.shift_right !x 4; r := !r + 4); |
| 188 |
if Int64.logand !x 0x3L = 0L then (x := Int64.shift_right !x 2; r := !r + 2); |
| 189 |
if Int64.logand !x 0x1L = 0L then (r := !r + 1); |
| 190 |
Int64.of_int !r |
| 191 |
|
| 192 |
|
| 193 |
let is_uint63 t = |
| 194 |
Obj.is_block t && Int.equal (Obj.tag t) Obj.custom_tag |
| 195 |
&& le (Obj.magic t) maxuint63 |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
type 'a carry = C0 of 'a | C1 of 'a |
| 201 |
|
| 202 |
let addc x y = |
| 203 |
let r = add x y in |
| 204 |
if lt r x then C1 r else C0 r |
| 205 |
|
| 206 |
let addcarryc x y = |
| 207 |
let r = addcarry x y in |
| 208 |
if le r x then C1 r else C0 r |
| 209 |
|
| 210 |
let subc x y = |
| 211 |
let r = sub x y in |
| 212 |
if le y x then C0 r else C1 r |
| 213 |
|
| 214 |
let subcarryc x y = |
| 215 |
let r = subcarry x y in |
| 216 |
if lt y x then C0 r else C1 r |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
let () = |
| 221 |
Callback.register "uint63 add" add; |
| 222 |
Callback.register "uint63 addcarry" addcarry; |
| 223 |
Callback.register "uint63 addmuldiv" addmuldiv; |
| 224 |
Callback.register "uint63 div" div; |
| 225 |
Callback.register "uint63 divs" divs; |
| 226 |
Callback.register "uint63 div21_ml" div21; |
| 227 |
Callback.register "uint63 eq" equal; |
| 228 |
Callback.register "uint63 eq0" (equal Int64.zero); |
| 229 |
Callback.register "uint63 eqm1" (equal (sub zero one)); |
| 230 |
Callback.register "uint63 head0" head0; |
| 231 |
Callback.register "uint63 land" l_and; |
| 232 |
Callback.register "uint63 leq" le; |
| 233 |
Callback.register "uint63 les" les; |
| 234 |
Callback.register "uint63 lor" l_or; |
| 235 |
Callback.register "uint63 lsl" l_sl; |
| 236 |
Callback.register "uint63 lsr" l_sr; |
| 237 |
Callback.register "uint63 asr" a_sr; |
| 238 |
Callback.register "uint63 lt" lt; |
| 239 |
Callback.register "uint63 lts" lts; |
| 240 |
Callback.register "uint63 lxor" l_xor; |
| 241 |
Callback.register "uint63 mod" rem; |
| 242 |
Callback.register "uint63 mods" rems; |
| 243 |
Callback.register "uint63 mul" mul; |
| 244 |
Callback.register "uint63 mulc_ml" mulc; |
| 245 |
Callback.register "uint63 zero" zero; |
| 246 |
Callback.register "uint63 one" one; |
| 247 |
Callback.register "uint63 sub" sub; |
| 248 |
Callback.register "uint63 neg" (sub zero); |
| 249 |
Callback.register "uint63 subcarry" subcarry; |
| 250 |
Callback.register "uint63 tail0" tail0; |
| 251 |
Callback.register "uint63 of_float" of_float; |
| 252 |
Callback.register "uint63 to_float" to_float; |
| 253 |
Callback.register "uint63 of_int" of_int; |
| 254 |
Callback.register "uint63 to_int_min" to_int_min |