Skip to content

Commit fbc8890

Browse files
committed
add add_aux sub_aux, inline neg
1 parent 0173c4c commit fbc8890

File tree

2 files changed

+39
-17
lines changed

2 files changed

+39
-17
lines changed

jscomp/runtime/caml_int64.ml

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636

3737
let (>>>) = Caml_nativeint_extern.shift_right_logical
3838
let (>>) = Caml_nativeint_extern.shift_right
39+
let (|~) = Caml_nativeint_extern.logor
3940
let ( +~ ) = Caml_nativeint_extern.add
4041
let ( *~ ) = Caml_nativeint_extern.mul
4142
let ( & ) = Caml_nativeint_extern.logand
@@ -71,17 +72,34 @@ let neg_one = mk ~lo:(-1n) ~hi:(-1n)
7172

7273
let neg_signed x = (x & 0x8000_0000n) <> 0n
7374

74-
let add
75-
(Int64 {lo = this_low_; hi = this_high_} : t)
76-
(Int64 {lo = other_low_; hi = other_high_} : t) =
77-
let lo = ( this_low_ +~ other_low_) & 0xffff_ffffn in
75+
let neg (Int64 {lo;hi} ) =
76+
let other_lo = (lognot lo +~ 1n) & 0xffff_ffffn in
77+
mk ~lo:other_lo
78+
~hi:((lognot hi +~ if other_lo = 0n then 1n else 0n) & 0xffff_ffffn)
79+
80+
81+
82+
83+
84+
85+
let add_aux
86+
(Int64 {lo = x_lo; hi = x_hi} : t)
87+
~y_lo ~y_hi =
88+
let lo = ( x_lo +~ y_lo) & 0xffff_ffffn in
7889
let overflow =
79-
if (neg_signed this_low_ && (neg_signed other_low_ || not (neg_signed lo)))
80-
|| (neg_signed other_low_ && not (neg_signed lo))
90+
if (neg_signed x_lo && (neg_signed y_lo || not (neg_signed lo)))
91+
|| (neg_signed y_lo && not (neg_signed lo))
8192
then 1n
8293
else 0n
8394
in
84-
mk ~lo ~hi:(( this_high_ +~ other_high_ +~ overflow) & 0xffff_ffffn)
95+
mk ~lo ~hi:(( x_hi +~ y_hi +~ overflow) & 0xffff_ffffn)
96+
97+
(** [add_lo self y_lo] === [add self (mk ~lo:y_lo ~hi:0n)] *)
98+
let add_lo self lo = add_aux self ~y_lo:(to_unsigned lo) ~y_hi:0n
99+
let add
100+
(self : t)
101+
(Int64 {lo = y_lo; hi = y_hi} : t) =
102+
add_aux self ~y_lo ~y_hi
85103

86104

87105
let not (Int64 {lo; hi }) = mk ~lo:(lognot lo) ~hi:(lognot hi)
@@ -101,14 +119,16 @@ let equal_nullable x y =
101119
| None -> false
102120
| Some y -> eq x y
103121

104-
let neg x =
105-
if eq x min_int then
106-
min_int
107-
else add (not x) one
108122

109123

110-
let sub x y =
111-
add x (neg y)
124+
(* when [lo] is unsigned integer, [lognot lo] is still an unsigned integer *)
125+
let sub_aux x ~lo ~hi =
126+
let neg_lo = to_unsigned ((lognot lo +~ 1n) & 0xffff_ffffn) in
127+
let neg_hi = ((lognot hi +~ if neg_lo = 0n then 1n else 0n) & 0xffff_ffffn) in
128+
add_aux x ~y_lo:neg_lo ~y_hi:neg_hi
129+
130+
let sub self (Int64{lo;hi})= sub_aux self ~lo ~hi
131+
let sub_lo self lo = sub_aux self ~lo ~hi:0n
112132

113133
let lsl_ (Int64 {lo; hi} as x) numBits =
114134
if numBits = 0 then
@@ -118,9 +138,9 @@ let lsl_ (Int64 {lo; hi} as x) numBits =
118138
else
119139
mk ~lo:(Caml_nativeint_extern.shift_left lo numBits)
120140
~hi:
121-
(Caml_nativeint_extern.logor
122-
( lo >>> (32 - numBits))
123-
(Caml_nativeint_extern.shift_left hi numBits))
141+
(
142+
( lo >>> (32 - numBits)) |~
143+
( hi << numBits))
124144

125145

126146
let lsr_ (Int64 {lo; hi} as x) numBits =

jscomp/runtime/caml_int64.mli

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
(** *)
3030

3131
type t
32+
val succ : t -> t
3233
val min_int : t
3334
val max_int : t
3435
val one : t
@@ -88,4 +89,5 @@ external unsafe_to_int64 : t -> int64 = "%identity"
8889
external unsafe_of_int64 : int64 -> t = "%identity"
8990
val div_mod : int64 -> int64 -> int64 * int64
9091
val to_hex : int64 -> string
91-
val discard_sign : int64 -> int64
92+
val discard_sign : int64 -> int64
93+
val to_string : t -> string

0 commit comments

Comments
 (0)