tbitops2.nim 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. discard """
  2. output: "OK"
  3. """
  4. import bitops
  5. proc main() =
  6. const U8 = 0b0011_0010'u8
  7. const I8 = 0b0011_0010'i8
  8. const U16 = 0b00100111_00101000'u16
  9. const I16 = 0b00100111_00101000'i16
  10. const U32 = 0b11010101_10011100_11011010_01010000'u32
  11. const I32 = 0b11010101_10011100_11011010_01010000'i32
  12. const U64A = 0b01000100_00111111_01111100_10001010_10011001_01001000_01111010_00010001'u64
  13. const I64A = 0b01000100_00111111_01111100_10001010_10011001_01001000_01111010_00010001'i64
  14. const U64B = 0b00110010_11011101_10001111_00101000_00000000_00000000_00000000_00000000'u64
  15. const I64B = 0b00110010_11011101_10001111_00101000_00000000_00000000_00000000_00000000'i64
  16. doAssert( U64A.fastLog2 == 62)
  17. doAssert( I64A.fastLog2 == 62)
  18. doAssert( U64A.countLeadingZeroBits == 1)
  19. doAssert( I64A.countLeadingZeroBits == 1)
  20. doAssert( U64A.countTrailingZeroBits == 0)
  21. doAssert( I64A.countTrailingZeroBits == 0)
  22. doAssert( U64A.firstSetBit == 1)
  23. doAssert( I64A.firstSetBit == 1)
  24. doAssert( U64A.parityBits == 1)
  25. doAssert( I64A.parityBits == 1)
  26. doAssert( U64A.countSetBits == 29)
  27. doAssert( I64A.countSetBits == 29)
  28. doAssert( U64A.rotateLeftBits(37) == 0b00101001_00001111_01000010_00101000_10000111_11101111_10010001_01010011'u64)
  29. doAssert( U64A.rotateRightBits(37) == 0b01010100_11001010_01000011_11010000_10001010_00100001_11111011_11100100'u64)
  30. doAssert( U64B.firstSetBit == 36)
  31. doAssert( I64B.firstSetBit == 36)
  32. doAssert( U32.fastLog2 == 31)
  33. doAssert( I32.fastLog2 == 31)
  34. doAssert( U32.countLeadingZeroBits == 0)
  35. doAssert( I32.countLeadingZeroBits == 0)
  36. doAssert( U32.countTrailingZeroBits == 4)
  37. doAssert( I32.countTrailingZeroBits == 4)
  38. doAssert( U32.firstSetBit == 5)
  39. doAssert( I32.firstSetBit == 5)
  40. doAssert( U32.parityBits == 0)
  41. doAssert( I32.parityBits == 0)
  42. doAssert( U32.countSetBits == 16)
  43. doAssert( I32.countSetBits == 16)
  44. doAssert( U32.rotateLeftBits(21) == 0b01001010_00011010_10110011_10011011'u32)
  45. doAssert( U32.rotateRightBits(21) == 0b11100110_11010010_10000110_10101100'u32)
  46. doAssert( U16.fastLog2 == 13)
  47. doAssert( I16.fastLog2 == 13)
  48. doAssert( U16.countLeadingZeroBits == 2)
  49. doAssert( I16.countLeadingZeroBits == 2)
  50. doAssert( U16.countTrailingZeroBits == 3)
  51. doAssert( I16.countTrailingZeroBits == 3)
  52. doAssert( U16.firstSetBit == 4)
  53. doAssert( I16.firstSetBit == 4)
  54. doAssert( U16.parityBits == 0)
  55. doAssert( I16.parityBits == 0)
  56. doAssert( U16.countSetBits == 6)
  57. doAssert( I16.countSetBits == 6)
  58. doAssert( U16.rotateLeftBits(12) == 0b10000010_01110010'u16)
  59. doAssert( U16.rotateRightBits(12) == 0b01110010_10000010'u16)
  60. doAssert( U8.fastLog2 == 5)
  61. doAssert( I8.fastLog2 == 5)
  62. doAssert( U8.countLeadingZeroBits == 2)
  63. doAssert( I8.countLeadingZeroBits == 2)
  64. doAssert( U8.countTrailingZeroBits == 1)
  65. doAssert( I8.countTrailingZeroBits == 1)
  66. doAssert( U8.firstSetBit == 2)
  67. doAssert( I8.firstSetBit == 2)
  68. doAssert( U8.parityBits == 1)
  69. doAssert( I8.parityBits == 1)
  70. doAssert( U8.countSetBits == 3)
  71. doAssert( I8.countSetBits == 3)
  72. doAssert( U8.rotateLeftBits(3) == 0b10010001'u8)
  73. doAssert( U8.rotateRightBits(3) == 0b0100_0110'u8)
  74. static :
  75. # test bitopts at compile time with vm
  76. doAssert( U8.fastLog2 == 5)
  77. doAssert( I8.fastLog2 == 5)
  78. doAssert( U8.countLeadingZeroBits == 2)
  79. doAssert( I8.countLeadingZeroBits == 2)
  80. doAssert( U8.countTrailingZeroBits == 1)
  81. doAssert( I8.countTrailingZeroBits == 1)
  82. doAssert( U8.firstSetBit == 2)
  83. doAssert( I8.firstSetBit == 2)
  84. doAssert( U8.parityBits == 1)
  85. doAssert( I8.parityBits == 1)
  86. doAssert( U8.countSetBits == 3)
  87. doAssert( I8.countSetBits == 3)
  88. doAssert( U8.rotateLeftBits(3) == 0b10010001'u8)
  89. doAssert( U8.rotateRightBits(3) == 0b0100_0110'u8)
  90. template test_undefined_impl(ffunc: untyped; expected: int; is_static: bool) =
  91. doAssert( ffunc(0'u8) == expected)
  92. doAssert( ffunc(0'i8) == expected)
  93. doAssert( ffunc(0'u16) == expected)
  94. doAssert( ffunc(0'i16) == expected)
  95. doAssert( ffunc(0'u32) == expected)
  96. doAssert( ffunc(0'i32) == expected)
  97. doAssert( ffunc(0'u64) == expected)
  98. doAssert( ffunc(0'i64) == expected)
  99. template test_undefined(ffunc: untyped; expected: int) =
  100. test_undefined_impl(ffunc, expected, false)
  101. static:
  102. test_undefined_impl(ffunc, expected, true)
  103. when defined(noUndefinedBitOpts):
  104. # check for undefined behavior with zero.
  105. test_undefined(countSetBits, 0)
  106. test_undefined(parityBits, 0)
  107. test_undefined(firstSetBit, 0)
  108. test_undefined(countLeadingZeroBits, 0)
  109. test_undefined(countTrailingZeroBits, 0)
  110. test_undefined(fastLog2, -1)
  111. # check for undefined behavior with rotate by zero.
  112. doAssert( U8.rotateLeftBits(0) == U8)
  113. doAssert( U8.rotateRightBits(0) == U8)
  114. doAssert( U16.rotateLeftBits(0) == U16)
  115. doAssert( U16.rotateRightBits(0) == U16)
  116. doAssert( U32.rotateLeftBits(0) == U32)
  117. doAssert( U32.rotateRightBits(0) == U32)
  118. doAssert( U64A.rotateLeftBits(0) == U64A)
  119. doAssert( U64A.rotateRightBits(0) == U64A)
  120. # check for undefined behavior with rotate by integer width.
  121. doAssert( U8.rotateLeftBits(8) == U8)
  122. doAssert( U8.rotateRightBits(8) == U8)
  123. doAssert( U16.rotateLeftBits(16) == U16)
  124. doAssert( U16.rotateRightBits(16) == U16)
  125. doAssert( U32.rotateLeftBits(32) == U32)
  126. doAssert( U32.rotateRightBits(32) == U32)
  127. doAssert( U64A.rotateLeftBits(64) == U64A)
  128. doAssert( U64A.rotateRightBits(64) == U64A)
  129. static: # check for undefined behavior with rotate by zero.
  130. doAssert( U8.rotateLeftBits(0) == U8)
  131. doAssert( U8.rotateRightBits(0) == U8)
  132. doAssert( U16.rotateLeftBits(0) == U16)
  133. doAssert( U16.rotateRightBits(0) == U16)
  134. doAssert( U32.rotateLeftBits(0) == U32)
  135. doAssert( U32.rotateRightBits(0) == U32)
  136. doAssert( U64A.rotateLeftBits(0) == U64A)
  137. doAssert( U64A.rotateRightBits(0) == U64A)
  138. # check for undefined behavior with rotate by integer width.
  139. doAssert( U8.rotateLeftBits(8) == U8)
  140. doAssert( U8.rotateRightBits(8) == U8)
  141. doAssert( U16.rotateLeftBits(16) == U16)
  142. doAssert( U16.rotateRightBits(16) == U16)
  143. doAssert( U32.rotateLeftBits(32) == U32)
  144. doAssert( U32.rotateRightBits(32) == U32)
  145. doAssert( U64A.rotateLeftBits(64) == U64A)
  146. doAssert( U64A.rotateRightBits(64) == U64A)
  147. echo "OK"
  148. main()