Skip to content

Commit

Permalink
Include gcd method for all types
Browse files Browse the repository at this point in the history
  • Loading branch information
propensive committed Jun 27, 2024
1 parent ea0053b commit 80c89bc
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/core/hypotenuse-core.scala
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ extension (byte: Byte)
@targetName("floorDivByte")
inline infix def \ (right: Int): Int = math.floorDiv(byte, right)

@tailrec @targetName("gcdByte")
def gcd(right: Byte): Byte = if right == 0 then byte else right.gcd((byte%right).toByte)

extension (short: Short)
@targetName("bitsShort")
inline def bits: B16 = short.asInstanceOf[B16]
Expand Down Expand Up @@ -209,6 +212,9 @@ extension (short: Short)
@targetName("floorDivShort")
inline infix def \ (right: Short): Short = math.floorDiv(short, right).toShort

@tailrec @targetName("gcdShort")
def gcd(right: Short): Short = if right == 0 then short else right.gcd((short%right).toShort)

extension (int: Int)
@targetName("bitsInt")
inline def bits: B32 = int.asInstanceOf[B32]
Expand Down Expand Up @@ -240,6 +246,9 @@ extension (int: Int)
@targetName("floorDivInt")
inline infix def \ (right: Int): Int = math.floorDiv(int, right)

@tailrec @targetName("gcdInt")
def gcd(right: Int): Int = if right == 0 then int else right.gcd(int%right)

extension (long: Long)
@targetName("absLong")
inline def abs: Long = math.abs(long)
Expand Down Expand Up @@ -268,6 +277,9 @@ extension (long: Long)
@targetName("powerLong")
inline infix def ** (exponent: Double): Double = math.pow(long.toDouble, exponent)

@tailrec @targetName("gcdLong")
def gcd(right: Long): Long = if right == 0 then long else right.gcd(long%right)

extension (doubleObject: Double.type)
inline def apply(long: Long): Double = JDouble.longBitsToDouble(long)

Expand Down
61 changes: 61 additions & 0 deletions src/core/hypotenuse.Hypotenuse.scala
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,15 @@ object Hypotenuse:
@targetName("bitsS64")
inline def bits: B64 = s64

@targetName("gcdS64")
def gcd(right: S64): S64 =
@tailrec
def recur(left: S64, right: S64): S64 =
if right == 0 then left else recur(right, left%right)

recur(s64, right)


extension (s32: S32)
@targetName("plusS32")
inline infix def + (right: into S32)(using overflow: CheckOverflow): overflow.Wrap[S32] =
Expand Down Expand Up @@ -483,6 +492,14 @@ object Hypotenuse:
@targetName("bitsS32")
inline def bits: B32 = s32

@targetName("gcdS32")
def gcd(right: S32): S32 =
@tailrec
def recur(left: S32, right: S32): S32 =
if right == 0 then left else recur(right, left%right)

recur(s32, right)

extension (s16: S16)
@targetName("plusS16")
inline infix def + (right: into S16)(using overflow: CheckOverflow): overflow.Wrap[S16] =
Expand Down Expand Up @@ -531,6 +548,14 @@ object Hypotenuse:
@targetName("bitsS16")
inline def bits: B16 = s16

@targetName("gcdS16")
def gcd(right: S16): S16 =
@tailrec
def recur(left: S16, right: S16): S16 =
if right == 0 then left else recur(right, (left%right).toShort)

recur(s16, right)

extension (s8: S8)
@targetName("plusS8")
inline infix def + (right: into S8)(using overflow: CheckOverflow): overflow.Wrap[S8] =
Expand Down Expand Up @@ -583,6 +608,14 @@ object Hypotenuse:
inline def bits: B8 = s8


@targetName("gcdS8")
def gcd(right: S8): S8 =
@tailrec
def recur(left: S8, right: S8): S8 =
if right == 0 then left else recur(right, (left%right).toByte)

recur(s8, right)

extension (bitmap: B8)
@targetName("rotateLeftB8")
inline infix def <<< (count: Int): B8 = ((bitmap << count%%8) | (bitmap >>> (8 - count%%8))).toByte
Expand Down Expand Up @@ -1056,6 +1089,12 @@ object Hypotenuse:
@targetName("modU64")
inline infix def % (right: into U64): U64 = JLong.remainderUnsigned(u64, right)

@targetName("gcdU64")
def gcd(right: U64): U64 =
@tailrec
def recur(left: U64, right: U64): U64 = if right == 0 then left else recur(right, left%right)
recur(u64, right)

extension (u32: U32)
@targetName("plusU32")
inline infix def + (right: into U32)(using overflow: CheckOverflow): overflow.Wrap[U32] =
Expand Down Expand Up @@ -1101,6 +1140,12 @@ object Hypotenuse:
@targetName("u32ToU64")
inline def u64: U64 = JInt.toUnsignedLong(u32)

@targetName("gcdU32")
def gcd(right: U32): U32 =
@tailrec
def recur(left: U32, right: U32): U32 = if right == 0 then left else recur(right, left%right)
recur(u32, right)

extension (u16: U16)
@targetName("plusU16")
inline infix def + (right: into U16)(using overflow: CheckOverflow): overflow.Wrap[U16] =
Expand Down Expand Up @@ -1156,6 +1201,14 @@ object Hypotenuse:
@targetName("u16ToU64")
inline def u64: U64 = JShort.toUnsignedLong(u16)

@targetName("gcdU16")
def gcd(right: U16): U16 =
@tailrec
def recur(left: U16, right: U16): U16 =
if right == 0 then left else recur(right, (left%right).toShort)

recur(u16, right)

extension (u8: U8)
@targetName("plusU8")
inline infix def + (right: into U8)(using overflow: CheckOverflow): overflow.Wrap[U8] =
Expand Down Expand Up @@ -1221,3 +1274,11 @@ object Hypotenuse:

@targetName("u8ToU64")
inline def u64: U64 = JByte.toUnsignedLong(u8)

@targetName("gcdU8")
def gcd(right: U8): U8 =
@tailrec
def recur(left: U8, right: U8): U8 =
if right == 0 then left else recur(right, (left%right).toByte)

recur(u8, right)

0 comments on commit 80c89bc

Please sign in to comment.