Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 13 additions & 34 deletions backend/src/Builtins/Builtins.Pure/Libs/Int128.fs
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,10 @@ let fns () : List<BuiltInFn> =
typeParams = []
parameters = [ Param.make "a" TInt128 ""; Param.make "b" TInt128 "" ]
returnType = TInt128
description = "Adds two 128-bit signed integers together"
description = "Adds two 128-bit signed integers together, wrapping on overflow"
fn =
(function
| _, vm, _, [ DInt128 a; DInt128 b ] ->
try
let result = System.Int128.op_CheckedAddition (a, b)
Ply(DInt128(result))
with :? System.OverflowException ->
RTE.Ints.OutOfRange |> RTE.Int |> raiseRTE vm.threadID
| _, _, _, [ DInt128 a; DInt128 b ] -> Ply(DInt128(a + b))
| _ -> incorrectArgs ())
sqlSpec = NotYetImplemented
previewable = Pure
Expand All @@ -114,15 +109,10 @@ let fns () : List<BuiltInFn> =
typeParams = []
parameters = [ Param.make "a" TInt128 ""; Param.make "b" TInt128 "" ]
returnType = TInt128
description = "Subtracts two 128-bit signed integers"
description = "Subtracts two 128-bit signed integers, wrapping on overflow"
fn =
(function
| _, vm, _, [ DInt128 a; DInt128 b ] ->
try
let result = System.Int128.op_CheckedSubtraction (a, b)
Ply(DInt128(result))
with :? System.OverflowException ->
RTE.Ints.OutOfRange |> RTE.Int |> raiseRTE vm.threadID
| _, _, _, [ DInt128 a; DInt128 b ] -> Ply(DInt128(a - b))
| _ -> incorrectArgs ())
sqlSpec = NotYetImplemented
previewable = Pure
Expand All @@ -135,15 +125,10 @@ let fns () : List<BuiltInFn> =
typeParams = []
parameters = [ Param.make "a" TInt128 ""; Param.make "b" TInt128 "" ]
returnType = TInt128
description = "Multiplies two 128-bit signed integers"
description = "Multiplies two 128-bit signed integers, wrapping on overflow"
fn =
(function
| _, vm, _, [ DInt128 a; DInt128 b ] ->
try
let result = System.Int128.op_CheckedMultiply (a, b)
Ply(DInt128(result))
with :? System.OverflowException ->
RTE.Ints.OutOfRange |> RTE.Int |> raiseRTE vm.threadID
| _, _, _, [ DInt128 a; DInt128 b ] -> Ply(DInt128(a * b))
| _ -> incorrectArgs ())
sqlSpec = NotYetImplemented
previewable = Pure
Expand All @@ -162,14 +147,13 @@ let fns () : List<BuiltInFn> =
fn =
(function
| _, vm, _, [ DInt128 a; DInt128 b ] ->
try
let result = System.Int128.op_Division (a, b)
Ply(DInt128(result))
with
| :? System.DivideByZeroException ->
if b = System.Int128.Zero then
RTE.Ints.DivideByZeroError |> RTE.Int |> raiseRTE vm.threadID
| :? System.OverflowException ->
RTE.Ints.OutOfRange |> RTE.Int |> raiseRTE vm.threadID
elif a = System.Int128.MinValue && b = System.Int128.NegativeOne then
// wraps back to MinValue (the division itself would throw)
Ply(DInt128 System.Int128.MinValue)
else
Ply(DInt128(a / b))
| _ -> incorrectArgs ())
sqlSpec = NotYetImplemented
previewable = Pure
Expand All @@ -184,12 +168,7 @@ let fns () : List<BuiltInFn> =
description = "Returns the negation of <param a>, {{-a}}"
fn =
(function
| _, vm, _, [ DInt128 a ] ->
try
let result = System.Int128.op_CheckedUnaryNegation a
Ply(DInt128(result))
with :? System.OverflowException ->
RTE.Ints.OutOfRange |> RTE.Int |> raiseRTE vm.threadID
| _, _, _, [ DInt128 a ] -> Ply(DInt128(-a))
| _ -> incorrectArgs ())
sqlSpec = NotYetImplemented
previewable = Pure
Expand Down
63 changes: 19 additions & 44 deletions backend/src/Builtins/Builtins.Pure/Libs/Int16.fs
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,10 @@ let fns () : List<BuiltInFn> =
typeParams = []
parameters = [ Param.make "a" TInt16 ""; Param.make "b" TInt16 "" ]
returnType = TInt16
description = "Adds two 16-bit signed integers together"
description = "Adds two 16-bit signed integers together, wrapping on overflow"
fn =
(function
| _, vm, _, [ DInt16 a; DInt16 b ] ->
try
let result = Checked.(+) a b
Ply(DInt16(result))
with :? System.OverflowException ->
RTE.Ints.OutOfRange |> RTE.Int |> raiseRTE vm.threadID
| _, _, _, [ DInt16 a; DInt16 b ] -> Ply(DInt16(a + b))
| _ -> incorrectArgs ())
sqlSpec = NotYetImplemented
previewable = Pure
Expand All @@ -115,15 +110,10 @@ let fns () : List<BuiltInFn> =
typeParams = []
parameters = [ Param.make "a" TInt16 ""; Param.make "b" TInt16 "" ]
returnType = TInt16
description = "Subtracts two 16-bit signed integers"
description = "Subtracts two 16-bit signed integers, wrapping on overflow"
fn =
(function
| _, vm, _, [ DInt16 a; DInt16 b ] ->
try
let result = Checked.(-) a b
Ply(DInt16(result))
with :? System.OverflowException ->
RTE.Ints.OutOfRange |> RTE.Int |> raiseRTE vm.threadID
| _, _, _, [ DInt16 a; DInt16 b ] -> Ply(DInt16(a - b))
| _ -> incorrectArgs ())
sqlSpec = NotYetImplemented
previewable = Pure
Expand All @@ -135,15 +125,10 @@ let fns () : List<BuiltInFn> =
typeParams = []
parameters = [ Param.make "a" TInt16 ""; Param.make "b" TInt16 "" ]
returnType = TInt16
description = "multiplies two 16-bit signed integers"
description = "Multiplies two 16-bit signed integers, wrapping on overflow"
fn =
(function
| _, vm, _, [ DInt16 a; DInt16 b ] ->
try
let result = Checked.(*) a b
Ply(DInt16(result))
with :? System.OverflowException ->
RTE.Ints.OutOfRange |> RTE.Int |> raiseRTE vm.threadID
| _, _, _, [ DInt16 a; DInt16 b ] -> Ply(DInt16(a * b))
| _ -> incorrectArgs ())
sqlSpec = NotYetImplemented
previewable = Pure
Expand All @@ -158,17 +143,19 @@ let fns () : List<BuiltInFn> =
description =
"Raise <param base> to the power of <param exponent>.
<param exponent> must to be positive.
Return value wrapped in a {{Result}} "
Overflow wraps around."
fn =
(function
| _, vm, _, [ DInt16 number; DInt16 exp ] ->
(try
if exp < 0s then
RTE.Ints.NegativeExponent |> RTE.Int |> raiseRTE vm.threadID
else
(bigint number) ** (int exp) |> int16 |> DInt16 |> Ply
with :? System.OverflowException ->
RTE.Ints.OutOfRange |> RTE.Int |> raiseRTE vm.threadID)
if exp < 0s then
RTE.Ints.NegativeExponent |> RTE.Int |> raiseRTE vm.threadID
else
// wrap on overflow via modular exponentiation
let m = System.Numerics.BigInteger.Pow(bigint 2, 16)
let r = System.Numerics.BigInteger.ModPow(bigint number, bigint exp, m)
let r = ((r % m) + m) % m
let r = if r >= m / bigint 2 then r - m else r
int16 r |> DInt16 |> Ply
| _ -> incorrectArgs ())
sqlSpec = NotYetImplemented
previewable = Pure
Expand All @@ -186,15 +173,9 @@ let fns () : List<BuiltInFn> =
| _, vm, _, [ DInt16 a; DInt16 b ] ->
if b = 0s then
RTE.Ints.DivideByZeroError |> RTE.Int |> raiseRTE vm.threadID
else if a = int16 System.Int16.MinValue && b = -1s then
RTE.Ints.OutOfRange |> RTE.Int |> raiseRTE vm.threadID
else
let result = a / b
if result < System.Int16.MinValue || result > System.Int16.MaxValue then
RTE.Ints.OutOfRange |> RTE.Int |> raiseRTE vm.threadID
else
Ply(DInt16(int16 result))

