mirror of
https://github.com/nim-lang/Nim.git
synced 2026-04-27 09:43:58 +00:00
Vm bitops fixes (#10520)
This commit is contained in:
committed by
Andreas Rumpf
parent
bcb5995ddf
commit
824f39b32e
@@ -1,9 +1,9 @@
|
||||
discard """
|
||||
nimout: "OK"
|
||||
output: "OK"
|
||||
"""
|
||||
import bitops
|
||||
|
||||
|
||||
proc main() =
|
||||
const U8 = 0b0011_0010'u8
|
||||
const I8 = 0b0011_0010'i8
|
||||
@@ -79,25 +79,6 @@ proc main() =
|
||||
doAssert( U8.rotateLeftBits(3) == 0b10010001'u8)
|
||||
doAssert( U8.rotateRightBits(3) == 0b0100_0110'u8)
|
||||
|
||||
static :
|
||||
# test bitopts at compile time with vm
|
||||
doAssert( U8.fastLog2 == 5)
|
||||
doAssert( I8.fastLog2 == 5)
|
||||
doAssert( U8.countLeadingZeroBits == 2)
|
||||
doAssert( I8.countLeadingZeroBits == 2)
|
||||
doAssert( U8.countTrailingZeroBits == 1)
|
||||
doAssert( I8.countTrailingZeroBits == 1)
|
||||
doAssert( U8.firstSetBit == 2)
|
||||
doAssert( I8.firstSetBit == 2)
|
||||
doAssert( U8.parityBits == 1)
|
||||
doAssert( I8.parityBits == 1)
|
||||
doAssert( U8.countSetBits == 3)
|
||||
doAssert( I8.countSetBits == 3)
|
||||
doAssert( U8.rotateLeftBits(3) == 0b10010001'u8)
|
||||
doAssert( U8.rotateRightBits(3) == 0b0100_0110'u8)
|
||||
|
||||
|
||||
|
||||
template test_undefined_impl(ffunc: untyped; expected: int; is_static: bool) =
|
||||
doAssert( ffunc(0'u8) == expected)
|
||||
doAssert( ffunc(0'i8) == expected)
|
||||
@@ -142,26 +123,6 @@ proc main() =
|
||||
doAssert( U64A.rotateLeftBits(64) == U64A)
|
||||
doAssert( U64A.rotateRightBits(64) == U64A)
|
||||
|
||||
static: # check for undefined behavior with rotate by zero.
|
||||
doAssert( U8.rotateLeftBits(0) == U8)
|
||||
doAssert( U8.rotateRightBits(0) == U8)
|
||||
doAssert( U16.rotateLeftBits(0) == U16)
|
||||
doAssert( U16.rotateRightBits(0) == U16)
|
||||
doAssert( U32.rotateLeftBits(0) == U32)
|
||||
doAssert( U32.rotateRightBits(0) == U32)
|
||||
doAssert( U64A.rotateLeftBits(0) == U64A)
|
||||
doAssert( U64A.rotateRightBits(0) == U64A)
|
||||
|
||||
# check for undefined behavior with rotate by integer width.
|
||||
doAssert( U8.rotateLeftBits(8) == U8)
|
||||
doAssert( U8.rotateRightBits(8) == U8)
|
||||
doAssert( U16.rotateLeftBits(16) == U16)
|
||||
doAssert( U16.rotateRightBits(16) == U16)
|
||||
doAssert( U32.rotateLeftBits(32) == U32)
|
||||
doAssert( U32.rotateRightBits(32) == U32)
|
||||
doAssert( U64A.rotateLeftBits(64) == U64A)
|
||||
doAssert( U64A.rotateRightBits(64) == U64A)
|
||||
|
||||
block:
|
||||
# mask operations
|
||||
var v: uint8
|
||||
@@ -207,18 +168,22 @@ proc main() =
|
||||
var v: uint64
|
||||
v.setBit(63)
|
||||
doAssert v == 0b1000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000'u64
|
||||
block:
|
||||
# Test if RangeError is thrown if indexing out of range
|
||||
try:
|
||||
var v: uint32
|
||||
var i = 32
|
||||
v.setBit(i)
|
||||
doAssert false
|
||||
except RangeError:
|
||||
discard
|
||||
except:
|
||||
doAssert false
|
||||
|
||||
echo "OK"
|
||||
|
||||
block: # not ready for vm because exception is compile error
|
||||
try:
|
||||
var v: uint32
|
||||
var i = 32
|
||||
v.setBit(i)
|
||||
doAssert false
|
||||
except RangeError:
|
||||
discard
|
||||
except:
|
||||
doAssert false
|
||||
|
||||
|
||||
main()
|
||||
static:
|
||||
# test everything on vm as well
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user