bitops.nim 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2017 Nim Authors
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## This module implements a series of low level methods for bit manipulation.
  10. ## By default, this module use compiler intrinsics where possible to improve performance
  11. ## on supported compilers: ``GCC``, ``LLVM_GCC``, ``CLANG``, ``VCC``, ``ICC``.
  12. ##
  13. ## The module will fallback to pure nim procs incase the backend is not supported.
  14. ## You can also use the flag `noIntrinsicsBitOpts` to disable compiler intrinsics.
  15. ##
  16. ## This module is also compatible with other backends: ``Javascript``, ``Nimscript``
  17. ## as well as the ``compiletime VM``.
  18. ##
  19. ## As a result of using optimized function/intrinsics some functions can return
  20. ## undefined results if the input is invalid. You can use the flag `noUndefinedBitOpts`
  21. ## to force predictable behaviour for all input, causing a small performance hit.
  22. ##
  23. ## At this time only `fastLog2`, `firstSetBit, `countLeadingZeroBits`, `countTrailingZeroBits`
  24. ## may return undefined and/or platform dependent value if given invalid input.
  25. proc bitnot*[T: SomeInteger](x: T): T {.magic: "BitnotI", noSideEffect.}
  26. ## Computes the `bitwise complement` of the integer `x`.
  27. proc bitand*[T: SomeInteger](x, y: T): T {.magic: "BitandI", noSideEffect.}
  28. ## Computes the `bitwise and` of numbers `x` and `y`.
  29. proc bitor*[T: SomeInteger](x, y: T): T {.magic: "BitorI", noSideEffect.}
  30. ## Computes the `bitwise or` of numbers `x` and `y`.
  31. proc bitxor*[T: SomeInteger](x, y: T): T {.magic: "BitxorI", noSideEffect.}
  32. ## Computes the `bitwise xor` of numbers `x` and `y`.
  33. const useBuiltins = not defined(noIntrinsicsBitOpts)
  34. const noUndefined = defined(noUndefinedBitOpts)
  35. const useGCC_builtins = (defined(gcc) or defined(llvm_gcc) or
  36. defined(clang)) and useBuiltins
  37. const useICC_builtins = defined(icc) and useBuiltins
  38. const useVCC_builtins = defined(vcc) and useBuiltins
  39. const arch64 = sizeof(int) == 8
  40. template toUnsigned(x: int8): uint8 = cast[uint8](x)
  41. template toUnsigned(x: int16): uint16 = cast[uint16](x)
  42. template toUnsigned(x: int32): uint32 = cast[uint32](x)
  43. template toUnsigned(x: int64): uint64 = cast[uint64](x)
  44. template toUnsigned(x: int): uint = cast[uint](x)
  45. template forwardImpl(impl, arg) {.dirty.} =
  46. when sizeof(x) <= 4:
  47. when x is SomeSignedInt:
  48. impl(cast[uint32](x.int32))
  49. else:
  50. impl(x.uint32)
  51. else:
  52. when x is SomeSignedInt:
  53. impl(cast[uint64](x.int64))
  54. else:
  55. impl(x.uint64)
  56. when defined(nimHasalignOf):
  57. import macros
  58. type BitsRange*[T] = range[0..sizeof(T)*8-1]
  59. ## Returns a range with all bit positions for type ``T``
  60. proc setMask*[T: SomeInteger](v: var T, mask: T) {.inline.} =
  61. ## Returns ``v``, with all the ``1`` bits from ``mask`` set to 1
  62. v = v or mask
  63. proc clearMask*[T: SomeInteger](v: var T, mask: T) {.inline.} =
  64. ## Returns ``v``, with all the ``1`` bits from ``mask`` set to 0
  65. v = v and not mask
  66. proc flipMask*[T: SomeInteger](v: var T, mask: T) {.inline.} =
  67. ## Returns ``v``, with all the ``1`` bits from ``mask`` flipped
  68. v = v xor mask
  69. proc setBit*[T: SomeInteger](v: var T, bit: BitsRange[T]) {.inline.} =
  70. ## Returns ``v``, with the bit at position ``bit`` set to 1
  71. v.setMask(1.T shl bit)
  72. proc clearBit*[T: SomeInteger](v: var T, bit: BitsRange[T]) {.inline.} =
  73. ## Returns ``v``, with the bit at position ``bit`` set to 0
  74. v.clearMask(1.T shl bit)
  75. proc flipBit*[T: SomeInteger](v: var T, bit: BitsRange[T]) {.inline.} =
  76. ## Returns ``v``, with the bit at position ``bit`` flipped
  77. v.flipMask(1.T shl bit)
  78. macro setBits*(v: typed, bits: varargs[typed]): untyped =
  79. ## Returns ``v``, with the bits at positions ``bits`` set to 1
  80. bits.expectKind(nnkBracket)
  81. result = newStmtList()
  82. for bit in bits:
  83. result.add newCall("setBit", v, bit)
  84. macro clearBits*(v: typed, bits: varargs[typed]): untyped =
  85. ## Returns ``v``, with the bits at positions ``bits`` set to 0
  86. bits.expectKind(nnkBracket)
  87. result = newStmtList()
  88. for bit in bits:
  89. result.add newCall("clearBit", v, bit)
  90. macro flipBits*(v: typed, bits: varargs[typed]): untyped =
  91. ## Returns ``v``, with the bits at positions ``bits`` set to 0
  92. bits.expectKind(nnkBracket)
  93. result = newStmtList()
  94. for bit in bits:
  95. result.add newCall("flipBit", v, bit)
  96. proc testBit*[T: SomeInteger](v: T, bit: BitsRange[T]): bool {.inline.} =
  97. ## Returns true if the bit in ``v`` at positions ``bit`` is set to 1
  98. let mask = 1.T shl bit
  99. return (v and mask) == mask
  100. # #### Pure Nim version ####
  101. proc firstSetBitNim(x: uint32): int {.inline, noSideEffect.} =
  102. ## Returns the 1-based index of the least significant set bit of x, or if x is zero, returns zero.
  103. # https://graphics.stanford.edu/%7Eseander/bithacks.html#ZerosOnRightMultLookup
  104. const lookup: array[32, uint8] = [0'u8, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15,
  105. 25, 17, 4, 8, 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9]
  106. var v = x.uint32
  107. var k = not v + 1 # get two's complement # cast[uint32](-cast[int32](v))
  108. result = 1 + lookup[uint32((v and k) * 0x077CB531'u32) shr 27].int
  109. proc firstSetBitNim(x: uint64): int {.inline, noSideEffect.} =
  110. ## Returns the 1-based index of the least significant set bit of x, or if x is zero, returns zero.
  111. # https://graphics.stanford.edu/%7Eseander/bithacks.html#ZerosOnRightMultLookup
  112. var v = uint64(x)
  113. var k = uint32(v and 0xFFFFFFFF'u32)
  114. if k == 0:
  115. k = uint32(v shr 32'u32) and 0xFFFFFFFF'u32
  116. result = 32
  117. result += firstSetBitNim(k)
  118. proc fastlog2Nim(x: uint32): int {.inline, noSideEffect.} =
  119. ## Quickly find the log base 2 of a 32-bit or less integer.
  120. # https://graphics.stanford.edu/%7Eseander/bithacks.html#IntegerLogDeBruijn
  121. # https://stackoverflow.com/questions/11376288/fast-computing-of-log2-for-64-bit-integers
  122. const lookup: array[32, uint8] = [0'u8, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18,
  123. 22, 25, 3, 30, 8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31]
  124. var v = x.uint32
  125. v = v or v shr 1 # first round down to one less than a power of 2
  126. v = v or v shr 2
  127. v = v or v shr 4
  128. v = v or v shr 8
  129. v = v or v shr 16
  130. result = lookup[uint32(v * 0x07C4ACDD'u32) shr 27].int
  131. proc fastlog2Nim(x: uint64): int {.inline, noSideEffect.} =
  132. ## Quickly find the log base 2 of a 64-bit integer.
  133. # https://graphics.stanford.edu/%7Eseander/bithacks.html#IntegerLogDeBruijn
  134. # https://stackoverflow.com/questions/11376288/fast-computing-of-log2-for-64-bit-integers
  135. const lookup: array[64, uint8] = [0'u8, 58, 1, 59, 47, 53, 2, 60, 39, 48, 27, 54,
  136. 33, 42, 3, 61, 51, 37, 40, 49, 18, 28, 20, 55, 30, 34, 11, 43, 14, 22, 4, 62,
  137. 57, 46, 52, 38, 26, 32, 41, 50, 36, 17, 19, 29, 10, 13, 21, 56, 45, 25, 31,
  138. 35, 16, 9, 12, 44, 24, 15, 8, 23, 7, 6, 5, 63]
  139. var v = x.uint64
  140. v = v or v shr 1 # first round down to one less than a power of 2
  141. v = v or v shr 2
  142. v = v or v shr 4
  143. v = v or v shr 8
  144. v = v or v shr 16
  145. v = v or v shr 32
  146. result = lookup[(v * 0x03F6EAF2CD271461'u64) shr 58].int
  147. # sets.nim cannot import bitops, but bitops can use include
  148. # system/sets to eliminate code duplication. sets.nim defines
  149. # countBits32 and countBits64.
  150. include system/sets
  151. template countSetBitsNim(n: uint32): int = countBits32(n)
  152. template countSetBitsNim(n: uint64): int = countBits64(n)
  153. template parityImpl[T](value: T): int =
  154. # formula id from: https://graphics.stanford.edu/%7Eseander/bithacks.html#ParityParallel
  155. var v = value
  156. when sizeof(T) == 8:
  157. v = v xor (v shr 32)
  158. when sizeof(T) >= 4:
  159. v = v xor (v shr 16)
  160. when sizeof(T) >= 2:
  161. v = v xor (v shr 8)
  162. v = v xor (v shr 4)
  163. v = v and 0xf
  164. ((0x6996'u shr v) and 1).int
  165. when useGCC_builtins:
  166. # Returns the number of set 1-bits in value.
  167. proc builtin_popcount(x: cuint): cint {.importc: "__builtin_popcount", cdecl.}
  168. proc builtin_popcountll(x: culonglong): cint {.
  169. importc: "__builtin_popcountll", cdecl.}
  170. # Returns the bit parity in value
  171. proc builtin_parity(x: cuint): cint {.importc: "__builtin_parity", cdecl.}
  172. proc builtin_parityll(x: culonglong): cint {.importc: "__builtin_parityll", cdecl.}
  173. # Returns one plus the index of the least significant 1-bit of x, or if x is zero, returns zero.
  174. proc builtin_ffs(x: cint): cint {.importc: "__builtin_ffs", cdecl.}
  175. proc builtin_ffsll(x: clonglong): cint {.importc: "__builtin_ffsll", cdecl.}
  176. # Returns the number of leading 0-bits in x, starting at the most significant bit position. If x is 0, the result is undefined.
  177. proc builtin_clz(x: cuint): cint {.importc: "__builtin_clz", cdecl.}
  178. proc builtin_clzll(x: culonglong): cint {.importc: "__builtin_clzll", cdecl.}
  179. # Returns the number of trailing 0-bits in x, starting at the least significant bit position. If x is 0, the result is undefined.
  180. proc builtin_ctz(x: cuint): cint {.importc: "__builtin_ctz", cdecl.}
  181. proc builtin_ctzll(x: culonglong): cint {.importc: "__builtin_ctzll", cdecl.}
  182. elif useVCC_builtins:
  183. # Counts the number of one bits (population count) in a 16-, 32-, or 64-byte unsigned integer.
  184. proc builtin_popcnt16(a2: uint16): uint16 {.
  185. importc: "__popcnt16"header: "<intrin.h>", noSideEffect.}
  186. proc builtin_popcnt32(a2: uint32): uint32 {.
  187. importc: "__popcnt"header: "<intrin.h>", noSideEffect.}
  188. proc builtin_popcnt64(a2: uint64): uint64 {.
  189. importc: "__popcnt64"header: "<intrin.h>", noSideEffect.}
  190. # Search the mask data from most significant bit (MSB) to least significant bit (LSB) for a set bit (1).
  191. proc bitScanReverse(index: ptr culong, mask: culong): cuchar {.
  192. importc: "_BitScanReverse", header: "<intrin.h>", noSideEffect.}
  193. proc bitScanReverse64(index: ptr culong, mask: uint64): cuchar {.
  194. importc: "_BitScanReverse64", header: "<intrin.h>", noSideEffect.}
  195. # Search the mask data from least significant bit (LSB) to the most significant bit (MSB) for a set bit (1).
  196. proc bitScanForward(index: ptr culong, mask: culong): cuchar {.
  197. importc: "_BitScanForward", header: "<intrin.h>", noSideEffect.}
  198. proc bitScanForward64(index: ptr culong, mask: uint64): cuchar {.
  199. importc: "_BitScanForward64", header: "<intrin.h>", noSideEffect.}
  200. template vcc_scan_impl(fnc: untyped; v: untyped): int =
  201. var index: culong
  202. discard fnc(index.addr, v)
  203. index.int
  204. elif useICC_builtins:
  205. # Intel compiler intrinsics: http://fulla.fnal.gov/intel/compiler_c/main_cls/intref_cls/common/intref_allia_misc.htm
  206. # see also: https://software.intel.com/en-us/node/523362
  207. # Count the number of bits set to 1 in an integer a, and return that count in dst.
  208. proc builtin_popcnt32(a: cint): cint {.
  209. importc: "_popcnt"header: "<immintrin.h>", noSideEffect.}
  210. proc builtin_popcnt64(a: uint64): cint {.
  211. importc: "_popcnt64"header: "<immintrin.h>", noSideEffect.}
  212. # Returns the number of trailing 0-bits in x, starting at the least significant bit position. If x is 0, the result is undefined.
  213. proc bitScanForward(p: ptr uint32, b: uint32): cuchar {.
  214. importc: "_BitScanForward", header: "<immintrin.h>", noSideEffect.}
  215. proc bitScanForward64(p: ptr uint32, b: uint64): cuchar {.
  216. importc: "_BitScanForward64", header: "<immintrin.h>", noSideEffect.}
  217. # Returns the number of leading 0-bits in x, starting at the most significant bit position. If x is 0, the result is undefined.
  218. proc bitScanReverse(p: ptr uint32, b: uint32): cuchar {.
  219. importc: "_BitScanReverse", header: "<immintrin.h>", noSideEffect.}
  220. proc bitScanReverse64(p: ptr uint32, b: uint64): cuchar {.
  221. importc: "_BitScanReverse64", header: "<immintrin.h>", noSideEffect.}
  222. template icc_scan_impl(fnc: untyped; v: untyped): int =
  223. var index: uint32
  224. discard fnc(index.addr, v)
  225. index.int
  226. proc countSetBits*(x: SomeInteger): int {.inline, noSideEffect.} =
  227. ## Counts the set bits in integer. (also called `Hamming weight`:idx:.)
  228. # TODO: figure out if ICC support _popcnt32/_popcnt64 on platform without POPCNT.
  229. # like GCC and MSVC
  230. when x is SomeSignedInt:
  231. let x = x.toUnsigned
  232. when nimvm:
  233. result = forwardImpl(countSetBitsNim, x)
  234. else:
  235. when useGCC_builtins:
  236. when sizeof(x) <= 4: result = builtin_popcount(x.cuint).int
  237. else: result = builtin_popcountll(x.culonglong).int
  238. elif useVCC_builtins:
  239. when sizeof(x) <= 2: result = builtin_popcnt16(x.uint16).int
  240. elif sizeof(x) <= 4: result = builtin_popcnt32(x.uint32).int
  241. elif arch64: result = builtin_popcnt64(x.uint64).int
  242. else: result = builtin_popcnt32((x.uint64 and 0xFFFFFFFF'u64).uint32).int +
  243. builtin_popcnt32((x.uint64 shr 32'u64).uint32).int
  244. elif useICC_builtins:
  245. when sizeof(x) <= 4: result = builtin_popcnt32(x.cint).int
  246. elif arch64: result = builtin_popcnt64(x.uint64).int
  247. else: result = builtin_popcnt32((x.uint64 and 0xFFFFFFFF'u64).cint).int +
  248. builtin_popcnt32((x.uint64 shr 32'u64).cint).int
  249. else:
  250. when sizeof(x) <= 4: result = countSetBitsNim(x.uint32)
  251. else: result = countSetBitsNim(x.uint64)
  252. proc popcount*(x: SomeInteger): int {.inline, noSideEffect.} =
  253. ## Alias for for countSetBits (Hamming weight.)
  254. result = countSetBits(x)
  255. proc parityBits*(x: SomeInteger): int {.inline, noSideEffect.} =
  256. ## Calculate the bit parity in integer. If number of 1-bit
  257. ## is odd parity is 1, otherwise 0.
  258. # Can be used a base if creating ASM version.
  259. # https://stackoverflow.com/questions/21617970/how-to-check-if-value-has-even-parity-of-bits-or-odd
  260. when x is SomeSignedInt:
  261. let x = x.toUnsigned
  262. when nimvm:
  263. result = forwardImpl(parityImpl, x)
  264. else:
  265. when useGCC_builtins:
  266. when sizeof(x) <= 4: result = builtin_parity(x.uint32).int
  267. else: result = builtin_parityll(x.uint64).int
  268. else:
  269. when sizeof(x) <= 4: result = parityImpl(x.uint32)
  270. else: result = parityImpl(x.uint64)
  271. proc firstSetBit*(x: SomeInteger): int {.inline, noSideEffect.} =
  272. ## Returns the 1-based index of the least significant set bit of x.
  273. ## If `x` is zero, when ``noUndefinedBitOpts`` is set, result is 0,
  274. ## otherwise result is undefined.
  275. # GCC builtin 'builtin_ffs' already handle zero input.
  276. when x is SomeSignedInt:
  277. let x = x.toUnsigned
  278. when nimvm:
  279. when noUndefined:
  280. if x == 0:
  281. return 0
  282. result = forwardImpl(firstSetBitNim, x)
  283. else:
  284. when noUndefined and not useGCC_builtins:
  285. if x == 0:
  286. return 0
  287. when useGCC_builtins:
  288. when sizeof(x) <= 4: result = builtin_ffs(cast[cint](x.cuint)).int
  289. else: result = builtin_ffsll(cast[clonglong](x.culonglong)).int
  290. elif useVCC_builtins:
  291. when sizeof(x) <= 4:
  292. result = 1 + vcc_scan_impl(bitScanForward, x.culong)
  293. elif arch64:
  294. result = 1 + vcc_scan_impl(bitScanForward64, x.uint64)
  295. else:
  296. result = firstSetBitNim(x.uint64)
  297. elif useICC_builtins:
  298. when sizeof(x) <= 4:
  299. result = 1 + icc_scan_impl(bitScanForward, x.uint32)
  300. elif arch64:
  301. result = 1 + icc_scan_impl(bitScanForward64, x.uint64)
  302. else:
  303. result = firstSetBitNim(x.uint64)
  304. else:
  305. when sizeof(x) <= 4: result = firstSetBitNim(x.uint32)
  306. else: result = firstSetBitNim(x.uint64)
  307. proc fastLog2*(x: SomeInteger): int {.inline, noSideEffect.} =
  308. ## Quickly find the log base 2 of an integer.
  309. ## If `x` is zero, when ``noUndefinedBitOpts`` is set, result is -1,
  310. ## otherwise result is undefined.
  311. when x is SomeSignedInt:
  312. let x = x.toUnsigned
  313. when noUndefined:
  314. if x == 0:
  315. return -1
  316. when nimvm:
  317. result = forwardImpl(fastlog2Nim, x)
  318. else:
  319. when useGCC_builtins:
  320. when sizeof(x) <= 4: result = 31 - builtin_clz(x.uint32).int
  321. else: result = 63 - builtin_clzll(x.uint64).int
  322. elif useVCC_builtins:
  323. when sizeof(x) <= 4:
  324. result = vcc_scan_impl(bitScanReverse, x.culong)
  325. elif arch64:
  326. result = vcc_scan_impl(bitScanReverse64, x.uint64)
  327. else:
  328. result = fastlog2Nim(x.uint64)
  329. elif useICC_builtins:
  330. when sizeof(x) <= 4:
  331. result = icc_scan_impl(bitScanReverse, x.uint32)
  332. elif arch64:
  333. result = icc_scan_impl(bitScanReverse64, x.uint64)
  334. else:
  335. result = fastlog2Nim(x.uint64)
  336. else:
  337. when sizeof(x) <= 4: result = fastlog2Nim(x.uint32)
  338. else: result = fastlog2Nim(x.uint64)
  339. proc countLeadingZeroBits*(x: SomeInteger): int {.inline, noSideEffect.} =
  340. ## Returns the number of leading zero bits in integer.
  341. ## If `x` is zero, when ``noUndefinedBitOpts`` is set, result is 0,
  342. ## otherwise result is undefined.
  343. when x is SomeSignedInt:
  344. let x = x.toUnsigned
  345. when noUndefined:
  346. if x == 0:
  347. return 0
  348. when nimvm:
  349. result = sizeof(x)*8 - 1 - forwardImpl(fastlog2Nim, x)
  350. else:
  351. when useGCC_builtins:
  352. when sizeof(x) <= 4: result = builtin_clz(x.uint32).int - (32 - sizeof(x)*8)
  353. else: result = builtin_clzll(x.uint64).int
  354. else:
  355. when sizeof(x) <= 4: result = sizeof(x)*8 - 1 - fastlog2Nim(x.uint32)
  356. else: result = sizeof(x)*8 - 1 - fastlog2Nim(x.uint64)
  357. proc countTrailingZeroBits*(x: SomeInteger): int {.inline, noSideEffect.} =
  358. ## Returns the number of trailing zeros in integer.
  359. ## If `x` is zero, when ``noUndefinedBitOpts`` is set, result is 0,
  360. ## otherwise result is undefined.
  361. when x is SomeSignedInt:
  362. let x = x.toUnsigned
  363. when noUndefined:
  364. if x == 0:
  365. return 0
  366. when nimvm:
  367. result = firstSetBit(x) - 1
  368. else:
  369. when useGCC_builtins:
  370. when sizeof(x) <= 4: result = builtin_ctz(x.uint32).int
  371. else: result = builtin_ctzll(x.uint64).int
  372. else:
  373. result = firstSetBit(x) - 1
  374. proc rotateLeftBits*(value: uint8;
  375. amount: range[0..8]): uint8 {.inline, noSideEffect.} =
  376. ## Left-rotate bits in a 8-bits value.
  377. # using this form instead of the one below should handle any value
  378. # out of range as well as negative values.
  379. # result = (value shl amount) or (value shr (8 - amount))
  380. # taken from: https://en.wikipedia.org/wiki/Circular_shift#Implementing_circular_shifts
  381. let amount = amount and 7
  382. result = (value shl amount) or (value shr ( (-amount) and 7))
  383. proc rotateLeftBits*(value: uint16;
  384. amount: range[0..16]): uint16 {.inline, noSideEffect.} =
  385. ## Left-rotate bits in a 16-bits value.
  386. let amount = amount and 15
  387. result = (value shl amount) or (value shr ( (-amount) and 15))
  388. proc rotateLeftBits*(value: uint32;
  389. amount: range[0..32]): uint32 {.inline, noSideEffect.} =
  390. ## Left-rotate bits in a 32-bits value.
  391. let amount = amount and 31
  392. result = (value shl amount) or (value shr ( (-amount) and 31))
  393. proc rotateLeftBits*(value: uint64;
  394. amount: range[0..64]): uint64 {.inline, noSideEffect.} =
  395. ## Left-rotate bits in a 64-bits value.
  396. let amount = amount and 63
  397. result = (value shl amount) or (value shr ( (-amount) and 63))
  398. proc rotateRightBits*(value: uint8;
  399. amount: range[0..8]): uint8 {.inline, noSideEffect.} =
  400. ## Right-rotate bits in a 8-bits value.
  401. let amount = amount and 7
  402. result = (value shr amount) or (value shl ( (-amount) and 7))
  403. proc rotateRightBits*(value: uint16;
  404. amount: range[0..16]): uint16 {.inline, noSideEffect.} =
  405. ## Right-rotate bits in a 16-bits value.
  406. let amount = amount and 15
  407. result = (value shr amount) or (value shl ( (-amount) and 15))
  408. proc rotateRightBits*(value: uint32;
  409. amount: range[0..32]): uint32 {.inline, noSideEffect.} =
  410. ## Right-rotate bits in a 32-bits value.
  411. let amount = amount and 31
  412. result = (value shr amount) or (value shl ( (-amount) and 31))
  413. proc rotateRightBits*(value: uint64;
  414. amount: range[0..64]): uint64 {.inline, noSideEffect.} =
  415. ## Right-rotate bits in a 64-bits value.
  416. let amount = amount and 63
  417. result = (value shr amount) or (value shl ( (-amount) and 63))
  418. proc repeatBits[T: SomeUnsignedInt](x: SomeUnsignedInt; retType: type[T]): T {.
  419. noSideEffect.} =
  420. result = x
  421. var i = 1
  422. while i != (sizeof(T) div sizeof(x)):
  423. result = (result shl (sizeof(x)*8*i)) or result
  424. i *= 2
  425. proc reverseBits*[T: SomeUnsignedInt](x: T): T {.noSideEffect.} =
  426. ## Return the bit reversal of x.
  427. runnableExamples:
  428. doAssert reverseBits(0b10100100'u8) == 0b00100101'u8
  429. doAssert reverseBits(0xdd'u8) == 0xbb'u8
  430. doAssert reverseBits(0xddbb'u16) == 0xddbb'u16
  431. doAssert reverseBits(0xdeadbeef'u32) == 0xf77db57b'u32
  432. template repeat(x: SomeUnsignedInt): T = repeatBits(x, T)
  433. result = x
  434. result =
  435. ((repeat(0x55u8) and result) shl 1) or
  436. ((repeat(0xaau8) and result) shr 1)
  437. result =
  438. ((repeat(0x33u8) and result) shl 2) or
  439. ((repeat(0xccu8) and result) shr 2)
  440. when sizeof(T) == 1:
  441. result = (result shl 4) or (result shr 4)
  442. when sizeof(T) >= 2:
  443. result =
  444. ((repeat(0x0fu8) and result) shl 4) or
  445. ((repeat(0xf0u8) and result) shr 4)
  446. when sizeof(T) == 2:
  447. result = (result shl 8) or (result shr 8)
  448. when sizeof(T) >= 4:
  449. result =
  450. ((repeat(0x00ffu16) and result) shl 8) or
  451. ((repeat(0xff00u16) and result) shr 8)
  452. when sizeof(T) == 4:
  453. result = (result shl 16) or (result shr 16)
  454. when sizeof(T) == 8:
  455. result =
  456. ((repeat(0x0000ffffu32) and result) shl 16) or
  457. ((repeat(0xffff0000u32) and result) shr 16)
  458. result = (result shl 32) or (result shr 32)