https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97759
--- Comment #9 from gcc-bugs at marehr dot dialup.fu-berlin.de --- Thank you for so many responses (In reply to Thomas Koenig from comment #1) > Could you post the benchmark and the exact architecture where the arithmetic > version is faster? ``` > cat /proc/cpuinfo rocessor : 0 vendor_id : GenuineIntel cpu family : 6 model : 158 model name : Intel(R) Core(TM) i7-7820HQ CPU @ 2.90GHz stepping : 9 microcode : 0xd6 cpu MHz : 3489.761 cache size : 8192 KB physical id : 0 siblings : 8 core id : 0 cpu cores : 4 apicid : 0 initial apicid : 0 fpu : yes fpu_exception : yes cpuid level : 22 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb invpcid_single pti ssbd ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm mpx rdseed adx smap clflushopt intel_pt xsaveopt xsavec xgetbv1 xsaves dtherm ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp md_clear flush_l1d vmx flags : vnmi preemption_timer invvpid ept_x_only ept_ad ept_1gb flexpriority tsc_offset vtpr mtf vapic ept vpid unrestricted_guest ple shadow_vmcs pml ept_mode_based_exec bugs : cpu_meltdown spectre_v1 spectre_v2 spec_store_bypass l1tf mds swapgs taa itlb_multihit srbds bogomips : 5802.42 clflush size : 64 cache_alignment : 64 address sizes : 39 bits physical, 48 bits virtual power management: ``` I added the sources. You can execute it by putting it into one folder and execute ``` > cmake -DCMAKE_BUILD_TYPE=Release ./ > make VERBOSE=1 /usr/bin/c++ -I/benchmark/_deps/gbenchmark_fetch_content-src/src/../include -O3 -DNDEBUG -std=gnu++2a -o CMakeFiles/has_single_bit_benchmark.dir/has_single_bit_benchmark.cpp.o -c /benchmark/has_single_bit_benchmark.cpp ``` ``` > ./has_single_bit_benchmark --benchmark_repetitions=9 --benchmark_min_time=2 2020-11-10T00:32:52+01:00 Running ./has_single_bit_benchmark Run on (8 X 3900 MHz CPU s) CPU Caches: L1 Data 32 KiB (x4) L1 Instruction 32 KiB (x4) L2 Unified 256 KiB (x4) L3 Unified 8192 KiB (x1) Load Average: 0.57, 0.82, 1.35 ***WARNING*** CPU scaling is enabled, the benchmark real time measurements may be noisy and will incur extra overhead. --------------------------------------------------------------------------- Benchmark Time CPU Iterations --------------------------------------------------------------------------- has_single_bit_arithmetic_mean 6.45 ns 6.44 ns 9 has_single_bit_arithmetic_median 6.44 ns 6.44 ns 9 has_single_bit_arithmetic_stddev 0.113 ns 0.111 ns 9 has_single_bit_popcount_mean 8.84 ns 8.82 ns 9 has_single_bit_popcount_median 8.84 ns 8.82 ns 9 has_single_bit_popcount_stddev 0.060 ns 0.061 ns 9 has_single_bit_std_mean 9.23 ns 9.21 ns 9 has_single_bit_std_median 9.23 ns 9.20 ns 9 has_single_bit_std_stddev 0.046 ns 0.048 ns 9 ``` ---- I thought about it and tried the same again with `-march=native` and noticed that popcount was now (slightly) more efficient. Some more testing showed that `-mpopcnt` is everything needed to make this test fly. --------------------------------------------------------------------------- Benchmark Time CPU Iterations --------------------------------------------------------------------------- has_single_bit_arithmetic_mean 6.81 ns 6.81 ns 9 has_single_bit_arithmetic_median 6.81 ns 6.80 ns 9 has_single_bit_arithmetic_stddev 0.201 ns 0.200 ns 9 has_single_bit_popcount_mean 6.47 ns 6.47 ns 9 has_single_bit_popcount_median 6.46 ns 6.46 ns 9 has_single_bit_popcount_stddev 0.043 ns 0.042 ns 9 has_single_bit_std_mean 6.50 ns 6.50 ns 9 has_single_bit_std_median 6.51 ns 6.50 ns 9 has_single_bit_std_stddev 0.031 ns 0.031 ns 9 So the use case would be generic builds like debian binaries.