Skip to content

Commit

Permalink
Include forgotten changes in math.scala too
Browse files Browse the repository at this point in the history
  • Loading branch information
propensive committed Feb 27, 2024
1 parent 1ba6094 commit 3a6a18f
Showing 1 changed file with 79 additions and 32 deletions.
111 changes: 79 additions & 32 deletions src/core/math.scala
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ package arithmeticOptions:
else U16((Short(left.bits)/Short(right.bits)).toShort.bits)

inline def divideI16(left: I16, right: I16): I16 raises DivisionError =
if right.int == 0 then raise(DivisionError())(I16(0.bits)) else I16((left.short/right.short).toShort.bits)
if right.int == 0 then raise(DivisionError())(I16(0.bits))
else I16((left.short/right.short).toShort.bits)

inline def divideU8(left: U8, right: U8): U8 raises DivisionError =
if right.int == 0 then raise(DivisionError())(U8(0.bits)) else U8((left.byte/right.byte).toByte.bits)
Expand Down Expand Up @@ -182,7 +183,9 @@ object Hypotenuse:

object F64:
erased given underlying: Underlying[F64, Double] = erasedValue
inline given canEqual: CanEqual[F64, F64 | I64 | I32 | I16 | I8 | Double | Long | Int | Short | Byte] = erasedValue

inline given canEqual: CanEqual[F64, F64 | I64 | I32 | I16 | I8 | Double | Long | Int | Short | Byte] =
erasedValue

inline def apply(sign: Boolean, exponent: B16, mantissa: B64): F64 =
F64((if sign then Long.MinValue else 0L) | ((exponent & 0xffL) << 52) | (mantissa & 0xfffffffffffffL))
Expand All @@ -191,22 +194,27 @@ object Hypotenuse:
inline def apply(double: Double): F64 = double

inline given inequality: Inequality[F64, F64] with
inline def compare(inline left: F64, inline right: F64, inline strict: Boolean,
inline greaterThan: Boolean): Boolean =

inline def compare
(inline left: F64, inline right: F64, inline strict: Boolean, inline greaterThan: Boolean)
: Boolean =

inline if greaterThan
then inline if strict then left > right else left >= right
else inline if strict then left < right else left <= right

inline given inequalityInt: Inequality[F64, Int] with
inline def compare(inline left: F64, inline right: Int, inline strict: Boolean,
inline greaterThan: Boolean): Boolean =

inline def compare
(inline left: F64, inline right: Int, inline strict: Boolean, inline greaterThan: Boolean)
: Boolean =

inline if greaterThan
then inline if strict then left > right else left >= right
else inline if strict then left < right else left <= right

inline given inequalityDouble: Inequality[F64, Double] with

inline def compare(inline left: F64, inline right: Double, inline strict: Boolean,
inline greaterThan: Boolean): Boolean =

Expand Down Expand Up @@ -249,11 +257,15 @@ object Hypotenuse:

object F32:
erased given underlying: Underlying[F32, Float] = erasedValue
inline given canEqual: CanEqual[F32, F32 | I64 | I32 | I16 | I8 | Float | Long | Int | Short | Byte] = erasedValue

inline given canEqual: CanEqual[F32, F32 | I64 | I32 | I16 | I8 | Float | Long | Int | Short | Byte] =
erasedValue

inline given inequality: Inequality[F32, F32] with
inline def compare(inline left: F32, inline right: F32, inline strict: Boolean,
inline greaterThan: Boolean): Boolean =

inline def compare
(inline left: F32, inline right: F32, inline strict: Boolean, inline greaterThan: Boolean)
: Boolean =

inline if greaterThan
then inline if strict then left > right else left >= right
Expand Down Expand Up @@ -298,8 +310,10 @@ object Hypotenuse:
inline def apply(bits: B64): U64 = bits

inline given inequality: Inequality[U64, U64] with
inline def compare(inline left: U64, inline right: U64, inline strict: Boolean,
inline greaterThan: Boolean): Boolean =

inline def compare
(inline left: U64, inline right: U64, inline strict: Boolean, inline greaterThan: Boolean)
: Boolean =