// -32768s / -1s divides in int32 then narrows, wrapping instead of throwing
Ply(DInt16(a / b))
| _ -> incorrectArgs ())
sqlSpec = NotYetImplemented
previewable = Pure
Expand All @@ -209,13 +190,7 @@ let fns () : List<BuiltInFn> =
description = "Returns the negation of <param a>, {{-a}}"
fn =
(function
| _, vm, _, [ DInt16 a ] ->
if a = System.Int16.MinValue then
RTE.Ints.OutOfRange |> RTE.Int |> raiseRTE vm.threadID
else
let result = -a
Ply(DInt16 result)

| _, _, _, [ DInt16 a ] -> Ply(DInt16(-a))
| _ -> incorrectArgs ())
sqlSpec = NotYetImplemented
previewable = Pure
Expand Down
51 changes: 19 additions & 32 deletions backend/src/Builtins/Builtins.Pure/Libs/Int32.fs
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,10 @@ let fns () : List<BuiltInFn> =
typeParams = []
parameters = [ Param.make "a" TInt32 ""; Param.make "b" TInt32 "" ]
returnType = TInt32
description = "Adds two 32-bit signed integers together"
description = "Adds two 32-bit signed integers together, wrapping on overflow"
fn =
(function
| _, vm, _, [ DInt32 a; DInt32 b ] ->
try
DInt32(Checked.(+) a b) |> Ply
with :? System.OverflowException ->
RTE.Ints.OutOfRange |> RTE.Int |> raiseRTE vm.threadID
| _, _, _, [ DInt32 a; DInt32 b ] -> Ply(DInt32(a + b))
| _ -> incorrectArgs ())
sqlSpec = NotYetImplemented
previewable = Pure
Expand All @@ -114,14 +110,10 @@ let fns () : List<BuiltInFn> =
typeParams = []
parameters = [ Param.make "a" TInt32 ""; Param.make "b" TInt32 "" ]
returnType = TInt32
description = "Subtracts two 32-bit signed integers"
description = "Subtracts two 32-bit signed integers, wrapping on overflow"
fn =
(function
| _, vm, _, [ DInt32 a; DInt32 b ] ->
try
DInt32(Checked.(-) a b) |> Ply
with :? System.OverflowException ->
RTE.Ints.OutOfRange |> RTE.Int |> raiseRTE vm.threadID
| _, _, _, [ DInt32 a; DInt32 b ] -> Ply(DInt32(a - b))
| _ -> incorrectArgs ())
sqlSpec = NotYetImplemented
previewable = Pure
Expand All @@ -133,14 +125,10 @@ let fns () : List<BuiltInFn> =
typeParams = []
parameters = [ Param.make "a" TInt32 ""; Param.make "b" TInt32 "" ]
returnType = TInt32
description = "Multiplies two 32-bit signed integers"
description = "Multiplies two 32-bit signed integers, wrapping on overflow"
fn =
(function
| _, vm, _, [ DInt32 a; DInt32 b ] ->
try
DInt32(Checked.(*) a b) |> Ply
with :? System.OverflowException ->
RTE.Ints.OutOfRange |> RTE.Int |> raiseRTE vm.threadID
| _, _, _, [ DInt32 a; DInt32 b ] -> Ply(DInt32(a * b))
| _ -> incorrectArgs ())
sqlSpec = NotYetImplemented
previewable = Pure
Expand All @@ -155,17 +143,19 @@ let fns () : List<BuiltInFn> =
description =
"Raise <param base> to the power of <param exponent>.
<param exponent> must to be positive.
Return value wrapped in a {{Result}} "
Overflow wraps around."
fn =
(function
| _, vm, _, [ DInt32 number; DInt32 exp ] ->
(try
if exp < 0 then
RTE.Ints.NegativeExponent |> RTE.Int |> raiseRTE vm.threadID
else
(bigint number) ** (int exp) |> int32 |> DInt32 |> Ply
with :? System.OverflowException ->
RTE.Ints.OutOfRange |> RTE.Int |> raiseRTE vm.threadID)
if exp < 0 then
RTE.Ints.NegativeExponent |> RTE.Int |> raiseRTE vm.threadID
else
// wrap on overflow via modular exponentiation
let m = System.Numerics.BigInteger.Pow(bigint 2, 32)
let r = System.Numerics.BigInteger.ModPow(bigint number, bigint exp, m)
let r = ((r % m) + m) % m
let r = if r >= m / bigint 2 then r - m else r
int32 r |> DInt32 |> Ply
| _ -> incorrectArgs ())
sqlSpec = NotYetImplemented
previewable = Pure
Expand All @@ -184,7 +174,8 @@ let fns () : List<BuiltInFn> =
if b = 0 then
RTE.Ints.DivideByZeroError |> RTE.Int |> raiseRTE vm.threadID
else if a = System.Int32.MinValue && b = -1 then
RTE.Ints.OutOfRange |> RTE.Int |> raiseRTE vm.threadID
// wraps back to MinValue (the division itself would throw)
Ply(DInt32 System.Int32.MinValue)
else
Ply(DInt32(a / b))
| _ -> incorrectArgs ())
Expand All @@ -201,11 +192,7 @@ let fns () : List<BuiltInFn> =
description = "Returns the negation of <param a>, {{-a}}"
fn =
(function
| _, vm, _, [ DInt32 a ] ->
if a = System.Int32.MinValue then
RTE.Ints.OutOfRange |> RTE.Int |> raiseRTE vm.threadID
else
Ply(DInt32(-a))
| _, _, _, [ DInt32 a ] -> Ply(DInt32(-a))
| _ -> incorrectArgs ())
sqlSpec = NotQueryable
previewable = Pure
Expand Down
Loading