diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2024-05-31 12:01:43 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2024-05-31 16:03:23 +0200 |
commit | 2075553a1b0eb843d4ee73a039b3e30bed9274e0 (patch) | |
tree | 904f192fa9d7c920c9dde6a7bc26b208c3b03312 /coreutils/nproc.c | |
parent | 5a68a246e750359819d63bcff5ef97dd9c7788fc (diff) | |
download | busybox-w32-2075553a1b0eb843d4ee73a039b3e30bed9274e0.tar.gz busybox-w32-2075553a1b0eb843d4ee73a039b3e30bed9274e0.tar.bz2 busybox-w32-2075553a1b0eb843d4ee73a039b3e30bed9274e0.zip |
libbb: add bit counting function, use where appropriate
Although "naive" counting function is not too slow and is smaller,
using it on e.g. each of 1024 words of CPU mask feels wrong.
function old new delta
bb_popcnt_32 - 52 +52
get_prefix 323 321 -2
nproc_main 206 199 -7
d4_run_script 739 731 -8
ipcalc_main 533 507 -26
------------------------------------------------------------------------------
(add/remove: 2/0 grow/shrink: 0/4 up/down: 52/-43) Total: 9 bytes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'coreutils/nproc.c')
-rw-r--r-- | coreutils/nproc.c | 9 |
1 files changed, 3 insertions, 6 deletions
diff --git a/coreutils/nproc.c b/coreutils/nproc.c index a0d818c59..df63bf57a 100644 --- a/coreutils/nproc.c +++ b/coreutils/nproc.c | |||
@@ -56,13 +56,10 @@ int nproc_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) | |||
56 | unsigned long *mask = get_malloc_cpu_affinity(0, &sz); | 56 | unsigned long *mask = get_malloc_cpu_affinity(0, &sz); |
57 | sz /= sizeof(long); | 57 | sz /= sizeof(long); |
58 | for (i = 0; i < sz; i++) { | 58 | for (i = 0; i < sz; i++) { |
59 | unsigned long m = mask[i]; | 59 | if (mask[i] != 0) /* most mask[i] are usually 0 */ |
60 | while (m) { | 60 | count += bb_popcnt_long(mask[i]); |
61 | if (m & 1) | ||
62 | count++; | ||
63 | m >>= 1; | ||
64 | } | ||
65 | } | 61 | } |
62 | IF_FEATURE_CLEAN_UP(free(mask);) | ||
66 | } | 63 | } |
67 | 64 | ||
68 | IF_LONG_OPTS(count -= ignore;) | 65 | IF_LONG_OPTS(count -= ignore;) |