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)
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)
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.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.2.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
#include <limits> #include <compare> #include <cstdint> #include <bit> #include <format> #include <cmath> #include <numbers> #include <functional> #include <concepts> #include <compare> #include <cinttypes> #include <cstdlib> #include <type_traits> #include <bit> #include <string> // TODO #include <bitset> // TODO #include <iostream> #include <assert.h> namespace leviathan::math::numeric { template <typename T> struct div_result { T quotient; T remainder; constexpr div_result(T quot, T rem) : quotient(quot), remainder(rem) { } constexpr div_result(std::div_t x) : quotient(x.quot), remainder(x.rem) { } constexpr div_result(std::ldiv_t x) : quotient(x.quot), remainder(x.rem) { } constexpr div_result(std::lldiv_t x) : quotient(x.quot), remainder(x.rem) { } constexpr div_result(std::imaxdiv_t x) : quotient(x.quot), remainder(x.rem) { } }; template <bool Signed, std::endian Endian> struct int128_layout { using lower_type = uint64_t; using upper_type = std::conditional_t<Signed, int64_t, uint64_t>; static constexpr bool lower_index = (Endian == std::endian::little); uint64_t m_data[2]; constexpr int128_layout() = default; constexpr int128_layout(upper_type upper, lower_type lower) : m_data{ static_cast<uint64_t>(upper), lower } { } upper_type upper() const { return static_cast<upper_type>(m_data[1 - lower_index]); } lower_type lower() const { return static_cast<lower_type>(m_data[lower_index]); } }; template <std::endian Endian = std::endian::native> struct uint128; template <std::endian Endian = std::endian::native> struct int128; template <std::endian Endian> class uint128 : int128_layout<false, Endian> { friend class int128<Endian>; using base = int128_layout<false, Endian>; template <typename T> constexpr static uint128 make_uint128_from_float(T v) { // Undefined behavior if v is NaN or cannot fit into uint128. assert(std::isfinite(v) && v >= 0 && (std::numeric_limits<T>::max_exponent <= 128 || v < std::ldexp(static_cast<T>(1), 128))); if (v >= std::ldexp(static_cast<T>(1), 64)) { const uint64_t hi = static_cast<uint64_t>(std::ldexp(v, -64)); const uint64_t lo = static_cast<uint64_t>(v - std::ldexp(static_cast<T>(hi), 64)); return uint128(hi, lo); } return uint128(0, static_cast<uint64_t>(v)); } template <typename T> constexpr static T make_float_from_uint128(uint128 x) { return static_cast<T>(x.lower()) + std::ldexp(static_cast<T>(x.upper()), 64); } template <typename Fn> constexpr static uint128 bit_op(Fn fn, uint128 u1, uint128 u2) { const auto hi = fn(u1.upper(), u2.upper()); const auto lo = fn(u1.lower(), u2.lower()); return uint128(hi, lo); } public: constexpr uint128() = default; constexpr uint128(const uint128&) = default; constexpr uint128(uint64_t upper, uint64_t lower) : base(upper, lower) { } constexpr uint128(int128<Endian> i128) : uint128(static_cast<uint64_t>(i128.upper()), i128.lower()) { } // Constructors from unsigned types constexpr uint128(uint64_t u) : uint128(0, u) { } constexpr uint128(uint32_t u) : uint128(static_cast<uint64_t>(u)) { } constexpr uint128(uint16_t u) : uint128(static_cast<uint64_t>(u)) { } constexpr uint128(uint8_t u) : uint128(static_cast<uint64_t>(u)) { } // Constructors from signed types constexpr uint128(int64_t i) : uint128(std::signbit(i) ? std::numeric_limits<uint64_t>::max() : 0, i) { } constexpr uint128(int32_t i) : uint128(static_cast<int64_t>(i)) { } constexpr uint128(int16_t i) : uint128(static_cast<int64_t>(i)) { } constexpr uint128(int8_t i) : uint128(static_cast<int64_t>(i)) { } // Constructors from floating types constexpr uint128(float f) : uint128(make_uint128_from_float(f)) { } constexpr uint128(double d) : uint128(make_uint128_from_float(d)) { } constexpr uint128(long double ld) : uint128(make_uint128_from_float(ld)) { } // Assignment operator from arithmetic types constexpr uint128& operator=(uint128 rhs) { this->m_data[0] = rhs.m_data[0]; this->m_data[1] = rhs.m_data[1]; return *this; } constexpr uint128& operator=(uint64_t u) { return *this = uint128(u); } constexpr uint128& operator=(uint32_t u) { return *this = uint128(u); } constexpr uint128& operator=(uint16_t u) { return *this = uint128(u); } constexpr uint128& operator=(uint8_t u) { return *this = uint128(u); } constexpr uint128& operator=(int64_t i) { return *this = uint128(i); } constexpr uint128& operator=(int32_t i) { return *this = uint128(i); } constexpr uint128& operator=(int16_t i) { return *this = uint128(i); } constexpr uint128& operator=(int8_t i) { return *this = uint128(i); } constexpr uint128& operator=(float f) { return *this = uint128(f); } constexpr uint128& operator=(double d) { return *this = uint128(d); } constexpr uint128& operator=(long double ld) { return *this = uint128(ld); } // Conversion operators to other arithmetic types constexpr explicit operator bool(this uint128 x) { return x.lower() || x.upper(); } constexpr explicit operator char(this uint128 x) { return static_cast<char>(x.lower()); } constexpr explicit operator wchar_t(this uint128 x) { return static_cast<wchar_t>(x.lower()); } constexpr explicit operator char8_t(this uint128 x) { return static_cast<char8_t>(x.lower()); } constexpr explicit operator char16_t(this uint128 x) { return static_cast<char16_t>(x.lower()); } constexpr explicit operator char32_t(this uint128 x) { return static_cast<char32_t>(x.lower()); } constexpr explicit operator int8_t(this uint128 x) { return static_cast<int8_t>(x.lower()); } constexpr explicit operator int16_t(this uint128 x) { return static_cast<int16_t>(x.lower()); } constexpr explicit operator int32_t(this uint128 x) { return static_cast<int32_t>(x.lower()); } constexpr explicit operator int64_t(this uint128 x) { return static_cast<int64_t>(x.lower()); } constexpr explicit operator int128<Endian>(this uint128 x) { return int128<Endian>( static_cast<int64_t>(x.upper()), x.lower() ); } constexpr explicit operator uint8_t(this uint128 x) { return static_cast<uint8_t>(x.lower()); } constexpr explicit operator uint16_t(this uint128 x) { return static_cast<uint16_t>(x.lower()); } constexpr explicit operator uint32_t(this uint128 x) { return static_cast<uint32_t>(x.lower()); } constexpr explicit operator uint64_t(this uint128 x) { return static_cast<uint64_t>(x.lower()); } constexpr explicit operator float(this uint128 x) { return make_float_from_uint128<float>(x); } constexpr explicit operator double(this uint128 x) { return make_float_from_uint128<double>(x); } constexpr explicit operator long double(this uint128 x) { return make_float_from_uint128<long double>(x); } // Unary operators constexpr uint128 operator+(this uint128 x) { return x; } constexpr uint128 operator-(this uint128 x) { const auto hi = ~x.upper() + static_cast<uint64_t>(x.lower() == 0); const auto lo = ~x.lower() + 1; return uint128(hi, lo); } constexpr bool operator!(this uint128 x) { return !static_cast<bool>(x); } constexpr uint128 operator~(this uint128 x) { return uint128(~x.upper(), ~x.lower()); } // Binary operators constexpr uint128 operator|(this uint128 lhs, uint128 rhs) { return bit_op(std::bit_or<>(), lhs, rhs); } constexpr uint128 operator&(this uint128 lhs, uint128 rhs) { return bit_op(std::bit_and<>(), lhs, rhs); } constexpr uint128 operator^(this uint128 lhs, uint128 rhs) { return bit_op(std::bit_xor<>(), lhs, rhs); } constexpr uint128 operator+(this uint128 lhs, uint128 rhs) { const auto lo = lhs.lower() + rhs.lower(); const auto carry = (lo < lhs.lower() ? 1 : 0); const auto hi = lhs.upper() + rhs.upper() + carry; return uint128(hi, lo); } constexpr uint128 operator-(this uint128 lhs, uint128 rhs) { const auto lo = lhs.lower() - rhs.lower(); const auto carry = (lo <= lhs.lower() ? 0 : 1); const auto hi = lhs.upper() - rhs.upper() - carry; return uint128(hi, lo); } constexpr uint128 operator*(this uint128 lhs, uint128 rhs) { // We split uint128 into three parts: // High64bit(H64), Low-High32bit(LH32) and Low-Low32bit(LL32) // |----------------|--------|--------| // 128 64 32 0 // H64 LH32 LL32 // L64 // A * B = // H64(A) * H64(B) => Overflow // H64(A) * L64(B) => H64 // L64(A) * H64(B) => H64 // L64(A) * L64(B) = // LH32(A) * LH32(B) => H64 // LL32(A) * LH32(B) => H64 or L64 // LH32(A) * LL32(B) => H64 or L64 // LL32(A) * LL32(B) => L64 constexpr uint64_t mask = 0xffffffff; // mask low 64-bit const uint64_t ah = lhs.lower() >> 32; const uint64_t al = lhs.lower() & mask; const uint64_t bh = rhs.lower() >> 32; const uint64_t bl = rhs.lower() & mask; const auto part_hi = lhs.upper() * rhs.lower() // H64(A) * L64(B) + lhs.lower() * rhs.upper() // L64(A) * H64(B) + ah * bh; // LH32(A) * LH32(B) const auto part_lo = al * bl; // LL32(A) * LL32(B) uint128 result(part_hi, part_lo); result += uint128(ah * bl) << 32; // LH32(A) * LL32(B) result += uint128(bh * al) << 32; // LL32(A) * LH32(B) return result; } constexpr uint128 operator/(this uint128 lhs, uint128 rhs) { return div_mod(lhs, rhs).quotient; } constexpr uint128 operator%(this uint128 lhs, uint128 rhs) { return div_mod(lhs, rhs).remainder; } constexpr uint128 operator<<(this uint128 lhs, uint64_t amount) { // We use uint64_t instead of int to make amount non-negative. // The result is undefined if the right operand is negative, or // greater than or equal to the number of bits in the left expression's type. assert(amount < 128 && ""); const auto hi = lhs.upper(); const auto lo = lhs.lower(); if (amount >= 64) { return uint128(lo << (amount - 64), 0); } else if (amount > 0) { return uint128((hi << amount) | (lo >> (64 - amount)), lo << amount); } else { return lhs; } } constexpr uint128 operator>>(this uint128 lhs, uint64_t amount) { // We use uint64_t instead of int to make amount non-negative. // The result is undefined if the right operand is negative, or // greater than or equal to the number of bits in the left expression's type. assert(amount < 128 && ""); const auto hi = lhs.upper(); const auto lo = lhs.lower(); if (amount >= 64) { return uint128(0, hi >> (amount - 64)); } else if (amount > 0) { return uint128(hi >> amount, (lo >> amount) | (hi << (64 - amount))); } else { return lhs; } } // Comparision constexpr bool operator==(this uint128 lhs, uint128 rhs) { return lhs.lower() == rhs.lower() && lhs.upper() == rhs.upper(); } constexpr auto operator<=>(this uint128 lhs, uint128 rhs) { return lhs.upper() != rhs.upper() ? lhs.upper() <=> rhs.upper() : lhs.lower() <=> rhs.lower(); } // Other operators constexpr uint128& operator+=(uint128 rhs) { return *this = *this + rhs; } constexpr uint128& operator-=(uint128 rhs) { return *this = *this - rhs; } constexpr uint128& operator*=(uint128 rhs) { return *this = *this * rhs; } constexpr uint128& operator/=(uint128 rhs) { return *this = *this / rhs; } constexpr uint128& operator%=(uint128 rhs) { return *this = *this % rhs; } constexpr uint128& operator|=(uint128 rhs) { return *this = *this | rhs; } constexpr uint128& operator^=(uint128 rhs) { return *this = *this ^ rhs; } constexpr uint128& operator&=(uint128 rhs) { return *this = *this & rhs; } constexpr uint128& operator<<=(int amount) { return *this = *this << amount; } constexpr uint128& operator>>=(int amount) { return *this = *this >> amount; } constexpr uint128& operator++() { return *this = *this + 1; } constexpr uint128& operator--() { return *this = *this - 1; } constexpr uint128 operator++(int) { auto temp = *this; ++*this; return temp; } constexpr uint128 operator--(int) { auto temp = *this; --*this; return temp; } // Bits constexpr friend int popcount(uint128 x) { return std::popcount(x.lower()) + std::popcount(x.upper()); } friend constexpr bool has_single_bit(uint128 x) { return popcount(x) == 1; } friend constexpr int countl_zero(uint128 x) { return x.upper() == 0 ? std::countl_zero(x.lower()) + 64 : std::countl_zero(x.upper()); } friend constexpr int countr_zero(uint128 x) { return x.lower() == 0 ? std::countr_zero(x.upper()) + 64 : std::countr_zero(x.lower()); } friend constexpr int countl_one(uint128 x) { return x.upper() == std::numeric_limits<uint64_t>::max() ? std::countl_one(x.lower()) + 64 : std::countl_one(x.upper()); } friend constexpr int countr_one(uint128 x) { return x.lower() == std::numeric_limits<uint64_t>::max() ? std::countr_one(x.upper()) + 64 : std::countr_one(x.lower()); } std::string to_string(this uint128 x) { return std::format("{}", x); } constexpr uint64_t hash_code(this uint128 x) { return hash_combine(x.upper(), x.lower()); } static consteval uint128 max() { // 11111...11111 return uint128( std::numeric_limits<uint64_t>::max(), std::numeric_limits<uint64_t>::max() ); } static consteval uint128 min() { return uint128(0, 0); } static constexpr div_result<uint128> div_mod(uint128 dividend, uint128 divisor) { assert(divisor != 0 && "dividend = quotient * divisor + remainder"); // https://stackoverflow.com/questions/5386377/division-without-using if (divisor > dividend) { return { uint128(0), dividend }; } if (divisor == dividend) { return { uint128(1), uint128(0) }; } uint128 denominator = divisor; uint128 current = 1; uint128 answer = 0; // Follow may be faster. // const int shift = denominator.countl_zero() - dividend.countl_zero() + 1; const int shift = countl_zero(denominator) - countl_zero(dividend) + 1; denominator <<= shift; current <<= shift; // After loop, the current will be zero. for (int i = 0; i <= shift; ++i) { if (dividend >= denominator) { dividend -= denominator; answer |= current; } current >>= 1; denominator >>= 1; } return { answer, dividend }; } constexpr auto lower() const { return base::lower(); } constexpr auto upper() const { return base::upper(); } }; template <std::endian Endian> class int128 : int128_layout<true, Endian> { friend class uint128<Endian>; using base = int128_layout<true, Endian>; template <typename T> static constexpr int128 make_int128_from_float(T v) { // Conversion when v is NaN or cannot fit into int128 would be undefined // behavior if using an intrinsic 128-bit integer. assert(std::isfinite(v) && (std::numeric_limits<T>::max_exponent <= 127 || (v >= -std::ldexp(static_cast<T>(1), 127) && v < std::ldexp(static_cast<T>(1), 127)))); const uint128<Endian> result = signbit(v) ? -uint128<Endian>(-v) : uint128<Endian>(v); return static_cast<int128>(result); } template <typename T> static constexpr T make_float_from_int128(int128 x) { // We must convert the absolute value and then negate as needed, because // floating point types are typically sign-magnitude. Otherwise, the // difference between the high and low 64 bits when interpreted as two's // complement overwhelms the precision of the mantissa. // // Also check to make sure we don't negate Int128Min() return x.upper() < 0 && x != int128::min() ? -static_cast<T>(-x) : static_cast<T>(x.lower()) + std::ldexp(static_cast<T>(x.upper()), 64); } template <typename Fn> static constexpr int128 do_as_uint128(Fn fn, int128 x, int128 y) { const uint128<Endian> ux = x, uy = y; return int128(fn(ux, uy)); } public: constexpr int128() = default; constexpr int128(const int128&) = default; constexpr int128(int64_t upper, uint64_t lower) : base(upper, lower) { } constexpr int128(uint128<Endian> u128) : int128(static_cast<int64_t>(u128.upper()), u128.lower()) { } // COnstructors from unsigned types constexpr int128(uint64_t u) : int128(0, u) { } constexpr int128(uint32_t u) : int128(static_cast<uint64_t>(u)) { } constexpr int128(uint16_t u) : int128(static_cast<uint64_t>(u)) { } constexpr int128(uint8_t u) : int128(static_cast<uint64_t>(u)) { } // Constructors from signed types constexpr int128(int64_t i) : int128((std::signbit(i) ? ~int64_t(0) : 0), static_cast<uint64_t>(i)) { } constexpr int128(int32_t i) : int128(static_cast<int64_t>(i)) { } constexpr int128(int16_t i) : int128(static_cast<int64_t>(i)) { } constexpr int128(int8_t i) : int128(static_cast<int64_t>(i)) { } // Constructors from floating types constexpr int128(float f) : int128(make_int128_from_float(f)) { } constexpr int128(double d) : int128(make_int128_from_float(d)) { } constexpr int128(long double ld) : int128(make_int128_from_float(ld)) { } constexpr int128& operator=(int128 rhs) { this->m_data[0] = rhs.m_data[0]; this->m_data[1] = rhs.m_data[1]; return *this; } constexpr int128& operator=(uint64_t u) { return *this = int128(u); } constexpr int128& operator=(uint32_t u) { return *this = int128(u); } constexpr int128& operator=(uint16_t u) { return *this = int128(u); } constexpr int128& operator=(uint8_t u) { return *this = int128(u); } constexpr int128& operator=(int64_t i) { return *this = int128(i); } constexpr int128& operator=(int32_t i) { return *this = int128(i); } constexpr int128& operator=(int16_t i) { return *this = int128(i); } constexpr int128& operator=(int8_t i) { return *this = int128(i); } constexpr int128& operator=(float f) { return *this = int128(f); } constexpr int128& operator=(double d) { return *this = int128(d); } constexpr int128& operator=(long double ld) { return *this = int128(ld); } // Conversion operators to other arithmetic types constexpr explicit operator bool(this int128 x) { return x.upper() || x.lower(); } constexpr explicit operator char(this int128 x) { return static_cast<char>(static_cast<int64_t>(x)); } constexpr explicit operator wchar_t(this int128 x) { return static_cast<wchar_t>(static_cast<int64_t>(x)); } constexpr explicit operator char8_t(this int128 x) { return static_cast<char8_t>(x.lower()); } constexpr explicit operator char16_t(this int128 x) { return static_cast<char16_t>(x.lower()); } constexpr explicit operator char32_t(this int128 x) { return static_cast<char32_t>(x.lower()); } constexpr explicit operator int8_t(this int128 x) { return static_cast<int8_t>(static_cast<int64_t>(x)); } constexpr explicit operator int16_t(this int128 x) { return static_cast<int16_t>(static_cast<int64_t>(x)); } constexpr explicit operator int32_t(this int128 x) { return static_cast<int32_t>(static_cast<int64_t>(x)); } constexpr explicit operator int64_t(this int128 x) { return static_cast<int64_t>(x.lower()); } constexpr explicit operator uint8_t(this int128 x) { return static_cast<uint8_t>(x.lower()); } constexpr explicit operator uint16_t(this int128 x) { return static_cast<uint16_t>(x.lower()); } constexpr explicit operator uint32_t(this int128 x) { return static_cast<uint32_t>(x.lower()); } constexpr explicit operator uint64_t(this int128 x) { return static_cast<uint64_t>(x.lower()); } constexpr explicit operator uint128<Endian>(this int128 x) { return uint128<Endian>( static_cast<int64_t>(x.upper()), x.lower() ); } constexpr explicit operator float(this int128 x) { return make_float_from_int128<float>(x); } constexpr explicit operator double(this int128 x) { return make_float_from_int128<double>(x); } constexpr explicit operator long double(this int128 x) { return make_float_from_int128<long double>(x); } // Unary operators constexpr int128 operator+(this int128 x) { return x; } constexpr int128 operator-(this int128 x) { const uint128<Endian> u = x; return -u; } constexpr bool operator!(this int128 x) { return !static_cast<bool>(x); } constexpr int128 operator~(this int128 x) { return int128(~x.upper(), ~x.lower()); } // Binary operators constexpr int128 operator|(this int128 lhs, int128 rhs) { return do_as_uint128(std::bit_or<>(), lhs, rhs); } constexpr int128 operator&(this int128 lhs, int128 rhs) { return do_as_uint128(std::bit_and<>(), lhs, rhs); } constexpr int128 operator^(this int128 lhs, int128 rhs) { return do_as_uint128(std::bit_xor<>(), lhs, rhs); } constexpr int128 operator+(this int128 lhs, int128 rhs) { return do_as_uint128(std::plus<>(), lhs, rhs); } constexpr int128 operator-(this int128 lhs, int128 rhs) { return do_as_uint128(std::minus<>(), lhs, rhs); } constexpr int128 operator*(this int128 lhs, int128 rhs) { return do_as_uint128(std::multiplies<>(), lhs, rhs); } constexpr int128 operator/(this int128 lhs, int128 rhs) { return div_mod(lhs, rhs).quotient; } constexpr int128 operator%(this int128 lhs, int128 rhs) { return div_mod(lhs, rhs).remainder; } constexpr int128 operator<<(this int128 lhs, uint64_t amount) { // We use uint64_t instead of int to make amount non-negative. // The result is undefined if the right operand is negative, or // greater than or equal to the number of bits in the left expression's type. assert(amount < 128 && ""); // The shift operation in signed integer and unsigned are same. return static_cast<uint128<Endian>>(lhs) << amount; } // The only difference between signed and unsigned is right shift operation. constexpr int128 operator>>(this int128 lhs, uint64_t amount) { // We use uint64_t instead of int to make amount non-negative. // The result is undefined if the right operand is negative, or // greater than or equal to the number of bits in the left expression's type. assert(amount < 128 && ""); const auto result = static_cast<uint128<Endian>>(lhs) >> amount; // Right-shift on signed integral types is an arithmetic right shift, // which performs sign-extension. So we must keep sign bit when shifting // signed integer. if (signbit(lhs.upper())) { return result | (uint128<Endian>::max() << (127 - amount)); } return result; } constexpr bool operator==(this int128 lhs, int128 rhs) { return lhs.upper() == rhs.upper() && lhs.lower() == rhs.lower(); } constexpr auto operator<=>(this int128 lhs, int128 rhs) { return lhs.upper() == rhs.upper() ? lhs.lower() <=> rhs.lower() : lhs.upper() <=> rhs.upper(); } // Other operators constexpr int128& operator+=(int128 rhs) { return *this = *this + rhs; } constexpr int128& operator-=(int128 rhs) { return *this = *this - rhs; } constexpr int128& operator*=(int128 rhs) { return *this = *this * rhs; } constexpr int128& operator/=(int128 rhs) { return *this = *this / rhs; } constexpr int128& operator%=(int128 rhs) { return *this = *this % rhs; } constexpr int128& operator|=(int128 rhs) { return *this = *this | rhs; } constexpr int128& operator^=(int128 rhs) { return *this = *this ^ rhs; } constexpr int128& operator&=(int128 rhs) { return *this = *this & rhs; } constexpr int128& operator<<=(int amount) { return *this = *this << amount; } constexpr int128& operator>>=(int amount) { return *this = *this >> amount; } constexpr int128& operator++() { return *this = *this + 1; } constexpr int128& operator--() { return *this = *this - 1; } constexpr int128 operator++(int) { auto temp = *this; ++*this; return temp; } constexpr int128 operator--(int) { auto temp = *this; --*this; return temp; } constexpr size_t hash_code(this int128 x) { return hash_combine(x.upper(), x.lower()); } static consteval int128 max() { // 0111111...111 return int128( std::numeric_limits<int64_t>::max(), std::numeric_limits<uint64_t>::max() ); } static consteval int128 min() { // 1000000...000 return int128( std::numeric_limits<int64_t>::min(), 0 ); } static constexpr div_result<int128> div_mod(int128 dividend, int128 divisor) { assert(dividend != int128::min() || divisor != -1); const uint128<Endian> quotient = dividend < 0 ? -uint128<Endian>(dividend) : uint128<Endian>(dividend); const uint128<Endian> remainder = divisor < 0 ? -uint128<Endian>(divisor) : uint128<Endian>(divisor); const auto [quot, rem] = uint128<Endian>::div_mod(quotient, remainder); return { static_cast<int128>(quot), static_cast<int128>(rem) }; } std::string to_string(this int128 x) { return std::format("{}", x); } constexpr auto lower() const { return base::lower(); } constexpr auto upper() const { return base::upper(); } }; using int128_t = int128<>; using uint128_t = uint128<>; // template class uint128<>; // template class int128<>; template <std::endian Endian> std::ostream& operator<<(std::ostream& os, uint128<Endian> x) { return os << x.to_string(); } template <std::endian Endian> std::ostream& operator<<(std::ostream& os, int128<Endian> x) { return os << x.to_string(); } } // Specialize for std::numeric_limits template <std::endian Endian> struct std::numeric_limits<leviathan::math::numeric::uint128<Endian>> { private: using uint128 = leviathan::math::numeric::uint128<Endian>; public: static constexpr bool is_specialized = true; static constexpr bool is_signed = false; static constexpr bool is_integer = true; 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 std::float_denorm_style has_denorm = std::float_denorm_style::denorm_absent; static constexpr bool has_denorm_loss = false; static constexpr std::float_round_style round_style = std::float_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 = 128; static constexpr int digits10 = 38; 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<uint64_t>::traps; static constexpr bool tinyness_before = false; static constexpr uint128 min() { return uint128::min(); } static constexpr uint128 lowest() { return 0; } static constexpr uint128 max() { return uint128::max(); } static constexpr uint128 epsilon() { return 0; } static constexpr uint128 round_error() { return 0; } static constexpr uint128 infinity() { return 0; } static constexpr uint128 quiet_NaN() { return 0; } static constexpr uint128 signaling_NaN() { return 0; } static constexpr uint128 denorm_min() { return 0; } }; template <std::endian Endian> struct std::numeric_limits<leviathan::math::numeric::int128<Endian>> { private: using int128 = leviathan::math::numeric::int128<Endian>; public: static constexpr bool is_specialized = true; static constexpr bool is_signed = true; static constexpr bool is_integer = true; 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 std::float_denorm_style has_denorm = std::float_denorm_style::denorm_absent; static constexpr bool has_denorm_loss = false; static constexpr std::float_round_style round_style = std::float_round_style::round_toward_zero; static constexpr bool is_iec559 = false; static constexpr bool is_bounded = true; static constexpr bool is_modulo = false; static constexpr int digits = 127; static constexpr int digits10 = 38; 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 = numeric_limits<uint64_t>::traps; static constexpr bool tinyness_before = false; static constexpr int128 min() { return int128::min(); } static constexpr int128 lowest() { return int128::min(); } static constexpr int128 max() { return int128::max(); } static constexpr int128 epsilon() { return 0; } static constexpr int128 round_error() { return 0; } static constexpr int128 infinity() { return 0; } static constexpr int128 quiet_NaN() { return 0; } static constexpr int128 signaling_NaN() { return 0; } static constexpr int128 denorm_min() { return 0; } }; // Specialize for std::hash template <std::endian Endian> struct std::hash<leviathan::math::numeric::uint128<Endian>> { static constexpr auto operator()(leviathan::math::numeric::uint128<Endian> x) { return x.hash_code(); } }; template <std::endian Endian> struct std::hash<leviathan::math::numeric::int128<Endian>> { static constexpr auto operator()(leviathan::math::numeric::int128<Endian> x) { return x.hash_code(); } }; // Specialize for std::formatter template <std::endian Endian, typename CharT> struct std::formatter<leviathan::math::numeric::uint128<Endian>, CharT> { using uint128 = leviathan::math::numeric::uint128<Endian>; template <typename ParseContext> constexpr typename ParseContext::iterator parse(ParseContext& ctx) { return m_fmt.parse(ctx); } template <typename FormatContext> typename FormatContext::iterator format(uint128 x, FormatContext& ctx) const { unsigned __int128 v = (static_cast<unsigned __int128>(x.upper()) << 64) | (static_cast<unsigned __int128>(x.lower())); return m_fmt.format(v, ctx); } std::formatter<unsigned __int128, CharT> m_fmt; }; template <std::endian Endian, typename CharT> struct std::formatter<leviathan::math::numeric::int128<Endian>, CharT> { using int128 = leviathan::math::numeric::int128<Endian>; template <typename ParseContext> constexpr typename ParseContext::iterator parse(ParseContext& ctx) { return m_fmt.parse(ctx); } template <typename FormatContext> typename FormatContext::iterator format(int128 x, FormatContext& ctx) const { signed __int128 v = (static_cast<signed __int128>(x.upper()) << 64) | (static_cast<signed __int128>(x.lower())); return m_fmt.format(v, ctx); } std::formatter<signed __int128, CharT> m_fmt; }; namespace leviathan { using leviathan::math::numeric::uint128_t; using leviathan::math::numeric::int128_t; } int main() { }
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