@@ -44,7 +44,7 @@ let ( & ) = Caml_nativeint_extern.logand
44
44
let ( << ) = Caml_nativeint_extern. shift_left
45
45
let lognot x = Caml_nativeint_extern. logxor x (- 1n )
46
46
47
- type t = { hi : nativeint ; lo : nativeint ; }
47
+ type t = Int64 of { hi : nativeint ; lo : nativeint ; }
48
48
49
49
external unsafe_to_int64 : t -> int64 = " %identity"
50
50
external unsafe_of_int64 : int64 -> t = " %identity"
@@ -53,7 +53,7 @@ external unsafe_of_int64 : int64 -> t = "%identity"
53
53
let to_unsigned (x : nativeint ) =
54
54
x >>> 0
55
55
56
- let mk ~lo ~hi = {lo = to_unsigned lo ; hi}
56
+ let mk ~lo ~hi = Int64 {lo = to_unsigned lo ; hi}
57
57
(*
58
58
module N = struct
59
59
type nonrec t = t = private { hi : nativeint; lo : nativeint ; }
@@ -74,8 +74,8 @@ let neg_one = mk ~lo:(-1n) ~hi:(-1n)
74
74
let neg_signed x = (x & 0x8000_0000n ) <> 0n
75
75
76
76
let add
77
- ({lo = this_low_ ; hi = this_high_ } : t )
78
- ({lo = other_low_ ; hi = other_high_ } : t ) =
77
+ (Int64 {lo = this_low_ ; hi = this_high_ } : t )
78
+ (Int64 {lo = other_low_ ; hi = other_high_ } : t ) =
79
79
let lo = ( this_low_ +~ other_low_) & 0xffff_ffffn in
80
80
let overflow =
81
81
if (neg_signed this_low_ && (neg_signed other_low_ || not (neg_signed lo)))
@@ -86,9 +86,9 @@ let add
86
86
mk ~lo ~hi: (( this_high_ +~ other_high_ +~ overflow) & 0xffff_ffffn )
87
87
88
88
89
- let not {lo; hi } = mk ~lo: (lognot lo) ~hi: (lognot hi)
89
+ let not (Int64 {lo; hi } ) = mk ~lo: (lognot lo) ~hi: (lognot hi)
90
90
91
- let eq x y = x.hi = y.hi && x.lo = y.lo
91
+ let eq (Int64 x ) (Int64 y ) = x.hi = y.hi && x.lo = y.lo
92
92
93
93
let equal_null x y =
94
94
match Js. nullToOption y with
@@ -103,7 +103,7 @@ let equal_nullable x y =
103
103
| None -> false
104
104
| Some y -> eq x y
105
105
106
- let neg ({lo; hi} as x ) =
106
+ let neg (Int64 {lo; hi} as x ) =
107
107
if eq x min_int then
108
108
min_int
109
109
else add (not x) one
@@ -112,7 +112,7 @@ let neg ({lo; hi} as x) =
112
112
let sub x y =
113
113
add x (neg y)
114
114
115
- let lsl_ ({lo; hi} as x ) numBits =
115
+ let lsl_ (Int64 {lo; hi} as x ) numBits =
116
116
if numBits = 0 then
117
117
x
118
118
else if numBits > = 32 then
@@ -125,7 +125,7 @@ let lsl_ ({lo; hi} as x) numBits =
125
125
(Caml_nativeint_extern. shift_left hi numBits))
126
126
127
127
128
- let lsr_ ({lo; hi} as x ) numBits =
128
+ let lsr_ (Int64 {lo; hi} as x ) numBits =
129
129
if numBits = 0 then x
130
130
else
131
131
let offset = numBits - 32 in
@@ -142,7 +142,7 @@ let lsr_ ({lo; hi} as x) numBits =
142
142
( lo >>> numBits))
143
143
144
144
145
- let asr_ ({lo; hi } as x ) numBits =
145
+ let asr_ (Int64 {lo; hi } as x ) numBits =
146
146
if numBits = 0 then
147
147
x
148
148
else
@@ -159,25 +159,25 @@ let asr_ ({lo; hi } as x) numBits =
159
159
160
160
161
161
let is_zero = function
162
- | {lo = 0n ; hi = 0n } -> true
162
+ | Int64 {lo = 0n ; hi = 0n } -> true
163
163
| _ -> false
164
164
165
165
166
166
167
167
let rec mul this
168
168
other =
169
169
match this, other with
170
- | {lo = 0n ; hi = 0n }, _
171
- | _, {lo = 0n ; hi = 0n }
170
+ | Int64 {lo = 0n ; hi = 0n }, _
171
+ | _, Int64 {lo = 0n ; hi = 0n }
172
172
-> zero
173
- | {lo = 0n ; hi = - 0x80000000n }, {lo }
174
- | {lo}, {lo = 0n ; hi = - 0x80000000n }
173
+ | Int64 {lo = 0n ; hi = - 0x80000000n }, Int64 {lo }
174
+ | Int64 {lo}, Int64 {lo = 0n ; hi = - 0x80000000n }
175
175
->
176
176
if (lo & 0x1n ) = 0n then
177
177
zero
178
178
else min_int
179
- | {lo = this_lo; hi = this_hi},
180
- {lo = other_lo; hi = other_hi }
179
+ | Int64 {lo = this_lo; hi = this_hi},
180
+ Int64 {lo = other_lo; hi = other_hi }
181
181
->
182
182
if this_hi < 0n then
183
183
if other_hi < 0n then
@@ -227,24 +227,24 @@ let rec mul this
227
227
228
228
229
229
230
- let swap {lo ; hi } =
230
+ let swap (Int64 {lo ; hi } ) =
231
231
mk ~lo: ( Caml_int32. caml_int32_bswap hi)
232
232
~hi: ( Caml_int32. caml_int32_bswap lo)
233
233
234
234
(* Dispatched by the compiler, idea: should we do maximum sharing
235
235
*)
236
- let xor {lo = this_lo ; hi = this_hi } {lo = other_lo ; hi = other_hi } =
236
+ let xor (Int64 {lo = this_lo ; hi = this_hi } ) (Int64 {lo = other_lo ; hi = other_hi } ) =
237
237
mk
238
238
~lo: (Caml_nativeint_extern. logxor this_lo other_lo)
239
239
~hi: (Caml_nativeint_extern. logxor this_hi other_hi)
240
240
241
241
242
- let or_ {lo = this_lo ; hi = this_hi } {lo = other_lo ; hi = other_hi } =
242
+ let or_ (Int64 {lo = this_lo ; hi = this_hi } ) (Int64 {lo = other_lo ; hi = other_hi } ) =
243
243
mk
244
244
~lo: (Caml_nativeint_extern. logor this_lo other_lo)
245
245
~hi: (Caml_nativeint_extern. logor this_hi other_hi)
246
246
247
- let and_ {lo = this_lo ; hi = this_hi } {lo = other_lo ; hi = other_hi } =
247
+ let and_ (Int64 {lo = this_lo ; hi = this_hi } ) (Int64 {lo = other_lo ; hi = other_hi } ) =
248
248
mk
249
249
~lo: (Caml_nativeint_extern. logand this_lo other_lo)
250
250
~hi: (Caml_nativeint_extern. logand this_hi other_hi)
@@ -258,7 +258,7 @@ let and_ {lo = this_lo; hi= this_hi} {lo = other_lo; hi = other_hi} =
258
258
259
259
type comparison = t -> t -> bool
260
260
261
- let ge ({hi; lo } : t ) ({hi = other_hi ; lo = other_lo } ) : bool =
261
+ let ge (Int64 {hi; lo } : t ) (Int64 {hi = other_hi ; lo = other_lo } ) : bool =
262
262
if hi > other_hi then true
263
263
else if hi < other_hi then false
264
264
else lo > = other_lo
@@ -267,7 +267,7 @@ let ge ({hi; lo } : t) ({hi = other_hi; lo = other_lo}) : bool =
267
267
268
268
let neq x y = Pervasives. not (eq x y)
269
269
let lt x y = Pervasives. not (ge x y)
270
- let gt x y =
270
+ let gt (Int64 x ) (Int64 y ) =
271
271
if x.hi > y.hi then
272
272
true
273
273
else if x.hi < y.hi then
@@ -280,7 +280,7 @@ let le x y = Pervasives.not (gt x y)
280
280
let min x y = if lt x y then x else y
281
281
let max x y = if gt x y then x else y
282
282
283
- let to_float ({hi; lo} : t ) =
283
+ let to_float (Int64 {hi; lo} : t ) =
284
284
Caml_nativeint_extern. to_float ( hi *~ [% raw{| 0x100000000 | }] +~ lo)
285
285
286
286
@@ -321,31 +321,31 @@ external ceil : float -> float = "ceil" [@@bs.val] [@@bs.scope "Math"]
321
321
322
322
let rec div self other =
323
323
match self, other with
324
- | _ , {lo = 0n ; hi = 0n } ->
324
+ | _ , Int64 {lo = 0n ; hi = 0n } ->
325
325
raise Division_by_zero
326
- | {lo = 0n ; hi = 0n }, _
326
+ | Int64 {lo = 0n ; hi = 0n }, _
327
327
-> zero
328
- | {lo = 0n ; hi = - 0x8000_0000n }, _
328
+ | Int64 {lo = 0n ; hi = - 0x8000_0000n }, _
329
329
->
330
330
begin
331
331
if eq other one || eq other neg_one then self
332
332
else if eq other min_int then one
333
333
else
334
- let other_hi = other.hi in
334
+ let ( Int64 {hi = other_hi}) = other in
335
335
(* now |other| >= 2, so |this/other| < |MIN_VALUE|*)
336
336
let half_this = asr_ self 1 in
337
337
let approx = lsl_ (div half_this other) 1 in
338
338
match approx with
339
- | {lo = 0n ; hi = 0n }
339
+ | Int64 {lo = 0n ; hi = 0n }
340
340
-> if other_hi < 0n then one else neg one
341
341
| _
342
342
->
343
343
let rem = sub self (mul other approx) in
344
344
add approx (div rem other)
345
345
end
346
- | _, {lo = 0n ; hi = - 0x8000_0000n }
346
+ | _, Int64 {lo = 0n ; hi = - 0x8000_0000n }
347
347
-> zero
348
- | {lo = _; hi = self_hi}, {lo = _; hi = other_hi}
348
+ | Int64 {lo = _; hi = self_hi}, Int64 {lo = _; hi = other_hi}
349
349
->
350
350
if self_hi < 0n then
351
351
if other_hi < 0n then
@@ -367,7 +367,7 @@ let rec div self other =
367
367
else 2. ** (log2 -. 48. ) in
368
368
let approxRes = ref (of_float approx.contents) in
369
369
let approxRem = ref (mul approxRes.contents other) in
370
- while approxRem.contents.hi < 0n || gt approxRem.contents rem.contents do
370
+ while ( match approxRem.contents with Int64 {hi} -> hi) < 0n || gt approxRem.contents rem.contents do
371
371
approx.contents < - approx.contents -. delta;
372
372
approxRes.contents < - of_float approx.contents;
373
373
approxRem.contents < - mul approxRes.contents other
@@ -387,7 +387,7 @@ let div_mod (self : int64) (other : int64) : int64 * int64 =
387
387
let quotient = div (unsafe_of_int64 self) (unsafe_of_int64 other) in
388
388
unsafe_to_int64 quotient, unsafe_to_int64 (sub (unsafe_of_int64 self) (mul quotient (unsafe_of_int64 other)))
389
389
390
- let compare self other =
390
+ let compare (Int64 self ) (Int64 other ) =
391
391
let v = Pervasives. compare self.hi other.hi in
392
392
if v = 0 then
393
393
Pervasives. compare self.lo other.lo
@@ -396,13 +396,13 @@ let compare self other =
396
396
let of_int32 (lo : nativeint ) =
397
397
mk ~lo ~hi: (if lo < 0n then - 1n else 0n )
398
398
399
- let to_int32 x = Caml_nativeint_extern. logor x.lo 0n (* signed integer *)
399
+ let to_int32 (Int64 x ) = Caml_nativeint_extern. logor x.lo 0n (* signed integer *)
400
400
401
401
402
402
(* width does matter, will it be relevant to endian order? *)
403
403
404
404
let to_hex (x : int64 ) =
405
- let {hi = x_hi; lo = x_lo} = unsafe_of_int64 x in
405
+ let Int64 {hi = x_hi; lo = x_lo} = unsafe_of_int64 x in
406
406
let aux v : string =
407
407
Caml_string_extern. of_int (Caml_nativeint_extern. to_int (Caml_nativeint_extern. shift_right_logical v 0 )) ~base: 16
408
408
in
@@ -422,7 +422,7 @@ let to_hex (x : int64) =
422
422
let discard_sign (x : int64 ) : int64 =
423
423
let v = unsafe_of_int64 x in
424
424
unsafe_to_int64
425
- { v with hi = Caml_nativeint_extern. logand 0x7fff_ffffn v.hi }
425
+ ( match v with Int64 v -> Int64 { v with hi = Caml_nativeint_extern. logand 0x7fff_ffffn v.hi })
426
426
427
427
(* >>> 0 does not change its bit representation
428
428
it simply makes sure it is an unsigned integer
@@ -436,7 +436,7 @@ let discard_sign (x : int64) : int64 =
436
436
]}
437
437
*)
438
438
439
- let float_of_bits ( x : t ) : float =
439
+ let float_of_bits (Int64 x : t ) : float =
440
440
([% raw{| function (lo ,hi ){ return (new Float64Array(new Int32Array([lo,hi]).buffer))[0]} |}] : _ -> _ -> _ ) x.lo x.hi
441
441
442
442
(* let to_int32 (x : nativeint) = x |> Caml_nativeint_extern.to_int32
0 commit comments