inline if greaterThan then
inline if strict then JLong.compareUnsigned(left, right) == 1
Expand All @@ -310,16 +324,21 @@ object Hypotenuse:

object I64:
erased given underlying: Underlying[I64, Long] = erasedValue
inline given canEqual: CanEqual[I64, F64 | F32 | I64 | I32 | I16 | I8 | Float | Double | Long | Int | Short | Byte] = erasedValue
inline given canEqual
: CanEqual[I64, F64 | F32 | I64 | I32 | I16 | I8 | Float | Double | Long | Int | Short | Byte] =
erasedValue

given fromDigits: FromDigits[I64] with
inline def fromDigits(digits: String): I64 = ${Hypotenuse2.parseI64('digits)}

given textualizer: Textualizer[I64] = _.toString.tt
inline def apply(bits: B64): I64 = bits

inline given inequality: Inequality[I64, I64] with
inline def compare(inline left: I64, inline right: I64, inline strict: Boolean,
inline greaterThan: Boolean): Boolean =

inline def compare
(inline left: I64, inline right: I64, inline strict: Boolean, inline greaterThan: Boolean)
: Boolean =

inline if greaterThan
then inline if strict then left > right else left >= right
Expand All @@ -329,15 +348,18 @@ object Hypotenuse:
object U32:
erased given underlying: Underlying[U32, Int] = erasedValue
inline given canEqual: CanEqual[U32, U32] = erasedValue

given fromDigits: FromDigits[U32] with
inline def fromDigits(digits: String): U32 = ${Hypotenuse2.parseU32('digits)}

given textualizer: Textualizer[U32] = JInt.toUnsignedString(_).nn.tt
inline def apply(bits: B32): U32 = bits

inline given inequality: Inequality[U32, U32] with
inline def compare(inline left: U32, inline right: U32, inline strict: Boolean,
inline greaterThan: Boolean): Boolean =

inline def compare
(inline left: U32, inline right: U32, inline strict: Boolean, inline greaterThan: Boolean)
: Boolean =

inline if greaterThan then
inline if strict then JLong.compareUnsigned(left, right) == 1
Expand All @@ -348,16 +370,21 @@ object Hypotenuse:

object I32:
erased given underlying: Underlying[I32, Int] = erasedValue
inline given canEqual: CanEqual[I32, F64 | F32 | I64 | I32 | I16 | I8 | Float | Double | Long | Int | Short | Byte] = erasedValue
inline given canEqual
: CanEqual[I32, F64 | F32 | I64 | I32 | I16 | I8 | Float | Double | Long | Int | Short | Byte] =
erasedValue

given fromDigits: FromDigits[I32] with
inline def fromDigits(digits: String): I32 = ${Hypotenuse2.parseI32('digits)}

given textualizer: Textualizer[I32] = _.toString.tt
inline def apply(bits: B32): I32 = bits

inline given inequality: Inequality[I32, I32] with
inline def compare(inline left: I32, inline right: I32, inline strict: Boolean,
inline greaterThan: Boolean): Boolean =

inline def compare
(inline left: I32, inline right: I32, inline strict: Boolean, inline greaterThan: Boolean)
: Boolean =

inline if greaterThan
then inline if strict then left > right else left >= right
Expand All @@ -366,15 +393,18 @@ object Hypotenuse:
object U16:
erased given underlying: Underlying[U16, Short] = erasedValue
inline given canEqual: CanEqual[U16, U16] = erasedValue

given fromDigits: FromDigits[U16] with
inline def fromDigits(digits: String): U16 = ${Hypotenuse2.parseU16('digits)}

given textualizer: Textualizer[U16] = u16 => JShort.toUnsignedInt(u16).toString.nn.tt
inline def apply(bits: B16): U16 = bits

inline given inequality: Inequality[U16, U16] with
inline def compare(inline left: U16, inline right: U16, inline strict: Boolean,
inline greaterThan: Boolean): Boolean =

inline def compare
(inline left: U16, inline right: U16, inline strict: Boolean, inline greaterThan: Boolean)
: Boolean =

val left2 = JShort.toUnsignedInt(left)
val right2 = JShort.toUnsignedInt(right)
Expand All @@ -385,16 +415,22 @@ object Hypotenuse:

object I16:
erased given underlying: Underlying[I16, Short] = erasedValue
inline given canEqual: CanEqual[I16, F64 | F32 | I64 | I32 | I16 | I8 | Float | Double | Long | Int | Short | Byte] = erasedValue

inline given canEqual
: CanEqual[I16, F64 | F32 | I64 | I32 | I16 | I8 | Float | Double | Long | Int | Short | Byte] =
erasedValue

given fromDigits: FromDigits[I16] with
inline def fromDigits(digits: String): I16 = ${Hypotenuse2.parseI16('digits)}

given textualizer: Textualizer[I16] = _.toString.tt
inline def apply(bits: B16): I16 = bits

inline given inequality: Inequality[I16, I16] with
inline def compare(inline left: I16, inline right: I16, inline strict: Boolean,
inline greaterThan: Boolean): Boolean =

inline def compare
(inline left: I16, inline right: I16, inline strict: Boolean, inline greaterThan: Boolean)
: Boolean =

inline if greaterThan
then inline if strict then left > right else left >= right
Expand All @@ -411,8 +447,10 @@ object Hypotenuse:


inline given inequality: Inequality[U8, U8] with
inline def compare(inline left: U8, inline right: U8, inline strict: Boolean,
inline greaterThan: Boolean): Boolean =

inline def compare
(inline left: U8, inline right: U8, inline strict: Boolean, inline greaterThan: Boolean)
: Boolean =

val left2 = JByte.toUnsignedInt(left)
val right2 = JByte.toUnsignedInt(right)
Expand All @@ -423,16 +461,22 @@ object Hypotenuse:

object I8:
erased given underlying: Underlying[I8, Byte] = erasedValue
inline given canEqual: CanEqual[I8, F64 | F32 | I64 | I32 | I16 | I8 | Float | Double | Long | Int | Short | Byte] = erasedValue

inline given canEqual
: CanEqual[I8, F64 | F32 | I64 | I32 | I16 | I8 | Float | Double | Long | Int | Short | Byte] =
erasedValue

given fromDigits: FromDigits[I8] with
inline def fromDigits(digits: String): I8 = ${Hypotenuse2.parseI8('digits)}

given textualizer: Textualizer[I8] = _.toString.tt
inline def apply(bits: B8): I8 = bits

inline given inequality: Inequality[I8, I8] with
inline def compare(inline left: I8, inline right: I8, inline strict: Boolean,
inline greaterThan: Boolean): Boolean =

inline def compare
(inline left: I8, inline right: I8, inline strict: Boolean, inline greaterThan: Boolean)
: Boolean =

inline if greaterThan
then inline if strict then left > right else left >= right
Expand Down Expand Up @@ -1144,7 +1188,8 @@ object Hypotenuse:

extension (u32: U32)
@targetName("plusU32")
inline infix def + (right: into U32)(using overflow: CheckOverflow): overflow.Wrap[U32] = overflow.addU32(u32, right)
inline infix def + (right: into U32)(using overflow: CheckOverflow): overflow.Wrap[U32] =
overflow.addU32(u32, right)

@targetName("bitsU32")
inline def bits: B32 = u32
Expand Down Expand Up @@ -1188,7 +1233,8 @@ object Hypotenuse:

extension (u16: U16)
@targetName("plusU16")
inline infix def + (right: into U16)(using overflow: CheckOverflow): overflow.Wrap[U16] = overflow.addU16(u16, right)
inline infix def + (right: into U16)(using overflow: CheckOverflow): overflow.Wrap[U16] =
overflow.addU16(u16, right)

@targetName("bitsU16")
inline def bits: B16 = u16
Expand Down Expand Up @@ -1242,7 +1288,8 @@ object Hypotenuse:

extension (u8: U8)
@targetName("plusU8")
inline infix def + (right: into U8)(using overflow: CheckOverflow): overflow.Wrap[U8] = overflow.addU8(u8, right)
inline infix def + (right: into U8)(using overflow: CheckOverflow): overflow.Wrap[U8] =
overflow.addU8(u8, right)

@targetName("bitsU8")
inline def bits: B8 = u8
Expand Down

0 comments on commit 3a6a18f

Please sign in to comment.