tbitops.nim 12 KB

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