tbitops.nim 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. discard """
  2. nimout: "OK"
  3. output: '''
  4. OK
  5. '''
  6. """
  7. import bitops
  8. import std/assertions
  9. proc main() =
  10. const U8 = 0b0011_0010'u8
  11. const I8 = 0b0011_0010'i8
  12. const U16 = 0b00100111_00101000'u16
  13. const I16 = 0b00100111_00101000'i16
  14. const U32 = 0b11010101_10011100_11011010_01010000'u32
  15. const I32 = 0b11010101_10011100_11011010_01010000'i32
  16. const U64A = 0b01000100_00111111_01111100_10001010_10011001_01001000_01111010_00010001'u64
  17. const I64A = 0b01000100_00111111_01111100_10001010_10011001_01001000_01111010_00010001'i64
  18. const U64B = 0b00110010_11011101_10001111_00101000_00000000_00000000_00000000_00000000'u64
  19. const I64B = 0b00110010_11011101_10001111_00101000_00000000_00000000_00000000_00000000'i64
  20. const U64C = 0b00101010_11110101_10001111_00101000_00000100_00000000_00000100_00000000'u64
  21. const I64C = 0b00101010_11110101_10001111_00101000_00000100_00000000_00000100_00000000'i64
  22. doAssert (U8 and U8) == bitand(U8,U8)
  23. doAssert (I8 and I8) == bitand(I8,I8)
  24. doAssert (U16 and U16) == bitand(U16,U16)
  25. doAssert (I16 and I16) == bitand(I16,I16)
  26. doAssert (U32 and U32) == bitand(U32,U32)
  27. doAssert (I32 and I32) == bitand(I32,I32)
  28. doAssert (U64A and U64B) == bitand(U64A,U64B)
  29. doAssert (I64A and I64B) == bitand(I64A,I64B)
  30. doAssert (U64A and U64B and U64C) == bitand(U64A,U64B,U64C)
  31. doAssert (I64A and I64B and I64C) == bitand(I64A,I64B,I64C)
  32. doAssert (U8 or U8) == bitor(U8,U8)
  33. doAssert (I8 or I8) == bitor(I8,I8)
  34. doAssert (U16 or U16) == bitor(U16,U16)
  35. doAssert (I16 or I16) == bitor(I16,I16)
  36. doAssert (U32 or U32) == bitor(U32,U32)
  37. doAssert (I32 or I32) == bitor(I32,I32)
  38. doAssert (U64A or U64B) == bitor(U64A,U64B)
  39. doAssert (I64A or I64B) == bitor(I64A,I64B)
  40. doAssert (U64A or U64B or U64C) == bitor(U64A,U64B,U64C)
  41. doAssert (I64A or I64B or I64C) == bitor(I64A,I64B,I64C)
  42. doAssert (U8 xor U8) == bitxor(U8,U8)
  43. doAssert (I8 xor I8) == bitxor(I8,I8)
  44. doAssert (U16 xor U16) == bitxor(U16,U16)
  45. doAssert (I16 xor I16) == bitxor(I16,I16)
  46. doAssert (U32 xor U32) == bitxor(U32,U32)
  47. doAssert (I32 xor I32) == bitxor(I32,I32)
  48. doAssert (U64A xor U64B) == bitxor(U64A,U64B)
  49. doAssert (I64A xor I64B) == bitxor(I64A,I64B)
  50. doAssert (U64A xor U64B xor U64C) == bitxor(U64A,U64B,U64C)
  51. doAssert (I64A xor I64B xor I64C) == bitxor(I64A,I64B,I64C)
  52. doAssert not(U8) == bitnot(U8)
  53. doAssert not(I8) == bitnot(I8)
  54. doAssert not(U16) == bitnot(U16)
  55. doAssert not(I16) == bitnot(I16)
  56. doAssert not(U32) == bitnot(U32)
  57. doAssert not(I32) == bitnot(I32)
  58. doAssert not(U64A) == bitnot(U64A)
  59. doAssert not(I64A) == bitnot(I64A)
  60. doAssert U64A.fastLog2 == 62
  61. doAssert I64A.fastLog2 == 62
  62. doAssert U64A.countLeadingZeroBits == 1
  63. doAssert I64A.countLeadingZeroBits == 1
  64. doAssert U64A.countTrailingZeroBits == 0
  65. doAssert I64A.countTrailingZeroBits == 0
  66. doAssert U64A.firstSetBit == 1
  67. doAssert I64A.firstSetBit == 1
  68. doAssert U64A.parityBits == 1
  69. doAssert I64A.parityBits == 1
  70. doAssert U64A.countSetBits == 29
  71. doAssert I64A.countSetBits == 29
  72. doAssert U64A.rotateLeftBits(37) == 0b00101001_00001111_01000010_00101000_10000111_11101111_10010001_01010011'u64
  73. doAssert U64A.rotateRightBits(37) == 0b01010100_11001010_01000011_11010000_10001010_00100001_11111011_11100100'u64
  74. doAssert U64B.firstSetBit == 36
  75. doAssert I64B.firstSetBit == 36
  76. doAssert U32.fastLog2 == 31
  77. doAssert I32.fastLog2 == 31
  78. doAssert U32.countLeadingZeroBits == 0
  79. doAssert I32.countLeadingZeroBits == 0
  80. doAssert U32.countTrailingZeroBits == 4
  81. doAssert I32.countTrailingZeroBits == 4
  82. doAssert U32.firstSetBit == 5
  83. doAssert I32.firstSetBit == 5
  84. doAssert U32.parityBits == 0
  85. doAssert I32.parityBits == 0
  86. doAssert U32.countSetBits == 16
  87. doAssert I32.countSetBits == 16
  88. doAssert U32.rotateLeftBits(21) == 0b01001010_00011010_10110011_10011011'u32
  89. doAssert U32.rotateRightBits(21) == 0b11100110_11010010_10000110_10101100'u32
  90. doAssert U16.fastLog2 == 13
  91. doAssert I16.fastLog2 == 13
  92. doAssert U16.countLeadingZeroBits == 2
  93. doAssert I16.countLeadingZeroBits == 2
  94. doAssert U16.countTrailingZeroBits == 3
  95. doAssert I16.countTrailingZeroBits == 3
  96. doAssert U16.firstSetBit == 4
  97. doAssert I16.firstSetBit == 4
  98. doAssert U16.parityBits == 0
  99. doAssert I16.parityBits == 0
  100. doAssert U16.countSetBits == 6
  101. doAssert I16.countSetBits == 6
  102. doAssert U16.rotateLeftBits(12) == 0b10000010_01110010'u16
  103. doAssert U16.rotateRightBits(12) == 0b01110010_10000010'u16
  104. doAssert U8.fastLog2 == 5
  105. doAssert I8.fastLog2 == 5
  106. doAssert U8.countLeadingZeroBits == 2
  107. doAssert I8.countLeadingZeroBits == 2
  108. doAssert U8.countTrailingZeroBits == 1
  109. doAssert I8.countTrailingZeroBits == 1
  110. doAssert U8.firstSetBit == 2
  111. doAssert I8.firstSetBit == 2
  112. doAssert U8.parityBits == 1
  113. doAssert I8.parityBits == 1
  114. doAssert U8.countSetBits == 3
  115. doAssert I8.countSetBits == 3
  116. doAssert U8.rotateLeftBits(3) == 0b10010001'u8
  117. doAssert U8.rotateRightBits(3) == 0b0100_0110'u8
  118. template test_undefined_impl(ffunc: untyped; expected: int; is_static: bool) =
  119. doAssert ffunc(0'u8) == expected
  120. doAssert ffunc(0'i8) == expected
  121. doAssert ffunc(0'u16) == expected
  122. doAssert ffunc(0'i16) == expected
  123. doAssert ffunc(0'u32) == expected
  124. doAssert ffunc(0'i32) == expected
  125. doAssert ffunc(0'u64) == expected
  126. doAssert ffunc(0'i64) == expected
  127. template test_undefined(ffunc: untyped; expected: int) =
  128. test_undefined_impl(ffunc, expected, false)
  129. static:
  130. test_undefined_impl(ffunc, expected, true)
  131. when defined(noUndefinedBitOpts):
  132. # check for undefined behavior with zero.
  133. test_undefined(countSetBits, 0)
  134. test_undefined(parityBits, 0)
  135. test_undefined(firstSetBit, 0)
  136. test_undefined(countLeadingZeroBits, 0)
  137. test_undefined(countTrailingZeroBits, 0)
  138. test_undefined(fastLog2, -1)
  139. # check for undefined behavior with rotate by zero.
  140. doAssert U8.rotateLeftBits(0) == U8
  141. doAssert U8.rotateRightBits(0) == U8
  142. doAssert U16.rotateLeftBits(0) == U16
  143. doAssert U16.rotateRightBits(0) == U16
  144. doAssert U32.rotateLeftBits(0) == U32
  145. doAssert U32.rotateRightBits(0) == U32
  146. doAssert U64A.rotateLeftBits(0) == U64A
  147. doAssert U64A.rotateRightBits(0) == U64A
  148. # check for undefined behavior with rotate by integer width.
  149. doAssert U8.rotateLeftBits(8) == U8
  150. doAssert U8.rotateRightBits(8) == U8
  151. doAssert U16.rotateLeftBits(16) == U16
  152. doAssert U16.rotateRightBits(16) == U16
  153. doAssert U32.rotateLeftBits(32) == U32
  154. doAssert U32.rotateRightBits(32) == U32
  155. doAssert U64A.rotateLeftBits(64) == U64A
  156. doAssert U64A.rotateRightBits(64) == U64A
  157. block:
  158. # basic mask operations (mutating)
  159. var v: uint8
  160. v.setMask(0b1100_0000)
  161. v.setMask(0b0000_1100)
  162. doAssert v == 0b1100_1100
  163. v.flipMask(0b0101_0101)
  164. doAssert v == 0b1001_1001
  165. v.clearMask(0b1000_1000)
  166. doAssert v == 0b0001_0001
  167. v.clearMask(0b0001_0001)
  168. doAssert v == 0b0000_0000
  169. v.setMask(0b0001_1110)
  170. doAssert v == 0b0001_1110
  171. v.mask(0b0101_0100)
  172. doAssert v == 0b0001_0100
  173. block:
  174. # basic mask operations (non-mutating)
  175. let v = 0b1100_0000'u8
  176. doAssert v.masked(0b0000_1100) == 0b0000_0000
  177. doAssert v.masked(0b1000_1100) == 0b1000_0000
  178. doAssert v.setMasked(0b0000_1100) == 0b1100_1100
  179. doAssert v.setMasked(0b1000_1110) == 0b1100_1110
  180. doAssert v.flipMasked(0b1100_1000) == 0b0000_1000
  181. doAssert v.flipMasked(0b0000_1100) == 0b1100_1100
  182. let t = 0b1100_0110'u8
  183. doAssert t.clearMasked(0b0100_1100) == 0b1000_0010
  184. doAssert t.clearMasked(0b1100_0000) == 0b0000_0110
  185. block:
  186. # basic bitslice opeartions
  187. let a = 0b1111_1011'u8
  188. doAssert a.bitsliced(0 .. 3) == 0b1011
  189. doAssert a.bitsliced(2 .. 3) == 0b10
  190. doAssert a.bitsliced(4 .. 7) == 0b1111
  191. # same thing, but with exclusive ranges.
  192. doAssert a.bitsliced(0 ..< 4) == 0b1011
  193. doAssert a.bitsliced(2 ..< 4) == 0b10
  194. doAssert a.bitsliced(4 ..< 8) == 0b1111
  195. # mutating
  196. var b = 0b1111_1011'u8
  197. b.bitslice(1 .. 3)
  198. doAssert b == 0b101
  199. # loop test:
  200. let c = 0b1111_1111'u8
  201. for i in 0 .. 7:
  202. doAssert c.bitsliced(i .. 7) == c shr i
  203. block:
  204. # bitslice versions of mask operations (mutating)
  205. var a = 0b1100_1100'u8
  206. let b = toMask[uint8](2 .. 3)
  207. a.mask(b)
  208. doAssert a == 0b0000_1100
  209. a.setMask(4 .. 7)
  210. doAssert a == 0b1111_1100
  211. a.flipMask(1 .. 3)
  212. doAssert a == 0b1111_0010
  213. a.flipMask(2 .. 4)
  214. doAssert a == 0b1110_1110
  215. a.clearMask(2 .. 4)
  216. doAssert a == 0b1110_0010
  217. a.mask(0 .. 3)
  218. doAssert a == 0b0000_0010
  219. # composition of mask from slices:
  220. let c = bitor(toMask[uint8](2 .. 3), toMask[uint8](5 .. 7))
  221. doAssert c == 0b1110_1100'u8
  222. block:
  223. # bitslice versions of mask operations (non-mutating)
  224. let a = 0b1100_1100'u8
  225. doAssert a.masked(toMask[uint8](2 .. 3)) == 0b0000_1100
  226. doAssert a.masked(2 .. 3) == 0b0000_1100
  227. doAssert a.setMasked(0 .. 3) == 0b1100_1111
  228. doAssert a.setMasked(3 .. 4) == 0b1101_1100
  229. doAssert a.flipMasked(0 .. 3) == 0b1100_0011
  230. doAssert a.flipMasked(0 .. 7) == 0b0011_0011
  231. doAssert a.flipMasked(2 .. 3) == 0b1100_0000
  232. doAssert a.clearMasked(2 .. 3) == 0b1100_0000
  233. doAssert a.clearMasked(3 .. 6) == 0b1000_0100
  234. block:
  235. # single bit operations
  236. var v: uint8
  237. v.setBit(0)
  238. doAssert v == 0x0000_0001
  239. v.setBit(1)
  240. doAssert v == 0b0000_0011
  241. v.flipBit(7)
  242. doAssert v == 0b1000_0011
  243. v.clearBit(0)
  244. doAssert v == 0b1000_0010
  245. v.flipBit(1)
  246. doAssert v == 0b1000_0000
  247. doAssert v.testBit(7)
  248. doAssert not v.testBit(6)
  249. block:
  250. # multi bit operations
  251. var v: uint8
  252. v.setBits(0, 1, 7)
  253. doAssert v == 0b1000_0011
  254. v.flipBits(2, 3)
  255. doAssert v == 0b1000_1111
  256. v.clearBits(7, 0, 1)
  257. doAssert v == 0b0000_1100
  258. block:
  259. # signed
  260. var v: int8
  261. v.setBit(7)
  262. doAssert v == -128
  263. block:
  264. var v: uint64
  265. v.setBit(63)
  266. doAssert v == 0b1000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000'u64
  267. block:
  268. proc testReverseBitsInvo(x: SomeUnsignedInt) =
  269. doAssert reverseBits(reverseBits(x)) == x
  270. proc testReverseBitsPerType(x, reversed: uint64) =
  271. doAssert reverseBits(x) == reversed
  272. doAssert reverseBits(cast[uint32](x)) == cast[uint32](reversed shr 32)
  273. doAssert reverseBits(cast[uint32](x shr 16)) == cast[uint32](reversed shr 16)
  274. doAssert reverseBits(cast[uint16](x)) == cast[uint16](reversed shr 48)
  275. doAssert reverseBits(cast[uint8](x)) == cast[uint8](reversed shr 56)
  276. testReverseBitsInvo(x)
  277. testReverseBitsInvo(cast[uint32](x))
  278. testReverseBitsInvo(cast[uint16](x))
  279. testReverseBitsInvo(cast[uint8](x))
  280. proc testReverseBitsRefl(x, reversed: uint64) =
  281. testReverseBitsPerType(x, reversed)
  282. testReverseBitsPerType(reversed, x)
  283. proc testReverseBitsShift(d, b: uint64) =
  284. var
  285. x = d
  286. y = b
  287. for i in 1..64:
  288. testReverseBitsRefl(x, y)
  289. x = x shl 1
  290. y = y shr 1
  291. proc testReverseBits(d, b: uint64) =
  292. testReverseBitsShift(d, b)
  293. testReverseBits(0x0u64, 0x0u64)
  294. testReverseBits(0xffffffffffffffffu64, 0xffffffffffffffffu64)
  295. testReverseBits(0x0123456789abcdefu64, 0xf7b3d591e6a2c480u64)
  296. testReverseBits(0x5555555555555555u64, 0xaaaaaaaaaaaaaaaau64)
  297. testReverseBits(0x5555555500000001u64, 0x80000000aaaaaaaau64)
  298. testReverseBits(0x55555555aaaaaaaau64, 0x55555555aaaaaaaau64)
  299. testReverseBits(0xf0f0f0f00f0f0f0fu64, 0xf0f0f0f00f0f0f0fu64)
  300. testReverseBits(0x181881810ff00916u64, 0x68900ff081811818u64)
  301. echo "OK"
  302. # bug #7587
  303. doAssert popcount(0b11111111'i8) == 8
  304. block: # not ready for vm because exception is compile error
  305. try:
  306. var v: uint32
  307. var i = 32
  308. v.setBit(i)
  309. doAssert false
  310. except RangeDefect:
  311. discard
  312. except:
  313. doAssert false
  314. main()
  315. static:
  316. # test everything on vm as well
  317. main()