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#
Go
Haskell
HLSL
Hook
Hylo
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
Swift
LLVM TableGen
Toit
TypeScript Native
V
Vala
Visual Basic
Zig
Javascript
GIMPLE
c++ 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
6502-c++ 11.1.0
ARM GCC 10.2.0
ARM GCC 10.3.0
ARM GCC 10.4.0
ARM GCC 10.5.0
ARM GCC 11.1.0
ARM GCC 11.2.0
ARM GCC 11.3.0
ARM GCC 11.4.0
ARM GCC 12.1.0
ARM GCC 12.2.0
ARM GCC 12.3.0
ARM GCC 13.1.0
ARM GCC 13.2.0
ARM GCC 13.2.0 (unknown-eabi)
ARM GCC 14.1.0
ARM GCC 4.5.4
ARM GCC 4.6.4
ARM GCC 5.4
ARM GCC 6.3.0
ARM GCC 6.4.0
ARM GCC 7.3.0
ARM GCC 7.5.0
ARM GCC 8.2.0
ARM GCC 8.5.0
ARM GCC 9.3.0
ARM GCC 9.4.0
ARM GCC 9.5.0
ARM GCC trunk
ARM gcc 10.2.1 (none)
ARM gcc 10.3.1 (2021.07 none)
ARM gcc 10.3.1 (2021.10 none)
ARM gcc 11.2.1 (none)
ARM gcc 5.4.1 (none)
ARM gcc 7.2.1 (none)
ARM gcc 8.2 (WinCE)
ARM gcc 8.3.1 (none)
ARM gcc 9.2.1 (none)
ARM msvc v19.0 (WINE)
ARM msvc v19.10 (WINE)
ARM msvc v19.14 (WINE)
ARM64 Morello gcc 10.1 Alpha 2
ARM64 gcc 10.2
ARM64 gcc 10.3
ARM64 gcc 10.4
ARM64 gcc 10.5.0
ARM64 gcc 11.1
ARM64 gcc 11.2
ARM64 gcc 11.3
ARM64 gcc 11.4.0
ARM64 gcc 12.1
ARM64 gcc 12.2.0
ARM64 gcc 12.3.0
ARM64 gcc 13.1.0
ARM64 gcc 13.2.0
ARM64 gcc 14.1.0
ARM64 gcc 4.9.4
ARM64 gcc 5.4
ARM64 gcc 5.5.0
ARM64 gcc 6.3
ARM64 gcc 6.4
ARM64 gcc 7.3
ARM64 gcc 7.5
ARM64 gcc 8.2
ARM64 gcc 8.5
ARM64 gcc 9.3
ARM64 gcc 9.4
ARM64 gcc 9.5
ARM64 gcc trunk
ARM64 msvc v19.14 (WINE)
AVR gcc 10.3.0
AVR gcc 11.1.0
AVR gcc 12.1.0
AVR gcc 12.2.0
AVR gcc 12.3.0
AVR gcc 13.1.0
AVR gcc 13.2.0
AVR gcc 14.1.0
AVR gcc 4.5.4
AVR gcc 4.6.4
AVR gcc 5.4.0
AVR gcc 9.2.0
AVR gcc 9.3.0
Arduino Mega (1.8.9)
Arduino Uno (1.8.9)
BPF clang (trunk)
BPF clang 13.0.0
BPF clang 14.0.0
BPF clang 15.0.0
BPF clang 16.0.0
BPF clang 17.0.1
BPF clang 18.1.0
BPF gcc 13.1.0
BPF gcc 13.2.0
BPF gcc trunk
EDG (experimental reflection)
EDG 6.5
EDG 6.5 (GNU mode gcc 13)
EDG 6.6
EDG 6.6 (GNU mode gcc 13)
FRC 2019
FRC 2020
FRC 2023
KVX ACB 4.1.0 (GCC 7.5.0)
KVX ACB 4.1.0-cd1 (GCC 7.5.0)
KVX ACB 4.10.0 (GCC 10.3.1)
KVX ACB 4.11.1 (GCC 10.3.1)
KVX ACB 4.12.0 (GCC 11.3.0)
KVX ACB 4.2.0 (GCC 7.5.0)
KVX ACB 4.3.0 (GCC 7.5.0)
KVX ACB 4.4.0 (GCC 7.5.0)
KVX ACB 4.6.0 (GCC 9.4.1)
KVX ACB 4.8.0 (GCC 9.4.1)
KVX ACB 4.9.0 (GCC 9.4.1)
KVX ACB 5.0.0 (GCC 12.2.1)
M68K gcc 13.1.0
M68K gcc 13.2.0
M68K gcc 14.1.0
M68k clang (trunk)
MRISC32 gcc (trunk)
MSP430 gcc 4.5.3
MSP430 gcc 5.3.0
MSP430 gcc 6.2.1
MinGW clang 14.0.3
MinGW clang 14.0.6
MinGW clang 15.0.7
MinGW clang 16.0.0
MinGW clang 16.0.2
MinGW gcc 11.3.0
MinGW gcc 12.1.0
MinGW gcc 12.2.0
MinGW gcc 13.1.0
RISC-V (32-bits) gcc (trunk)
RISC-V (32-bits) gcc 10.2.0
RISC-V (32-bits) gcc 10.3.0
RISC-V (32-bits) gcc 11.2.0
RISC-V (32-bits) gcc 11.3.0
RISC-V (32-bits) gcc 11.4.0
RISC-V (32-bits) gcc 12.1.0
RISC-V (32-bits) gcc 12.2.0
RISC-V (32-bits) gcc 12.3.0
RISC-V (32-bits) gcc 13.1.0
RISC-V (32-bits) gcc 13.2.0
RISC-V (32-bits) gcc 14.1.0
RISC-V (32-bits) gcc 8.2.0
RISC-V (32-bits) gcc 8.5.0
RISC-V (32-bits) gcc 9.4.0
RISC-V (64-bits) gcc (trunk)
RISC-V (64-bits) gcc 10.2.0
RISC-V (64-bits) gcc 10.3.0
RISC-V (64-bits) gcc 11.2.0
RISC-V (64-bits) gcc 11.3.0
RISC-V (64-bits) gcc 11.4.0
RISC-V (64-bits) gcc 12.1.0
RISC-V (64-bits) gcc 12.2.0
RISC-V (64-bits) gcc 12.3.0
RISC-V (64-bits) gcc 13.1.0
RISC-V (64-bits) gcc 13.2.0
RISC-V (64-bits) gcc 14.1.0
RISC-V (64-bits) gcc 8.2.0
RISC-V (64-bits) gcc 8.5.0
RISC-V (64-bits) gcc 9.4.0
RISC-V rv32gc clang (trunk)
RISC-V rv32gc clang 10.0.0
RISC-V rv32gc clang 10.0.1
RISC-V rv32gc clang 11.0.0
RISC-V rv32gc clang 11.0.1
RISC-V rv32gc clang 12.0.0
RISC-V rv32gc clang 12.0.1
RISC-V rv32gc clang 13.0.0
RISC-V rv32gc clang 13.0.1
RISC-V rv32gc clang 14.0.0
RISC-V rv32gc clang 15.0.0
RISC-V rv32gc clang 16.0.0
RISC-V rv32gc clang 17.0.1
RISC-V rv32gc clang 18.1.0
RISC-V rv32gc clang 9.0.0
RISC-V rv32gc clang 9.0.1
RISC-V rv64gc clang (trunk)
RISC-V rv64gc clang 10.0.0
RISC-V rv64gc clang 10.0.1
RISC-V rv64gc clang 11.0.0
RISC-V rv64gc clang 11.0.1
RISC-V rv64gc clang 12.0.0
RISC-V rv64gc clang 12.0.1
RISC-V rv64gc clang 13.0.0
RISC-V rv64gc clang 13.0.1
RISC-V rv64gc clang 14.0.0
RISC-V rv64gc clang 15.0.0
RISC-V rv64gc clang 16.0.0
RISC-V rv64gc clang 17.0.1
RISC-V rv64gc clang 18.1.0
RISC-V rv64gc clang 9.0.0
RISC-V rv64gc clang 9.0.1
Raspbian Buster
Raspbian Stretch
SPARC LEON gcc 12.2.0
SPARC LEON gcc 12.3.0
SPARC LEON gcc 13.1.0
SPARC LEON gcc 13.2.0
SPARC gcc 12.2.0
SPARC gcc 12.3.0
SPARC gcc 13.1.0
SPARC gcc 13.2.0
SPARC gcc 14.1.0
SPARC64 gcc 12.2.0
SPARC64 gcc 12.3.0
SPARC64 gcc 13.1.0
SPARC64 gcc 13.2.0
SPARC64 gcc 14.1.0
TI C6x gcc 12.2.0
TI C6x gcc 12.3.0
TI C6x gcc 13.1.0
TI C6x gcc 13.2.0
TI C6x gcc 14.1.0
TI CL430 21.6.1
VAX gcc NetBSDELF 10.4.0
VAX gcc NetBSDELF 10.5.0 (Nov 15 03:50:22 2023)
WebAssembly clang (trunk)
Xtensa ESP32 gcc 11.2.0 (2022r1)
Xtensa ESP32 gcc 12.2.0 (20230208)
Xtensa ESP32 gcc 8.2.0 (2019r2)
Xtensa ESP32 gcc 8.2.0 (2020r1)
Xtensa ESP32 gcc 8.2.0 (2020r2)
Xtensa ESP32 gcc 8.4.0 (2020r3)
Xtensa ESP32 gcc 8.4.0 (2021r1)
Xtensa ESP32 gcc 8.4.0 (2021r2)
Xtensa ESP32-S2 gcc 11.2.0 (2022r1)
Xtensa ESP32-S2 gcc 12.2.0 (20230208)
Xtensa ESP32-S2 gcc 8.2.0 (2019r2)
Xtensa ESP32-S2 gcc 8.2.0 (2020r1)
Xtensa ESP32-S2 gcc 8.2.0 (2020r2)
Xtensa ESP32-S2 gcc 8.4.0 (2020r3)
Xtensa ESP32-S2 gcc 8.4.0 (2021r1)
Xtensa ESP32-S2 gcc 8.4.0 (2021r2)
Xtensa ESP32-S3 gcc 11.2.0 (2022r1)
Xtensa ESP32-S3 gcc 12.2.0 (20230208)
Xtensa ESP32-S3 gcc 8.4.0 (2020r3)
Xtensa ESP32-S3 gcc 8.4.0 (2021r1)
Xtensa ESP32-S3 gcc 8.4.0 (2021r2)
arm64 msvc v19.28 VS16.9
arm64 msvc v19.29 VS16.10
arm64 msvc v19.29 VS16.11
arm64 msvc v19.30
arm64 msvc v19.31
arm64 msvc v19.32
arm64 msvc v19.33
arm64 msvc v19.34
arm64 msvc v19.35
arm64 msvc v19.36
arm64 msvc v19.37
arm64 msvc v19.38
arm64 msvc v19.latest
armv7-a clang (trunk)
armv7-a clang 10.0.0
armv7-a clang 10.0.1
armv7-a clang 11.0.0
armv7-a clang 11.0.1
armv7-a clang 12.0.0
armv7-a clang 12.0.1
armv7-a clang 13.0.0
armv7-a clang 13.0.1
armv7-a clang 14.0.0
armv7-a clang 15.0.0
armv7-a clang 16.0.0
armv7-a clang 17.0.1
armv7-a clang 18.1.0
armv7-a clang 9.0.0
armv7-a clang 9.0.1
armv8-a clang (all architectural features, trunk)
armv8-a clang (trunk)
armv8-a clang 10.0.0
armv8-a clang 10.0.1
armv8-a clang 11.0.0
armv8-a clang 11.0.1
armv8-a clang 12.0.0
armv8-a clang 13.0.0
armv8-a clang 14.0.0
armv8-a clang 15.0.0
armv8-a clang 16.0.0
armv8-a clang 17.0.1
armv8-a clang 18.1.0
armv8-a clang 9.0.0
armv8-a clang 9.0.1
ellcc 0.1.33
ellcc 0.1.34
ellcc 2017-07-16
hexagon-clang 16.0.5
llvm-mos atari2600-3e
llvm-mos atari2600-4k
llvm-mos atari2600-common
llvm-mos atari5200-supercart
llvm-mos atari8-cart-megacart
llvm-mos atari8-cart-std
llvm-mos atari8-cart-xegs
llvm-mos atari8-common
llvm-mos atari8-dos
llvm-mos c128
llvm-mos c64
llvm-mos commodore
llvm-mos cpm65
llvm-mos cx16
llvm-mos dodo
llvm-mos eater
llvm-mos mega65
llvm-mos nes
llvm-mos nes-action53
llvm-mos nes-cnrom
llvm-mos nes-gtrom
llvm-mos nes-mmc1
llvm-mos nes-mmc3
llvm-mos nes-nrom
llvm-mos nes-unrom
llvm-mos nes-unrom-512
llvm-mos osi-c1p
llvm-mos pce
llvm-mos pce-cd
llvm-mos pce-common
llvm-mos pet
llvm-mos rp6502
llvm-mos rpc8e
llvm-mos supervision
llvm-mos vic20
loongarch64 gcc 12.2.0
loongarch64 gcc 12.3.0
loongarch64 gcc 13.1.0
loongarch64 gcc 13.2.0
loongarch64 gcc 14.1.0
mips clang 13.0.0
mips clang 14.0.0
mips clang 15.0.0
mips clang 16.0.0
mips clang 17.0.1
mips clang 18.1.0
mips gcc 11.2.0
mips gcc 12.1.0
mips gcc 12.2.0
mips gcc 12.3.0
mips gcc 13.1.0
mips gcc 13.2.0
mips gcc 14.1.0
mips gcc 4.9.4
mips gcc 5.4
mips gcc 5.5.0
mips gcc 9.3.0 (codescape)
mips gcc 9.5.0
mips64 (el) gcc 12.1.0
mips64 (el) gcc 12.2.0
mips64 (el) gcc 12.3.0
mips64 (el) gcc 13.1.0
mips64 (el) gcc 13.2.0
mips64 (el) gcc 14.1.0
mips64 (el) gcc 4.9.4
mips64 (el) gcc 5.4.0
mips64 (el) gcc 5.5.0
mips64 (el) gcc 9.5.0
mips64 clang 13.0.0
mips64 clang 14.0.0
mips64 clang 15.0.0
mips64 clang 16.0.0
mips64 clang 17.0.1
mips64 clang 18.1.0
mips64 gcc 11.2.0
mips64 gcc 12.1.0
mips64 gcc 12.2.0
mips64 gcc 12.3.0
mips64 gcc 13.1.0
mips64 gcc 13.2.0
mips64 gcc 14.1.0
mips64 gcc 4.9.4
mips64 gcc 5.4.0
mips64 gcc 5.5.0
mips64 gcc 9.5.0
mips64el clang 13.0.0
mips64el clang 14.0.0
mips64el clang 15.0.0
mips64el clang 16.0.0
mips64el clang 17.0.1
mips64el clang 18.1.0
mipsel clang 13.0.0
mipsel clang 14.0.0
mipsel clang 15.0.0
mipsel clang 16.0.0
mipsel clang 17.0.1
mipsel clang 18.1.0
mipsel gcc 12.1.0
mipsel gcc 12.2.0
mipsel gcc 12.3.0
mipsel gcc 13.1.0
mipsel gcc 13.2.0
mipsel gcc 14.1.0
mipsel gcc 4.9.4
mipsel gcc 5.4.0
mipsel gcc 5.5.0
mipsel gcc 9.5.0
nanoMIPS gcc 6.3.0 (mtk)
power gcc 11.2.0
power gcc 12.1.0
power gcc 12.2.0
power gcc 12.3.0
power gcc 13.1.0
power gcc 13.2.0
power gcc 14.1.0
power gcc 4.8.5
power64 AT12.0 (gcc8)
power64 AT13.0 (gcc9)
power64 gcc 11.2.0
power64 gcc 12.1.0
power64 gcc 12.2.0
power64 gcc 12.3.0
power64 gcc 13.1.0
power64 gcc 13.2.0
power64 gcc 14.1.0
power64 gcc trunk
power64le AT12.0 (gcc8)
power64le AT13.0 (gcc9)
power64le clang (trunk)
power64le gcc 11.2.0
power64le gcc 12.1.0
power64le gcc 12.2.0
power64le gcc 12.3.0
power64le gcc 13.1.0
power64le gcc 13.2.0
power64le gcc 14.1.0
power64le gcc 6.3.0
power64le gcc trunk
powerpc64 clang (trunk)
s390x gcc 11.2.0
s390x gcc 12.1.0
s390x gcc 12.2.0
s390x gcc 12.3.0
s390x gcc 13.1.0
s390x gcc 13.2.0
s390x gcc 14.1.0
sh gcc 12.2.0
sh gcc 12.3.0
sh gcc 13.1.0
sh gcc 13.2.0
sh gcc 14.1.0
sh gcc 4.9.4
sh gcc 9.5.0
vast (trunk)
x64 msvc v19.0 (WINE)
x64 msvc v19.10 (WINE)
x64 msvc v19.14
x64 msvc v19.14 (WINE)
x64 msvc v19.15
x64 msvc v19.16
x64 msvc v19.20
x64 msvc v19.21
x64 msvc v19.22
x64 msvc v19.23
x64 msvc v19.24
x64 msvc v19.25
x64 msvc v19.26
x64 msvc v19.27
x64 msvc v19.28
x64 msvc v19.28 VS16.9
x64 msvc v19.29 VS16.10
x64 msvc v19.29 VS16.11
x64 msvc v19.30
x64 msvc v19.31
x64 msvc v19.32
x64 msvc v19.33
x64 msvc v19.34
x64 msvc v19.35
x64 msvc v19.36
x64 msvc v19.37
x64 msvc v19.38
x64 msvc v19.latest
x86 djgpp 4.9.4
x86 djgpp 5.5.0
x86 djgpp 6.4.0
x86 djgpp 7.2.0
x86 msvc v19.0 (WINE)
x86 msvc v19.10 (WINE)
x86 msvc v19.14
x86 msvc v19.14 (WINE)
x86 msvc v19.15
x86 msvc v19.16
x86 msvc v19.20
x86 msvc v19.21
x86 msvc v19.22
x86 msvc v19.23
x86 msvc v19.24
x86 msvc v19.25
x86 msvc v19.26
x86 msvc v19.27
x86 msvc v19.28
x86 msvc v19.28 VS16.9
x86 msvc v19.29 VS16.10
x86 msvc v19.29 VS16.11
x86 msvc v19.30
x86 msvc v19.31
x86 msvc v19.32
x86 msvc v19.33
x86 msvc v19.34
x86 msvc v19.35
x86 msvc v19.36
x86 msvc v19.37
x86 msvc v19.38
x86 msvc v19.latest
x86 nvc++ 22.11
x86 nvc++ 22.7
x86 nvc++ 22.9
x86 nvc++ 23.1
x86 nvc++ 23.11
x86 nvc++ 23.3
x86 nvc++ 23.5
x86 nvc++ 23.7
x86 nvc++ 23.9
x86 nvc++ 24.1
x86 nvc++ 24.3
x86-64 Zapcc 190308
x86-64 clang (amd-stg-open)
x86-64 clang (assertions trunk)
x86-64 clang (clangir)
x86-64 clang (experimental -Wlifetime)
x86-64 clang (experimental P1061)
x86-64 clang (experimental P1144)
x86-64 clang (experimental P1221)
x86-64 clang (experimental P2996)
x86-64 clang (experimental P3068)
x86-64 clang (experimental metaprogramming - P2632)
x86-64 clang (experimental pattern matching)
x86-64 clang (old concepts branch)
x86-64 clang (reflection)
x86-64 clang (resugar)
x86-64 clang (thephd.dev)
x86-64 clang (trunk)
x86-64 clang (variadic friends - P2893)
x86-64 clang (widberg)
x86-64 clang 10.0.0
x86-64 clang 10.0.0 (assertions)
x86-64 clang 10.0.1
x86-64 clang 11.0.0
x86-64 clang 11.0.0 (assertions)
x86-64 clang 11.0.1
x86-64 clang 12.0.0
x86-64 clang 12.0.0 (assertions)
x86-64 clang 12.0.1
x86-64 clang 13.0.0
x86-64 clang 13.0.0 (assertions)
x86-64 clang 13.0.1
x86-64 clang 14.0.0
x86-64 clang 14.0.0 (assertions)
x86-64 clang 15.0.0
x86-64 clang 15.0.0 (assertions)
x86-64 clang 16.0.0
x86-64 clang 16.0.0 (assertions)
x86-64 clang 17.0.1
x86-64 clang 17.0.1 (assertions)
x86-64 clang 18.1.0
x86-64 clang 18.1.0 (assertions)
x86-64 clang 2.6.0 (assertions)
x86-64 clang 2.7.0 (assertions)
x86-64 clang 2.8.0 (assertions)
x86-64 clang 2.9.0 (assertions)
x86-64 clang 3.0.0
x86-64 clang 3.0.0 (assertions)
x86-64 clang 3.1
x86-64 clang 3.1 (assertions)
x86-64 clang 3.2
x86-64 clang 3.2 (assertions)
x86-64 clang 3.3
x86-64 clang 3.3 (assertions)
x86-64 clang 3.4 (assertions)
x86-64 clang 3.4.1
x86-64 clang 3.5
x86-64 clang 3.5 (assertions)
x86-64 clang 3.5.1
x86-64 clang 3.5.2
x86-64 clang 3.6
x86-64 clang 3.6 (assertions)
x86-64 clang 3.7
x86-64 clang 3.7 (assertions)
x86-64 clang 3.7.1
x86-64 clang 3.8
x86-64 clang 3.8 (assertions)
x86-64 clang 3.8.1
x86-64 clang 3.9.0
x86-64 clang 3.9.0 (assertions)
x86-64 clang 3.9.1
x86-64 clang 4.0.0
x86-64 clang 4.0.0 (assertions)
x86-64 clang 4.0.1
x86-64 clang 5.0.0
x86-64 clang 5.0.0 (assertions)
x86-64 clang 5.0.1
x86-64 clang 5.0.2
x86-64 clang 6.0.0
x86-64 clang 6.0.0 (assertions)
x86-64 clang 6.0.1
x86-64 clang 7.0.0
x86-64 clang 7.0.0 (assertions)
x86-64 clang 7.0.1
x86-64 clang 7.1.0
x86-64 clang 8.0.0
x86-64 clang 8.0.0 (assertions)
x86-64 clang 8.0.1
x86-64 clang 9.0.0
x86-64 clang 9.0.0 (assertions)
x86-64 clang 9.0.1
x86-64 clang rocm-4.5.2
x86-64 clang rocm-5.0.2
x86-64 clang rocm-5.1.3
x86-64 clang rocm-5.2.3
x86-64 clang rocm-5.3.3
x86-64 clang rocm-5.7.0
x86-64 gcc (contract labels)
x86-64 gcc (contracts natural syntax)
x86-64 gcc (contracts)
x86-64 gcc (coroutines)
x86-64 gcc (modules)
x86-64 gcc (trunk)
x86-64 gcc 10.1
x86-64 gcc 10.2
x86-64 gcc 10.3
x86-64 gcc 10.4
x86-64 gcc 10.5
x86-64 gcc 11.1
x86-64 gcc 11.2
x86-64 gcc 11.3
x86-64 gcc 11.4
x86-64 gcc 12.1
x86-64 gcc 12.2
x86-64 gcc 12.3
x86-64 gcc 13.1
x86-64 gcc 13.2
x86-64 gcc 14.1
x86-64 gcc 3.4.6
x86-64 gcc 4.0.4
x86-64 gcc 4.1.2
x86-64 gcc 4.4.7
x86-64 gcc 4.5.3
x86-64 gcc 4.6.4
x86-64 gcc 4.7.1
x86-64 gcc 4.7.2
x86-64 gcc 4.7.3
x86-64 gcc 4.7.4
x86-64 gcc 4.8.1
x86-64 gcc 4.8.2
x86-64 gcc 4.8.3
x86-64 gcc 4.8.4
x86-64 gcc 4.8.5
x86-64 gcc 4.9.0
x86-64 gcc 4.9.1
x86-64 gcc 4.9.2
x86-64 gcc 4.9.3
x86-64 gcc 4.9.4
x86-64 gcc 5.1
x86-64 gcc 5.2
x86-64 gcc 5.3
x86-64 gcc 5.4
x86-64 gcc 5.5
x86-64 gcc 6.1
x86-64 gcc 6.2
x86-64 gcc 6.3
x86-64 gcc 6.4
x86-64 gcc 6.5
x86-64 gcc 7.1
x86-64 gcc 7.2
x86-64 gcc 7.3
x86-64 gcc 7.4
x86-64 gcc 7.5
x86-64 gcc 8.1
x86-64 gcc 8.2
x86-64 gcc 8.3
x86-64 gcc 8.4
x86-64 gcc 8.5
x86-64 gcc 9.1
x86-64 gcc 9.2
x86-64 gcc 9.3
x86-64 gcc 9.4
x86-64 gcc 9.5
x86-64 icc 13.0.1
x86-64 icc 16.0.3
x86-64 icc 17.0.0
x86-64 icc 18.0.0
x86-64 icc 19.0.0
x86-64 icc 19.0.1
x86-64 icc 2021.1.2
x86-64 icc 2021.10.0
x86-64 icc 2021.2.0
x86-64 icc 2021.3.0
x86-64 icc 2021.4.0
x86-64 icc 2021.5.0
x86-64 icc 2021.6.0
x86-64 icc 2021.7.0
x86-64 icc 2021.7.1
x86-64 icc 2021.8.0
x86-64 icc 2021.9.0
x86-64 icx (latest)
x86-64 icx 2021.1.2
x86-64 icx 2021.2.0
x86-64 icx 2021.3.0
x86-64 icx 2021.4.0
x86-64 icx 2022.0.0
x86-64 icx 2022.1.0
x86-64 icx 2022.2.0
x86-64 icx 2022.2.1
x86-64 icx 2023.0.0
x86-64 icx 2023.1.0
x86-64 icx 2023.2.1
x86-64 icx 2024.0.0
zig c++ 0.10.0
zig c++ 0.11.0
zig c++ 0.12.0
zig c++ 0.6.0
zig c++ 0.7.0
zig c++ 0.7.1
zig c++ 0.8.0
zig c++ 0.9.0
zig c++ trunk
Options
Source code
#include <algorithm> #include <type_traits> #include <array> #include <cstdint> #include <utility> #include <immintrin.h> #if defined(__clang__) #define CLANG 1 #else #define CLANG 0 #endif #if defined(__GNUC__)&&!defined(__clang__) #define GCC 1 #else #define GCC 0 #endif #if defined(_MSC_VER)&&!defined(__clang__) #define MSVC 1 #else #define MSVC 0 #endif #if defined(_MSC_VER)&&defined(__clang__) #define CLANG_CL 1 #else #define CLANG_CL 0 #endif static_assert(CLANG + GCC + MSVC == 1); #if MSVC || CLANG_CL #if _WIN32 || _WIN64 #if _WIN64 #define BITNESS 64 #else #define BITNESS 32 #endif #endif #else #if __x86_64__ || __ppc64__ #define BITNESS 64 #else #define BITNESS 32 #endif #endif static_assert(sizeof(void*) * 8 == BITNESS); #if CLANG || GCC #define UNREACHABLE __builtin_unreachable() #define Inline __attribute__((always_inline)) inline #define LambdaInline __attribute__((always_inline)) #else #define UNREACHABLE __assume(false) #define Inline __forceinline #define LambdaInline [[msvc::forceinline]] #endif #define FWD(...) static_cast<decltype(__VA_ARGS__)&&>(__VA_ARGS__) #define MOV(...) static_cast<typename std::remove_reference<decltype(__VA_ARGS__)>::type &&>(__VA_ARGS__) #if CLANG || GCC template<class T, class T0>concept $Same = __is_same(T, T0); template<class T, class...Ts>constexpr bool all_same{ (__is_same(T, Ts)&&...) }; #else template<class T, class T0>concept $Same = std::is_same_v<T, T0>; template<class T, class...Ts>constexpr bool all_same{ (std::is_same_v<T, Ts>&&...) }; #endif namespace Algorithm { template<class Arg0, class...Args> requires(all_same<Arg0, Args...>&& std::is_trivially_copyable_v<Arg0> && sizeof(Arg0) <= 2 * sizeof(void*)) Inline constexpr Arg0 max(Arg0 arg0, const Args...args)noexcept { ((arg0 = (args > arg0) ? args : arg0), ...); return arg0; } } namespace details { template<std::uint64_t MaxValue>struct compact_impl; template<std::uint64_t MaxValue>requires((MaxValue >= 0) && MaxValue <= std::uint8_t(-1)) struct compact_impl<MaxValue> { using type = std::uint8_t; }; template<std::uint64_t MaxValue>requires((MaxValue > std::uint8_t(-1)) && MaxValue <= std::uint16_t(-1)) struct compact_impl<MaxValue> { using type = std::uint16_t; }; template<std::uint64_t MaxValue>requires((MaxValue > std::uint16_t(-1)) && MaxValue <= std::uint32_t(-1)) struct compact_impl<MaxValue> { using type = std::uint32_t; }; template<std::uint64_t MaxValue>requires((MaxValue > std::uint32_t(-1)) && MaxValue <= std::uint64_t(-1)) struct compact_impl<MaxValue> { using type = std::uint64_t; }; } template<std::uint64_t...MaxValue>using Compact = typename details::compact_impl<Algorithm::max(MaxValue...)>::type; template<std::uint64_t Value>static constexpr Compact<Value> compact{static_cast<Compact<Value>>(Value)}; template<class...>struct LikeImpl; template<class From, class To>struct LikeImpl<From&, To> { using type = To&; }; template<class From, class To>struct LikeImpl<const From&, To> { using type = const To&; }; template<class From, class To>struct LikeImpl<volatile From&, To> { using type = volatile To&; }; template<class From, class To>struct LikeImpl<const volatile From&, To> { using type = const volatile To&; }; template<class From, class To>struct LikeImpl<From&&, To> { using type = To&&; }; template<class From, class To>struct LikeImpl<const From&&, To> { using type = const To&&; }; template<class From, class To>struct LikeImpl<volatile From&&, To> { using type = volatile To&&; }; template<class From, class To>struct LikeImpl<const volatile From&&, To> { using type = const volatile To&&; }; template<class From, class To>struct LikeImpl<From, To> { using type = To&&; }; template<class From, class To>struct LikeImpl<const From, To> { using type = const To&&; }; template<class From, class To>struct LikeImpl<volatile From, To> { using type = volatile To&&; }; template<class From, class To>struct LikeImpl<const volatile From, To> { using type = const volatile To&&; }; template<class From, class To>using Like = typename LikeImpl<From, std::remove_cvref_t<To>>::type; template<class From, class To>using LikePtr = std::remove_reference_t<Like<From, To>>*; template<class, std::size_t>struct ConstVector; template<class T, std::size_t N> requires(N > 0) struct ConstVector<T, N> { using index_t = Compact<N>; T m_data[N]; index_t m_size; constexpr ConstVector(auto&& ...args)noexcept requires(sizeof...(args) <= N) :m_data{ FWD(args)... }, m_size{ sizeof...(args) }{} constexpr index_t size()const noexcept { return m_size; } static constexpr index_t capacity() noexcept { return N; } template<class Self>constexpr Like<Self, T> front(this Self&& self) noexcept { return FWD(self).m_data[0]; } template<class Self>constexpr Like<Self, T> back(this Self&& self) noexcept { return FWD(self).m_data[self.m_size - 1]; } template<class Self>constexpr Like<Self, T> operator[](this Self&& self, index_t index) noexcept { return static_cast<Like<Self, T>>(FWD(self).m_data[index]); }//msvc bug template<class Self>constexpr LikePtr<Self, T> data(this Self&& self) noexcept { return self.m_data; } template<class Self>constexpr LikePtr<Self, T> begin(this Self&& self) noexcept { return self.m_data; } template<class Self>constexpr LikePtr<Self, T> end(this Self&& self) noexcept { return self.begin() + self.m_size; } constexpr void clear()noexcept { m_size = {}; } constexpr void push_back(T entry)noexcept { m_data[m_size++] = entry; } }; template<class T> struct ConstVector<T, 0> { constexpr ConstVector()noexcept {} static constexpr std::uint8_t size() noexcept { return 0; } static constexpr std::uint8_t capacity() noexcept { return 0; } constexpr const T* data()const noexcept { return nullptr; } constexpr const T* begin()const noexcept { return nullptr; } constexpr const T* end()const noexcept { return nullptr; } }; template<class, class Sig>struct CallableImpl; template<class T, class Ret, class...Args>struct CallableImpl<T, Ret(Args...)> { static constexpr bool value{ requires(T f, Args...args) { { FWD(f)(FWD(args)...) }->$Same<Ret>; } }; }; template<class T, class Sig>concept Callable = CallableImpl<T, Sig>::value; template<class T, std::size_t N> struct FixedString { private: template<std::size_t...I> Inline constexpr FixedString(const T(&str)[N + 1], std::index_sequence<I...>)noexcept :m_data{ str[I]... } {} public: Inline constexpr FixedString(const T(&str)[N + 1])noexcept : FixedString{ str, std::make_index_sequence<N>{} } {} Inline constexpr FixedString(auto...chars)noexcept : m_data{ chars... } {} Inline static constexpr Compact<N> size()noexcept { return N; } template<class Self>Inline constexpr Like<Self, T> operator[](this Self&& self, std::size_t index) noexcept { return static_cast<Like<Self, T>>(FWD(self).m_data[index]); }//msvc bug Inline constexpr const T* begin()const noexcept { return m_data; } Inline constexpr const T* end()const noexcept { return m_data + size(); } Inline constexpr const T* data()const noexcept { return begin(); } Inline constexpr bool operator==(const FixedString<T, N>&)const noexcept = default; Inline constexpr bool operator!=(const FixedString<T, N>&)const noexcept = default; using char_type = T; T m_data[N]; }; template<class T> struct FixedString<T, 0> { Inline constexpr FixedString(const T(&)[1])noexcept {} Inline constexpr FixedString()noexcept {} Inline static constexpr std::uint8_t size()noexcept { return 0; } template<class Self>Inline constexpr Like<Self, T> operator[](this Self&&, std::size_t) noexcept; Inline constexpr const T* begin()const noexcept { return nullptr; } Inline constexpr const T* end()const noexcept { return nullptr; } Inline constexpr const T* data()const noexcept { return nullptr; } Inline constexpr bool operator==(const FixedString<T, 0>&)const noexcept { return true; } Inline constexpr bool operator!=(const FixedString<T, 0>&)const noexcept { return false; } using char_type = T; }; template<class Char, std::size_t N>FixedString(const Char(&)[N]) -> FixedString<Char, N - 1>; template<class Char, $Same<Char>...Chars>FixedString(Char, Chars...) -> FixedString<Char, sizeof...(Chars) + 1>; template<class T, std::size_t Size> concept $Size = sizeof(T) == Size; template<class T>concept $HasBeginEnd = requires(const T container) { container.begin()!=container.end(); }; template<class T>concept $HasDataSize = requires(const T container) { container.data() + container.size(); }; template<class T>concept $CArray = std::is_array<typename std::remove_cvref<T>::type>::value; namespace details { template<class Container>struct ElementOfImpl { using type = std::remove_cvref_t<decltype(*std::declval<const std::remove_cvref_t<Container>>().data())>; }; template<$CArray Container>struct ElementOfImpl<Container> { using type = std::remove_cvref_t<decltype(*std::declval<Container>())>; }; } template<class Container>using ElementOf = typename details::ElementOfImpl<Container>::type; namespace Algorithm { template<$HasDataSize Container> Inline constexpr void bubble_sort(Container&& container, Callable<bool(const ElementOf<Container>, const ElementOf<Container>)> auto&& compare)noexcept { using Index = std::remove_cvref_t<decltype(container.size())>; const Index n{ container.size() }; if(n) for (Index i{}; i != n - 1; ++i) for (Index j{}; j < n - i - 1; ++j) if (!FWD(compare)(container[j], container[j + 1])) std::swap(container[j], container[j + 1]); } template<$HasBeginEnd Container> Inline constexpr void stable_sort(Container&& container, Callable<bool(const ElementOf<Container>, const ElementOf<Container>)> auto&& compare)noexcept { if (std::is_constant_evaluated()) bubble_sort(FWD(container), FWD(compare)); else std::stable_sort(container.begin(), container.end(), FWD(compare)); } } template<auto Value>constexpr auto constant{ Value }; template<std::uint8_t Bits>struct UnsignedBitsImpl {}; template<std::uint8_t Bits> using UnsignedBits = typename UnsignedBitsImpl<Bits>::type; template<> struct UnsignedBitsImpl<8> { using type = std::uint8_t; }; template<> struct UnsignedBitsImpl<16> { using type = std::uint16_t; }; template<> struct UnsignedBitsImpl<32> { using type = std::uint32_t; }; template<> struct UnsignedBitsImpl<64> { using type = std::uint64_t; }; template<class T>concept $Dtor = !std::is_trivially_destructible_v<T>; template<class T>concept $NotCvref = $Same<T, std::remove_cvref_t<T>>; template<$NotCvref T> struct Opt { #if GCC//gcc bug T m_value{}; #else union { T m_value; }; #endif bool m_hasValue; Inline constexpr Opt()noexcept :m_hasValue{} {} Inline constexpr Opt(auto&&...args)noexcept requires(requires{T{ FWD(args)... }; }) : m_value{ FWD(args)... }, m_hasValue{ true } {} Inline constexpr Opt& operator=(const Opt& rhs)noexcept { (*this).~Opt(); std::construct_at(this, rhs); return *this; } Inline constexpr Opt& operator=(Opt&& rhs)noexcept { (*this).~Opt(); std::construct_at(this, MOV(rhs)); return *this; } Inline constexpr Opt& operator=(auto&& args)noexcept requires(requires{T{ FWD(args) }; }) { (*this).~Opt(); std::construct_at(this, FWD(args)); return *this; } Inline constexpr void emplace(auto&&...args)noexcept requires(requires{T{ FWD(args)... }; }) { std::construct_at(this, FWD(args)...); m_hasValue = true; } Inline constexpr void reset()noexcept { if (m_hasValue) { if constexpr ($Dtor<T>) m_value.~T(); m_hasValue = false; } } Inline constexpr ~Opt()noexcept { if constexpr ($Dtor<T>) if (m_hasValue) m_value.~T(); } Inline constexpr Opt(const Opt& rhs)noexcept :m_hasValue{ rhs.m_hasValue } { if (m_hasValue) std::construct_at(__builtin_addressof(m_value), rhs.m_value); } Inline constexpr Opt(Opt&& rhs)noexcept :m_hasValue{ rhs.m_hasValue } { if (m_hasValue) { std::construct_at(__builtin_addressof(m_value), MOV(rhs.m_value)); rhs.m_hasValue = false; } } Inline constexpr explicit operator bool()const noexcept { return m_hasValue; } Inline constexpr bool has_value()const noexcept { return m_hasValue; } template<class Self>Inline constexpr Like<Self, T> value(this Self&& self) noexcept { return FWD(self).m_value; } template<class Self>Inline constexpr Like<Self, T> operator*(this Self&& self) noexcept { return FWD(self).m_value; } template<class Self>Inline constexpr LikePtr<Self, T> operator->(this Self&& self) noexcept { return __builtin_addressof(self.m_value); } }; template<class T>Inline constexpr bool operator==(const Opt<T>& l, const Opt<T>& r)noexcept { if (l.has_value()) return r.has_value() && *l == *r; else return !r.has_value(); } template<class T>Inline constexpr bool operator!=(const Opt<T>& l, const Opt<T>& r)noexcept { return !(l == r); } template<class T>Inline constexpr bool operator==(const Opt<T>& l, const T& r)noexcept{return l && (*l == r);} template<class T>Inline constexpr bool operator==(const T& l, const Opt<T>& r)noexcept{return r && (l == *r);} template<class T>Inline constexpr bool operator!=(const Opt<T>& l, const T& r)noexcept{return !l || (*l != r);} template<class T>Inline constexpr bool operator!=(const T& l, const Opt<T>& r)noexcept{return !r || (l != *r);} #if CLANG || GCC template<class T, class...Ts>concept $AnyOf = (__is_same(T, Ts) || ...); #else template<class T, class...Ts>concept $AnyOf = (std::is_same_v<T, Ts> || ...); #endif template<class T, class U>concept $Assignable = requires(U& u, const T x){u = x; }; namespace Bytes { template<$Same<std::uint16_t> T>Inline constexpr T concat(std::uint8_t v0, std::uint8_t v1) noexcept { return v0 | (v1 << 8); } template<$Same<std::uint32_t> T>Inline constexpr T concat(std::uint8_t v0, std::uint8_t v1, std::uint8_t v2, std::uint8_t v3) noexcept { return v0 | (v1 << 8) | (v2 << 16) | (v3 << 24); } template<$Same<std::uint64_t> T>Inline constexpr T concat(std::uint8_t v0, std::uint8_t v1, std::uint8_t v2, std::uint8_t v3, std::uint8_t v4, std::uint8_t v5, std::uint8_t v6, std::uint8_t v7) noexcept { return std::uint64_t(v0) | (std::uint64_t(v1) << 8) | ((std::uint64_t)(v2) << 16) | (std::uint64_t(v3) << 24) | (std::uint64_t(v4) << 32) | (std::uint64_t(v5) << 40) | (std::uint64_t(v6) << 48) | (std::uint64_t(v7) << 56); } } namespace BytePattern { struct Nibbles { std::uint8_t low : 4; std::uint8_t high : 4; }; struct ComparisonEntryNibble { std::size_t offset; Opt<std::uint8_t> low; Opt<std::uint8_t> high; }; struct ComparisonEntryBytes { std::size_t offset{}; std::uint8_t size{}; }; struct Element { Opt<std::uint8_t> low, high, byte; constexpr Element(Opt<std::uint8_t> low, Opt<std::uint8_t> high)noexcept :low{ low }, high{ high }, byte{ (low && high) ? static_cast<std::uint8_t>(*low | (*high << 4)) : Opt<std::uint8_t>{} } {} constexpr Element(std::uint8_t byte)noexcept :low{ static_cast<std::uint8_t>(byte & 0x0f) }, high{ static_cast<std::uint8_t>(static_cast<std::uint8_t>(byte & 0xf0) >> 4) }, byte{ (low && high) ? static_cast<std::uint8_t>(*low | (*high << 4)) : Opt<std::uint8_t>{} } {} constexpr Element()noexcept {} constexpr bool operator==(const Element& r)noexcept { return low == r.low && high == r.high; } }; template<auto Elements> struct Sequence { static constexpr std::size_t numElements{ Elements.size() }; static constexpr std::array<Element, numElements> elements{ Elements }; static constexpr ConstVector<ComparisonEntryBytes, numElements> bytesEntries{ [] { ConstVector<ComparisonEntryBytes, numElements> result; std::size_t start{}; for(;;) { while (start < numElements && !elements[start].byte)++start;//skip wildcards/nibbles if (start >= numElements)break; std::size_t iNextNonByte{ start + 1 }; while (iNextNonByte < numElements&& elements[iNextNonByte].byte)++iNextNonByte; const std::size_t dist{ iNextNonByte - start }; const std::uint8_t compareLength{ (dist >= 8 && sizeof(void*) >= 8) ? std::uint8_t(8) : (dist >= 4 ? std::uint8_t(4) : (dist >= 2 ? std::uint8_t(2) : std::uint8_t(1)))}; result.push_back({ start, compareLength }); start += compareLength; } Algorithm::stable_sort(result,[](const ComparisonEntryBytes& l, const ComparisonEntryBytes& r){ return l.size > r.size; }); return result; }() }; static constexpr ConstVector<ComparisonEntryNibble, numElements> nibbleEntries{ [] { ConstVector<ComparisonEntryNibble, numElements> result; std::size_t offset{}; for (Element e : elements) { if (e.low.has_value() ^ e.high.has_value()) result.push_back({ offset, e.low, e.high }); ++offset; } return result; }() }; template<auto I, $Size<1> Uint8> Inline static constexpr bool cmpBytesEntries(Uint8* ptr)noexcept { static constexpr ComparisonEntryBytes entry{ bytesEntries[I] }; static constexpr std::size_t offset{ entry.offset }; static constexpr std::uint8_t size{ entry.size }; static constexpr auto bytes{ [] ()->UnsignedBits<size * 8> { /* */if constexpr (size == 1)return *elements[offset].byte; else if constexpr (size == 2)return Bytes::concat<std::uint16_t>(*elements[offset].byte, *elements[offset + 1].byte); else if constexpr (size == 4)return Bytes::concat<std::uint32_t>(*elements[offset].byte, *elements[offset + 1].byte, *elements[offset + 2].byte, *elements[offset + 3].byte); else if constexpr (size == 8)return Bytes::concat<std::uint64_t>(*elements[offset].byte, *elements[offset + 1].byte, *elements[offset + 2].byte, *elements[offset + 3].byte, *elements[offset + 4].byte, *elements[offset + 5].byte, *elements[offset + 6].byte, *elements[offset + 7].byte); }()}; return *reinterpret_cast<const decltype(bytes)*>(ptr + offset) == bytes; } template<auto I, $Size<1> Uint8> Inline static constexpr bool cmpNibbleEntry(Uint8* ptr)noexcept { static constexpr ComparisonEntryNibble entry{ nibbleEntries[I] }; static constexpr std::size_t offset{ entry.offset }; static constexpr Opt<std::uint8_t> low{ entry.low }; static constexpr Opt<std::uint8_t> high{ entry.high }; if constexpr (low) return reinterpret_cast<const Nibbles*>(ptr + offset)->low == constant<*low>; else if constexpr (high) return reinterpret_cast<const Nibbles*>(ptr + offset)->high == constant<*high>; } template<$Size<1> Uint8, auto...I, auto...J> Inline static constexpr bool isMatchImpl(Uint8* ptr, std::index_sequence<I...>, std::index_sequence<J...>)noexcept { return (cmpBytesEntries<I>(ptr)&&...) && (cmpNibbleEntry<J>(ptr)&&...); } template<$Size<1> Uint8> Inline static constexpr bool isMatch(Uint8* ptr)noexcept { return isMatchImpl(ptr, std::make_index_sequence<bytesEntries.size()>{}, std::make_index_sequence<nibbleEntries.size()>{}); } template<std::size_t...Indexes> static consteval auto deleteElement()noexcept { return Sequence<[] { std::array<Element, numElements> new_result{ elements }; ((new_result[Indexes] = Element{}),...); return new_result; }()>{}; } }; template<FixedString str> constexpr ConstVector<Element, str.size()> parsePatternString()noexcept { constexpr auto isHexDigit{ [](char c) {return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'); } }; constexpr auto char2hexDigit{ [](char c)-> std::uint8_t { if (c >= '0' && c <= '9') return c - '0'; if (c >= 'a' && c <= 'f') return c - 'a' + 10; if (c >= 'A' && c <= 'F') return c - 'A' + 10; UNREACHABLE; } }; constexpr std::size_t size{ str.size()}; const char* pBegin{ str.data()}; const char* pEnd{ pBegin + size }; ConstVector<Element, size> result; if constexpr (size) { bool expectSpace{}; for (const char* pChar{ pBegin }; pChar != pEnd;) { const char c{ *pChar }; if (c == ' ') { if (!expectSpace) UNREACHABLE; ++pChar; expectSpace = false; } else { if (expectSpace)UNREACHABLE; if (c == '?') { if (pChar + 1 == pEnd) { pChar += 1; result.push_back({}); } else { const char c1{ pChar[1] }; if (c1 == ' ') { pChar += 1; result.push_back({}); } else if (c1 == '?') { pChar += 2; result.push_back({}); } else if (isHexDigit(c1)) { result.push_back(Element{ char2hexDigit(c1), {} }); pChar += 2; } else UNREACHABLE; } } else { if (!isHexDigit(c))UNREACHABLE; if (pChar + 1 == pEnd) { result.push_back(char2hexDigit(c)); pChar += 1; } else { const char c1{ pChar[1] }; if (c1 == ' ') { result.push_back(char2hexDigit(c)); pChar += 1; } else if (c1 == '?') { result.push_back(Element{ {}, char2hexDigit(c) }); pChar += 2; } else if (isHexDigit(c1)) { result.push_back(static_cast<std::uint8_t>(char2hexDigit(c) * 0x10 + char2hexDigit(c1))); pChar += 2; } else UNREACHABLE; } } expectSpace = true; } } } return result; } template<FixedString str> constexpr auto makeBytePattern()noexcept { constexpr ConstVector<Element, str.size()> arr{ parsePatternString<str>() }; return[=]<auto...I>(std::index_sequence<I...>) { return Sequence<std::array<Element, arr.size()>{arr[I]...}>{}; }(std::make_index_sequence<arr.size()>{}); } } template<class Container, class T>concept LinearContainerOf = requires{ *(std::declval<Container>().data() + std::declval<Container>().size()); } && $Same<std::remove_cvref_t<decltype(*(std::declval<Container>().data() + std::declval<Container>().size()))>, std::remove_cvref_t<T>>; template<class Container, class...Ts>concept LinearContainerAnyOf = (LinearContainerOf<Container,Ts> || ...); template<class T>concept $UInt = std::is_integral_v<std::remove_cvref_t<T>> && std::is_unsigned_v<std::remove_cvref_t<T>>; namespace Bits { template <$UInt T>Inline constexpr T clearLowestBit(T value) noexcept{return value & (value - 1);} //Returns the number of trailing 0-bits in x, starting at the least significant bit position. If x is 0, the result is undefined Inline constexpr std::uint32_t trailingZeroCount(std::uint32_t x)noexcept { #if MSVC if (std::is_constant_evaluated()) { std::uint32_t flag{ 1 }; for (std::uint8_t i{}; i != sizeof(x) * 8; ++i) { if (flag & x) return i; flag <<= 1; } return sizeof(x) * 8; } else { unsigned long result; _BitScanForward(&result, x); return static_cast<std::uint32_t>(result); } #else return static_cast<std::uint32_t>(__builtin_ctz(x)); #endif } } #if MSVC #define DEFINE_AVX_BYTES(name,x) static constexpr __m256i name{ .m256i_u8{x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x} } #else #define DEFINE_AVX_BYTES(name,x) static constexpr __m256i name{ __builtin_bit_cast(__m256i,std::array{x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x}) } #endif namespace BytePattern { template<auto pattern, std::intptr_t Offset> struct Scanner { using Pattern = decltype(pattern); static constexpr std::intptr_t offset{ Offset }; static constexpr std::size_t numElements{ Pattern::numElements }; static constexpr Opt<std::size_t> iFirstByte{ []()->Opt<std::size_t> { for (std::size_t i{}; i < Pattern::elements.size(); ++i) if (Pattern::elements[i].byte) return i; return {}; }() }; static constexpr Opt<std::size_t> iLastByte{ []()->Opt<std::size_t> { std::size_t result{Pattern::elements.size()}; for (std::size_t i{}; i < Pattern::elements.size(); ++i) if (Pattern::elements[i].byte) result = i; if (result == Pattern::elements.size())return {}; else return result; }() }; //bytes == 0, nibbles >= 1 template<$Size<1> Uint8, Callable<void(Uint8* const)>...Action> requires(!iFirstByte.has_value()) Inline static std::conditional_t<sizeof...(Action) == 0, Uint8*, void> search_unbounded(Uint8* pBegin, Action&&...action)noexcept//todo:optimize { for(;;) { if (Pattern::isMatch(pBegin)) [[unlikely]] { (action(pBegin + offset), ...); if constexpr (sizeof...(action) == 0) return pBegin + offset; } ++pBegin; } } //bytes == 1, nibbles >= 0 template<$Size<1> Uint8, Callable<void(Uint8* const)>...Action> requires(iFirstByte.has_value() && iLastByte.has_value() && *iFirstByte == *iLastByte) Inline static std::conditional_t<sizeof...(Action) == 0, Uint8*, void> search_unbounded(Uint8* pBegin, Action&&...action)noexcept { static constexpr auto iFirst{ compact<*iFirstByte> }; DEFINE_AVX_BYTES(first, *Pattern::elements[iFirst].byte); for (;; pBegin += sizeof(__m256i)) { const __m256i block{ _mm256_loadu_si256(reinterpret_cast<const __m256i*>(pBegin + iFirst)) }; const __m256i eq{ _mm256_cmpeq_epi8(first, block) }; for (std::uint32_t mask{ static_cast<std::uint32_t>(_mm256_movemask_epi8(eq)) }; mask; mask = Bits::clearLowestBit(mask)) [[unlikely]] { const auto bitPos{ Bits::trailingZeroCount(mask) }; if (decltype(Pattern::template deleteElement<iFirst>())::isMatch(pBegin + bitPos)) [[unlikely]] { (action(pBegin + bitPos + offset), ...); if constexpr (sizeof...(action) == 0) return pBegin + bitPos + offset; } } } } //bytes >= 2, nibbles >= 0 template<$Size<1> Uint8, Callable<void(Uint8* const)>...Action> requires(iFirstByte.has_value() && iLastByte.has_value() && *iFirstByte != *iLastByte) Inline static std::conditional_t<sizeof...(Action) == 0, Uint8*, void> search_unbounded(Uint8* pBegin, Action&&...action)noexcept { static constexpr auto iFirst{ compact<*iFirstByte> }; static constexpr auto iLast{ compact<*iLastByte> }; DEFINE_AVX_BYTES(first,*Pattern::elements[iFirst].byte); DEFINE_AVX_BYTES(last ,*Pattern::elements[iLast].byte); for(;; pBegin += sizeof(__m256i)) { const __m256i block_first{ _mm256_loadu_si256(reinterpret_cast<const __m256i*>(pBegin + iFirst)) }; const __m256i block_last{ _mm256_loadu_si256(reinterpret_cast<const __m256i*>(pBegin + iLast)) }; const __m256i eq_first{ _mm256_cmpeq_epi8(first, block_first) }; const __m256i eq_last{ _mm256_cmpeq_epi8(last, block_last) }; for (std::uint32_t mask{ static_cast<std::uint32_t>(_mm256_movemask_epi8(_mm256_and_si256(eq_first, eq_last))) }; mask != 0 ; mask = Bits::clearLowestBit(mask)) [[unlikely]] { const auto bitPos{ Bits::trailingZeroCount(mask) }; if (decltype(Pattern::template deleteElement<iFirst, iLast>())::isMatch(pBegin + bitPos)) [[unlikely]] { (action(pBegin + bitPos + offset), ...); if constexpr (sizeof...(action) == 0) return pBegin + bitPos + offset; } } } } //bytes == 0, nibbles >= 1 template<$Size<1> Uint8, Callable<void(Uint8* const)>...Action> requires(!iFirstByte.has_value()) Inline static std::conditional_t<sizeof...(Action) == 0, Uint8*, void> search(Uint8* pBegin, std::size_t size, Action&&...action)noexcept//todo:optimize { if (size >= numElements) [[likely]]//prevent pEnd underflow { Uint8* const pEnd{ pBegin + size - numElements }; while (pBegin <= pEnd) [[likely]] { if (Pattern::isMatch(pBegin)) [[unlikely]] { (action(pBegin + offset), ...); if constexpr (sizeof...(action) == 0) return pBegin + offset; } ++pBegin; } } if constexpr (sizeof...(action) == 0) return nullptr; } //bytes == 1, nibbles >= 0 template<$Size<1> Uint8, Callable<void(Uint8* const)>...Action> requires(iFirstByte.has_value() && iLastByte.has_value() && *iFirstByte == *iLastByte) Inline static std::conditional_t<sizeof...(Action) == 0, Uint8*, void> search(Uint8* pBegin, std::size_t size, Action&&...action)noexcept { static constexpr auto iFirst{ compact<*iFirstByte> }; DEFINE_AVX_BYTES(first, *Pattern::elements[iFirst].byte); if (size >= numElements) [[likely]]//prevent pEnd underflow { Uint8* const pEnd{ pBegin + size - numElements }; if (size >= numElements + sizeof(__m256i)) [[likely]]//prevent pEndSimd underflow { Uint8* const pEndSimd{ pBegin + size - numElements - sizeof(__m256i) }; for (; pBegin <= pEndSimd; pBegin += sizeof(__m256i)) { const __m256i block{ _mm256_loadu_si256(reinterpret_cast<const __m256i*>(pBegin + iFirst)) }; const __m256i eq{ _mm256_cmpeq_epi8(first, block) }; for (std::uint32_t mask{ static_cast<std::uint32_t>(_mm256_movemask_epi8(eq)) }; mask; mask = Bits::clearLowestBit(mask)) [[unlikely]] { const auto bitPos{ Bits::trailingZeroCount(mask) }; if (decltype(Pattern::template deleteElement<iFirst>())::isMatch(pBegin + bitPos)) [[unlikely]] { (action(pBegin + bitPos + offset), ...); if constexpr (sizeof...(action) == 0) return pBegin + bitPos + offset; } } } } [[unlikely]]; for (; pBegin <= pEnd; ++pBegin) if (Pattern::isMatch(pBegin)) [[unlikely]] { (action(pBegin + offset), ...); if constexpr (sizeof...(action) == 0) return pBegin + offset; } } if constexpr (sizeof...(action) == 0) return nullptr; } //bytes >= 2, nibbles >= 0 template<$Size<1> Uint8, Callable<void(Uint8* const)>...Action> requires(iFirstByte.has_value() && iLastByte.has_value() && *iFirstByte != *iLastByte) Inline static std::conditional_t<sizeof...(Action) == 0, Uint8*, void> search(Uint8* pBegin, std::size_t size, Action&&...action)noexcept { static constexpr auto iFirst{ compact<*iFirstByte> }; static constexpr auto iLast{ compact<*iLastByte> }; DEFINE_AVX_BYTES(first, *Pattern::elements[iFirst].byte); DEFINE_AVX_BYTES(last, *Pattern::elements[iLast].byte); if (size >= numElements) [[likely]]//prevent pEnd underflow { Uint8* const pEnd{ pBegin + size - numElements }; if (size >= sizeof(__m256i) + iLast) [[likely]]//prevent pEndSimd underflow { Uint8* const pEndSimd{ pBegin + size - sizeof(__m256i) - iLast };//p+iLast+sizeof(__m256i)<=pBegin+size; p<=pBegin+size-sizeof(__m256i)-iLast for (; pBegin <= pEndSimd; pBegin += sizeof(__m256i)) [[likely]] { const __m256i block_first{ _mm256_loadu_si256(reinterpret_cast<const __m256i*>(pBegin + iFirst)) }; const __m256i block_last{ _mm256_loadu_si256(reinterpret_cast<const __m256i*>(pBegin + iLast)) }; const __m256i eq_first{ _mm256_cmpeq_epi8(first, block_first) }; const __m256i eq_last{ _mm256_cmpeq_epi8(last, block_last) }; for (std::uint32_t mask{ static_cast<std::uint32_t>(_mm256_movemask_epi8(_mm256_and_si256(eq_first, eq_last))) }; mask != 0; mask = Bits::clearLowestBit(mask)) [[unlikely]] { const auto bitPos{ Bits::trailingZeroCount(mask) }; if (decltype(Pattern::template deleteElement<iFirst, iLast>())::isMatch(pBegin + bitPos)) [[unlikely]] { (action(pBegin + bitPos + offset), ...); if constexpr (sizeof...(action) == 0) return pBegin + bitPos + offset; } } } } [[unlikely]]; for (; pBegin <= pEnd; ++pBegin) if (Pattern::isMatch(pBegin)) [[unlikely]] { (action(pBegin + offset), ...); if constexpr (sizeof...(action) == 0) return pBegin + offset; } } if constexpr (sizeof...(action) == 0) return nullptr; } template<$Size<1> Uint8, Callable<void(Uint8* const)>...Action>Inline static std::conditional_t<sizeof...(Action) == 0, Uint8*, void> search(Uint8* pBegin, Uint8* pEnd, Action&&...action)noexcept { return search(pBegin, static_cast<std::size_t>(pEnd - pBegin), action...); } template<LinearContainerAnyOf<char, std::uint8_t> Container, Callable<void(ElementOf<Container>*)>...Action>Inline static auto search(Container&& bytes, Action&&...action)noexcept { return search(bytes.data(), bytes.size(), action...); } }; } template<FixedString str, std::intptr_t offset = 0>constexpr BytePattern::Scanner<BytePattern::makeBytePattern<str>(), offset> byte_scanner{}; #define EXAMPLE 5 #if EXAMPLE == 0//return first match between [pBegin,pBegin+size], or nullptr if not found const std::uint8_t* example(const std::uint8_t* pBegin, std::size_t size) { return byte_scanner<"40 53 48 83 EC 20 44 8B 41 ? 45 33 DB 4C 8B 0D ? ? ? ? 48 8B D9">.search(pBegin, size); } #elif EXAMPLE == 1//return first match between [pBegin,pEnd], or nullptr if not found const std::uint8_t* example(const std::uint8_t* pBegin, const std::uint8_t* pEnd) { return byte_scanner<"1? 12 ?3">.search(pBegin, pEnd); } #elif EXAMPLE == 2//return first match between [bytes.data(),bytes.data()+bytes.size()], or nullptr if not found #include <span> const std::uint8_t* example(std::span<const std::uint8_t> bytes) { return byte_scanner<"40 53 48 83 EC 20 44 8B 41 ? 45 33 DB 4C 8B 0D ? ? ? ? 48 8B D9">.search(bytes); } #elif EXAMPLE == 3//return first match start from pBegin, or keep searching until crash with access violation const std::uint8_t* example(const std::uint8_t* pBegin) { return byte_scanner<"40 53 48 83 EC 20 44 8B 41 ? 45 33 DB 4C 8B 0D ? ? ? ? 48 8B D9">.search_unbounded(pBegin); } #elif EXAMPLE == 4//enumerate all matches between [bytes.data(),bytes.data()+bytes.size()] #include <iostream> int main() { static constexpr std::array<std::uint8_t, 10> bytes{0,1,2,0,1,2,0,1,2,0}; byte_scanner<"01 02 00">.search(bytes.data(), bytes.size(), [](const std::uint8_t* p) { const std::size_t offset{static_cast<std::size_t>(p-bytes.data())}; std::cout << R"(found "11 22 00" at offset[)" << offset << "]\n";//prints [1,4,7] }); } #elif EXAMPLE == 5//enumerate all matches between [bytes.data(),bytes.data()+bytes.size()] #include <iostream> #include <vector> int main() { static constexpr std::array<std::uint8_t, 10> bytes{0,0x11,0x22,0,0x31,0x42,0,0x51,0x62,0}; std::vector<std::size_t> result; byte_scanner<"?1 ?2 00">.search(bytes, [&](const std::uint8_t* p) { const std::size_t offset{static_cast<std::size_t>(p-bytes.data())}; result.push_back(offset); }); for(const std::size_t offset: result) std::cout << R"(found "?1 ?2 00" at offset[)" << offset << "]\n";//prints [1,4,7] } #endif
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