tbitops.nim 12 KB

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