Thanks for using Compiler Explorer
Sponsors
Jakt
C++
Ada
Analysis
Android Java
Android Kotlin
Assembly
C
C3
Carbon
C++ (Circle)
CIRCT
Clean
CMake
CMakeScript
COBOL
C++ for OpenCL
MLIR
Cppx
Cppx-Blue
Cppx-Gold
Cpp2-cppfront
Crystal
C#
CUDA C++
D
Dart
Elixir
Erlang
Fortran
F#
GLSL
Go
Haskell
HLSL
Hook
Hylo
IL
ispc
Java
Julia
Kotlin
LLVM IR
LLVM MIR
Modula-2
Nim
Objective-C
Objective-C++
OCaml
OpenCL C
Pascal
Pony
Python
Racket
Ruby
Rust
Snowball
Scala
Solidity
Spice
SPIR-V
Swift
LLVM TableGen
Toit
TypeScript Native
V
Vala
Visual Basic
WASM
Zig
Javascript
GIMPLE
Ygen
assembly source #1
Output
Compile to binary object
Link to binary
Execute the code
Intel asm syntax
Demangle identifiers
Verbose demangling
Filters
Unused labels
Library functions
Directives
Comments
Horizontal whitespace
Debug intrinsics
Compiler
AArch64 binutils 2.28
AArch64 binutils 2.31.1
AArch64 binutils 2.33.1
AArch64 binutils 2.35.1
AArch64 binutils 2.38
ARM binutils 2.25
ARM binutils 2.28
ARM binutils 2.31.1
ARM gcc 10.2 (linux)
ARM gcc 9.3 (linux)
ARMhf binutils 2.28
BeebAsm 1.09
NASM 2.12.02
NASM 2.13.02
NASM 2.13.03
NASM 2.14.02
NASM 2.16.01
PTX Assembler 10.0.130
PTX Assembler 10.1.105
PTX Assembler 10.1.168
PTX Assembler 10.1.243
PTX Assembler 10.2.89
PTX Assembler 11.0.2
PTX Assembler 11.0.3
PTX Assembler 11.1.0
PTX Assembler 11.1.1
PTX Assembler 11.2.0
PTX Assembler 11.2.1
PTX Assembler 11.2.2
PTX Assembler 11.3.0
PTX Assembler 11.3.1
PTX Assembler 11.4.0
PTX Assembler 11.4.1
PTX Assembler 11.5.0
PTX Assembler 11.6.1
PTX Assembler 11.6.2
PTX Assembler 11.7.0
PTX Assembler 11.7.1
PTX Assembler 11.8.0
PTX Assembler 12.0.0
PTX Assembler 12.0.1
PTX Assembler 12.1.0
PTX Assembler 12.2.1
PTX Assembler 12.3.1
PTX Assembler 12.4.1
PTX Assembler 12.5.1
PTX Assembler 12.6.1
PTX Assembler 9.1.85
PTX Assembler 9.2.88
RISC-V binutils 2.31.1
RISC-V binutils 2.31.1
RISC-V binutils 2.35.1
RISC-V binutils 2.35.1
RISC-V binutils 2.37.0
RISC-V binutils 2.37.0
RISC-V binutils 2.38.0
RISC-V binutils 2.38.0
x86-64 binutils (trunk)
x86-64 binutils 2.27
x86-64 binutils 2.28
x86-64 binutils 2.29.1
x86-64 binutils 2.34
x86-64 binutils 2.36.1
x86-64 binutils 2.38
x86-64 clang (assertions trunk)
x86-64 clang (trunk)
x86-64 clang 10.0.0
x86-64 clang 10.0.1
x86-64 clang 11.0.0
x86-64 clang 11.0.1
x86-64 clang 12.0.0
x86-64 clang 12.0.1
x86-64 clang 13.0.0
x86-64 clang 14.0.0
x86-64 clang 15.0.0
x86-64 clang 16.0.0
x86-64 clang 17.0.1
x86-64 clang 18.1.0
x86-64 clang 3.0.0
x86-64 clang 3.1
x86-64 clang 3.2
x86-64 clang 3.3
x86-64 clang 3.4.1
x86-64 clang 3.5
x86-64 clang 3.5.1
x86-64 clang 3.5.2
x86-64 clang 3.6
x86-64 clang 3.7
x86-64 clang 3.7.1
x86-64 clang 3.8
x86-64 clang 3.8.1
x86-64 clang 3.9.0
x86-64 clang 3.9.1
x86-64 clang 4.0.0
x86-64 clang 4.0.1
x86-64 clang 5.0.0
x86-64 clang 6.0.0
x86-64 clang 7.0.0
x86-64 clang 8.0.0
x86-64 clang 9.0.0
Options
Source code
%if 0 $ taskset -c 3 perf stat --all-user -etask-clock,context-switches,cpu-migrations,page-faults,cycles,branches,branch-misses,instructions,idq.dsb_uops,idq.mite_uops -r 2 ./a.out ans: 12509316 ans: 12509316 Performance counter stats for './a.out' (2 runs): 593.33 msec task-clock # 0.999 CPUs utilized ( +- 0.10% ) 0 context-switches # 0.000 K/sec 0 cpu-migrations # 0.000 K/sec 58 page-faults # 0.098 K/sec ( +- 3.45% ) 2,311,512,376 cycles # 3.896 GHz ( +- 0.10% ) 2,000,637,317 branches # 3371.896 M/sec ( +- 0.00% ) 1,690,747 branch-misses # 0.08% of all branches ( +- 0.60% ) 11,015,217,579 instructions # 4.77 insn per cycle ( +- 0.00% ) 9,204,430,548 idq.dsb_uops # 15513.249 M/sec ( +- 0.00% ) 1,008,922 idq.mite_uops # 1.700 M/sec ( +- 20.51% ) 0.593699 +- 0.000523 seconds time elapsed ( +- 0.09% ) %endif %use SMARTALIGN alignmode p6, 64 section .bss wordbits: resb 65536 ; n resd 1 ; k resd 1 ans resd 1 section .rodata n: dd 1000000000 k: dd 8 print_fmt: db `ans: %d\n`, 0 section .text global main main: ; no popcnt version push edi ; save some call-preserved registers push esi push ebx mov edi, wordbits %define wordbits edi ; ugly hack to use indexed addressing to save code-size ;fill in wordbits, ecx is wordbits array index mov ecx, 1 ; leave wordbits[0] = 0 .init_loop: mov eax,ecx xor ebx,ebx .popc_loop: lea edx, [eax-1] inc ebx and eax,edx ; v &= v - 1; // blsr jnz .popc_loop ;computed bits set mov [wordbits + ecx], bl inc ecx cmp ecx,65536 jna .init_loop ; GET_DEC 4,n ; GET_DEC 4,k mov ecx, [n] ; ecx counts from n down to 1 mov esi, [k] xor ebx, ebx ; ebx = ans align 32 .loop: ;popcnt eax, ecx movzx edx, cx mov eax, ecx shr eax, 16 movzx edx, byte [wordbits + edx] add dl, [wordbits + eax] ; no partial-reg stall on Sandybridge-family for this lea edx, [edx + esi - 1] ; xor eax, eax ; break false dependency, or just let OoO exec hide it after breaking once per iter bsr eax, ecx ; eax = 31-lzcnt ; xor eax, 0x1f ; eax = lzcnt (for non-zero x) ; want: 32- clz(x) - popcnt_u32(x) = (31-clz) + 1-popcnt = (31-clz) - (popcnt-1) cmp eax, edx ; k + 1-popcnt == 31-clz je .yesk ; not-taken is the more common fast path .done_inc: dec ecx jnz .loop ; }while(--n >= 0U) .print_and_exit: ;print ans ; PRINT_DEC 4,ans push ebx push print_fmt extern printf call printf add esp, 8 pop ebx pop esi pop edi xor eax, eax ret align 8 .yesk: inc ebx ; jmp .done_inc dec ecx ;; tail duplication is a *tiny* bit faster jnz .loop jmp .print_and_exit
Become a Patron
Sponsor on GitHub
Donate via PayPal
Source on GitHub
Mailing list
Installed libraries
Wiki
Report an issue
How it works
Contact the author
CE on Mastodon
About the author
Statistics
Changelog
Version tree