Skip to content

Commit

Permalink
Skip range checks when desugaring integer constants
Browse files Browse the repository at this point in the history
  • Loading branch information
minoki committed Jul 16, 2023
1 parent b0271a6 commit 788bc83
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 17 deletions.
12 changes: 9 additions & 3 deletions lib/lunarml/ml/basis/js-common/int.sml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
structure Int8 :> INTEGER = struct
structure Int8Impl :> sig include INTEGER; val fromIntUnchecked : Int.int -> int end = struct
type int = int
val MAX = 127
val MIN = ~128
Expand All @@ -10,6 +10,7 @@ fun fromInt (x : Int.int) = if MIN <= x andalso x <= MAX then
x
else
raise Overflow
fun fromIntUnchecked (x : Int.int) = x
fun toLarge x = Int.toLarge (toInt x)
fun fromLarge x = fromInt (Int.fromLarge x)
val op + = fn (x, y) => fromInt (x + y)
Expand Down Expand Up @@ -37,6 +38,7 @@ fun scan radix getc strm = case Int.scan radix getc strm of
| NONE => NONE
fun fromString s = Option.map fromInt (Int.fromString s)
end;
structure Int8 : INTEGER = Int8Impl;
_overload "Int" [Int8.int] { + = Int8.+
, - = Int8.-
, * = Int8.*
Expand All @@ -53,7 +55,7 @@ _overload "Int" [Int8.int] { + = Int8.+
, maxInt = 0x7f
};

structure Int16 :> INTEGER = struct
structure Int16Impl :> sig include INTEGER; val fromIntUnchecked : Int.int -> int end = struct
type int = int
val MAX = 0x7fff
val MIN = ~0x8000
Expand All @@ -65,6 +67,7 @@ fun fromInt (x : Int.int) = if MIN <= x andalso x <= MAX then
x
else
raise Overflow
fun fromIntUnchecked (x : Int.int) = x
fun toLarge x = Int.toLarge (toInt x)
fun fromLarge x = fromInt (Int.fromLarge x)
val op + = fn (x, y) => fromInt (x + y)
Expand Down Expand Up @@ -92,6 +95,7 @@ fun scan radix getc strm = case Int.scan radix getc strm of
| NONE => NONE
fun fromString s = Option.map fromInt (Int.fromString s)
end;
structure Int16 : INTEGER = Int16Impl;
_overload "Int" [Int16.int] { + = Int16.+
, - = Int16.-
, * = Int16.*
Expand All @@ -108,7 +112,7 @@ _overload "Int" [Int16.int] { + = Int16.+
, maxInt = 0x7fff
};

structure Int32 :> INTEGER = struct
structure Int32Impl :> sig include INTEGER; val fromIntUnchecked : Int.int -> int end = struct
type int = _Prim.Int32.int
fun Int32_equal (x, y) = _primCall "Int32.=" (x, y)
_equality int = Int32_equal;
Expand All @@ -120,6 +124,7 @@ fun fromInt (x : Int.int) : int = if ~0x80000000 <= x andalso x <= 0x7fffffff th
Unsafe.cast x
else
raise Overflow
fun fromIntUnchecked (x : Int.int) = x
fun toLarge x = Int.toLarge (toInt x)
fun fromLarge x = fromInt (Int.fromLarge x)
fun x + y = _primCall "Int32.+" (x, y)
Expand Down Expand Up @@ -166,6 +171,7 @@ fun scan radix getc strm = case Int.scan radix getc strm of
| NONE => NONE
fun fromString s = Option.map fromInt (Int.fromString s)
end;
structure Int32 : INTEGER = Int32Impl;
_overload "Int" [Int32.int] { + = Int32.+
, - = Int32.-
, * = Int32.*
Expand Down
24 changes: 16 additions & 8 deletions lib/lunarml/ml/basis/lua/int.sml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
structure Int8 :> INTEGER = struct
structure Int8Impl :> sig include INTEGER; val fromIntUnchecked : Int.int -> int end = struct
type int = int
val MAX = 127
val MIN = ~128
Expand All @@ -10,6 +10,7 @@ fun fromInt (x : Int.int) = if MIN <= x andalso x <= MAX then
x
else
raise Overflow
fun fromIntUnchecked (x : Int.int) = x
fun toLarge x = Int.toLarge (toInt x)
fun fromLarge x = fromInt (Int.fromLarge x)
val op + = fn (x, y) => fromInt (x + y)
Expand Down Expand Up @@ -37,6 +38,7 @@ fun scan radix getc strm = case Int.scan radix getc strm of
| NONE => NONE
fun fromString s = Option.map fromInt (Int.fromString s)
end;
structure Int8 : INTEGER = Int8Impl;
_overload "Int" [Int8.int] { + = Int8.+
, - = Int8.-
, * = Int8.*
Expand All @@ -48,12 +50,12 @@ _overload "Int" [Int8.int] { + = Int8.+
, <= = Int8.<=
, > = Int8.>
, >= = Int8.>=
, fromInt = Int8.fromInt
, fromInt = Int8Impl.fromIntUnchecked
, minInt = ~0x80
, maxInt = 0x7f
};

structure Int16 :> INTEGER = struct
structure Int16Impl :> sig include INTEGER; val fromIntUnchecked : Int.int -> int end = struct
type int = int
val MAX = 0x7fff
val MIN = ~0x8000
Expand All @@ -65,6 +67,7 @@ fun fromInt (x : Int.int) = if MIN <= x andalso x <= MAX then
x
else
raise Overflow
fun fromIntUnchecked (x : Int.int) = x
fun toLarge x = Int.toLarge (toInt x)
fun fromLarge x = fromInt (Int.fromLarge x)
val op + = fn (x, y) => fromInt (x + y)
Expand Down Expand Up @@ -92,6 +95,7 @@ fun scan radix getc strm = case Int.scan radix getc strm of
| NONE => NONE
fun fromString s = Option.map fromInt (Int.fromString s)
end;
structure Int16 : INTEGER = Int16Impl;
_overload "Int" [Int16.int] { + = Int16.+
, - = Int16.-
, * = Int16.*
Expand All @@ -103,12 +107,12 @@ _overload "Int" [Int16.int] { + = Int16.+
, <= = Int16.<=
, > = Int16.>
, >= = Int16.>=
, fromInt = Int16.fromInt
, fromInt = Int16Impl.fromIntUnchecked
, minInt = ~0x8000
, maxInt = 0x7fff
};

structure Int32 :> INTEGER = struct
structure Int32Impl :> sig include INTEGER; val fromIntUnchecked : Int.int -> int end = struct
type int = int
val MAX = 0x7fffffff
val MIN = ~0x80000000
Expand All @@ -120,6 +124,7 @@ fun fromInt (x : Int.int) = if MIN <= x andalso x <= MAX then
x
else
raise Overflow
fun fromIntUnchecked (x : Int.int) = x
fun toLarge x = Int.toLarge (toInt x)
fun fromLarge x = fromInt (Int.fromLarge x)
val op + = fn (x, y) => fromInt (x + y)
Expand Down Expand Up @@ -147,6 +152,7 @@ fun scan radix getc strm = case Int.scan radix getc strm of
| NONE => NONE
fun fromString s = Option.map fromInt (Int.fromString s)
end;
structure Int32 : INTEGER = Int32Impl;
_overload "Int" [Int32.int] { + = Int32.+
, - = Int32.-
, * = Int32.*
Expand All @@ -158,13 +164,13 @@ _overload "Int" [Int32.int] { + = Int32.+
, <= = Int32.<=
, > = Int32.>
, >= = Int32.>=
, fromInt = Int32.fromInt
, fromInt = Int32Impl.fromIntUnchecked
, minInt = ~0x8000_0000
, maxInt = 0x7fff_ffff
};

(* Assume Int is 64-bit *)
structure Int54 :> INTEGER = struct
structure Int54Impl :> sig include INTEGER; val fromIntUnchecked : Int.int -> int end = struct
type int = int
val MAX = 0x1f_ffff_ffff_ffff
val MIN = ~0x20_0000_0000_0000
Expand All @@ -176,6 +182,7 @@ fun fromInt (x : Int.int) = if MIN <= x andalso x <= MAX then
x
else
raise Overflow
fun fromIntUnchecked (x : Int.int) = x
fun toLarge x = Int.toLarge (toInt x)
fun fromLarge x = fromInt (Int.fromLarge x)
val op + = fn (x, y) => fromInt (x + y)
Expand Down Expand Up @@ -203,6 +210,7 @@ fun scan radix getc strm = case Int.scan radix getc strm of
| NONE => NONE
fun fromString s = Option.map fromInt (Int.fromString s)
end;
structure Int54 : INTEGER = Int54Impl;
_overload "Int" [Int54.int] { + = Int54.+
, - = Int54.-
, * = Int54.*
Expand All @@ -214,7 +222,7 @@ _overload "Int" [Int54.int] { + = Int54.+
, <= = Int54.<=
, > = Int54.>
, >= = Int54.>=
, fromInt = Int54.fromInt
, fromInt = Int54Impl.fromIntUnchecked
, minInt = ~0x20_0000_0000_0000
, maxInt = 0x1f_ffff_ffff_ffff
};
Expand Down
18 changes: 12 additions & 6 deletions lib/lunarml/ml/basis/luajit/int.sml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
structure Int8 :> INTEGER = struct
structure Int8Impl :> sig include INTEGER; val fromIntUnchecked : Int.int -> int end = struct
type int = int
val MAX = 127
val MIN = ~128
Expand All @@ -10,6 +10,7 @@ fun fromInt (x : Int.int) = if MIN <= x andalso x <= MAX then
x
else
raise Overflow
fun fromIntUnchecked (x : Int.int) = x
fun toLarge x = Int.toLarge (toInt x)
fun fromLarge x = fromInt (Int.fromLarge x)
val op + = fn (x, y) => fromInt (x + y)
Expand Down Expand Up @@ -37,6 +38,7 @@ fun scan radix getc strm = case Int.scan radix getc strm of
| NONE => NONE
fun fromString s = Option.map fromInt (Int.fromString s)
end;
structure Int8 : INTEGER = Int8Impl;
_overload "Int" [Int8.int] { + = Int8.+
, - = Int8.-
, * = Int8.*
Expand All @@ -48,12 +50,12 @@ _overload "Int" [Int8.int] { + = Int8.+
, <= = Int8.<=
, > = Int8.>
, >= = Int8.>=
, fromInt = Int8.fromInt
, fromInt = Int8Impl.fromIntUnchecked
, minInt = ~0x80
, maxInt = 0x7f
};

structure Int16 :> INTEGER = struct
structure Int16Impl :> sig include INTEGER; val fromIntUnchecked : Int.int -> int end = struct
type int = int
val MAX = 0x7fff
val MIN = ~0x8000
Expand All @@ -65,6 +67,7 @@ fun fromInt (x : Int.int) = if MIN <= x andalso x <= MAX then
x
else
raise Overflow
fun fromIntUnchecked (x : Int.int) = x
fun toLarge x = Int.toLarge (toInt x)
fun fromLarge x = fromInt (Int.fromLarge x)
val op + = fn (x, y) => fromInt (x + y)
Expand Down Expand Up @@ -92,6 +95,7 @@ fun scan radix getc strm = case Int.scan radix getc strm of
| NONE => NONE
fun fromString s = Option.map fromInt (Int.fromString s)
end;
structure Int16 : INTEGER = Int16Impl
_overload "Int" [Int16.int] { + = Int16.+
, - = Int16.-
, * = Int16.*
Expand All @@ -103,12 +107,12 @@ _overload "Int" [Int16.int] { + = Int16.+
, <= = Int16.<=
, > = Int16.>
, >= = Int16.>=
, fromInt = Int16.fromInt
, fromInt = Int16Impl.fromIntUnchecked
, minInt = ~0x8000
, maxInt = 0x7fff
};

structure Int32 :> INTEGER = struct
structure Int32Impl :> sig include INTEGER; val fromIntUnchecked : Int.int -> int end = struct
type int = int
val MAX = 0x7fffffff
val MIN = ~0x80000000
Expand All @@ -120,6 +124,7 @@ fun fromInt (x : Int.int) = if MIN <= x andalso x <= MAX then
x
else
raise Overflow
fun fromIntUnchecked (x : Int.int) = x
fun toLarge x = Int.toLarge (toInt x)
fun fromLarge x = fromInt (Int.fromLarge x)
val op + = fn (x, y) => fromInt (x + y)
Expand Down Expand Up @@ -147,6 +152,7 @@ fun scan radix getc strm = case Int.scan radix getc strm of
| NONE => NONE
fun fromString s = Option.map fromInt (Int.fromString s)
end;
structure Int32 : INTEGER = Int32Impl;
_overload "Int" [Int32.int] { + = Int32.+
, - = Int32.-
, * = Int32.*
Expand All @@ -158,7 +164,7 @@ _overload "Int" [Int32.int] { + = Int32.+
, <= = Int32.<=
, > = Int32.>
, >= = Int32.>=
, fromInt = Int32.fromInt
, fromInt = Int32Impl.fromIntUnchecked
, minInt = ~0x8000_0000
, maxInt = 0x7fff_ffff
};
Expand Down

0 comments on commit 788bc83

Please sign in to comment.