From cfeb9d2c5007172e5f879143d68740f515b32304 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arne=20D=C3=B6ring?= Date: Thu, 6 Jun 2019 08:36:20 +0200 Subject: [PATCH] Bit operator names 2 (#11413) * add bit operator names * add test for bitop alias --- lib/pure/bitops.nim | 11 +++++++++++ tests/stdlib/tbitops.nim | 38 +++++++++++++++++++++++++++++++++++++- 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/lib/pure/bitops.nim b/lib/pure/bitops.nim index 51e4a6c6a0..c3cfc03a90 100644 --- a/lib/pure/bitops.nim +++ b/lib/pure/bitops.nim @@ -25,6 +25,17 @@ ## At this time only `fastLog2`, `firstSetBit, `countLeadingZeroBits`, `countTrailingZeroBits` ## may return undefined and/or platform dependant value if given invalid input. +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`. + +proc bitor*[T: SomeInteger](x, y: T): T {.magic: "BitorI", noSideEffect.} + ## Computes the `bitwise or` of numbers `x` and `y`. + +proc bitxor*[T: SomeInteger](x, y: T): T {.magic: "BitxorI", noSideEffect.} + ## Computes the `bitwise xor` of numbers `x` and `y`. const useBuiltins = not defined(noIntrinsicsBitOpts) const noUndefined = defined(noUndefinedBitOpts) diff --git a/tests/stdlib/tbitops.nim b/tests/stdlib/tbitops.nim index 640b289a1a..b2393b7551 100644 --- a/tests/stdlib/tbitops.nim +++ b/tests/stdlib/tbitops.nim @@ -16,6 +16,42 @@ proc main() = const U64B = 0b00110010_11011101_10001111_00101000_00000000_00000000_00000000_00000000'u64 const I64B = 0b00110010_11011101_10001111_00101000_00000000_00000000_00000000_00000000'i64 + doAssert( (U8 and U8) == bitand(U8,U8) ) + doAssert( (I8 and I8) == bitand(I8,I8) ) + doAssert( (U16 and U16) == bitand(U16,U16) ) + doAssert( (I16 and I16) == bitand(I16,I16) ) + doAssert( (U32 and U32) == bitand(U32,U32) ) + doAssert( (I32 and I32) == bitand(I32,I32) ) + doAssert( (U64A and U64B) == bitand(U64A,U64B) ) + doAssert( (I64A and I64B) == bitand(I64A,I64B) ) + + doAssert( (U8 or U8) == bitor(U8,U8) ) + doAssert( (I8 or I8) == bitor(I8,I8) ) + doAssert( (U16 or U16) == bitor(U16,U16) ) + doAssert( (I16 or I16) == bitor(I16,I16) ) + doAssert( (U32 or U32) == bitor(U32,U32) ) + doAssert( (I32 or I32) == bitor(I32,I32) ) + doAssert( (U64A or U64B) == bitor(U64A,U64B) ) + doAssert( (I64A or I64B) == bitor(I64A,I64B) ) + + doAssert( (U8 xor U8) == bitxor(U8,U8) ) + doAssert( (I8 xor I8) == bitxor(I8,I8) ) + doAssert( (U16 xor U16) == bitxor(U16,U16) ) + doAssert( (I16 xor I16) == bitxor(I16,I16) ) + doAssert( (U32 xor U32) == bitxor(U32,U32) ) + doAssert( (I32 xor I32) == bitxor(I32,I32) ) + doAssert( (U64A xor U64B) == bitxor(U64A,U64B) ) + doAssert( (I64A xor I64B) == bitxor(I64A,I64B) ) + + doAssert( not(U8) == bitnot(U8) ) + doAssert( not(I8) == bitnot(I8) ) + doAssert( not(U16) == bitnot(U16) ) + doAssert( not(I16) == bitnot(I16) ) + doAssert( not(U32) == bitnot(U32) ) + doAssert( not(I32) == bitnot(I32) ) + doAssert( not(U64A) == bitnot(U64A) ) + doAssert( not(I64A) == bitnot(I64A) ) + doAssert( U64A.fastLog2 == 62) doAssert( I64A.fastLog2 == 62) doAssert( U64A.countLeadingZeroBits == 1) @@ -172,7 +208,7 @@ proc main() = block: proc testReverseBitsInvo(x: SomeUnsignedInt) = doAssert(reverseBits(reverseBits(x)) == x) - + proc testReverseBitsPerType(x, reversed: uint64) = doAssert reverseBits(x) == reversed doAssert reverseBits(uint32(x)) == uint32(reversed shr 32)