Skip to content

Commit

Permalink
shiftLeft/shiftRight shift limit
Browse files Browse the repository at this point in the history
  • Loading branch information
kushti committed Sep 11, 2024
1 parent fa2c2df commit 48db432
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 10 deletions.
18 changes: 15 additions & 3 deletions data/shared/src/main/scala/sigma/data/BigIntegerOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,21 @@ object NumericOps {

override def bitwiseXor(x: BigInt, y: BigInt): BigInt = x.xor(y)

override def shiftLeft(x: BigInt, y: Int): BigInt = x.shiftLeft(y)

override def shiftRight(x: BigInt, y: Int): BigInt = x.shiftRight(y)
override def shiftLeft(x: BigInt, bits: Int): BigInt = {
if (bits < 0 || bits >= 256) {
throw new IllegalArgumentException(s"Wrong argument in BigInt.shiftRight: bits < 0 || bits >= 256 ($bits)")
} else {
x.shiftLeft(bits)
}
}

override def shiftRight(x: BigInt, bits: Int): BigInt = {
if (bits < 0 || bits >= 256) {
throw new IllegalArgumentException(s"Wrong argument in BigInt.shiftRight: bits < 0 || bits >= 256 ($bits)")
} else {
x.shiftRight(bits)
}
}
}

/** The instance of [[scalan.ExactOrdering]] typeclass for [[BigInt]]. */
Expand Down
42 changes: 35 additions & 7 deletions data/shared/src/main/scala/sigma/data/ExactIntegral.scala
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,15 @@ object ExactIntegral {
override def bitwiseOr(x: Byte, y: Byte): Byte = (x | y).toByte
override def bitwiseAnd(x: Byte, y: Byte): Byte = (x & y).toByte
override def bitwiseXor(x: Byte, y: Byte): Byte = (x ^ y).toByte
override def shiftLeft(x: Byte, bits: Int): Byte = (x << bits).toByte
override def shiftLeft(x: Byte, bits: Int): Byte = {
if (bits < 0 || bits >= 8) {
throw new IllegalArgumentException(s"Wrong argument in Byte.shiftRight: bits < 0 || bits >= 8 ($bits)")
} else {
(x << bits).toByte
}
}
override def shiftRight(x: Byte, bits: Int): Byte = {
if (bits < 0 || bits >= 8){
if (bits < 0 || bits >= 8) {
throw new IllegalArgumentException(s"Wrong argument in Byte.shiftRight: bits < 0 || bits >= 8 ($bits)")
} else {
(x >> bits).toByte
Expand All @@ -64,7 +70,13 @@ object ExactIntegral {
override def bitwiseOr(x: Short, y: Short): Short = (x | y).toShort
override def bitwiseAnd(x: Short, y: Short): Short = (x & y).toShort
override def bitwiseXor(x: Short, y: Short): Short = (x ^ y).toShort
override def shiftLeft(x: Short, y: Int): Short = (x << y).toShort
override def shiftLeft(x: Short, bits: Int): Short = {
if (bits < 0 || bits >= 16) {
throw new IllegalArgumentException(s"Wrong argument in Short.shiftRight: bits < 0 || bits >= 16 ($bits)")
} else {
(x << bits).toShort
}
}
override def shiftRight(x: Short, bits: Int): Short = {
if (bits < 0 || bits >= 16){
throw new IllegalArgumentException(s"Wrong argument in Short.shiftRight: bits < 0 || bits >= 16 ($bits)")
Expand All @@ -85,9 +97,17 @@ object ExactIntegral {
override def bitwiseOr(x: Int, y: Int): Int = x | y
override def bitwiseAnd(x: Int, y: Int): Int = x & y
override def bitwiseXor(x: Int, y: Int): Int = x ^ y
override def shiftLeft(x: Int, y: Int): Int = x << y

override def shiftLeft(x: Int, bits: Int): Int = {
if (bits < 0 || bits >= 32) {
throw new IllegalArgumentException(s"Wrong argument in Byte.shiftRight: bits < 0 || bits >= 32 ($bits)")
} else {
x << bits
}
}

override def shiftRight(x: Int, bits: Int): Int = {
if (bits < 0 || bits >= 32){
if (bits < 0 || bits >= 32) {
throw new IllegalArgumentException(s"Wrong argument in Int.shiftRight: bits < 0 || bits >= 32 ($bits)")
} else {
x >> bits
Expand All @@ -106,9 +126,17 @@ object ExactIntegral {
override def bitwiseOr(x: Long, y: Long): Long = x | y
override def bitwiseAnd(x: Long, y: Long): Long = x & y
override def bitwiseXor(x: Long, y: Long): Long = x ^ y
override def shiftLeft(x: Long, y: Int): Long = x << y

override def shiftLeft(x: Long, bits: Int): Long = {
if (bits < 0 || bits >= 64) {
throw new IllegalArgumentException(s"Wrong argument in Long.shiftRight: bits < 0 || bits >= 64 ($bits)")
} else {
x << bits
}
}

override def shiftRight(x: Long, bits: Int): Long = {
if (bits < 0 || bits >= 64){
if (bits < 0 || bits >= 64) {
throw new IllegalArgumentException(s"Wrong argument in Long.shiftRight: bits < 0 || bits >= 64 ($bits)")
} else {
x >> bits
Expand Down

0 comments on commit 48db432

Please sign in to comment.