-
-
Notifications
You must be signed in to change notification settings - Fork 419
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[RFC 58] Add partial arithmetic for integer types (#2865)
* [RFC 58] Add partial arithmetic for integer types * fix failing VerifyTest.PartialSugaredBinaryOperatorCall as it mapped +? to partial add, not to add_partial so in this respect, a class could not have both methods, but the integer typed need to have. * remove division test this one should be included in another PR fixing the underlying issue with detecting overflow on division for I128 on platforms without native 128 bit int support * remove empty lines [skip ci]
- Loading branch information
Showing
10 changed files
with
543 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
|
||
trait _PartialArithmetic | ||
fun add_partial[T: (Integer[T] val & Int)](x: T, y: T): T? => | ||
(let r: T, let overflow: Bool) = x.addc(y) | ||
if overflow then error else r end | ||
|
||
fun sub_partial[T: (Integer[T] val & Int)](x: T, y: T): T? => | ||
(let r: T, let overflow: Bool) = x.subc(y) | ||
if overflow then error else r end | ||
|
||
fun mul_partial[T: (Integer[T] val & Int)](x: T, y: T): T? => | ||
(let r: T, let overflow: Bool) = x.mulc(y) | ||
if overflow then error else r end | ||
|
||
primitive _UnsignedPartialArithmetic is _PartialArithmetic | ||
fun div_partial[T: _UnsignedInteger[T] val](x: T, y: T): T? => | ||
if (y == T.from[U8](0)) then | ||
error | ||
else | ||
x /~ y | ||
end | ||
|
||
fun mod_partial[T: _UnsignedInteger[T] val](x: T, y: T): T? => | ||
if (y == T.from[U8](0)) then | ||
error | ||
else | ||
x %~ y | ||
end | ||
|
||
fun divmod_partial[T: _UnsignedInteger[T] val](x: T, y: T): (T, T)? => | ||
if (y == T.from[U8](0)) then | ||
error | ||
else | ||
(x /~ y, x %~ y) | ||
end | ||
|
||
primitive _SignedPartialArithmetic is _PartialArithmetic | ||
|
||
fun div_partial[T: (_SignedInteger[T, U] val & Signed), U: _UnsignedInteger[U] val](x: T, y: T): T? => | ||
if (y == T.from[I8](0)) or ((y == T.from[I8](I8(-1))) and (x == T.min_value())) then | ||
error | ||
else | ||
x /~ y | ||
end | ||
|
||
fun mod_partial[T: (_SignedInteger[T, U] val & Signed), U: _UnsignedInteger[U] val](x: T, y: T): T? => | ||
if (y == T.from[I8](0)) or ((y == T.from[I8](I8(-1))) and (x == T.min_value())) then | ||
error | ||
else | ||
x %~ y | ||
end | ||
|
||
fun divmod_partial[T: (_SignedInteger[T, U] val & Signed), U: _UnsignedInteger[U] val](x: T, y: T): (T, T)? => | ||
if (y == T.from[I8](0)) or ((y == T.from[I8](I8(-1))) and (x == T.min_value())) then | ||
error | ||
else | ||
(x/~y, x %~ y) | ||
end | ||
|
||
fun neg_partial[T: (_SignedInteger[T, U] val & Signed), U: _UnsignedInteger[U] val](x: T): T? => | ||
if x == T.min_value() then | ||
error | ||
else | ||
-~x | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.