Thanks for using Compiler Explorer
Sponsors
Jakt
C++
Ada
Algol68
Analysis
Android Java
Android Kotlin
Assembly
C
C3
Carbon
C with Coccinelle
C++ with Coccinelle
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
Mojo
Nim
Numba
Nix
Objective-C
Objective-C++
OCaml
Odin
OpenCL C
Pascal
Pony
PTX
Python
Racket
Raku
Ruby
Rust
Sail
Snowball
Scala
Slang
Solidity
Spice
SPIR-V
Swift
LLVM TableGen
Toit
Triton
TypeScript Native
V
Vala
Visual Basic
Vyper
WASM
Zig
Javascript
GIMPLE
Ygen
sway
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
AArch64 binutils 2.45
ARM binutils 2.25
ARM binutils 2.28
ARM binutils 2.31.1
ARM gcc 10.2 (linux)
ARM gcc 13.2 (linux)
ARM gcc 15.1 (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
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
RISC-V binutils 2.42.0
RISC-V binutils 2.42.0
RISC-V binutils 2.45.0
RISC-V binutils 2.45.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 binutils 2.42
x86-64 binutils 2.45
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 19.1.0
x86-64 clang 20.1.0
x86-64 clang 21.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 ;; Perf counter results on i7-6700k at 3.9GHz $ nasm -felf32 foo.asm && gcc -m32 -no-pie -fno-pie -fno-plt foo.o $ taskset -c 3 perf stat -B --all-user -etask-clock,context-switches,cpu-migrations,page-faults,cycles,branches,branch-misses,instructions,uops_issued.any,uops_executed.thread -r 1 ./a.out ans: 12509316 Performance counter stats for './a.out': 536.49 msec task-clock # 0.999 CPUs utilized 0 context-switches # 0.000 K/sec 0 cpu-migrations # 0.000 K/sec 44 page-faults # 0.082 K/sec 2,089,479,797 cycles # 3.895 GHz 2,012,556,778 branches # 3751.376 M/sec 2,047,286 branch-misses # 0.10% of all branches 9,025,236,489 instructions # 4.32 insn per cycle 7,133,922,940 uops_issued.any # 13297.525 M/sec 6,084,088,670 uops_executed.thread # 11340.650 M/sec 0.536929691 seconds time elapsed 0.536516000 seconds user 0.000000000 seconds sys %endif %use SMARTALIGN alignmode p6, 64 section .bss ; 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: push esi push ebx ; 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: mov eax, ecx popcnt eax,eax lea edx, [dword eax - 32 + 31] ; popcnt - 32 = -(bits not set) ; dword displacement pads the cmp/jnz location to avoid the JCC erratum penalty on Intel ; 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-__builtin_clz(x)-_mm_popcnt_u32(x) = (31-clz) + 1-popcnt = (31-clz) - (popcnt-1) sub eax, edx cmp eax, esi ;is there k non-leading bits in ecx? %if 0 jnz .notk inc ebx ;if so, then increment ans .notk: %else jz .yesk ; not-taken is the more common fast path .done_inc: %endif dec ecx jnz .loop ; }while(--n >= 0U) ;print ans ; PRINT_DEC 4,ans push ebx push print_fmt extern printf call printf add esp, 8 pop ebx pop esi xor eax, eax ret .yesk: inc ebx jmp .done_inc
Become a Patron
Sponsor on GitHub
Donate via PayPal
Compiler Explorer Shop
Source on GitHub
Mailing list
Installed libraries
Wiki
Report an issue
How it works
Contact the author
CE on Mastodon
CE on Bluesky
Statistics
Changelog
Version tree