Skip to content

Commit

Permalink
scaladoc improvements, Ints.toBits
Browse files Browse the repository at this point in the history
  • Loading branch information
kushti committed Sep 20, 2024
1 parent 55d03cd commit 5f0c5c5
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 14 deletions.
9 changes: 4 additions & 5 deletions core/shared/src/main/scala/sigma/SigmaDsl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -154,19 +154,18 @@ trait BigInt {
def |(that: BigInt): BigInt = or(that)

/**
* @return a big integer whose value is `this xor that`
* @return a big integer whose value is `this xor that`.
* This method returns a negative BigInteger if and only if exactly one of this and val are negative.
*/
def xor(that: BigInt): BigInt

/**
* @return a 256-bit signed integer whose value is (this << n). The shift distance, n, may be negative,
* in which case this method performs a right shift. (Computes floor(this * 2n).)
* @return a 256-bit signed integer whose value is (this << n). `n` should be in 0..255 range (inclusive).
*/
def shiftLeft(n: Int): BigInt

/**
* @return a 256-bit signed integer whose value is (this >> n). Sign extension is performed. The shift distance, n,
* may be negative, in which case this method performs a left shift. (Computes floor(this / 2n).)
* @return a 256-bit signed integer whose value is (this >> n). `n` should be in 0..255 range (inclusive).
*/
def shiftRight(n: Int): BigInt
}
Expand Down
14 changes: 7 additions & 7 deletions data/shared/src/main/scala/sigma/ast/methods.scala
Original file line number Diff line number Diff line change
Expand Up @@ -275,10 +275,10 @@ object SNumericTypeMethods extends MethodsContainer {
""".stripMargin)

/** Cost of inverting bits of a number. */
val BitwiseInverse_CostKind = FixedCost(JitCost(5))
val BitwiseOp_CostKind = FixedCost(JitCost(5))

val BitwiseInverseMethod: SMethod = SMethod(
this, "bitwiseInverse", SFunc(tNum, tNum), 8, BitwiseInverse_CostKind)
this, "bitwiseInverse", SFunc(tNum, tNum), 8, BitwiseOp_CostKind)
.withIRInfo(MethodCallIrBuilder)
.withUserDefinedInvoke({ (m: SMethod, obj: Any, _: Array[Any]) =>
m.objType match {
Expand All @@ -292,7 +292,7 @@ object SNumericTypeMethods extends MethodsContainer {
.withInfo(PropertyCall, desc = "Returns bitwise inverse of this numeric. ")

val BitwiseOrMethod: SMethod = SMethod(
this, "bitwiseOr", SFunc(Array(tNum, tNum), tNum), 9, BitwiseInverse_CostKind)
this, "bitwiseOr", SFunc(Array(tNum, tNum), tNum), 9, BitwiseOp_CostKind)
.withIRInfo(MethodCallIrBuilder)
.withUserDefinedInvoke({ (m: SMethod, obj: Any, other: Array[Any]) =>
m.objType match {
Expand All @@ -308,7 +308,7 @@ object SNumericTypeMethods extends MethodsContainer {
ArgInfo("that", "A numeric value to calculate or with."))

val BitwiseAndMethod: SMethod = SMethod(
this, "bitwiseAnd", SFunc(Array(tNum, tNum), tNum), 10, BitwiseInverse_CostKind)
this, "bitwiseAnd", SFunc(Array(tNum, tNum), tNum), 10, BitwiseOp_CostKind)
.withIRInfo(MethodCallIrBuilder)
.withUserDefinedInvoke({ (m: SMethod, obj: Any, other: Array[Any]) =>
m.objType match {
Expand All @@ -324,7 +324,7 @@ object SNumericTypeMethods extends MethodsContainer {
ArgInfo("that", "A numeric value to calculate and with."))

val BitwiseXorMethod: SMethod = SMethod(
this, "bitwiseXor", SFunc(Array(tNum, tNum), tNum), 11, BitwiseInverse_CostKind)
this, "bitwiseXor", SFunc(Array(tNum, tNum), tNum), 11, BitwiseOp_CostKind)
.withIRInfo(MethodCallIrBuilder)
.withUserDefinedInvoke({ (m: SMethod, obj: Any, other: Array[Any]) =>
m.objType match {
Expand All @@ -340,7 +340,7 @@ object SNumericTypeMethods extends MethodsContainer {
ArgInfo("that", "A numeric value to calculate xor with."))

val ShiftLeftMethod: SMethod = SMethod(
this, "shiftLeft", SFunc(Array(tNum, SInt), tNum), 12, BitwiseInverse_CostKind)
this, "shiftLeft", SFunc(Array(tNum, SInt), tNum), 12, BitwiseOp_CostKind)
.withIRInfo(MethodCallIrBuilder)
.withUserDefinedInvoke({ (m: SMethod, obj: Any, other: Array[Any]) =>
m.objType match {
Expand All @@ -359,7 +359,7 @@ object SNumericTypeMethods extends MethodsContainer {
"the size of the number in bits (e.g. 64 for Long, 256 for BigInt)"))

val ShiftRightMethod: SMethod = SMethod(
this, "shiftRight", SFunc(Array(tNum, SInt), tNum), 13, BitwiseInverse_CostKind)
this, "shiftRight", SFunc(Array(tNum, SInt), tNum), 13, BitwiseOp_CostKind)
.withIRInfo(MethodCallIrBuilder)
.withUserDefinedInvoke({ (m: SMethod, obj: Any, other: Array[Any]) =>
m.objType match {
Expand Down
3 changes: 1 addition & 2 deletions data/shared/src/main/scala/sigma/data/ExactIntegral.scala
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ object ExactIntegral {

implicit object ShortIsExactIntegral extends ExactIntegral[Short] {
val n = scala.math.Numeric.ShortIsIntegral
override def plus(x: Short, y: Short):
Short = x.addExact(y)
override def plus(x: Short, y: Short): Short = x.addExact(y)
override def minus(x: Short, y: Short): Short = x.subtractExact(y)
override def times(x: Short, y: Short): Short = x.multiplyExact(y)
override def toBigEndianBytes(x: Short): Coll[Byte] = Colls.fromItems((x >> 8).toByte, x.toByte)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,22 @@ class BasicOpsSpecification extends CompilerTestingCommons
}
}

property("Int.toBits") {
def toBytesTest() = test("Int.toBytes", env, ext,
"""{
| val l = 1477959696
| l.toBits == Coll(false, true, false, true, true, false, false, false, false, false, false, true, false, true, true ,true, true, true, true, false, false, false, false, false, false, false, false, true, false, false, false, false)
| }""".stripMargin,
null
)

if (VersionContext.current.isV6SoftForkActivated) {
toBytesTest()
} else {
an[Exception] shouldBe thrownBy(toBytesTest())
}
}

property("Byte.toBytes") {
def toBytesTest() = test("Byte.toBytes", env, ext,
"""{
Expand Down

0 comments on commit 5f0c5c5

Please sign in to comment.