From 6038535db14dfd45fe22f3328d58846d466e2fdc Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Wed, 17 Dec 2025 21:07:18 +0800 Subject: [PATCH] include system/countbits_impl --- lib/pure/bitops.nim | 2 +- lib/pure/math.nim | 3 +-- lib/std/private/bitops_utils.nim | 7 +++++++ lib/system.nim | 2 +- lib/system/countbits_impl.nim | 13 +++++-------- 5 files changed, 15 insertions(+), 12 deletions(-) diff --git a/lib/pure/bitops.nim b/lib/pure/bitops.nim index 3ba110d392..effc94c290 100644 --- a/lib/pure/bitops.nim +++ b/lib/pure/bitops.nim @@ -491,7 +491,7 @@ func countSetBits*(x: SomeInteger): int {.inline.} = doAssert countSetBits(0b0000_0011'u8) == 2 doAssert countSetBits(0b1010_1010'u8) == 4 - result = countSetBitsImpl(x) + result = countSetBitsSysimpl(x) func popcount*(x: SomeInteger): int {.inline.} = ## Alias for `countSetBits <#countSetBits,SomeInteger>`_ (Hamming weight). diff --git a/lib/pure/math.nim b/lib/pure/math.nim index 2fb4257b2b..66434bba2e 100644 --- a/lib/pure/math.nim +++ b/lib/pure/math.nim @@ -58,7 +58,6 @@ import std/private/since # of the standard library! import std/[bitops, fenv] -import system/countbits_impl when defined(nimPreviewSlimSystem): import std/assertions @@ -1302,7 +1301,7 @@ func gcd*[T](x, y: T): T = swap x, y abs x -when useBuiltins: +when not defined(noIntrinsicsBitOpts): ## this func uses bitwise comparisons from C compilers, which are not always available. func gcd*(x, y: SomeInteger): SomeInteger = ## Computes the greatest common (positive) divisor of `x` and `y`, diff --git a/lib/std/private/bitops_utils.nim b/lib/std/private/bitops_utils.nim index 980333dc3d..a3db871fba 100644 --- a/lib/std/private/bitops_utils.nim +++ b/lib/std/private/bitops_utils.nim @@ -1,3 +1,10 @@ +const useBuiltins = not defined(noIntrinsicsBitOpts) +const noUndefined = defined(noUndefinedBitOpts) +const useGCC_builtins = (defined(gcc) or defined(llvm_gcc) or + defined(clang)) and useBuiltins +const useICC_builtins = defined(icc) and useBuiltins +const useVCC_builtins = defined(vcc) and useBuiltins + template forwardImpl(impl, arg) {.dirty.} = when sizeof(x) <= 4: when x is SomeSignedInt: diff --git a/lib/system.nim b/lib/system.nim index 102ed4827b..086ec39ab4 100644 --- a/lib/system.nim +++ b/lib/system.nim @@ -2340,7 +2340,7 @@ when not defined(js): when notJSnotNims: - import system/countbits_impl + include system/countbits_impl include "system/sets" when defined(gogc): diff --git a/lib/system/countbits_impl.nim b/lib/system/countbits_impl.nim index 976e6b3762..8159b5d7a1 100644 --- a/lib/system/countbits_impl.nim +++ b/lib/system/countbits_impl.nim @@ -11,13 +11,7 @@ include std/private/bitops_utils -const useBuiltins* = not defined(noIntrinsicsBitOpts) -const noUndefined* = defined(noUndefinedBitOpts) -const useGCC_builtins* = (defined(gcc) or defined(llvm_gcc) or - defined(clang)) and useBuiltins -const useICC_builtins* = defined(icc) and useBuiltins -const useVCC_builtins* = defined(vcc) and useBuiltins -const arch64* = sizeof(int) == 8 +const arch64 = sizeof(int) == 8 template countBitsImpl(n: uint32): int = # generic formula is from: https://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel @@ -60,7 +54,7 @@ elif useICC_builtins: importc: "_popcnt64", header: "".} -func countSetBitsImpl*(x: SomeInteger): int {.inline.} = +func countSetBitsImpl(x: SomeInteger): int {.inline.} = ## Counts the set bits in an integer (also called `Hamming weight`:idx:). # TODO: figure out if ICC support _popcnt32/_popcnt64 on platform without POPCNT. # like GCC and MSVC @@ -85,3 +79,6 @@ func countSetBitsImpl*(x: SomeInteger): int {.inline.} = else: when sizeof(x) <= 4: result = countBitsImpl(x.uint32) else: result = countBitsImpl(x.uint64) + +func countSetBitsSysimpl*(x: SomeInteger): int {.inline.} = + result = countSetBitsImpl(x)