Make bitand, bitor, bitxor varargs-friendly (#13985)

* made bitand, bitor, bitxor varargs friendly
* changed new bitops to macros
* changed macro signature for consistency (this technically doesn't matter)
* added tests
* removed redundant assert
* fix literal
This commit is contained in:
awr1
2020-04-20 07:48:37 -05:00
committed by GitHub
parent 59f1462b95
commit 59aeaa1c98
2 changed files with 34 additions and 9 deletions

View File

@@ -25,17 +25,37 @@
## At this time only `fastLog2`, `firstSetBit, `countLeadingZeroBits`, `countTrailingZeroBits`
## may return undefined and/or platform dependent value if given invalid input.
import macros
proc bitnot*[T: SomeInteger](x: T): T {.magic: "BitnotI", noSideEffect.}
## Computes the `bitwise complement` of the integer `x`.
proc bitand*[T: SomeInteger](x, y: T): T {.magic: "BitandI", noSideEffect.}
## Computes the `bitwise and` of numbers `x` and `y`.
func internalBitand[T: SomeInteger](x, y: T): T {.magic: "BitandI".}
proc bitor*[T: SomeInteger](x, y: T): T {.magic: "BitorI", noSideEffect.}
## Computes the `bitwise or` of numbers `x` and `y`.
func internalBitor[T: SomeInteger](x, y: T): T {.magic: "BitorI".}
proc bitxor*[T: SomeInteger](x, y: T): T {.magic: "BitxorI", noSideEffect.}
## Computes the `bitwise xor` of numbers `x` and `y`.
func internalBitxor[T: SomeInteger](x, y: T): T {.magic: "BitxorI".}
macro bitand*[T: SomeInteger](x, y: T; z: varargs[T]): T =
## Computes the `bitwise and` of all arguments collectively.
let fn = bindSym("internalBitand")
result = newCall(fn, x, y)
for extra in z:
result = newCall(fn, result, extra)
macro bitor*[T: SomeInteger](x, y: T; z: varargs[T]): T =
## Computes the `bitwise or` of all arguments collectively.
let fn = bindSym("internalBitor")
result = newCall(fn, x, y)
for extra in z:
result = newCall(fn, result, extra)
macro bitxor*[T: SomeInteger](x, y: T; z: varargs[T]): T =
## Computes the `bitwise xor` of all arguments collectively.
let fn = bindSym("internalBitxor")
result = newCall(fn, x, y)
for extra in z:
result = newCall(fn, result, extra)
const useBuiltins = not defined(noIntrinsicsBitOpts)
const noUndefined = defined(noUndefinedBitOpts)
@@ -64,9 +84,6 @@ template forwardImpl(impl, arg) {.dirty.} =
impl(x.uint64)
when defined(nimHasalignOf):
import macros
type BitsRange*[T] = range[0..sizeof(T)*8-1]
## Returns a range with all bit positions for type ``T``.

View File

@@ -18,6 +18,8 @@ proc main1() =
const I64A = 0b01000100_00111111_01111100_10001010_10011001_01001000_01111010_00010001'i64
const U64B = 0b00110010_11011101_10001111_00101000_00000000_00000000_00000000_00000000'u64
const I64B = 0b00110010_11011101_10001111_00101000_00000000_00000000_00000000_00000000'i64
const U64C = 0b00101010_11110101_10001111_00101000_00000100_00000000_00000100_00000000'u64
const I64C = 0b00101010_11110101_10001111_00101000_00000100_00000000_00000100_00000000'i64
doAssert( (U8 and U8) == bitand(U8,U8) )
doAssert( (I8 and I8) == bitand(I8,I8) )
@@ -27,6 +29,8 @@ proc main1() =
doAssert( (I32 and I32) == bitand(I32,I32) )
doAssert( (U64A and U64B) == bitand(U64A,U64B) )
doAssert( (I64A and I64B) == bitand(I64A,I64B) )
doAssert( (U64A and U64B and U64C) == bitand(U64A,U64B,U64C) )
doAssert( (I64A and I64B and I64C) == bitand(I64A,I64B,I64C) )
doAssert( (U8 or U8) == bitor(U8,U8) )
doAssert( (I8 or I8) == bitor(I8,I8) )
@@ -36,6 +40,8 @@ proc main1() =
doAssert( (I32 or I32) == bitor(I32,I32) )
doAssert( (U64A or U64B) == bitor(U64A,U64B) )
doAssert( (I64A or I64B) == bitor(I64A,I64B) )
doAssert( (U64A or U64B or U64C) == bitor(U64A,U64B,U64C) )
doAssert( (I64A or I64B or I64C) == bitor(I64A,I64B,I64C) )
doAssert( (U8 xor U8) == bitxor(U8,U8) )
doAssert( (I8 xor I8) == bitxor(I8,I8) )
@@ -45,6 +51,8 @@ proc main1() =
doAssert( (I32 xor I32) == bitxor(I32,I32) )
doAssert( (U64A xor U64B) == bitxor(U64A,U64B) )
doAssert( (I64A xor I64B) == bitxor(I64A,I64B) )
doAssert( (U64A xor U64B xor U64C) == bitxor(U64A,U64B,U64C) )
doAssert( (I64A xor I64B xor I64C) == bitxor(I64A,I64B,I64C) )
doAssert( not(U8) == bitnot(U8) )
doAssert( not(I8) == bitnot(I8) )