styleCheck: make the compiler and large parts of the stdlib compatible with --styleCheck:error

This commit is contained in:
Araq 2019-07-10 12:42:23 +02:00
commit c94647aeca
90 changed files with 680 additions and 652 deletions

View file

@ -121,7 +121,7 @@ when defined(nimHasalignOf):
# #### Pure Nim version ####
proc firstSetBit_nim(x: uint32): int {.inline, nosideeffect.} =
proc firstSetBitNim(x: uint32): int {.inline, noSideEffect.} =
## 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
const lookup: array[32, uint8] = [0'u8, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15,
@ -130,7 +130,7 @@ proc firstSetBit_nim(x: uint32): int {.inline, nosideeffect.} =
var 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
proc firstSetBit_nim(x: uint64): int {.inline, nosideeffect.} =
proc firstSetBitNim(x: uint64): int {.inline, noSideEffect.} =
## 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
var v = uint64(x)
@ -138,9 +138,9 @@ proc firstSetBit_nim(x: uint64): int {.inline, nosideeffect.} =
if k == 0:
k = uint32(v shr 32'u32) and 0xFFFFFFFF'u32
result = 32
result += firstSetBit_nim(k)
result += firstSetBitNim(k)
proc fastlog2_nim(x: uint32): int {.inline, nosideeffect.} =
proc fastlog2Nim(x: uint32): int {.inline, noSideEffect.} =
## Quickly find the log base 2 of a 32-bit or less integer.
# https://graphics.stanford.edu/%7Eseander/bithacks.html#IntegerLogDeBruijn
# https://stackoverflow.com/questions/11376288/fast-computing-of-log2-for-64-bit-integers
@ -154,7 +154,7 @@ proc fastlog2_nim(x: uint32): int {.inline, nosideeffect.} =
v = v or v shr 16
result = lookup[uint32(v * 0x07C4ACDD'u32) shr 27].int
proc fastlog2_nim(x: uint64): int {.inline, nosideeffect.} =
proc fastlog2Nim(x: uint64): int {.inline, noSideEffect.} =
## Quickly find the log base 2 of a 64-bit integer.
# https://graphics.stanford.edu/%7Eseander/bithacks.html#IntegerLogDeBruijn
# https://stackoverflow.com/questions/11376288/fast-computing-of-log2-for-64-bit-integers
@ -176,10 +176,10 @@ proc fastlog2_nim(x: uint64): int {.inline, nosideeffect.} =
# countBits32 and countBits64.
include system/sets
template countSetBits_nim(n: uint32): int = countBits32(n)
template countSetBits_nim(n: uint64): int = countBits64(n)
template countSetBitsNim(n: uint32): int = countBits32(n)
template countSetBitsNim(n: uint64): int = countBits64(n)
template parity_impl[T](value: T): int =
template parityImpl[T](value: T): int =
# formula id from: https://graphics.stanford.edu/%7Eseander/bithacks.html#ParityParallel
var v = value
when sizeof(T) == 8:
@ -216,17 +216,17 @@ when useGCC_builtins:
elif useVCC_builtins:
# Counts the number of one bits (population count) in a 16-, 32-, or 64-byte unsigned integer.
proc builtin_popcnt16(a2: uint16): uint16 {.importc: "__popcnt16" header: "<intrin.h>", nosideeffect.}
proc builtin_popcnt32(a2: uint32): uint32 {.importc: "__popcnt" header: "<intrin.h>", nosideeffect.}
proc builtin_popcnt64(a2: uint64): uint64 {.importc: "__popcnt64" header: "<intrin.h>", nosideeffect.}
proc builtin_popcnt16(a2: uint16): uint16 {.importc: "__popcnt16" header: "<intrin.h>", noSideEffect.}
proc builtin_popcnt32(a2: uint32): uint32 {.importc: "__popcnt" header: "<intrin.h>", noSideEffect.}
proc builtin_popcnt64(a2: uint64): uint64 {.importc: "__popcnt64" header: "<intrin.h>", noSideEffect.}
# 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 {.importc: "_BitScanReverse", header: "<intrin.h>", nosideeffect.}
proc bitScanReverse64(index: ptr culong, mask: uint64): cuchar {.importc: "_BitScanReverse64", header: "<intrin.h>", nosideeffect.}
proc bitScanReverse(index: ptr culong, mask: culong): cuchar {.importc: "_BitScanReverse", header: "<intrin.h>", noSideEffect.}
proc bitScanReverse64(index: ptr culong, mask: uint64): cuchar {.importc: "_BitScanReverse64", header: "<intrin.h>", noSideEffect.}
# 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 {.importc: "_BitScanForward", header: "<intrin.h>", nosideeffect.}
proc bitScanForward64(index: ptr culong, mask: uint64): cuchar {.importc: "_BitScanForward64", header: "<intrin.h>", nosideeffect.}
proc bitScanForward(index: ptr culong, mask: culong): cuchar {.importc: "_BitScanForward", header: "<intrin.h>", noSideEffect.}
proc bitScanForward64(index: ptr culong, mask: uint64): cuchar {.importc: "_BitScanForward64", header: "<intrin.h>", noSideEffect.}
template vcc_scan_impl(fnc: untyped; v: untyped): int =
var index: culong
@ -238,16 +238,16 @@ elif useICC_builtins:
# 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
# Count the number of bits set to 1 in an integer a, and return that count in dst.
proc builtin_popcnt32(a: cint): cint {.importc: "_popcnt" header: "<immintrin.h>", nosideeffect.}
proc builtin_popcnt64(a: uint64): cint {.importc: "_popcnt64" header: "<immintrin.h>", nosideeffect.}
proc builtin_popcnt32(a: cint): cint {.importc: "_popcnt" header: "<immintrin.h>", noSideEffect.}
proc builtin_popcnt64(a: uint64): cint {.importc: "_popcnt64" header: "<immintrin.h>", noSideEffect.}
# 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 {.importc: "_BitScanForward", header: "<immintrin.h>", nosideeffect.}
proc bitScanForward64(p: ptr uint32, b: uint64): cuchar {.importc: "_BitScanForward64", header: "<immintrin.h>", nosideeffect.}
proc bitScanForward(p: ptr uint32, b: uint32): cuchar {.importc: "_BitScanForward", header: "<immintrin.h>", noSideEffect.}
proc bitScanForward64(p: ptr uint32, b: uint64): cuchar {.importc: "_BitScanForward64", header: "<immintrin.h>", noSideEffect.}
# 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 {.importc: "_BitScanReverse", header: "<immintrin.h>", nosideeffect.}
proc bitScanReverse64(p: ptr uint32, b: uint64): cuchar {.importc: "_BitScanReverse64", header: "<immintrin.h>", nosideeffect.}
proc bitScanReverse(p: ptr uint32, b: uint32): cuchar {.importc: "_BitScanReverse", header: "<immintrin.h>", noSideEffect.}
proc bitScanReverse64(p: ptr uint32, b: uint64): cuchar {.importc: "_BitScanReverse64", header: "<immintrin.h>", noSideEffect.}
template icc_scan_impl(fnc: untyped; v: untyped): int =
var index: uint32
@ -255,14 +255,14 @@ elif useICC_builtins:
index.int
proc countSetBits*(x: SomeInteger): int {.inline, nosideeffect.} =
proc countSetBits*(x: SomeInteger): int {.inline, noSideEffect.} =
## Counts the set bits in integer. (also called `Hamming weight`:idx:.)
# TODO: figure out if ICC support _popcnt32/_popcnt64 on platform without POPCNT.
# like GCC and MSVC
when x is SomeSignedInt:
let x = x.toUnsigned
when nimvm:
result = forwardImpl(countSetBits_nim, x)
result = forwardImpl(countSetBitsNim, x)
else:
when useGCC_builtins:
when sizeof(x) <= 4: result = builtin_popcount(x.cuint).int
@ -279,14 +279,14 @@ proc countSetBits*(x: SomeInteger): int {.inline, nosideeffect.} =
else: result = builtin_popcnt32((x.uint64 and 0xFFFFFFFF'u64).cint ).int +
builtin_popcnt32((x.uint64 shr 32'u64).cint ).int
else:
when sizeof(x) <= 4: result = countSetBits_nim(x.uint32)
else: result = countSetBits_nim(x.uint64)
when sizeof(x) <= 4: result = countSetBitsNim(x.uint32)
else: result = countSetBitsNim(x.uint64)
proc popcount*(x: SomeInteger): int {.inline, nosideeffect.} =
proc popcount*(x: SomeInteger): int {.inline, noSideEffect.} =
## Alias for for countSetBits (Hamming weight.)
result = countSetBits(x)
proc parityBits*(x: SomeInteger): int {.inline, nosideeffect.} =
proc parityBits*(x: SomeInteger): int {.inline, noSideEffect.} =
## Calculate the bit parity in integer. If number of 1-bit
## is odd parity is 1, otherwise 0.
# Can be used a base if creating ASM version.
@ -294,16 +294,16 @@ proc parityBits*(x: SomeInteger): int {.inline, nosideeffect.} =
when x is SomeSignedInt:
let x = x.toUnsigned
when nimvm:
result = forwardImpl(parity_impl, x)
result = forwardImpl(parityImpl, x)
else:
when useGCC_builtins:
when sizeof(x) <= 4: result = builtin_parity(x.uint32).int
else: result = builtin_parityll(x.uint64).int
else:
when sizeof(x) <= 4: result = parity_impl(x.uint32)
else: result = parity_impl(x.uint64)
when sizeof(x) <= 4: result = parityImpl(x.uint32)
else: result = parityImpl(x.uint64)
proc firstSetBit*(x: SomeInteger): int {.inline, nosideeffect.} =
proc firstSetBit*(x: SomeInteger): int {.inline, noSideEffect.} =
## Returns the 1-based index of the least significant set bit of x.
## If `x` is zero, when ``noUndefinedBitOpts`` is set, result is 0,
## otherwise result is undefined.
@ -314,7 +314,7 @@ proc firstSetBit*(x: SomeInteger): int {.inline, nosideeffect.} =
when noUndefined:
if x == 0:
return 0
result = forwardImpl(firstSetBit_nim, x)
result = forwardImpl(firstSetBitNim, x)
else:
when noUndefined and not useGCC_builtins:
if x == 0:
@ -328,19 +328,19 @@ proc firstSetBit*(x: SomeInteger): int {.inline, nosideeffect.} =
elif arch64:
result = 1 + vcc_scan_impl(bitScanForward64, x.uint64)
else:
result = firstSetBit_nim(x.uint64)
result = firstSetBitNim(x.uint64)
elif useICC_builtins:
when sizeof(x) <= 4:
result = 1 + icc_scan_impl(bitScanForward, x.uint32)
elif arch64:
result = 1 + icc_scan_impl(bitScanForward64, x.uint64)
else:
result = firstSetBit_nim(x.uint64)
result = firstSetBitNim(x.uint64)
else:
when sizeof(x) <= 4: result = firstSetBit_nim(x.uint32)
else: result = firstSetBit_nim(x.uint64)
when sizeof(x) <= 4: result = firstSetBitNim(x.uint32)
else: result = firstSetBitNim(x.uint64)
proc fastLog2*(x: SomeInteger): int {.inline, nosideeffect.} =
proc fastLog2*(x: SomeInteger): int {.inline, noSideEffect.} =
## Quickly find the log base 2 of an integer.
## If `x` is zero, when ``noUndefinedBitOpts`` is set, result is -1,
## otherwise result is undefined.
@ -350,7 +350,7 @@ proc fastLog2*(x: SomeInteger): int {.inline, nosideeffect.} =
if x == 0:
return -1
when nimvm:
result = forwardImpl(fastlog2_nim, x)
result = forwardImpl(fastlog2Nim, x)
else:
when useGCC_builtins:
when sizeof(x) <= 4: result = 31 - builtin_clz(x.uint32).int
@ -361,19 +361,19 @@ proc fastLog2*(x: SomeInteger): int {.inline, nosideeffect.} =
elif arch64:
result = vcc_scan_impl(bitScanReverse64, x.uint64)
else:
result = fastlog2_nim(x.uint64)
result = fastlog2Nim(x.uint64)
elif useICC_builtins:
when sizeof(x) <= 4:
result = icc_scan_impl(bitScanReverse, x.uint32)
elif arch64:
result = icc_scan_impl(bitScanReverse64, x.uint64)
else:
result = fastlog2_nim(x.uint64)
result = fastlog2Nim(x.uint64)
else:
when sizeof(x) <= 4: result = fastlog2_nim(x.uint32)
else: result = fastlog2_nim(x.uint64)
when sizeof(x) <= 4: result = fastlog2Nim(x.uint32)
else: result = fastlog2Nim(x.uint64)
proc countLeadingZeroBits*(x: SomeInteger): int {.inline, nosideeffect.} =
proc countLeadingZeroBits*(x: SomeInteger): int {.inline, noSideEffect.} =
## Returns the number of leading zero bits in integer.
## If `x` is zero, when ``noUndefinedBitOpts`` is set, result is 0,
## otherwise result is undefined.
@ -383,16 +383,16 @@ proc countLeadingZeroBits*(x: SomeInteger): int {.inline, nosideeffect.} =
if x == 0:
return 0
when nimvm:
result = sizeof(x)*8 - 1 - forwardImpl(fastlog2_nim, x)
result = sizeof(x)*8 - 1 - forwardImpl(fastlog2Nim, x)
else:
when useGCC_builtins:
when sizeof(x) <= 4: result = builtin_clz(x.uint32).int - (32 - sizeof(x)*8)
else: result = builtin_clzll(x.uint64).int
else:
when sizeof(x) <= 4: result = sizeof(x)*8 - 1 - fastlog2_nim(x.uint32)
else: result = sizeof(x)*8 - 1 - fastlog2_nim(x.uint64)
when sizeof(x) <= 4: result = sizeof(x)*8 - 1 - fastlog2Nim(x.uint32)
else: result = sizeof(x)*8 - 1 - fastlog2Nim(x.uint64)
proc countTrailingZeroBits*(x: SomeInteger): int {.inline, nosideeffect.} =
proc countTrailingZeroBits*(x: SomeInteger): int {.inline, noSideEffect.} =
## Returns the number of trailing zeros in integer.
## If `x` is zero, when ``noUndefinedBitOpts`` is set, result is 0,
## otherwise result is undefined.
@ -471,7 +471,7 @@ proc repeatBits[T: SomeUnsignedInt](x: SomeUnsignedInt; retType: type[T]): T {.
while i != (sizeof(T) div sizeof(x)):
result = (result shl (sizeof(x)*8*i)) or result
i *= 2
proc reverseBits*[T: SomeUnsignedInt](x: T): T {.noSideEffect.} =
## Return the bit reversal of x.
runnableExamples: