Improve documentation for bitops (#16961)

* Improve documentation for bitops

Use func
Use let in runnableExamples

* Remove unnecessary tests

Fix #7587
This commit is contained in:
konsumlamm 2021-02-15 21:17:45 +01:00 • committed by GitHub
commit 0a9a90d991
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 296 additions and 456 deletions

View file

@ -8,27 +8,27 @@
# #
## This module implements a series of low level methods for bit manipulation. ## This module implements a series of low level methods for bit manipulation.
##
## By default, this module use compiler intrinsics where possible to improve performance ## By default, compiler intrinsics are used where possible to improve performance
## on supported compilers: ``GCC``, ``LLVM_GCC``, ``CLANG``, ``VCC``, ``ICC``. ## on supported compilers: `GCC`, `LLVM_GCC`, `CLANG`, `VCC`, `ICC`.
## ##
## The module will fallback to pure nim procs in case the backend is not supported. ## The module will fallback to pure nim procs in case the backend is not supported.
## You can also use the flag `noIntrinsicsBitOpts` to disable compiler intrinsics. ## You can also use the flag `noIntrinsicsBitOpts` to disable compiler intrinsics.
## ##
## This module is also compatible with other backends: ``Javascript``, ``Nimscript`` ## This module is also compatible with other backends: `JavaScript`, `NimScript`
## as well as the ``compiletime VM``. ## as well as the `compiletime VM`.
## ##
## As a result of using optimized function/intrinsics some functions can return ## As a result of using optimized functions/intrinsics, some functions can return
## undefined results if the input is invalid. You can use the flag `noUndefinedBitOpts` ## undefined results if the input is invalid. You can use the flag `noUndefinedBitOpts`
## to force predictable behaviour for all input, causing a small performance hit. ## to force predictable behaviour for all input, causing a small performance hit.
## ##
## At this time only `fastLog2`, `firstSetBit, `countLeadingZeroBits`, `countTrailingZeroBits` ## At this time only `fastLog2`, `firstSetBit`, `countLeadingZeroBits` and `countTrailingZeroBits`
## may return undefined and/or platform dependent value if given invalid input. ## may return undefined and/or platform dependent values if given invalid input.
import macros import macros
import std/private/since import std/private/since
proc bitnot*[T: SomeInteger](x: T): T {.magic: "BitnotI", noSideEffect.} func bitnot*[T: SomeInteger](x: T): T {.magic: "BitnotI".}
## Computes the `bitwise complement` of the integer `x`. ## Computes the `bitwise complement` of the integer `x`.
func internalBitand[T: SomeInteger](x, y: T): T {.magic: "BitandI".} func internalBitand[T: SomeInteger](x, y: T): T {.magic: "BitandI".}
@ -89,10 +89,10 @@ template forwardImpl(impl, arg) {.dirty.} =
when defined(nimHasalignOf): when defined(nimHasalignOf):
type BitsRange*[T] = range[0..sizeof(T)*8-1] type BitsRange*[T] = range[0..sizeof(T)*8-1]
## A range with all bit positions for type ``T`` ## A range with all bit positions for type `T`.
func bitsliced*[T: SomeInteger](v: T; slice: Slice[int]): T {.inline, since: (1, 3).} = func bitsliced*[T: SomeInteger](v: T; slice: Slice[int]): T {.inline, since: (1, 3).} =
## Returns an extracted (and shifted) slice of bits from ``v``. ## Returns an extracted (and shifted) slice of bits from `v`.
runnableExamples: runnableExamples:
doAssert 0b10111.bitsliced(2 .. 4) == 0b101 doAssert 0b10111.bitsliced(2 .. 4) == 0b101
doAssert 0b11100.bitsliced(0 .. 2) == 0b100 doAssert 0b11100.bitsliced(0 .. 2) == 0b100
@ -104,7 +104,7 @@ when defined(nimHasalignOf):
(uv shl (upmost - slice.b) shr (upmost - slice.b + slice.a)).T (uv shl (upmost - slice.b) shr (upmost - slice.b + slice.a)).T
proc bitslice*[T: SomeInteger](v: var T; slice: Slice[int]) {.inline, since: (1, 3).} = proc bitslice*[T: SomeInteger](v: var T; slice: Slice[int]) {.inline, since: (1, 3).} =
## Mutates ``v`` into an extracted (and shifted) slice of bits from ``v``. ## Mutates `v` into an extracted (and shifted) slice of bits from `v`.
runnableExamples: runnableExamples:
var x = 0b101110 var x = 0b101110
x.bitslice(2 .. 4) x.bitslice(2 .. 4)
@ -130,32 +130,32 @@ when defined(nimHasalignOf):
(bitmask shl (upmost - slice.b + slice.a) shr (upmost - slice.b)).T (bitmask shl (upmost - slice.b + slice.a) shr (upmost - slice.b)).T
proc masked*[T: SomeInteger](v, mask :T): T {.inline, since: (1, 3).} = proc masked*[T: SomeInteger](v, mask :T): T {.inline, since: (1, 3).} =
## Returns ``v``, with only the ``1`` bits from ``mask`` matching those of ## Returns `v`, with only the `1` bits from `mask` matching those of
## ``v`` set to 1. ## `v` set to 1.
## ##
## Effectively maps to a `bitand` operation. ## Effectively maps to a `bitand <#bitand.m,T,T,varargs[T]>`_ operation.
runnableExamples: runnableExamples:
var v = 0b0000_0011'u8 let v = 0b0000_0011'u8
doAssert v.masked(0b0000_1010'u8) == 0b0000_0010'u8 doAssert v.masked(0b0000_1010'u8) == 0b0000_0010'u8
bitand(v, mask) bitand(v, mask)
func masked*[T: SomeInteger](v: T; slice: Slice[int]): T {.inline, since: (1, 3).} = func masked*[T: SomeInteger](v: T; slice: Slice[int]): T {.inline, since: (1, 3).} =
## Mutates ``v``, with only the ``1`` bits in the range of ``slice`` ## Returns `v`, with only the `1` bits in the range of `slice`
## matching those of ``v`` set to 1. ## matching those of `v` set to 1.
## ##
## Effectively maps to a `bitand` operation. ## Effectively maps to a `bitand <#bitand.m,T,T,varargs[T]>`_ operation.
runnableExamples: runnableExamples:
var v = 0b0000_1011'u8 let v = 0b0000_1011'u8
doAssert v.masked(1 .. 3) == 0b0000_1010'u8 doAssert v.masked(1 .. 3) == 0b0000_1010'u8
bitand(v, toMask[T](slice)) bitand(v, toMask[T](slice))
proc mask*[T: SomeInteger](v: var T; mask: T) {.inline, since: (1, 3).} = proc mask*[T: SomeInteger](v: var T; mask: T) {.inline, since: (1, 3).} =
## Mutates ``v``, with only the ``1`` bits from ``mask`` matching those of ## Mutates `v`, with only the `1` bits from `mask` matching those of
## ``v`` set to 1. ## `v` set to 1.
## ##
## Effectively maps to a `bitand` operation. ## Effectively maps to a `bitand <#bitand.m,T,T,varargs[T]>`_ operation.
runnableExamples: runnableExamples:
var v = 0b0000_0011'u8 var v = 0b0000_0011'u8
v.mask(0b0000_1010'u8) v.mask(0b0000_1010'u8)
@ -164,10 +164,10 @@ when defined(nimHasalignOf):
v = bitand(v, mask) v = bitand(v, mask)
proc mask*[T: SomeInteger](v: var T; slice: Slice[int]) {.inline, since: (1, 3).} = proc mask*[T: SomeInteger](v: var T; slice: Slice[int]) {.inline, since: (1, 3).} =
## Mutates ``v``, with only the ``1`` bits in the range of ``slice`` ## Mutates `v`, with only the `1` bits in the range of `slice`
## matching those of ``v`` set to 1. ## matching those of `v` set to 1.
## ##
## Effectively maps to a `bitand` operation. ## Effectively maps to a `bitand <#bitand.m,T,T,varargs[T]>`_ operation.
runnableExamples: runnableExamples:
var v = 0b0000_1011'u8 var v = 0b0000_1011'u8
v.mask(1 .. 3) v.mask(1 .. 3)
@ -176,29 +176,29 @@ when defined(nimHasalignOf):
v = bitand(v, toMask[T](slice)) v = bitand(v, toMask[T](slice))
func setMasked*[T: SomeInteger](v, mask :T): T {.inline, since: (1, 3).} = func setMasked*[T: SomeInteger](v, mask :T): T {.inline, since: (1, 3).} =
## Returns ``v``, with all the ``1`` bits from ``mask`` set to 1. ## Returns `v`, with all the `1` bits from `mask` set to 1.
## ##
## Effectively maps to a `bitor` operation. ## Effectively maps to a `bitor <#bitor.m,T,T,varargs[T]>`_ operation.
runnableExamples: runnableExamples:
var v = 0b0000_0011'u8 let v = 0b0000_0011'u8
doAssert v.setMasked(0b0000_1010'u8) == 0b0000_1011'u8 doAssert v.setMasked(0b0000_1010'u8) == 0b0000_1011'u8
bitor(v, mask) bitor(v, mask)
func setMasked*[T: SomeInteger](v: T; slice: Slice[int]): T {.inline, since: (1, 3).} = func setMasked*[T: SomeInteger](v: T; slice: Slice[int]): T {.inline, since: (1, 3).} =
## Returns ``v``, with all the ``1`` bits in the range of ``slice`` set to 1. ## Returns `v`, with all the `1` bits in the range of `slice` set to 1.
## ##
## Effectively maps to a `bitor` operation. ## Effectively maps to a `bitor <#bitor.m,T,T,varargs[T]>`_ operation.
runnableExamples: runnableExamples:
var v = 0b0000_0011'u8 let v = 0b0000_0011'u8
doAssert v.setMasked(2 .. 3) == 0b0000_1111'u8 doAssert v.setMasked(2 .. 3) == 0b0000_1111'u8
bitor(v, toMask[T](slice)) bitor(v, toMask[T](slice))
proc setMask*[T: SomeInteger](v: var T; mask: T) {.inline.} = proc setMask*[T: SomeInteger](v: var T; mask: T) {.inline.} =
## Mutates ``v``, with all the ``1`` bits from ``mask`` set to 1. ## Mutates `v`, with all the `1` bits from `mask` set to 1.
## ##
## Effectively maps to a `bitor` operation. ## Effectively maps to a `bitor <#bitor.m,T,T,varargs[T]>`_ operation.
runnableExamples: runnableExamples:
var v = 0b0000_0011'u8 var v = 0b0000_0011'u8
v.setMask(0b0000_1010'u8) v.setMask(0b0000_1010'u8)
@ -207,9 +207,9 @@ when defined(nimHasalignOf):
v = bitor(v, mask) v = bitor(v, mask)
proc setMask*[T: SomeInteger](v: var T; slice: Slice[int]) {.inline, since: (1, 3).} = proc setMask*[T: SomeInteger](v: var T; slice: Slice[int]) {.inline, since: (1, 3).} =
## Mutates ``v``, with all the ``1`` bits in the range of ``slice`` set to 1. ## Mutates `v`, with all the `1` bits in the range of `slice` set to 1.
## ##
## Effectively maps to a `bitor` operation. ## Effectively maps to a `bitor <#bitor.m,T,T,varargs[T]>`_ operation.
runnableExamples: runnableExamples:
var v = 0b0000_0011'u8 var v = 0b0000_0011'u8
v.setMask(2 .. 3) v.setMask(2 .. 3)
@ -218,29 +218,32 @@ when defined(nimHasalignOf):
v = bitor(v, toMask[T](slice)) v = bitor(v, toMask[T](slice))
func clearMasked*[T: SomeInteger](v, mask :T): T {.inline, since: (1, 3).} = func clearMasked*[T: SomeInteger](v, mask :T): T {.inline, since: (1, 3).} =
## Returns ``v``, with all the ``1`` bits from ``mask`` set to 0. ## Returns `v`, with all the `1` bits from `mask` set to 0.
## ##
## Effectively maps to a `bitand` operation with an *inverted mask.* ## Effectively maps to a `bitand <#bitand.m,T,T,varargs[T]>`_ operation
## with an *inverted mask*.
runnableExamples: runnableExamples:
var v = 0b0000_0011'u8 let v = 0b0000_0011'u8
doAssert v.clearMasked(0b0000_1010'u8) == 0b0000_0001'u8 doAssert v.clearMasked(0b0000_1010'u8) == 0b0000_0001'u8
bitand(v, bitnot(mask)) bitand(v, bitnot(mask))
func clearMasked*[T: SomeInteger](v: T; slice: Slice[int]): T {.inline, since: (1, 3).} = func clearMasked*[T: SomeInteger](v: T; slice: Slice[int]): T {.inline, since: (1, 3).} =
## Returns ``v``, with all the ``1`` bits in the range of ``slice`` set to 0. ## Returns `v`, with all the `1` bits in the range of `slice` set to 0.
## ##
## Effectively maps to a `bitand` operation with an *inverted mask.* ## Effectively maps to a `bitand <#bitand.m,T,T,varargs[T]>`_ operation
## with an *inverted mask*.
runnableExamples: runnableExamples:
var v = 0b0000_0011'u8 let v = 0b0000_0011'u8
doAssert v.clearMasked(1 .. 3) == 0b0000_0001'u8 doAssert v.clearMasked(1 .. 3) == 0b0000_0001'u8
bitand(v, bitnot(toMask[T](slice))) bitand(v, bitnot(toMask[T](slice)))
proc clearMask*[T: SomeInteger](v: var T; mask: T) {.inline.} = proc clearMask*[T: SomeInteger](v: var T; mask: T) {.inline.} =
## Mutates ``v``, with all the ``1`` bits from ``mask`` set to 0. ## Mutates `v`, with all the `1` bits from `mask` set to 0.
## ##
## Effectively maps to a `bitand` operation with an *inverted mask.* ## Effectively maps to a `bitand <#bitand.m,T,T,varargs[T]>`_ operation
## with an *inverted mask*.
runnableExamples: runnableExamples:
var v = 0b0000_0011'u8 var v = 0b0000_0011'u8
v.clearMask(0b0000_1010'u8) v.clearMask(0b0000_1010'u8)
@ -249,9 +252,10 @@ when defined(nimHasalignOf):
v = bitand(v, bitnot(mask)) v = bitand(v, bitnot(mask))
proc clearMask*[T: SomeInteger](v: var T; slice: Slice[int]) {.inline, since: (1, 3).} = proc clearMask*[T: SomeInteger](v: var T; slice: Slice[int]) {.inline, since: (1, 3).} =
## Mutates ``v``, with all the ``1`` bits in the range of ``slice`` set to 0. ## Mutates `v`, with all the `1` bits in the range of `slice` set to 0.
## ##
## Effectively maps to a `bitand` operation with an *inverted mask.* ## Effectively maps to a `bitand <#bitand.m,T,T,varargs[T]>`_ operation
## with an *inverted mask*.
runnableExamples: runnableExamples:
var v = 0b0000_0011'u8 var v = 0b0000_0011'u8
v.clearMask(1 .. 3) v.clearMask(1 .. 3)
@ -260,29 +264,29 @@ when defined(nimHasalignOf):
v = bitand(v, bitnot(toMask[T](slice))) v = bitand(v, bitnot(toMask[T](slice)))
func flipMasked*[T: SomeInteger](v, mask :T): T {.inline, since: (1, 3).} = func flipMasked*[T: SomeInteger](v, mask :T): T {.inline, since: (1, 3).} =
## Returns ``v``, with all the ``1`` bits from ``mask`` flipped. ## Returns `v`, with all the `1` bits from `mask` flipped.
## ##
## Effectively maps to a `bitxor` operation. ## Effectively maps to a `bitxor <#bitxor.m,T,T,varargs[T]>`_ operation.
runnableExamples: runnableExamples:
var v = 0b0000_0011'u8 let v = 0b0000_0011'u8
doAssert v.flipMasked(0b0000_1010'u8) == 0b0000_1001'u8 doAssert v.flipMasked(0b0000_1010'u8) == 0b0000_1001'u8
bitxor(v, mask) bitxor(v, mask)
func flipMasked*[T: SomeInteger](v: T; slice: Slice[int]): T {.inline, since: (1, 3).} = func flipMasked*[T: SomeInteger](v: T; slice: Slice[int]): T {.inline, since: (1, 3).} =
## Returns ``v``, with all the ``1`` bits in the range of ``slice`` flipped. ## Returns `v`, with all the `1` bits in the range of `slice` flipped.
## ##
## Effectively maps to a `bitxor` operation. ## Effectively maps to a `bitxor <#bitxor.m,T,T,varargs[T]>`_ operation.
runnableExamples: runnableExamples:
var v = 0b0000_0011'u8 let v = 0b0000_0011'u8
doAssert v.flipMasked(1 .. 3) == 0b0000_1101'u8 doAssert v.flipMasked(1 .. 3) == 0b0000_1101'u8
bitxor(v, toMask[T](slice)) bitxor(v, toMask[T](slice))
proc flipMask*[T: SomeInteger](v: var T; mask: T) {.inline.} = proc flipMask*[T: SomeInteger](v: var T; mask: T) {.inline.} =
## Mutates ``v``, with all the ``1`` bits from ``mask`` flipped. ## Mutates `v`, with all the `1` bits from `mask` flipped.
## ##
## Effectively maps to a `bitxor` operation. ## Effectively maps to a `bitxor <#bitxor.m,T,T,varargs[T]>`_ operation.
runnableExamples: runnableExamples:
var v = 0b0000_0011'u8 var v = 0b0000_0011'u8
v.flipMask(0b0000_1010'u8) v.flipMask(0b0000_1010'u8)
@ -291,9 +295,9 @@ when defined(nimHasalignOf):
v = bitxor(v, mask) v = bitxor(v, mask)
proc flipMask*[T: SomeInteger](v: var T; slice: Slice[int]) {.inline, since: (1, 3).} = proc flipMask*[T: SomeInteger](v: var T; slice: Slice[int]) {.inline, since: (1, 3).} =
## Mutates ``v``, with all the ``1`` bits in the range of ``slice`` flipped. ## Mutates `v`, with all the `1` bits in the range of `slice` flipped.
## ##
## Effectively maps to a `bitxor` operation. ## Effectively maps to a `bitxor <#bitxor.m,T,T,varargs[T]>`_ operation.
runnableExamples: runnableExamples:
var v = 0b0000_0011'u8 var v = 0b0000_0011'u8
v.flipMask(1 .. 3) v.flipMask(1 .. 3)
@ -302,7 +306,7 @@ when defined(nimHasalignOf):
v = bitxor(v, toMask[T](slice)) v = bitxor(v, toMask[T](slice))
proc setBit*[T: SomeInteger](v: var T; bit: BitsRange[T]) {.inline.} = proc setBit*[T: SomeInteger](v: var T; bit: BitsRange[T]) {.inline.} =
## Mutates ``v``, with the bit at position ``bit`` set to 1 ## Mutates `v`, with the bit at position `bit` set to 1.
runnableExamples: runnableExamples:
var v = 0b0000_0011'u8 var v = 0b0000_0011'u8
v.setBit(5'u8) v.setBit(5'u8)
@ -311,7 +315,7 @@ when defined(nimHasalignOf):
v.setMask(1.T shl bit) v.setMask(1.T shl bit)
proc clearBit*[T: SomeInteger](v: var T; bit: BitsRange[T]) {.inline.} = proc clearBit*[T: SomeInteger](v: var T; bit: BitsRange[T]) {.inline.} =
## Mutates ``v``, with the bit at position ``bit`` set to 0 ## Mutates `v`, with the bit at position `bit` set to 0.
runnableExamples: runnableExamples:
var v = 0b0000_0011'u8 var v = 0b0000_0011'u8
v.clearBit(1'u8) v.clearBit(1'u8)
@ -320,7 +324,7 @@ when defined(nimHasalignOf):
v.clearMask(1.T shl bit) v.clearMask(1.T shl bit)
proc flipBit*[T: SomeInteger](v: var T; bit: BitsRange[T]) {.inline.} = proc flipBit*[T: SomeInteger](v: var T; bit: BitsRange[T]) {.inline.} =
## Mutates ``v``, with the bit at position ``bit`` flipped ## Mutates `v`, with the bit at position `bit` flipped.
runnableExamples: runnableExamples:
var v = 0b0000_0011'u8 var v = 0b0000_0011'u8
v.flipBit(1'u8) v.flipBit(1'u8)
@ -333,7 +337,7 @@ when defined(nimHasalignOf):
v.flipMask(1.T shl bit) v.flipMask(1.T shl bit)
macro setBits*(v: typed; bits: varargs[typed]): untyped = macro setBits*(v: typed; bits: varargs[typed]): untyped =
## Mutates ``v``, with the bits at positions ``bits`` set to 1 ## Mutates `v`, with the bits at positions `bits` set to 1.
runnableExamples: runnableExamples:
var v = 0b0000_0011'u8 var v = 0b0000_0011'u8
v.setBits(3, 5, 7) v.setBits(3, 5, 7)
@ -345,7 +349,7 @@ when defined(nimHasalignOf):
result.add newCall("setBit", v, bit) result.add newCall("setBit", v, bit)
macro clearBits*(v: typed; bits: varargs[typed]): untyped = macro clearBits*(v: typed; bits: varargs[typed]): untyped =
## Mutates ``v``, with the bits at positions ``bits`` set to 0 ## Mutates `v`, with the bits at positions `bits` set to 0.
runnableExamples: runnableExamples:
var v = 0b1111_1111'u8 var v = 0b1111_1111'u8
v.clearBits(1, 3, 5, 7) v.clearBits(1, 3, 5, 7)
@ -357,7 +361,7 @@ when defined(nimHasalignOf):
result.add newCall("clearBit", v, bit) result.add newCall("clearBit", v, bit)
macro flipBits*(v: typed; bits: varargs[typed]): untyped = macro flipBits*(v: typed; bits: varargs[typed]): untyped =
## Mutates ``v``, with the bits at positions ``bits`` set to 0 ## Mutates `v`, with the bits at positions `bits` set to 0.
runnableExamples: runnableExamples:
var v = 0b0000_1111'u8 var v = 0b0000_1111'u8
v.flipBits(1, 3, 5, 7) v.flipBits(1, 3, 5, 7)
@ -370,9 +374,9 @@ when defined(nimHasalignOf):
proc testBit*[T: SomeInteger](v: T; bit: BitsRange[T]): bool {.inline.} = proc testBit*[T: SomeInteger](v: T; bit: BitsRange[T]): bool {.inline.} =
## Returns true if the bit in ``v`` at positions ``bit`` is set to 1 ## Returns true if the bit in `v` at positions `bit` is set to 1.
runnableExamples: runnableExamples:
var v = 0b0000_1111'u8 let v = 0b0000_1111'u8
doAssert v.testBit(0) doAssert v.testBit(0)
doAssert not v.testBit(7) doAssert not v.testBit(7)
@ -381,19 +385,19 @@ when defined(nimHasalignOf):
# #### Pure Nim version #### # #### Pure Nim version ####
proc firstSetBitNim(x: uint32): int {.inline, noSideEffect.} = func firstSetBitNim(x: uint32): int {.inline.} =
## Returns the 1-based index of the least significant set bit of x, or if x is zero, returns zero. ## Returns the 1-based index of the least significant set bit of x, or if x is zero, returns zero.
# https://graphics.stanford.edu/%7Eseander/bithacks.html#ZerosOnRightMultLookup # https://graphics.stanford.edu/%7Eseander/bithacks.html#ZerosOnRightMultLookup
const lookup: array[32, uint8] = [0'u8, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, const lookup: array[32, uint8] = [0'u8, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15,
25, 17, 4, 8, 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9] 25, 17, 4, 8, 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9]
var v = x.uint32 let v = x.uint32
var k = not v + 1 # get two's complement # cast[uint32](-cast[int32](v)) let k = not v + 1 # get two's complement # cast[uint32](-cast[int32](v))
result = 1 + lookup[uint32((v and k) * 0x077CB531'u32) shr 27].int result = 1 + lookup[uint32((v and k) * 0x077CB531'u32) shr 27].int
proc firstSetBitNim(x: uint64): int {.inline, noSideEffect.} = func firstSetBitNim(x: uint64): int {.inline.} =
## Returns the 1-based index of the least significant set bit of x, or if x is zero, returns zero. ## Returns the 1-based index of the least significant set bit of x, or if x is zero, returns zero.
# https://graphics.stanford.edu/%7Eseander/bithacks.html#ZerosOnRightMultLookup # https://graphics.stanford.edu/%7Eseander/bithacks.html#ZerosOnRightMultLookup
var v = uint64(x) let v = uint64(x)
var k = uint32(v and 0xFFFFFFFF'u32) var k = uint32(v and 0xFFFFFFFF'u32)
if k == 0: if k == 0:
k = uint32(v shr 32'u32) and 0xFFFFFFFF'u32 k = uint32(v shr 32'u32) and 0xFFFFFFFF'u32
@ -402,7 +406,7 @@ proc firstSetBitNim(x: uint64): int {.inline, noSideEffect.} =
result = 0 result = 0
result += firstSetBitNim(k) result += firstSetBitNim(k)
proc fastlog2Nim(x: uint32): int {.inline, noSideEffect.} = func fastlog2Nim(x: uint32): int {.inline.} =
## Quickly find the log base 2 of a 32-bit or less integer. ## Quickly find the log base 2 of a 32-bit or less integer.
# https://graphics.stanford.edu/%7Eseander/bithacks.html#IntegerLogDeBruijn # https://graphics.stanford.edu/%7Eseander/bithacks.html#IntegerLogDeBruijn
# https://stackoverflow.com/questions/11376288/fast-computing-of-log2-for-64-bit-integers # https://stackoverflow.com/questions/11376288/fast-computing-of-log2-for-64-bit-integers
@ -416,7 +420,7 @@ proc fastlog2Nim(x: uint32): int {.inline, noSideEffect.} =
v = v or v shr 16 v = v or v shr 16
result = lookup[uint32(v * 0x07C4ACDD'u32) shr 27].int result = lookup[uint32(v * 0x07C4ACDD'u32) shr 27].int
proc fastlog2Nim(x: uint64): int {.inline, noSideEffect.} = func fastlog2Nim(x: uint64): int {.inline.} =
## Quickly find the log base 2 of a 64-bit integer. ## Quickly find the log base 2 of a 64-bit integer.
# https://graphics.stanford.edu/%7Eseander/bithacks.html#IntegerLogDeBruijn # https://graphics.stanford.edu/%7Eseander/bithacks.html#IntegerLogDeBruijn
# https://stackoverflow.com/questions/11376288/fast-computing-of-log2-for-64-bit-integers # https://stackoverflow.com/questions/11376288/fast-computing-of-log2-for-64-bit-integers
@ -479,24 +483,24 @@ when useGCC_builtins:
elif useVCC_builtins: elif useVCC_builtins:
# Counts the number of one bits (population count) in a 16-, 32-, or 64-byte unsigned integer. # Counts the number of one bits (population count) in a 16-, 32-, or 64-byte unsigned integer.
proc builtin_popcnt16(a2: uint16): uint16 {. func builtin_popcnt16(a2: uint16): uint16 {.
importc: "__popcnt16", header: "<intrin.h>", noSideEffect.} importc: "__popcnt16", header: "<intrin.h>".}
proc builtin_popcnt32(a2: uint32): uint32 {. func builtin_popcnt32(a2: uint32): uint32 {.
importc: "__popcnt", header: "<intrin.h>", noSideEffect.} importc: "__popcnt", header: "<intrin.h>".}
proc builtin_popcnt64(a2: uint64): uint64 {. func builtin_popcnt64(a2: uint64): uint64 {.
importc: "__popcnt64", header: "<intrin.h>", noSideEffect.} importc: "__popcnt64", header: "<intrin.h>".}
# Search the mask data from most significant bit (MSB) to least significant bit (LSB) for a set bit (1). # Search the mask data from most significant bit (MSB) to least significant bit (LSB) for a set bit (1).
proc bitScanReverse(index: ptr culong, mask: culong): cuchar {. func bitScanReverse(index: ptr culong, mask: culong): cuchar {.
importc: "_BitScanReverse", header: "<intrin.h>", noSideEffect.} importc: "_BitScanReverse", header: "<intrin.h>".}
proc bitScanReverse64(index: ptr culong, mask: uint64): cuchar {. func bitScanReverse64(index: ptr culong, mask: uint64): cuchar {.
importc: "_BitScanReverse64", header: "<intrin.h>", noSideEffect.} importc: "_BitScanReverse64", header: "<intrin.h>".}
# Search the mask data from least significant bit (LSB) to the most significant bit (MSB) for a set bit (1). # Search the mask data from least significant bit (LSB) to the most significant bit (MSB) for a set bit (1).
proc bitScanForward(index: ptr culong, mask: culong): cuchar {. func bitScanForward(index: ptr culong, mask: culong): cuchar {.
importc: "_BitScanForward", header: "<intrin.h>", noSideEffect.} importc: "_BitScanForward", header: "<intrin.h>".}
proc bitScanForward64(index: ptr culong, mask: uint64): cuchar {. func bitScanForward64(index: ptr culong, mask: uint64): cuchar {.
importc: "_BitScanForward64", header: "<intrin.h>", noSideEffect.} importc: "_BitScanForward64", header: "<intrin.h>".}
template vcc_scan_impl(fnc: untyped; v: untyped): int = template vcc_scan_impl(fnc: untyped; v: untyped): int =
var index: culong var index: culong
@ -508,22 +512,22 @@ elif useICC_builtins:
# Intel compiler intrinsics: http://fulla.fnal.gov/intel/compiler_c/main_cls/intref_cls/common/intref_allia_misc.htm # Intel compiler intrinsics: http://fulla.fnal.gov/intel/compiler_c/main_cls/intref_cls/common/intref_allia_misc.htm
# see also: https://software.intel.com/en-us/node/523362 # see also: https://software.intel.com/en-us/node/523362
# Count the number of bits set to 1 in an integer a, and return that count in dst. # Count the number of bits set to 1 in an integer a, and return that count in dst.
proc builtin_popcnt32(a: cint): cint {. func builtin_popcnt32(a: cint): cint {.
importc: "_popcnt", header: "<immintrin.h>", noSideEffect.} importc: "_popcnt", header: "<immintrin.h>".}
proc builtin_popcnt64(a: uint64): cint {. func builtin_popcnt64(a: uint64): cint {.
importc: "_popcnt64", header: "<immintrin.h>", noSideEffect.} importc: "_popcnt64", header: "<immintrin.h>".}
# Returns the number of trailing 0-bits in x, starting at the least significant bit position. If x is 0, the result is undefined. # Returns the number of trailing 0-bits in x, starting at the least significant bit position. If x is 0, the result is undefined.
proc bitScanForward(p: ptr uint32, b: uint32): cuchar {. func bitScanForward(p: ptr uint32, b: uint32): cuchar {.
importc: "_BitScanForward", header: "<immintrin.h>", noSideEffect.} importc: "_BitScanForward", header: "<immintrin.h>".}
proc bitScanForward64(p: ptr uint32, b: uint64): cuchar {. func bitScanForward64(p: ptr uint32, b: uint64): cuchar {.
importc: "_BitScanForward64", header: "<immintrin.h>", noSideEffect.} importc: "_BitScanForward64", header: "<immintrin.h>".}
# Returns the number of leading 0-bits in x, starting at the most significant bit position. If x is 0, the result is undefined. # Returns the number of leading 0-bits in x, starting at the most significant bit position. If x is 0, the result is undefined.
proc bitScanReverse(p: ptr uint32, b: uint32): cuchar {. func bitScanReverse(p: ptr uint32, b: uint32): cuchar {.
importc: "_BitScanReverse", header: "<immintrin.h>", noSideEffect.} importc: "_BitScanReverse", header: "<immintrin.h>".}
proc bitScanReverse64(p: ptr uint32, b: uint64): cuchar {. func bitScanReverse64(p: ptr uint32, b: uint64): cuchar {.
importc: "_BitScanReverse64", header: "<immintrin.h>", noSideEffect.} importc: "_BitScanReverse64", header: "<immintrin.h>".}
template icc_scan_impl(fnc: untyped; v: untyped): int = template icc_scan_impl(fnc: untyped; v: untyped): int =
var index: uint32 var index: uint32
@ -531,8 +535,8 @@ elif useICC_builtins:
index.int index.int
proc countSetBits*(x: SomeInteger): int {.inline, noSideEffect.} = func countSetBits*(x: SomeInteger): int {.inline.} =
## Counts the set bits in integer. (also called `Hamming weight`:idx:.) ## Counts the set bits in an integer (also called `Hamming weight`:idx:).
runnableExamples: runnableExamples:
doAssert countSetBits(0b0000_0011'u8) == 2 doAssert countSetBits(0b0000_0011'u8) == 2
doAssert countSetBits(0b1010_1010'u8) == 4 doAssert countSetBits(0b1010_1010'u8) == 4
@ -562,13 +566,13 @@ proc countSetBits*(x: SomeInteger): int {.inline, noSideEffect.} =
when sizeof(x) <= 4: result = countSetBitsNim(x.uint32) when sizeof(x) <= 4: result = countSetBitsNim(x.uint32)
else: result = countSetBitsNim(x.uint64) else: result = countSetBitsNim(x.uint64)
proc popcount*(x: SomeInteger): int {.inline, noSideEffect.} = func popcount*(x: SomeInteger): int {.inline.} =
## Alias for for `countSetBits <#countSetBits,SomeInteger>`_. (Hamming weight.) ## Alias for `countSetBits <#countSetBits,SomeInteger>`_ (Hamming weight).
result = countSetBits(x) result = countSetBits(x)
proc parityBits*(x: SomeInteger): int {.inline, noSideEffect.} = func parityBits*(x: SomeInteger): int {.inline.} =
## Calculate the bit parity in integer. If number of 1-bit ## Calculate the bit parity in an integer. If the number of 1-bits
## is odd parity is 1, otherwise 0. ## is odd, the parity is 1, otherwise 0.
runnableExamples: runnableExamples:
doAssert parityBits(0b0000_0000'u8) == 0 doAssert parityBits(0b0000_0000'u8) == 0
doAssert parityBits(0b0101_0001'u8) == 1 doAssert parityBits(0b0101_0001'u8) == 1
@ -589,10 +593,10 @@ proc parityBits*(x: SomeInteger): int {.inline, noSideEffect.} =
when sizeof(x) <= 4: result = parityImpl(x.uint32) when sizeof(x) <= 4: result = parityImpl(x.uint32)
else: result = parityImpl(x.uint64) else: result = parityImpl(x.uint64)
proc firstSetBit*(x: SomeInteger): int {.inline, noSideEffect.} = func firstSetBit*(x: SomeInteger): int {.inline.} =
## Returns the 1-based index of the least significant set bit of x. ## Returns the 1-based index of the least significant set bit of `x`.
## If `x` is zero, when ``noUndefinedBitOpts`` is set, result is 0, ## If `x` is zero, when `noUndefinedBitOpts` is set, the result is 0,
## otherwise result is undefined. ## otherwise the result is undefined.
runnableExamples: runnableExamples:
doAssert firstSetBit(0b0000_0001'u8) == 1 doAssert firstSetBit(0b0000_0001'u8) == 1
doAssert firstSetBit(0b0000_0010'u8) == 2 doAssert firstSetBit(0b0000_0010'u8) == 2
@ -633,10 +637,10 @@ proc firstSetBit*(x: SomeInteger): int {.inline, noSideEffect.} =
when sizeof(x) <= 4: result = firstSetBitNim(x.uint32) when sizeof(x) <= 4: result = firstSetBitNim(x.uint32)
else: result = firstSetBitNim(x.uint64) else: result = firstSetBitNim(x.uint64)
proc fastLog2*(x: SomeInteger): int {.inline, noSideEffect.} = func fastLog2*(x: SomeInteger): int {.inline.} =
## Quickly find the log base 2 of an integer. ## Quickly find the log base 2 of an integer.
## If `x` is zero, when ``noUndefinedBitOpts`` is set, result is -1, ## If `x` is zero, when `noUndefinedBitOpts` is set, the result is -1,
## otherwise result is undefined. ## otherwise the result is undefined.
runnableExamples: runnableExamples:
doAssert fastLog2(0b0000_0001'u8) == 0 doAssert fastLog2(0b0000_0001'u8) == 0
doAssert fastLog2(0b0000_0010'u8) == 1 doAssert fastLog2(0b0000_0010'u8) == 1
@ -673,12 +677,12 @@ proc fastLog2*(x: SomeInteger): int {.inline, noSideEffect.} =
when sizeof(x) <= 4: result = fastlog2Nim(x.uint32) when sizeof(x) <= 4: result = fastlog2Nim(x.uint32)
else: result = fastlog2Nim(x.uint64) else: result = fastlog2Nim(x.uint64)
proc countLeadingZeroBits*(x: SomeInteger): int {.inline, noSideEffect.} = func countLeadingZeroBits*(x: SomeInteger): int {.inline.} =
## Returns the number of leading zero bits in integer. ## Returns the number of leading zero bits in an integer.
## If `x` is zero, when ``noUndefinedBitOpts`` is set, result is 0, ## If `x` is zero, when `noUndefinedBitOpts` is set, the result is 0,
## otherwise result is undefined. ## otherwise the result is undefined.
## ##
## See also: ## **See also:**
## * `countTrailingZeroBits proc <#countTrailingZeroBits,SomeInteger>`_ ## * `countTrailingZeroBits proc <#countTrailingZeroBits,SomeInteger>`_
runnableExamples: runnableExamples:
doAssert countLeadingZeroBits(0b0000_0001'u8) == 7 doAssert countLeadingZeroBits(0b0000_0001'u8) == 7
@ -702,12 +706,12 @@ proc countLeadingZeroBits*(x: SomeInteger): int {.inline, noSideEffect.} =
when sizeof(x) <= 4: result = sizeof(x)*8 - 1 - fastlog2Nim(x.uint32) when sizeof(x) <= 4: result = sizeof(x)*8 - 1 - fastlog2Nim(x.uint32)
else: result = sizeof(x)*8 - 1 - fastlog2Nim(x.uint64) else: result = sizeof(x)*8 - 1 - fastlog2Nim(x.uint64)
proc countTrailingZeroBits*(x: SomeInteger): int {.inline, noSideEffect.} = func countTrailingZeroBits*(x: SomeInteger): int {.inline.} =
## Returns the number of trailing zeros in integer. ## Returns the number of trailing zeros in an integer.
## If `x` is zero, when ``noUndefinedBitOpts`` is set, result is 0, ## If `x` is zero, when `noUndefinedBitOpts` is set, the result is 0,
## otherwise result is undefined. ## otherwise the result is undefined.
## ##
## See also: ## **See also:**
## * `countLeadingZeroBits proc <#countLeadingZeroBits,SomeInteger>`_ ## * `countLeadingZeroBits proc <#countLeadingZeroBits,SomeInteger>`_
runnableExamples: runnableExamples:
doAssert countTrailingZeroBits(0b0000_0001'u8) == 0 doAssert countTrailingZeroBits(0b0000_0001'u8) == 0
@ -969,15 +973,14 @@ func rotateRightBits*(value: uint64, shift: range[0..64]): uint64 {.inline.} =
else: else:
rotr(value, shift.int32) rotr(value, shift.int32)
proc repeatBits[T: SomeUnsignedInt](x: SomeUnsignedInt; retType: type[T]): T {. func repeatBits[T: SomeUnsignedInt](x: SomeUnsignedInt; retType: type[T]): T =
noSideEffect.} =
result = x result = x
var i = 1 var i = 1
while i != (sizeof(T) div sizeof(x)): while i != (sizeof(T) div sizeof(x)):
result = (result shl (sizeof(x)*8*i)) or result result = (result shl (sizeof(x)*8*i)) or result
i *= 2 i *= 2
proc reverseBits*[T: SomeUnsignedInt](x: T): T {.noSideEffect.} = func reverseBits*[T: SomeUnsignedInt](x: T): T =
## Return the bit reversal of x. ## Return the bit reversal of x.
runnableExamples: runnableExamples:
doAssert reverseBits(0b10100100'u8) == 0b00100101'u8 doAssert reverseBits(0b10100100'u8) == 0b00100101'u8

View file

@ -2,12 +2,11 @@ discard """
nimout: "OK" nimout: "OK"
output: ''' output: '''
OK OK
OK
''' '''
""" """
import bitops import bitops
proc main1() = proc main() =
const U8 = 0b0011_0010'u8 const U8 = 0b0011_0010'u8
const I8 = 0b0011_0010'i8 const I8 = 0b0011_0010'i8
const U16 = 0b00100111_00101000'u16 const U16 = 0b00100111_00101000'u16
@ -21,120 +20,120 @@ proc main1() =
const U64C = 0b00101010_11110101_10001111_00101000_00000100_00000000_00000100_00000000'u64 const U64C = 0b00101010_11110101_10001111_00101000_00000100_00000000_00000100_00000000'u64
const I64C = 0b00101010_11110101_10001111_00101000_00000100_00000000_00000100_00000000'i64 const I64C = 0b00101010_11110101_10001111_00101000_00000100_00000000_00000100_00000000'i64
doAssert( (U8 and U8) == bitand(U8,U8) ) doAssert (U8 and U8) == bitand(U8,U8)
doAssert( (I8 and I8) == bitand(I8,I8) ) doAssert (I8 and I8) == bitand(I8,I8)
doAssert( (U16 and U16) == bitand(U16,U16) ) doAssert (U16 and U16) == bitand(U16,U16)
doAssert( (I16 and I16) == bitand(I16,I16) ) doAssert (I16 and I16) == bitand(I16,I16)
doAssert( (U32 and U32) == bitand(U32,U32) ) doAssert (U32 and U32) == bitand(U32,U32)
doAssert( (I32 and I32) == bitand(I32,I32) ) doAssert (I32 and I32) == bitand(I32,I32)
doAssert( (U64A and U64B) == bitand(U64A,U64B) ) doAssert (U64A and U64B) == bitand(U64A,U64B)
doAssert( (I64A and I64B) == bitand(I64A,I64B) ) doAssert (I64A and I64B) == bitand(I64A,I64B)
doAssert( (U64A and U64B and U64C) == bitand(U64A,U64B,U64C) ) doAssert (U64A and U64B and U64C) == bitand(U64A,U64B,U64C)
doAssert( (I64A and I64B and I64C) == bitand(I64A,I64B,I64C) ) doAssert (I64A and I64B and I64C) == bitand(I64A,I64B,I64C)
doAssert( (U8 or U8) == bitor(U8,U8) ) doAssert (U8 or U8) == bitor(U8,U8)
doAssert( (I8 or I8) == bitor(I8,I8) ) doAssert (I8 or I8) == bitor(I8,I8)
doAssert( (U16 or U16) == bitor(U16,U16) ) doAssert (U16 or U16) == bitor(U16,U16)
doAssert( (I16 or I16) == bitor(I16,I16) ) doAssert (I16 or I16) == bitor(I16,I16)
doAssert( (U32 or U32) == bitor(U32,U32) ) doAssert (U32 or U32) == bitor(U32,U32)
doAssert( (I32 or I32) == bitor(I32,I32) ) doAssert (I32 or I32) == bitor(I32,I32)
doAssert( (U64A or U64B) == bitor(U64A,U64B) ) doAssert (U64A or U64B) == bitor(U64A,U64B)
doAssert( (I64A or I64B) == bitor(I64A,I64B) ) doAssert (I64A or I64B) == bitor(I64A,I64B)
doAssert( (U64A or U64B or U64C) == bitor(U64A,U64B,U64C) ) doAssert (U64A or U64B or U64C) == bitor(U64A,U64B,U64C)
doAssert( (I64A or I64B or I64C) == bitor(I64A,I64B,I64C) ) doAssert (I64A or I64B or I64C) == bitor(I64A,I64B,I64C)
doAssert( (U8 xor U8) == bitxor(U8,U8) ) doAssert (U8 xor U8) == bitxor(U8,U8)
doAssert( (I8 xor I8) == bitxor(I8,I8) ) doAssert (I8 xor I8) == bitxor(I8,I8)
doAssert( (U16 xor U16) == bitxor(U16,U16) ) doAssert (U16 xor U16) == bitxor(U16,U16)
doAssert( (I16 xor I16) == bitxor(I16,I16) ) doAssert (I16 xor I16) == bitxor(I16,I16)
doAssert( (U32 xor U32) == bitxor(U32,U32) ) doAssert (U32 xor U32) == bitxor(U32,U32)
doAssert( (I32 xor I32) == bitxor(I32,I32) ) doAssert (I32 xor I32) == bitxor(I32,I32)
doAssert( (U64A xor U64B) == bitxor(U64A,U64B) ) doAssert (U64A xor U64B) == bitxor(U64A,U64B)
doAssert( (I64A xor I64B) == bitxor(I64A,I64B) ) doAssert (I64A xor I64B) == bitxor(I64A,I64B)
doAssert( (U64A xor U64B xor U64C) == bitxor(U64A,U64B,U64C) ) doAssert (U64A xor U64B xor U64C) == bitxor(U64A,U64B,U64C)
doAssert( (I64A xor I64B xor I64C) == bitxor(I64A,I64B,I64C) ) doAssert (I64A xor I64B xor I64C) == bitxor(I64A,I64B,I64C)
doAssert( not(U8) == bitnot(U8) ) doAssert not(U8) == bitnot(U8)
doAssert( not(I8) == bitnot(I8) ) doAssert not(I8) == bitnot(I8)
doAssert( not(U16) == bitnot(U16) ) doAssert not(U16) == bitnot(U16)
doAssert( not(I16) == bitnot(I16) ) doAssert not(I16) == bitnot(I16)
doAssert( not(U32) == bitnot(U32) ) doAssert not(U32) == bitnot(U32)
doAssert( not(I32) == bitnot(I32) ) doAssert not(I32) == bitnot(I32)
doAssert( not(U64A) == bitnot(U64A) ) doAssert not(U64A) == bitnot(U64A)
doAssert( not(I64A) == bitnot(I64A) ) doAssert not(I64A) == bitnot(I64A)
doAssert( U64A.fastLog2 == 62) doAssert U64A.fastLog2 == 62
doAssert( I64A.fastLog2 == 62) doAssert I64A.fastLog2 == 62
doAssert( U64A.countLeadingZeroBits == 1) doAssert U64A.countLeadingZeroBits == 1
doAssert( I64A.countLeadingZeroBits == 1) doAssert I64A.countLeadingZeroBits == 1
doAssert( U64A.countTrailingZeroBits == 0) doAssert U64A.countTrailingZeroBits == 0
doAssert( I64A.countTrailingZeroBits == 0) doAssert I64A.countTrailingZeroBits == 0
doAssert( U64A.firstSetBit == 1) doAssert U64A.firstSetBit == 1
doAssert( I64A.firstSetBit == 1) doAssert I64A.firstSetBit == 1
doAssert( U64A.parityBits == 1) doAssert U64A.parityBits == 1
doAssert( I64A.parityBits == 1) doAssert I64A.parityBits == 1
doAssert( U64A.countSetBits == 29) doAssert U64A.countSetBits == 29
doAssert( I64A.countSetBits == 29) doAssert I64A.countSetBits == 29
doAssert( U64A.rotateLeftBits(37) == 0b00101001_00001111_01000010_00101000_10000111_11101111_10010001_01010011'u64) doAssert U64A.rotateLeftBits(37) == 0b00101001_00001111_01000010_00101000_10000111_11101111_10010001_01010011'u64
doAssert( U64A.rotateRightBits(37) == 0b01010100_11001010_01000011_11010000_10001010_00100001_11111011_11100100'u64) doAssert U64A.rotateRightBits(37) == 0b01010100_11001010_01000011_11010000_10001010_00100001_11111011_11100100'u64
doAssert( U64B.firstSetBit == 36) doAssert U64B.firstSetBit == 36
doAssert( I64B.firstSetBit == 36) doAssert I64B.firstSetBit == 36
doAssert( U32.fastLog2 == 31) doAssert U32.fastLog2 == 31
doAssert( I32.fastLog2 == 31) doAssert I32.fastLog2 == 31
doAssert( U32.countLeadingZeroBits == 0) doAssert U32.countLeadingZeroBits == 0
doAssert( I32.countLeadingZeroBits == 0) doAssert I32.countLeadingZeroBits == 0
doAssert( U32.countTrailingZeroBits == 4) doAssert U32.countTrailingZeroBits == 4
doAssert( I32.countTrailingZeroBits == 4) doAssert I32.countTrailingZeroBits == 4
doAssert( U32.firstSetBit == 5) doAssert U32.firstSetBit == 5
doAssert( I32.firstSetBit == 5) doAssert I32.firstSetBit == 5
doAssert( U32.parityBits == 0) doAssert U32.parityBits == 0
doAssert( I32.parityBits == 0) doAssert I32.parityBits == 0
doAssert( U32.countSetBits == 16) doAssert U32.countSetBits == 16
doAssert( I32.countSetBits == 16) doAssert I32.countSetBits == 16
doAssert( U32.rotateLeftBits(21) == 0b01001010_00011010_10110011_10011011'u32) doAssert U32.rotateLeftBits(21) == 0b01001010_00011010_10110011_10011011'u32
doAssert( U32.rotateRightBits(21) == 0b11100110_11010010_10000110_10101100'u32) doAssert U32.rotateRightBits(21) == 0b11100110_11010010_10000110_10101100'u32
doAssert( U16.fastLog2 == 13) doAssert U16.fastLog2 == 13
doAssert( I16.fastLog2 == 13) doAssert I16.fastLog2 == 13
doAssert( U16.countLeadingZeroBits == 2) doAssert U16.countLeadingZeroBits == 2
doAssert( I16.countLeadingZeroBits == 2) doAssert I16.countLeadingZeroBits == 2
doAssert( U16.countTrailingZeroBits == 3) doAssert U16.countTrailingZeroBits == 3
doAssert( I16.countTrailingZeroBits == 3) doAssert I16.countTrailingZeroBits == 3
doAssert( U16.firstSetBit == 4) doAssert U16.firstSetBit == 4
doAssert( I16.firstSetBit == 4) doAssert I16.firstSetBit == 4
doAssert( U16.parityBits == 0) doAssert U16.parityBits == 0
doAssert( I16.parityBits == 0) doAssert I16.parityBits == 0
doAssert( U16.countSetBits == 6) doAssert U16.countSetBits == 6
doAssert( I16.countSetBits == 6) doAssert I16.countSetBits == 6
doAssert( U16.rotateLeftBits(12) == 0b10000010_01110010'u16) doAssert U16.rotateLeftBits(12) == 0b10000010_01110010'u16
doAssert( U16.rotateRightBits(12) == 0b01110010_10000010'u16) doAssert U16.rotateRightBits(12) == 0b01110010_10000010'u16
doAssert( U8.fastLog2 == 5) doAssert U8.fastLog2 == 5
doAssert( I8.fastLog2 == 5) doAssert I8.fastLog2 == 5
doAssert( U8.countLeadingZeroBits == 2) doAssert U8.countLeadingZeroBits == 2
doAssert( I8.countLeadingZeroBits == 2) doAssert I8.countLeadingZeroBits == 2
doAssert( U8.countTrailingZeroBits == 1) doAssert U8.countTrailingZeroBits == 1
doAssert( I8.countTrailingZeroBits == 1) doAssert I8.countTrailingZeroBits == 1
doAssert( U8.firstSetBit == 2) doAssert U8.firstSetBit == 2
doAssert( I8.firstSetBit == 2) doAssert I8.firstSetBit == 2
doAssert( U8.parityBits == 1) doAssert U8.parityBits == 1
doAssert( I8.parityBits == 1) doAssert I8.parityBits == 1
doAssert( U8.countSetBits == 3) doAssert U8.countSetBits == 3
doAssert( I8.countSetBits == 3) doAssert I8.countSetBits == 3
doAssert( U8.rotateLeftBits(3) == 0b10010001'u8) doAssert U8.rotateLeftBits(3) == 0b10010001'u8
doAssert( U8.rotateRightBits(3) == 0b0100_0110'u8) doAssert U8.rotateRightBits(3) == 0b0100_0110'u8
template test_undefined_impl(ffunc: untyped; expected: int; is_static: bool) = template test_undefined_impl(ffunc: untyped; expected: int; is_static: bool) =
doAssert( ffunc(0'u8) == expected) doAssert ffunc(0'u8) == expected
doAssert( ffunc(0'i8) == expected) doAssert ffunc(0'i8) == expected
doAssert( ffunc(0'u16) == expected) doAssert ffunc(0'u16) == expected
doAssert( ffunc(0'i16) == expected) doAssert ffunc(0'i16) == expected
doAssert( ffunc(0'u32) == expected) doAssert ffunc(0'u32) == expected
doAssert( ffunc(0'i32) == expected) doAssert ffunc(0'i32) == expected
doAssert( ffunc(0'u64) == expected) doAssert ffunc(0'u64) == expected
doAssert( ffunc(0'i64) == expected) doAssert ffunc(0'i64) == expected
template test_undefined(ffunc: untyped; expected: int) = template test_undefined(ffunc: untyped; expected: int) =
test_undefined_impl(ffunc, expected, false) test_undefined_impl(ffunc, expected, false)
@ -151,106 +150,106 @@ proc main1() =
test_undefined(fastLog2, -1) test_undefined(fastLog2, -1)
# check for undefined behavior with rotate by zero. # check for undefined behavior with rotate by zero.
doAssert( U8.rotateLeftBits(0) == U8) doAssert U8.rotateLeftBits(0) == U8
doAssert( U8.rotateRightBits(0) == U8) doAssert U8.rotateRightBits(0) == U8
doAssert( U16.rotateLeftBits(0) == U16) doAssert U16.rotateLeftBits(0) == U16
doAssert( U16.rotateRightBits(0) == U16) doAssert U16.rotateRightBits(0) == U16
doAssert( U32.rotateLeftBits(0) == U32) doAssert U32.rotateLeftBits(0) == U32
doAssert( U32.rotateRightBits(0) == U32) doAssert U32.rotateRightBits(0) == U32
doAssert( U64A.rotateLeftBits(0) == U64A) doAssert U64A.rotateLeftBits(0) == U64A
doAssert( U64A.rotateRightBits(0) == U64A) doAssert U64A.rotateRightBits(0) == U64A
# check for undefined behavior with rotate by integer width. # check for undefined behavior with rotate by integer width.
doAssert( U8.rotateLeftBits(8) == U8) doAssert U8.rotateLeftBits(8) == U8
doAssert( U8.rotateRightBits(8) == U8) doAssert U8.rotateRightBits(8) == U8
doAssert( U16.rotateLeftBits(16) == U16) doAssert U16.rotateLeftBits(16) == U16
doAssert( U16.rotateRightBits(16) == U16) doAssert U16.rotateRightBits(16) == U16
doAssert( U32.rotateLeftBits(32) == U32) doAssert U32.rotateLeftBits(32) == U32
doAssert( U32.rotateRightBits(32) == U32) doAssert U32.rotateRightBits(32) == U32
doAssert( U64A.rotateLeftBits(64) == U64A) doAssert U64A.rotateLeftBits(64) == U64A
doAssert( U64A.rotateRightBits(64) == U64A) doAssert U64A.rotateRightBits(64) == U64A
block: block:
# basic mask operations (mutating) # basic mask operations (mutating)
var v: uint8 var v: uint8
v.setMask(0b1100_0000) v.setMask(0b1100_0000)
v.setMask(0b0000_1100) v.setMask(0b0000_1100)
doAssert(v == 0b1100_1100) doAssert v == 0b1100_1100
v.flipMask(0b0101_0101) v.flipMask(0b0101_0101)
doAssert(v == 0b1001_1001) doAssert v == 0b1001_1001
v.clearMask(0b1000_1000) v.clearMask(0b1000_1000)
doAssert(v == 0b0001_0001) doAssert v == 0b0001_0001
v.clearMask(0b0001_0001) v.clearMask(0b0001_0001)
doAssert(v == 0b0000_0000) doAssert v == 0b0000_0000
v.setMask(0b0001_1110) v.setMask(0b0001_1110)
doAssert(v == 0b0001_1110) doAssert v == 0b0001_1110
v.mask(0b0101_0100) v.mask(0b0101_0100)
doAssert(v == 0b0001_0100) doAssert v == 0b0001_0100
block: block:
# basic mask operations (non-mutating) # basic mask operations (non-mutating)
let v = 0b1100_0000'u8 let v = 0b1100_0000'u8
doAssert(v.masked(0b0000_1100) == 0b0000_0000) doAssert v.masked(0b0000_1100) == 0b0000_0000
doAssert(v.masked(0b1000_1100) == 0b1000_0000) doAssert v.masked(0b1000_1100) == 0b1000_0000
doAssert(v.setMasked(0b0000_1100) == 0b1100_1100) doAssert v.setMasked(0b0000_1100) == 0b1100_1100
doAssert(v.setMasked(0b1000_1110) == 0b1100_1110) doAssert v.setMasked(0b1000_1110) == 0b1100_1110
doAssert(v.flipMasked(0b1100_1000) == 0b0000_1000) doAssert v.flipMasked(0b1100_1000) == 0b0000_1000
doAssert(v.flipMasked(0b0000_1100) == 0b1100_1100) doAssert v.flipMasked(0b0000_1100) == 0b1100_1100
let t = 0b1100_0110'u8 let t = 0b1100_0110'u8
doAssert(t.clearMasked(0b0100_1100) == 0b1000_0010) doAssert t.clearMasked(0b0100_1100) == 0b1000_0010
doAssert(t.clearMasked(0b1100_0000) == 0b0000_0110) doAssert t.clearMasked(0b1100_0000) == 0b0000_0110
block: block:
# basic bitslice opeartions # basic bitslice opeartions
let a = 0b1111_1011'u8 let a = 0b1111_1011'u8
doAssert(a.bitsliced(0 .. 3) == 0b1011) doAssert a.bitsliced(0 .. 3) == 0b1011
doAssert(a.bitsliced(2 .. 3) == 0b10) doAssert a.bitsliced(2 .. 3) == 0b10
doAssert(a.bitsliced(4 .. 7) == 0b1111) doAssert a.bitsliced(4 .. 7) == 0b1111
# same thing, but with exclusive ranges. # same thing, but with exclusive ranges.
doAssert(a.bitsliced(0 ..< 4) == 0b1011) doAssert a.bitsliced(0 ..< 4) == 0b1011
doAssert(a.bitsliced(2 ..< 4) == 0b10) doAssert a.bitsliced(2 ..< 4) == 0b10
doAssert(a.bitsliced(4 ..< 8) == 0b1111) doAssert a.bitsliced(4 ..< 8) == 0b1111
# mutating # mutating
var b = 0b1111_1011'u8 var b = 0b1111_1011'u8
b.bitslice(1 .. 3) b.bitslice(1 .. 3)
doAssert(b == 0b101) doAssert b == 0b101
# loop test: # loop test:
let c = 0b1111_1111'u8 let c = 0b1111_1111'u8
for i in 0 .. 7: for i in 0 .. 7:
doAssert(c.bitsliced(i .. 7) == c shr i) doAssert c.bitsliced(i .. 7) == c shr i
block: block:
# bitslice versions of mask operations (mutating) # bitslice versions of mask operations (mutating)
var a = 0b1100_1100'u8 var a = 0b1100_1100'u8
let b = toMask[uint8](2 .. 3) let b = toMask[uint8](2 .. 3)
a.mask(b) a.mask(b)
doAssert(a == 0b0000_1100) doAssert a == 0b0000_1100
a.setMask(4 .. 7) a.setMask(4 .. 7)
doAssert(a == 0b1111_1100) doAssert a == 0b1111_1100
a.flipMask(1 .. 3) a.flipMask(1 .. 3)
doAssert(a == 0b1111_0010) doAssert a == 0b1111_0010
a.flipMask(2 .. 4) a.flipMask(2 .. 4)
doAssert(a == 0b1110_1110) doAssert a == 0b1110_1110
a.clearMask(2 .. 4) a.clearMask(2 .. 4)
doAssert(a == 0b1110_0010) doAssert a == 0b1110_0010
a.mask(0 .. 3) a.mask(0 .. 3)
doAssert(a == 0b0000_0010) doAssert a == 0b0000_0010
# composition of mask from slices: # composition of mask from slices:
let c = bitor(toMask[uint8](2 .. 3), toMask[uint8](5 .. 7)) let c = bitor(toMask[uint8](2 .. 3), toMask[uint8](5 .. 7))
doAssert(c == 0b1110_1100'u8) doAssert c == 0b1110_1100'u8
block: block:
# bitslice versions of mask operations (non-mutating) # bitslice versions of mask operations (non-mutating)
let a = 0b1100_1100'u8 let a = 0b1100_1100'u8
doAssert(a.masked(toMask[uint8](2 .. 3)) == 0b0000_1100) doAssert a.masked(toMask[uint8](2 .. 3)) == 0b0000_1100
doAssert(a.masked(2 .. 3) == 0b0000_1100) doAssert a.masked(2 .. 3) == 0b0000_1100
doAssert(a.setMasked(0 .. 3) == 0b1100_1111) doAssert a.setMasked(0 .. 3) == 0b1100_1111
doAssert(a.setMasked(3 .. 4) == 0b1101_1100) doAssert a.setMasked(3 .. 4) == 0b1101_1100
doAssert(a.flipMasked(0 .. 3) == 0b1100_0011) doAssert a.flipMasked(0 .. 3) == 0b1100_0011
doAssert(a.flipMasked(0 .. 7) == 0b0011_0011) doAssert a.flipMasked(0 .. 7) == 0b0011_0011
doAssert(a.flipMasked(2 .. 3) == 0b1100_0000) doAssert a.flipMasked(2 .. 3) == 0b1100_0000
doAssert(a.clearMasked(2 .. 3) == 0b1100_0000) doAssert a.clearMasked(2 .. 3) == 0b1100_0000
doAssert(a.clearMasked(3 .. 6) == 0b1000_0100) doAssert a.clearMasked(3 .. 6) == 0b1000_0100
block: block:
# single bit operations # single bit operations
var v: uint8 var v: uint8
@ -287,7 +286,7 @@ proc main1() =
block: block:
proc testReverseBitsInvo(x: SomeUnsignedInt) = proc testReverseBitsInvo(x: SomeUnsignedInt) =
doAssert(reverseBits(reverseBits(x)) == x) doAssert reverseBits(reverseBits(x)) == x
proc testReverseBitsPerType(x, reversed: uint64) = proc testReverseBitsPerType(x, reversed: uint64) =
doAssert reverseBits(x) == reversed doAssert reverseBits(x) == reversed
@ -329,6 +328,9 @@ proc main1() =
echo "OK" echo "OK"
# bug #7587
doAssert popcount(0b11111111'i8) == 8
block: # not ready for vm because exception is compile error block: # not ready for vm because exception is compile error
try: try:
var v: uint32 var v: uint32
@ -341,172 +343,7 @@ block: # not ready for vm because exception is compile error
doAssert false doAssert false
main1() main()
static: static:
# test everything on vm as well # test everything on vm as well
main1() main()
proc main2() =
const U8 = 0b0011_0010'u8
const I8 = 0b0011_0010'i8
const U16 = 0b00100111_00101000'u16
const I16 = 0b00100111_00101000'i16
const U32 = 0b11010101_10011100_11011010_01010000'u32
const I32 = 0b11010101_10011100_11011010_01010000'i32
const U64A = 0b01000100_00111111_01111100_10001010_10011001_01001000_01111010_00010001'u64
const I64A = 0b01000100_00111111_01111100_10001010_10011001_01001000_01111010_00010001'i64
const U64B = 0b00110010_11011101_10001111_00101000_00000000_00000000_00000000_00000000'u64
const I64B = 0b00110010_11011101_10001111_00101000_00000000_00000000_00000000_00000000'i64
doAssert( U64A.fastLog2 == 62)
doAssert( I64A.fastLog2 == 62)
doAssert( U64A.countLeadingZeroBits == 1)
doAssert( I64A.countLeadingZeroBits == 1)
doAssert( U64A.countTrailingZeroBits == 0)
doAssert( I64A.countTrailingZeroBits == 0)
doAssert( U64A.firstSetBit == 1)
doAssert( I64A.firstSetBit == 1)
doAssert( U64A.parityBits == 1)
doAssert( I64A.parityBits == 1)
doAssert( U64A.countSetBits == 29)
doAssert( I64A.countSetBits == 29)
doAssert( U64A.rotateLeftBits(37) == 0b00101001_00001111_01000010_00101000_10000111_11101111_10010001_01010011'u64)
doAssert( U64A.rotateRightBits(37) == 0b01010100_11001010_01000011_11010000_10001010_00100001_11111011_11100100'u64)
doAssert( U64B.firstSetBit == 36)
doAssert( I64B.firstSetBit == 36)
doAssert( U32.fastLog2 == 31)
doAssert( I32.fastLog2 == 31)
doAssert( U32.countLeadingZeroBits == 0)
doAssert( I32.countLeadingZeroBits == 0)
doAssert( U32.countTrailingZeroBits == 4)
doAssert( I32.countTrailingZeroBits == 4)
doAssert( U32.firstSetBit == 5)
doAssert( I32.firstSetBit == 5)
doAssert( U32.parityBits == 0)
doAssert( I32.parityBits == 0)
doAssert( U32.countSetBits == 16)
doAssert( I32.countSetBits == 16)
doAssert( U32.rotateLeftBits(21) == 0b01001010_00011010_10110011_10011011'u32)
doAssert( U32.rotateRightBits(21) == 0b11100110_11010010_10000110_10101100'u32)
doAssert( U16.fastLog2 == 13)
doAssert( I16.fastLog2 == 13)
doAssert( U16.countLeadingZeroBits == 2)
doAssert( I16.countLeadingZeroBits == 2)
doAssert( U16.countTrailingZeroBits == 3)
doAssert( I16.countTrailingZeroBits == 3)
doAssert( U16.firstSetBit == 4)
doAssert( I16.firstSetBit == 4)
doAssert( U16.parityBits == 0)
doAssert( I16.parityBits == 0)
doAssert( U16.countSetBits == 6)
doAssert( I16.countSetBits == 6)
doAssert( U16.rotateLeftBits(12) == 0b10000010_01110010'u16)
doAssert( U16.rotateRightBits(12) == 0b01110010_10000010'u16)
doAssert( U8.fastLog2 == 5)
doAssert( I8.fastLog2 == 5)
doAssert( U8.countLeadingZeroBits == 2)
doAssert( I8.countLeadingZeroBits == 2)
doAssert( U8.countTrailingZeroBits == 1)
doAssert( I8.countTrailingZeroBits == 1)
doAssert( U8.firstSetBit == 2)
doAssert( I8.firstSetBit == 2)
doAssert( U8.parityBits == 1)
doAssert( I8.parityBits == 1)
doAssert( U8.countSetBits == 3)
doAssert( I8.countSetBits == 3)
doAssert( U8.rotateLeftBits(3) == 0b10010001'u8)
doAssert( U8.rotateRightBits(3) == 0b0100_0110'u8)
static :
# test bitopts at compile time with vm
doAssert( U8.fastLog2 == 5)
doAssert( I8.fastLog2 == 5)
doAssert( U8.countLeadingZeroBits == 2)
doAssert( I8.countLeadingZeroBits == 2)
doAssert( U8.countTrailingZeroBits == 1)
doAssert( I8.countTrailingZeroBits == 1)
doAssert( U8.firstSetBit == 2)
doAssert( I8.firstSetBit == 2)
doAssert( U8.parityBits == 1)
doAssert( I8.parityBits == 1)
doAssert( U8.countSetBits == 3)
doAssert( I8.countSetBits == 3)
doAssert( U8.rotateLeftBits(3) == 0b10010001'u8)
doAssert( U8.rotateRightBits(3) == 0b0100_0110'u8)
template test_undefined_impl(ffunc: untyped; expected: int; is_static: bool) =
doAssert( ffunc(0'u8) == expected)
doAssert( ffunc(0'i8) == expected)
doAssert( ffunc(0'u16) == expected)
doAssert( ffunc(0'i16) == expected)
doAssert( ffunc(0'u32) == expected)
doAssert( ffunc(0'i32) == expected)
doAssert( ffunc(0'u64) == expected)
doAssert( ffunc(0'i64) == expected)
template test_undefined(ffunc: untyped; expected: int) =
test_undefined_impl(ffunc, expected, false)
static:
test_undefined_impl(ffunc, expected, true)
when defined(noUndefinedBitOpts):
# check for undefined behavior with zero.
test_undefined(countSetBits, 0)
test_undefined(parityBits, 0)
test_undefined(firstSetBit, 0)
test_undefined(countLeadingZeroBits, 0)
test_undefined(countTrailingZeroBits, 0)
test_undefined(fastLog2, -1)
# check for undefined behavior with rotate by zero.
doAssert( U8.rotateLeftBits(0) == U8)
doAssert( U8.rotateRightBits(0) == U8)
doAssert( U16.rotateLeftBits(0) == U16)
doAssert( U16.rotateRightBits(0) == U16)
doAssert( U32.rotateLeftBits(0) == U32)
doAssert( U32.rotateRightBits(0) == U32)
doAssert( U64A.rotateLeftBits(0) == U64A)
doAssert( U64A.rotateRightBits(0) == U64A)
# check for undefined behavior with rotate by integer width.
doAssert( U8.rotateLeftBits(8) == U8)
doAssert( U8.rotateRightBits(8) == U8)
doAssert( U16.rotateLeftBits(16) == U16)
doAssert( U16.rotateRightBits(16) == U16)
doAssert( U32.rotateLeftBits(32) == U32)
doAssert( U32.rotateRightBits(32) == U32)
doAssert( U64A.rotateLeftBits(64) == U64A)
doAssert( U64A.rotateRightBits(64) == U64A)
static: # check for undefined behavior with rotate by zero.
doAssert( U8.rotateLeftBits(0) == U8)
doAssert( U8.rotateRightBits(0) == U8)
doAssert( U16.rotateLeftBits(0) == U16)
doAssert( U16.rotateRightBits(0) == U16)
doAssert( U32.rotateLeftBits(0) == U32)
doAssert( U32.rotateRightBits(0) == U32)
doAssert( U64A.rotateLeftBits(0) == U64A)
doAssert( U64A.rotateRightBits(0) == U64A)
# check for undefined behavior with rotate by integer width.
doAssert( U8.rotateLeftBits(8) == U8)
doAssert( U8.rotateRightBits(8) == U8)
doAssert( U16.rotateLeftBits(16) == U16)
doAssert( U16.rotateRightBits(16) == U16)
doAssert( U32.rotateLeftBits(32) == U32)
doAssert( U32.rotateRightBits(32) == U32)
doAssert( U64A.rotateLeftBits(64) == U64A)
doAssert( U64A.rotateRightBits(64) == U64A)
echo "OK"
main2()