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
Clojure
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
Helion
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
Yul (Solidity IR)
Zig
Javascript
GIMPLE
Ygen
sway
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 12.4.0
ARM GCC 12.5.0
ARM GCC 13.1.0
ARM GCC 13.2.0
ARM GCC 13.2.0 (unknown-eabi)
ARM GCC 13.3.0
ARM GCC 13.3.0 (unknown-eabi)
ARM GCC 13.4.0
ARM GCC 13.4.0 (unknown-eabi)
ARM GCC 14.1.0
ARM GCC 14.1.0 (unknown-eabi)
ARM GCC 14.2.0
ARM GCC 14.2.0 (unknown-eabi)
ARM GCC 14.3.0
ARM GCC 14.3.0 (unknown-eabi)
ARM GCC 15.1.0
ARM GCC 15.1.0 (unknown-eabi)
ARM GCC 15.2.0
ARM GCC 15.2.0 (unknown-eabi)
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 (ex-WINE)
ARM msvc v19.10 (ex-WINE)
ARM msvc v19.14 (ex-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 12.4.0
ARM64 gcc 12.5.0
ARM64 gcc 13.1.0
ARM64 gcc 13.2.0
ARM64 gcc 13.3.0
ARM64 gcc 13.4.0
ARM64 gcc 14.1.0
ARM64 gcc 14.2.0
ARM64 gcc 14.3.0
ARM64 gcc 15.1.0
ARM64 gcc 15.2.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 (ex-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 12.4.0
AVR gcc 12.5.0
AVR gcc 13.1.0
AVR gcc 13.2.0
AVR gcc 13.3.0
AVR gcc 13.4.0
AVR gcc 14.1.0
AVR gcc 14.2.0
AVR gcc 14.3.0
AVR gcc 15.1.0
AVR gcc 15.2.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 clang 19.1.0
BPF clang 20.1.0
BPF clang 21.1.0
EDG (experimental reflection)
EDG 6.5
EDG 6.5 (GNU mode gcc 13)
EDG 6.6
EDG 6.6 (GNU mode gcc 13)
EDG 6.7
EDG 6.7 (GNU mode gcc 14)
EDG 6.8
EDG 6.8 (GNU mode gcc 15)
FRC 2019
FRC 2020
FRC 2023
HPPA gcc 14.2.0
HPPA gcc 14.3.0
HPPA gcc 15.1.0
HPPA gcc 15.2.0
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)
KVX ACB 5.2.0 (GCC 13.2.1)
LoongArch64 clang (trunk)
LoongArch64 clang 17.0.1
LoongArch64 clang 18.1.0
LoongArch64 clang 19.1.0
LoongArch64 clang 20.1.0
LoongArch64 clang 21.1.0
M68K gcc 13.1.0
M68K gcc 13.2.0
M68K gcc 13.3.0
M68K gcc 13.4.0
M68K gcc 14.1.0
M68K gcc 14.2.0
M68K gcc 14.3.0
M68K gcc 15.1.0
M68K gcc 15.2.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
MinGW gcc 14.3.0
MinGW gcc 15.2.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 12.4.0
RISC-V (32-bits) gcc 12.5.0
RISC-V (32-bits) gcc 13.1.0
RISC-V (32-bits) gcc 13.2.0
RISC-V (32-bits) gcc 13.3.0
RISC-V (32-bits) gcc 13.4.0
RISC-V (32-bits) gcc 14.1.0
RISC-V (32-bits) gcc 14.2.0
RISC-V (32-bits) gcc 14.3.0
RISC-V (32-bits) gcc 15.1.0
RISC-V (32-bits) gcc 15.2.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 12.4.0
RISC-V (64-bits) gcc 12.5.0
RISC-V (64-bits) gcc 13.1.0
RISC-V (64-bits) gcc 13.2.0
RISC-V (64-bits) gcc 13.3.0
RISC-V (64-bits) gcc 13.4.0
RISC-V (64-bits) gcc 14.1.0
RISC-V (64-bits) gcc 14.2.0
RISC-V (64-bits) gcc 14.3.0
RISC-V (64-bits) gcc 15.1.0
RISC-V (64-bits) gcc 15.2.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 19.1.0
RISC-V rv32gc clang 20.1.0
RISC-V rv32gc clang 21.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 19.1.0
RISC-V rv64gc clang 20.1.0
RISC-V rv64gc clang 21.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 12.4.0
SPARC LEON gcc 12.5.0
SPARC LEON gcc 13.1.0
SPARC LEON gcc 13.2.0
SPARC LEON gcc 13.3.0
SPARC LEON gcc 13.4.0
SPARC LEON gcc 14.1.0
SPARC LEON gcc 14.2.0
SPARC LEON gcc 14.3.0
SPARC LEON gcc 15.1.0
SPARC LEON gcc 15.2.0
SPARC gcc 12.2.0
SPARC gcc 12.3.0
SPARC gcc 12.4.0
SPARC gcc 12.5.0
SPARC gcc 13.1.0
SPARC gcc 13.2.0
SPARC gcc 13.3.0
SPARC gcc 13.4.0
SPARC gcc 14.1.0
SPARC gcc 14.2.0
SPARC gcc 14.3.0
SPARC gcc 15.1.0
SPARC gcc 15.2.0
SPARC64 gcc 12.2.0
SPARC64 gcc 12.3.0
SPARC64 gcc 12.4.0
SPARC64 gcc 12.5.0
SPARC64 gcc 13.1.0
SPARC64 gcc 13.2.0
SPARC64 gcc 13.3.0
SPARC64 gcc 13.4.0
SPARC64 gcc 14.1.0
SPARC64 gcc 14.2.0
SPARC64 gcc 14.3.0
SPARC64 gcc 15.1.0
SPARC64 gcc 15.2.0
TI C6x gcc 12.2.0
TI C6x gcc 12.3.0
TI C6x gcc 12.4.0
TI C6x gcc 12.5.0
TI C6x gcc 13.1.0
TI C6x gcc 13.2.0
TI C6x gcc 13.3.0
TI C6x gcc 13.4.0
TI C6x gcc 14.1.0
TI C6x gcc 14.2.0
TI C6x gcc 14.3.0
TI C6x gcc 15.1.0
TI C6x gcc 15.2.0
TI CL430 21.6.1
Tricore gcc 11.3.0 (EEESlab)
VAX gcc NetBSDELF 10.4.0
VAX gcc NetBSDELF 10.5.0 (Nov 15 03:50:22 2023)
VAX gcc NetBSDELF 12.4.0 (Apr 16 05:27 2025)
WebAssembly clang (trunk)
Xtensa ESP32 gcc 11.2.0 (2022r1)
Xtensa ESP32 gcc 12.2.0 (20230208)
Xtensa ESP32 gcc 14.2.0 (20241119)
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 14.2.0 (20241119)
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 14.2.0 (20241119)
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.20 VS16.0
arm64 msvc v19.21 VS16.1
arm64 msvc v19.22 VS16.2
arm64 msvc v19.23 VS16.3
arm64 msvc v19.24 VS16.4
arm64 msvc v19.25 VS16.5
arm64 msvc v19.27 VS16.7
arm64 msvc v19.28 VS16.8
arm64 msvc v19.28 VS16.9
arm64 msvc v19.29 VS16.10
arm64 msvc v19.29 VS16.11
arm64 msvc v19.30 VS17.0
arm64 msvc v19.31 VS17.1
arm64 msvc v19.32 VS17.2
arm64 msvc v19.33 VS17.3
arm64 msvc v19.34 VS17.4
arm64 msvc v19.35 VS17.5
arm64 msvc v19.36 VS17.6
arm64 msvc v19.37 VS17.7
arm64 msvc v19.38 VS17.8
arm64 msvc v19.39 VS17.9
arm64 msvc v19.40 VS17.10
arm64 msvc v19.41 VS17.11
arm64 msvc v19.42 VS17.12
arm64 msvc v19.43 VS17.13
arm64 msvc v19.44 VS17.14
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 19.1.0
armv7-a clang 20.1.0
armv7-a clang 21.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 19.1.0
armv8-a clang 20.1.0
armv8-a clang 21.1.0
armv8-a clang 9.0.0
armv8-a clang 9.0.1
clad trunk (clang 21.1.0)
clad v1.10 (clang 20.1.0)
clad v1.8 (clang 18.1.0)
clad v1.9 (clang 19.1.0)
clad v2.00 (clang 20.1.0)
clad v2.1 (clang 21.1.0)
clad v2.2 (clang 21.1.0)
clang-cl 18.1.0
ellcc 0.1.33
ellcc 0.1.34
ellcc 2017-07-16
ez80-clang 15.0.0
ez80-clang 15.0.7
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 12.4.0
loongarch64 gcc 12.5.0
loongarch64 gcc 13.1.0
loongarch64 gcc 13.2.0
loongarch64 gcc 13.3.0
loongarch64 gcc 13.4.0
loongarch64 gcc 14.1.0
loongarch64 gcc 14.2.0
loongarch64 gcc 14.3.0
loongarch64 gcc 15.1.0
loongarch64 gcc 15.2.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 clang 19.1.0
mips clang 20.1.0
mips clang 21.1.0
mips gcc 11.2.0
mips gcc 12.1.0
mips gcc 12.2.0
mips gcc 12.3.0
mips gcc 12.4.0
mips gcc 12.5.0
mips gcc 13.1.0
mips gcc 13.2.0
mips gcc 13.3.0
mips gcc 13.4.0
mips gcc 14.1.0
mips gcc 14.2.0
mips gcc 14.3.0
mips gcc 15.1.0
mips gcc 15.2.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 12.4.0
mips64 (el) gcc 12.5.0
mips64 (el) gcc 13.1.0
mips64 (el) gcc 13.2.0
mips64 (el) gcc 13.3.0
mips64 (el) gcc 13.4.0
mips64 (el) gcc 14.1.0
mips64 (el) gcc 14.2.0
mips64 (el) gcc 14.3.0
mips64 (el) gcc 15.1.0
mips64 (el) gcc 15.2.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 clang 19.1.0
mips64 clang 20.1.0
mips64 clang 21.1.0
mips64 gcc 11.2.0
mips64 gcc 12.1.0
mips64 gcc 12.2.0
mips64 gcc 12.3.0
mips64 gcc 12.4.0
mips64 gcc 12.5.0
mips64 gcc 13.1.0
mips64 gcc 13.2.0
mips64 gcc 13.3.0
mips64 gcc 13.4.0
mips64 gcc 14.1.0
mips64 gcc 14.2.0
mips64 gcc 14.3.0
mips64 gcc 15.1.0
mips64 gcc 15.2.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
mips64el clang 19.1.0
mips64el clang 20.1.0
mips64el clang 21.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 clang 19.1.0
mipsel clang 20.1.0
mipsel clang 21.1.0
mipsel gcc 12.1.0
mipsel gcc 12.2.0
mipsel gcc 12.3.0
mipsel gcc 12.4.0
mipsel gcc 12.5.0
mipsel gcc 13.1.0
mipsel gcc 13.2.0
mipsel gcc 13.3.0
mipsel gcc 13.4.0
mipsel gcc 14.1.0
mipsel gcc 14.2.0
mipsel gcc 14.3.0
mipsel gcc 15.1.0
mipsel gcc 15.2.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 12.4.0
power gcc 12.5.0
power gcc 13.1.0
power gcc 13.2.0
power gcc 13.3.0
power gcc 13.4.0
power gcc 14.1.0
power gcc 14.2.0
power gcc 14.3.0
power gcc 15.1.0
power gcc 15.2.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 12.4.0
power64 gcc 12.5.0
power64 gcc 13.1.0
power64 gcc 13.2.0
power64 gcc 13.3.0
power64 gcc 13.4.0
power64 gcc 14.1.0
power64 gcc 14.2.0
power64 gcc 14.3.0
power64 gcc 15.1.0
power64 gcc 15.2.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 12.4.0
power64le gcc 12.5.0
power64le gcc 13.1.0
power64le gcc 13.2.0
power64le gcc 13.3.0
power64le gcc 13.4.0
power64le gcc 14.1.0
power64le gcc 14.2.0
power64le gcc 14.3.0
power64le gcc 15.1.0
power64le gcc 15.2.0
power64le gcc 6.3.0
power64le gcc trunk
powerpc64 clang (trunk)
qnx 8.0.0
s390x gcc 11.2.0
s390x gcc 12.1.0
s390x gcc 12.2.0
s390x gcc 12.3.0
s390x gcc 12.4.0
s390x gcc 12.5.0
s390x gcc 13.1.0
s390x gcc 13.2.0
s390x gcc 13.3.0
s390x gcc 13.4.0
s390x gcc 14.1.0
s390x gcc 14.2.0
s390x gcc 14.3.0
s390x gcc 15.1.0
s390x gcc 15.2.0
sh gcc 12.2.0
sh gcc 12.3.0
sh gcc 12.4.0
sh gcc 12.5.0
sh gcc 13.1.0
sh gcc 13.2.0
sh gcc 13.3.0
sh gcc 13.4.0
sh gcc 14.1.0
sh gcc 14.2.0
sh gcc 14.3.0
sh gcc 15.1.0
sh gcc 15.2.0
sh gcc 4.9.4
sh gcc 9.5.0
vast (trunk)
x64 msvc v19.0 (ex-WINE)
x64 msvc v19.10 (ex-WINE)
x64 msvc v19.14 (ex-WINE)
x64 msvc v19.20 VS16.0
x64 msvc v19.21 VS16.1
x64 msvc v19.22 VS16.2
x64 msvc v19.23 VS16.3
x64 msvc v19.24 VS16.4
x64 msvc v19.25 VS16.5
x64 msvc v19.27 VS16.7
x64 msvc v19.28 VS16.8
x64 msvc v19.28 VS16.9
x64 msvc v19.29 VS16.10
x64 msvc v19.29 VS16.11
x64 msvc v19.30 VS17.0
x64 msvc v19.31 VS17.1
x64 msvc v19.32 VS17.2
x64 msvc v19.33 VS17.3
x64 msvc v19.34 VS17.4
x64 msvc v19.35 VS17.5
x64 msvc v19.36 VS17.6
x64 msvc v19.37 VS17.7
x64 msvc v19.38 VS17.8
x64 msvc v19.39 VS17.9
x64 msvc v19.40 VS17.10
x64 msvc v19.41 VS17.11
x64 msvc v19.42 VS17.12
x64 msvc v19.43 VS17.13
x64 msvc v19.44 VS17.14
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 g++ 1.27
x86 msvc v19.0 (ex-WINE)
x86 msvc v19.10 (ex-WINE)
x86 msvc v19.14 (ex-WINE)
x86 msvc v19.20 VS16.0
x86 msvc v19.21 VS16.1
x86 msvc v19.22 VS16.2
x86 msvc v19.23 VS16.3
x86 msvc v19.24 VS16.4
x86 msvc v19.25 VS16.5
x86 msvc v19.27 VS16.7
x86 msvc v19.28 VS16.8
x86 msvc v19.28 VS16.9
x86 msvc v19.29 VS16.10
x86 msvc v19.29 VS16.11
x86 msvc v19.30 VS17.0
x86 msvc v19.31 VS17.1
x86 msvc v19.32 VS17.2
x86 msvc v19.33 VS17.3
x86 msvc v19.34 VS17.4
x86 msvc v19.35 VS17.5
x86 msvc v19.36 VS17.6
x86 msvc v19.37 VS17.7
x86 msvc v19.38 VS17.8
x86 msvc v19.39 VS17.9
x86 msvc v19.40 VS17.10
x86 msvc v19.41 VS17.11
x86 msvc v19.42 VS17.12
x86 msvc v19.43 VS17.13
x86 msvc v19.44 VS17.14
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.11
x86 nvc++ 24.3
x86 nvc++ 24.5
x86 nvc++ 24.7
x86 nvc++ 24.9
x86 nvc++ 25.1
x86 nvc++ 25.11
x86 nvc++ 25.3
x86 nvc++ 25.5
x86 nvc++ 25.7
x86 nvc++ 25.9
x86-64 Zapcc 190308
x86-64 clang (-fimplicit-constexpr)
x86-64 clang (Chris Bazley N3089)
x86-64 clang (EricWF contracts)
x86-64 clang (amd-staging)
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 P2561)
x86-64 clang (experimental P2998)
x86-64 clang (experimental P3068)
x86-64 clang (experimental P3309)
x86-64 clang (experimental P3334)
x86-64 clang (experimental P3367)
x86-64 clang (experimental P3372)
x86-64 clang (experimental P3385)
x86-64 clang (experimental P3776)
x86-64 clang (experimental metaprogramming - P2632)
x86-64 clang (old concepts branch)
x86-64 clang (p1974)
x86-64 clang (pattern matching - P2688)
x86-64 clang (reflection - C++26)
x86-64 clang (reflection - TS)
x86-64 clang (resugar)
x86-64 clang (string interpolation - P3412)
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 19.1.0
x86-64 clang 19.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 20.1.0
x86-64 clang 20.1.0 (assertions)
x86-64 clang 21.1.0
x86-64 clang 21.1.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 clang rocm-6.0.2
x86-64 clang rocm-6.1.2
x86-64 clang rocm-6.2.4
x86-64 clang rocm-6.3.3
x86-64 clang rocm-6.4.0
x86-64 clang rocm-7.0.1
x86-64 gcc (C++26 contracts + GNU extensions)
x86-64 gcc (C++26 contracts)
x86-64 gcc (C++26 reflection)
x86-64 gcc (P2034 lambdas)
x86-64 gcc (Thomas Healy)
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.3 (assertions)
x86-64 gcc 10.4
x86-64 gcc 10.4 (assertions)
x86-64 gcc 10.5
x86-64 gcc 10.5 (assertions)
x86-64 gcc 11.1
x86-64 gcc 11.1 (assertions)
x86-64 gcc 11.2
x86-64 gcc 11.2 (assertions)
x86-64 gcc 11.3
x86-64 gcc 11.3 (assertions)
x86-64 gcc 11.4
x86-64 gcc 11.4 (assertions)
x86-64 gcc 12.1
x86-64 gcc 12.1 (assertions)
x86-64 gcc 12.2
x86-64 gcc 12.2 (assertions)
x86-64 gcc 12.3
x86-64 gcc 12.3 (assertions)
x86-64 gcc 12.4
x86-64 gcc 12.4 (assertions)
x86-64 gcc 12.5
x86-64 gcc 12.5 (assertions)
x86-64 gcc 13.1
x86-64 gcc 13.1 (assertions)
x86-64 gcc 13.2
x86-64 gcc 13.2 (assertions)
x86-64 gcc 13.3
x86-64 gcc 13.3 (assertions)
x86-64 gcc 13.4
x86-64 gcc 13.4 (assertions)
x86-64 gcc 14.1
x86-64 gcc 14.1 (assertions)
x86-64 gcc 14.2
x86-64 gcc 14.2 (assertions)
x86-64 gcc 14.3
x86-64 gcc 14.3 (assertions)
x86-64 gcc 15.1
x86-64 gcc 15.1 (assertions)
x86-64 gcc 15.2
x86-64 gcc 15.2 (assertions)
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 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
x86-64 icx 2024.1.0
x86-64 icx 2024.2.0
x86-64 icx 2024.2.1
x86-64 icx 2025.0.0
x86-64 icx 2025.0.1
x86-64 icx 2025.0.3
x86-64 icx 2025.0.4
x86-64 icx 2025.1.0
x86-64 icx 2025.1.1
x86-64 icx 2025.2.0
x86-64 icx 2025.2.1
x86-64 icx 2025.3.0
x86-64 icx 2025.3.1
x86-64 icx 2025.3.1
z180-clang 15.0.0
z180-clang 15.0.7
z80-clang 15.0.0
z80-clang 15.0.7
zig c++ 0.10.0
zig c++ 0.11.0
zig c++ 0.12.0
zig c++ 0.12.1
zig c++ 0.13.0
zig c++ 0.14.0
zig c++ 0.14.1
zig c++ 0.15.1
zig c++ 0.15.2
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
// intx: extended precision integer library. // Copyright 2019-2020 Pawel Bylica. // Licensed under the Apache License, Version 2.0. #pragma once #include <algorithm> #include <cassert> #include <climits> #include <cstdint> #include <limits> #include <stdexcept> #include <string> #include <tuple> #include <type_traits> #ifndef __has_builtin #define __has_builtin(NAME) 0 #endif #ifdef _MSC_VER #include <intrin.h> #endif #if !defined(__has_builtin) #define __has_builtin(NAME) 0 #endif #if !defined(__has_feature) #define __has_feature(NAME) 0 #endif #if !defined(NDEBUG) #define INTX_UNREACHABLE() assert(false) #elif __has_builtin(__builtin_unreachable) #define INTX_UNREACHABLE() __builtin_unreachable() #elif defined(_MSC_VER) #define INTX_UNREACHABLE() __assume(0) #else #define INTX_UNREACHABLE() (void)0 #endif #if __has_builtin(__builtin_expect) #define INTX_UNLIKELY(EXPR) __builtin_expect(bool{EXPR}, false) #else #define INTX_UNLIKELY(EXPR) (bool{EXPR}) #endif #if !defined(NDEBUG) #define INTX_REQUIRE assert #else #define INTX_REQUIRE(X) (X) ? (void)0 : INTX_UNREACHABLE() #endif // Detect compiler support for 128-bit integer __int128 #if defined(__SIZEOF_INT128__) #define INTX_HAS_BUILTIN_INT128 1 #else #define INTX_HAS_BUILTIN_INT128 0 #endif namespace intx { #if INTX_HAS_BUILTIN_INT128 #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wpedantic" // Usage of __int128 triggers a pedantic warning. /// Alias for the compiler supported unsigned __int128 type. using builtin_uint128 = unsigned __int128; #pragma GCC diagnostic pop #endif template <unsigned N> struct uint; /// The 128-bit unsigned integer. /// /// This type is defined as a specialization of uint<> to easier integration with full intx package, /// however, uint128 may be used independently. template <> struct uint<128> { static constexpr unsigned num_bits = 128; static constexpr unsigned num_words = 2; uint64_t lo = 0; uint64_t hi = 0; constexpr uint() noexcept = default; constexpr uint(uint64_t high, uint64_t low) noexcept : lo{low}, hi{high} {} template <typename T, typename = typename std::enable_if_t<std::is_convertible<T, uint64_t>::value>> constexpr uint(T x) noexcept : lo(static_cast<uint64_t>(x)) // NOLINT {} #if INTX_HAS_BUILTIN_INT128 constexpr uint(builtin_uint128 x) noexcept // NOLINT : lo{uint64_t(x)}, hi{uint64_t(x >> 64)} {} constexpr explicit operator builtin_uint128() const noexcept { return (builtin_uint128{hi} << 64) | lo; } #endif constexpr uint64_t& operator[](size_t i) noexcept { return (i == 0) ? lo : hi; } constexpr const uint64_t& operator[](size_t i) const noexcept { return (i == 0) ? lo : hi; } constexpr explicit operator bool() const noexcept { return hi | lo; } /// Explicit converting operator for all builtin integral types. template <typename Int, typename = typename std::enable_if<std::is_integral<Int>::value>::type> constexpr explicit operator Int() const noexcept { return static_cast<Int>(lo); } }; using uint128 = uint<128>; inline constexpr uint64_t hi(uint128 x) noexcept { return x[1]; } inline constexpr uint64_t lo(uint128 x) noexcept { return x[0]; } inline constexpr bool is_constant_evaluated() noexcept { #if __has_builtin(__builtin_is_constant_evaluated) || (defined(_MSC_VER) && _MSC_VER >= 1925) return __builtin_is_constant_evaluated(); #else return true; #endif } /// Contains result of add/sub/etc with a carry flag. template <typename T> struct result_with_carry { T value; bool carry; /// Conversion to tuple of references, to allow usage with std::tie(). constexpr operator std::tuple<T&, bool&>() noexcept { return {value, carry}; } }; /// Linear arithmetic operators. /// @{ inline constexpr result_with_carry<uint64_t> add_with_carry( uint64_t x, uint64_t y, bool carry = false) noexcept { const auto s = x + y; const auto carry1 = s < x; const auto t = s + carry; const auto carry2 = t < s; return {t, carry1 || carry2}; } template <unsigned N> inline constexpr result_with_carry<uint<N>> add_with_carry( const uint<N>& x, const uint<N>& y, bool carry = false) noexcept { uint<N> s; bool k = carry; for (size_t i = 0; i < uint<N>::num_words; ++i) { s[i] = x[i] + y[i]; const auto k1 = s[i] < x[i]; s[i] += k; k = (s[i] < uint64_t{k}) || k1; } return {s, k}; } inline constexpr uint128 operator+(uint128 x, uint128 y) noexcept { return add_with_carry(x, y).value; } inline constexpr uint128 operator+(uint128 x) noexcept { return x; } inline constexpr result_with_carry<uint64_t> sub_with_carry( uint64_t x, uint64_t y, bool carry = false) noexcept { const auto d = x - y; const auto carry1 = x < y; const auto e = d - carry; const auto carry2 = d < uint64_t{carry}; return {e, carry1 || carry2}; } /// Performs subtraction of two unsigned numbers and returns the difference /// and the carry bit (aka borrow, overflow). template <unsigned N> inline constexpr result_with_carry<uint<N>> sub_with_carry( const uint<N>& x, const uint<N>& y, bool carry = false) noexcept { uint<N> z; bool k = carry; for (size_t i = 0; i < uint<N>::num_words; ++i) { z[i] = x[i] - y[i]; const auto k1 = x[i] < y[i]; const auto k2 = z[i] < uint64_t{k}; z[i] -= k; k = k1 || k2; } return {z, k}; } inline constexpr uint128 operator-(uint128 x, uint128 y) noexcept { return sub_with_carry(x, y).value; } inline constexpr uint128 operator-(uint128 x) noexcept { // Implementing as subtraction is better than ~x + 1. // Clang9: Perfect. // GCC8: Does something weird. return 0 - x; } inline uint128& operator++(uint128& x) noexcept { return x = x + 1; } inline uint128& operator--(uint128& x) noexcept { return x = x - 1; } inline uint128 operator++(uint128& x, int) noexcept { auto ret = x; ++x; return ret; } inline uint128 operator--(uint128& x, int) noexcept { auto ret = x; --x; return ret; } /// Optimized addition. /// /// This keeps the multiprecision addition until CodeGen so the pattern is not /// broken during other optimizations. inline constexpr uint128 fast_add(uint128 x, uint128 y) noexcept { #if INTX_HAS_BUILTIN_INT128 return builtin_uint128{x} + builtin_uint128{y}; #else return x + y; // Fallback to generic addition. #endif } /// @} /// Comparison operators. /// /// In all implementations bitwise operators are used instead of logical ones /// to avoid branching. /// /// @{ inline constexpr bool operator==(uint128 x, uint128 y) noexcept { // Clang7: generates perfect xor based code, // much better than __int128 where it uses vector instructions. // GCC8: generates a bit worse cmp based code // although it generates the xor based one for __int128. return (x[0] == y[0]) & (x[1] == y[1]); } inline constexpr bool operator!=(uint128 x, uint128 y) noexcept { // Analogous to ==, but == not used directly, because that confuses GCC 8-9. return (x[0] != y[0]) | (x[1] != y[1]); } inline constexpr bool operator<(uint128 x, uint128 y) noexcept { // OPT: This should be implemented by checking the borrow of x - y, // but compilers (GCC8, Clang7) // have problem with properly optimizing subtraction. return (x[1] < y[1]) | ((x[1] == y[1]) & (x[0] < y[0])); } inline constexpr bool operator<=(uint128 x, uint128 y) noexcept { return !(y < x); } inline constexpr bool operator>(uint128 x, uint128 y) noexcept { return y < x; } inline constexpr bool operator>=(uint128 x, uint128 y) noexcept { return !(x < y); } /// @} /// Bitwise operators. /// @{ inline constexpr uint128 operator~(uint128 x) noexcept { return {~x[1], ~x[0]}; } inline constexpr uint128 operator|(uint128 x, uint128 y) noexcept { // Clang7: perfect. // GCC8: stupidly uses a vector instruction in all bitwise operators. return {x[1] | y[1], x[0] | y[0]}; } inline constexpr uint128 operator&(uint128 x, uint128 y) noexcept { return {x[1] & y[1], x[0] & y[0]}; } inline constexpr uint128 operator^(uint128 x, uint128 y) noexcept { return {x[1] ^ y[1], x[0] ^ y[0]}; } inline constexpr uint128 operator<<(uint128 x, uint64_t shift) noexcept { return (shift < 64) ? // Find the part moved from lo to hi. // For shift == 0 right shift by (64 - shift) is invalid so // split it into 2 shifts by 1 and (63 - shift). uint128{(x[1] << shift) | ((x[0] >> 1) >> (63 - shift)), x[0] << shift} : // Guarantee "defined" behavior for shifts larger than 128. (shift < 128) ? uint128{x[0] << (shift - 64), 0} : 0; } inline constexpr uint128 operator<<(uint128 x, uint128 shift) noexcept { if (shift < 128) return x << static_cast<uint64_t>(shift); return 0; } inline constexpr uint128 operator>>(uint128 x, uint64_t shift) noexcept { return (shift < 64) ? // Find the part moved from lo to hi. // For shift == 0 left shift by (64 - shift) is invalid so // split it into 2 shifts by 1 and (63 - shift). uint128{x[1] >> shift, (x[0] >> shift) | ((x[1] << 1) << (63 - shift))} : // Guarantee "defined" behavior for shifts larger than 128. (shift < 128) ? uint128{0, x[1] >> (shift - 64)} : 0; } inline constexpr uint128 operator>>(uint128 x, uint128 shift) noexcept { if (shift < 128) return x >> static_cast<uint64_t>(shift); return 0; } /// @} /// Multiplication /// @{ /// Full unsigned multiplication 64 x 64 -> 128. inline constexpr uint128 umul(uint64_t x, uint64_t y) noexcept { #if INTX_HAS_BUILTIN_INT128 return builtin_uint128{x} * builtin_uint128{y}; #elif defined(_MSC_VER) && _MSC_VER >= 1925 if (!is_constant_evaluated()) { unsigned __int64 hi = 0; const auto lo = _umul128(x, y, &hi); return {hi, lo}; } // For constexpr fallback to portable variant. #endif // Portable full unsigned multiplication 64 x 64 -> 128. uint64_t xl = x & 0xffffffff; uint64_t xh = x >> 32; uint64_t yl = y & 0xffffffff; uint64_t yh = y >> 32; uint64_t t0 = xl * yl; uint64_t t1 = xh * yl; uint64_t t2 = xl * yh; uint64_t t3 = xh * yh; uint64_t u1 = t1 + (t0 >> 32); uint64_t u2 = t2 + (u1 & 0xffffffff); uint64_t lo = (u2 << 32) | (t0 & 0xffffffff); uint64_t hi = t3 + (u2 >> 32) + (u1 >> 32); return {hi, lo}; } inline constexpr uint128 operator*(uint128 x, uint128 y) noexcept { auto p = umul(x[0], y[0]); p[1] += (x[0] * y[1]) + (x[1] * y[0]); return {p[1], p[0]}; } /// @} /// Assignment operators. /// @{ inline constexpr uint128& operator+=(uint128& x, uint128 y) noexcept { return x = x + y; } inline constexpr uint128& operator-=(uint128& x, uint128 y) noexcept { return x = x - y; } inline uint128& operator*=(uint128& x, uint128 y) noexcept { return x = x * y; } inline constexpr uint128& operator|=(uint128& x, uint128 y) noexcept { return x = x | y; } inline constexpr uint128& operator&=(uint128& x, uint128 y) noexcept { return x = x & y; } inline constexpr uint128& operator^=(uint128& x, uint128 y) noexcept { return x = x ^ y; } inline constexpr uint128& operator<<=(uint128& x, uint64_t shift) noexcept { return x = x << shift; } inline constexpr uint128& operator>>=(uint128& x, uint64_t shift) noexcept { return x = x >> shift; } /// @} inline constexpr unsigned clz_generic(uint32_t x) noexcept { unsigned n = 32; for (int i = 4; i >= 0; --i) { const auto s = unsigned{1} << i; const auto hi = x >> s; if (hi != 0) { n -= s; x = hi; } } return n - x; } inline constexpr unsigned clz_generic(uint64_t x) noexcept { unsigned n = 64; for (int i = 5; i >= 0; --i) { const auto s = unsigned{1} << i; const auto hi = x >> s; if (hi != 0) { n -= s; x = hi; } } return n - static_cast<unsigned>(x); } inline constexpr unsigned clz(uint32_t x) noexcept { #ifdef _MSC_VER return clz_generic(x); #else return x != 0 ? unsigned(__builtin_clz(x)) : 32; #endif } inline constexpr unsigned clz(uint64_t x) noexcept { #ifdef _MSC_VER return clz_generic(x); #else return x != 0 ? unsigned(__builtin_clzll(x)) : 64; #endif } inline constexpr unsigned clz(uint128 x) noexcept { // In this order `h == 0` we get less instructions than in case of `h != 0`. return x[1] == 0 ? clz(x[0]) + 64 : clz(x[1]); } inline constexpr uint64_t bswap(uint64_t x) noexcept { #if __has_builtin(__builtin_bswap64) return __builtin_bswap64(x); #else #ifdef _MSC_VER if (!is_constant_evaluated()) return _byteswap_uint64(x); #endif const auto a = ((x << 8) & 0xFF00FF00FF00FF00) | ((x >> 8) & 0x00FF00FF00FF00FF); const auto b = ((a << 16) & 0xFFFF0000FFFF0000) | ((a >> 16) & 0x0000FFFF0000FFFF); return (b << 32) | (b >> 32); #endif } inline constexpr uint128 bswap(uint128 x) noexcept { return {bswap(x[0]), bswap(x[1])}; } /// Division. /// @{ template <typename QuotT, typename RemT = QuotT> struct div_result { QuotT quot; RemT rem; /// Conversion to tuple of references, to allow usage with std::tie(). constexpr operator std::tuple<QuotT&, RemT&>() noexcept { return {quot, rem}; } }; namespace internal { inline constexpr uint16_t reciprocal_table_item(uint8_t d9) noexcept { return uint16_t(0x7fd00 / (0x100 | d9)); } #define REPEAT4(x) \ reciprocal_table_item((x) + 0), reciprocal_table_item((x) + 1), \ reciprocal_table_item((x) + 2), reciprocal_table_item((x) + 3) #define REPEAT32(x) \ REPEAT4((x) + 4 * 0), REPEAT4((x) + 4 * 1), REPEAT4((x) + 4 * 2), REPEAT4((x) + 4 * 3), \ REPEAT4((x) + 4 * 4), REPEAT4((x) + 4 * 5), REPEAT4((x) + 4 * 6), REPEAT4((x) + 4 * 7) #define REPEAT256() \ REPEAT32(32 * 0), REPEAT32(32 * 1), REPEAT32(32 * 2), REPEAT32(32 * 3), REPEAT32(32 * 4), \ REPEAT32(32 * 5), REPEAT32(32 * 6), REPEAT32(32 * 7) /// Reciprocal lookup table. constexpr uint16_t reciprocal_table[] = {REPEAT256()}; #undef REPEAT4 #undef REPEAT32 #undef REPEAT256 } // namespace internal /// Computes the reciprocal (2^128 - 1) / d - 2^64 for normalized d. /// /// Based on Algorithm 2 from "Improved division by invariant integers". inline uint64_t reciprocal_2by1(uint64_t d) noexcept { INTX_REQUIRE(d & 0x8000000000000000); // Must be normalized. const uint64_t d9 = d >> 55; const uint32_t v0 = internal::reciprocal_table[d9 - 256]; const uint64_t d40 = (d >> 24) + 1; const uint64_t v1 = (v0 << 11) - uint32_t(v0 * v0 * d40 >> 40) - 1; const uint64_t v2 = (v1 << 13) + (v1 * (0x1000000000000000 - v1 * d40) >> 47); const uint64_t d0 = d & 1; const uint64_t d63 = (d >> 1) + d0; // ceil(d/2) const uint64_t e = ((v2 >> 1) & (0 - d0)) - v2 * d63; const uint64_t v3 = (umul(v2, e)[1] >> 1) + (v2 << 31); const uint64_t v4 = v3 - (umul(v3, d) + d)[1] - d; return v4; } inline uint64_t reciprocal_3by2(uint128 d) noexcept { auto v = reciprocal_2by1(d[1]); auto p = d[1] * v; p += d[0]; if (p < d[0]) { --v; if (p >= d[1]) { --v; p -= d[1]; } p -= d[1]; } const auto t = umul(v, d[0]); p += t[1]; if (p < t[1]) { --v; if (p >= d[1]) { if (p > d[1] || t[0] >= d[0]) --v; } } return v; } inline div_result<uint64_t> udivrem_2by1(uint128 u, uint64_t d, uint64_t v) noexcept { auto q = umul(v, u[1]); q = fast_add(q, u); ++q[1]; auto r = u[0] - q[1] * d; if (r > q[0]) { --q[1]; r += d; } if (r >= d) { ++q[1]; r -= d; } return {q[1], r}; } inline div_result<uint64_t, uint128> udivrem_3by2( uint64_t u2, uint64_t u1, uint64_t u0, uint128 d, uint64_t v) noexcept { auto q = umul(v, u2); q = fast_add(q, {u2, u1}); auto r1 = u1 - q[1] * d[1]; auto t = umul(d[0], q[1]); auto r = uint128{r1, u0} - t - d; r1 = r[1]; ++q[1]; if (r1 >= q[0]) { --q[1]; r += d; } if (r >= d) { ++q[1]; r -= d; } return {q[1], r}; } inline div_result<uint128> udivrem(uint128 x, uint128 y) noexcept { if (y[1] == 0) { INTX_REQUIRE(y[0] != 0); // Division by 0. const auto lsh = clz(y[0]); const auto rsh = (64 - lsh) % 64; const auto rsh_mask = uint64_t{lsh == 0} - 1; const auto yn = y[0] << lsh; const auto xn_lo = x[0] << lsh; const auto xn_hi = (x[1] << lsh) | ((x[0] >> rsh) & rsh_mask); const auto xn_ex = (x[1] >> rsh) & rsh_mask; const auto v = reciprocal_2by1(yn); const auto res1 = udivrem_2by1({xn_ex, xn_hi}, yn, v); const auto res2 = udivrem_2by1({res1.rem, xn_lo}, yn, v); return {{res1.quot, res2.quot}, res2.rem >> lsh}; } if (y[1] > x[1]) return {0, x}; const auto lsh = clz(y[1]); if (lsh == 0) { const auto q = unsigned{y[1] < x[1]} | unsigned{y[0] <= x[0]}; return {q, x - (q ? y : 0)}; } const auto rsh = 64 - lsh; const auto yn_lo = y[0] << lsh; const auto yn_hi = (y[1] << lsh) | (y[0] >> rsh); const auto xn_lo = x[0] << lsh; const auto xn_hi = (x[1] << lsh) | (x[0] >> rsh); const auto xn_ex = x[1] >> rsh; const auto v = reciprocal_3by2({yn_hi, yn_lo}); const auto res = udivrem_3by2(xn_ex, xn_hi, xn_lo, {yn_hi, yn_lo}, v); return {res.quot, res.rem >> lsh}; } inline div_result<uint128> sdivrem(uint128 x, uint128 y) noexcept { constexpr auto sign_mask = uint128{1} << 127; const auto x_is_neg = (x & sign_mask) != 0; const auto y_is_neg = (y & sign_mask) != 0; const auto x_abs = x_is_neg ? -x : x; const auto y_abs = y_is_neg ? -y : y; const auto q_is_neg = x_is_neg ^ y_is_neg; const auto res = udivrem(x_abs, y_abs); return {q_is_neg ? -res.quot : res.quot, x_is_neg ? -res.rem : res.rem}; } inline uint128 operator/(uint128 x, uint128 y) noexcept { return udivrem(x, y).quot; } inline uint128 operator%(uint128 x, uint128 y) noexcept { return udivrem(x, y).rem; } inline uint128& operator/=(uint128& x, uint128 y) noexcept { return x = x / y; } inline uint128& operator%=(uint128& x, uint128 y) noexcept { return x = x % y; } /// @} } // namespace intx namespace std { template <unsigned N> struct numeric_limits<intx::uint<N>> { using type = intx::uint<N>; static constexpr bool is_specialized = true; static constexpr bool is_integer = true; static constexpr bool is_signed = false; static constexpr bool is_exact = true; static constexpr bool has_infinity = false; static constexpr bool has_quiet_NaN = false; static constexpr bool has_signaling_NaN = false; static constexpr float_denorm_style has_denorm = denorm_absent; static constexpr bool has_denorm_loss = false; static constexpr float_round_style round_style = round_toward_zero; static constexpr bool is_iec559 = false; static constexpr bool is_bounded = true; static constexpr bool is_modulo = true; static constexpr int digits = CHAR_BIT * sizeof(type); static constexpr int digits10 = int(0.3010299956639812 * digits); static constexpr int max_digits10 = 0; static constexpr int radix = 2; static constexpr int min_exponent = 0; static constexpr int min_exponent10 = 0; static constexpr int max_exponent = 0; static constexpr int max_exponent10 = 0; static constexpr bool traps = std::numeric_limits<unsigned>::traps; static constexpr bool tinyness_before = false; static constexpr type min() noexcept { return 0; } static constexpr type lowest() noexcept { return min(); } static constexpr type max() noexcept { return ~type{0}; } static constexpr type epsilon() noexcept { return 0; } static constexpr type round_error() noexcept { return 0; } static constexpr type infinity() noexcept { return 0; } static constexpr type quiet_NaN() noexcept { return 0; } static constexpr type signaling_NaN() noexcept { return 0; } static constexpr type denorm_min() noexcept { return 0; } }; } // namespace std namespace intx { template <typename T> [[noreturn]] inline void throw_(const char* what) { #if __cpp_exceptions throw T{what}; #else std::fputs(what, stderr); std::abort(); #endif } inline constexpr int from_dec_digit(char c) { if (c < '0' || c > '9') throw_<std::invalid_argument>("invalid digit"); return c - '0'; } inline constexpr int from_hex_digit(char c) { if (c >= 'a' && c <= 'f') return c - ('a' - 10); if (c >= 'A' && c <= 'F') return c - ('A' - 10); return from_dec_digit(c); } template <typename Int> inline constexpr Int from_string(const char* str) { auto s = str; auto x = Int{}; int num_digits = 0; if (s[0] == '0' && s[1] == 'x') { s += 2; while (const auto c = *s++) { if (++num_digits > int{sizeof(x) * 2}) throw_<std::out_of_range>(str); x = (x << 4) | from_hex_digit(c); } return x; } while (const auto c = *s++) { if (num_digits++ > std::numeric_limits<Int>::digits10) throw_<std::out_of_range>(str); const auto d = from_dec_digit(c); x = x * Int{10} + d; if (x < d) throw_<std::out_of_range>(str); } return x; } template <typename Int> inline constexpr Int from_string(const std::string& s) { return from_string<Int>(s.c_str()); } inline constexpr uint128 operator""_u128(const char* s) { return from_string<uint128>(s); } template <unsigned N> inline std::string to_string(uint<N> x, int base = 10) { if (base < 2 || base > 36) throw_<std::invalid_argument>("invalid base"); if (x == 0) return "0"; auto s = std::string{}; while (x != 0) { // TODO: Use constexpr udivrem_1? const auto res = udivrem(x, uint<N>{base}); const auto d = int(res.rem); const auto c = d < 10 ? '0' + d : 'a' + d - 10; s.push_back(char(c)); x = res.quot; } std::reverse(s.begin(), s.end()); return s; } template <unsigned N> inline std::string hex(uint<N> x) { return to_string(x, 16); } } // namespace intx using namespace intx; constexpr auto xxx = bswap(uint64_t{1}); auto bs(uint64_t x) { return bswap(x); }
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