Thanks for using Compiler Explorer
Sponsors
Jakt
C++
Ada
Analysis
Android Java
Android Kotlin
Assembly
C
C3
Carbon
C++ (Circle)
CIRCT
Clean
CMake
CMakeScript
COBOL
C++ for OpenCL
MLIR
Cppx
Cppx-Blue
Cppx-Gold
Cpp2-cppfront
Crystal
C#
CUDA C++
D
Dart
Elixir
Erlang
Fortran
F#
GLSL
Go
Haskell
HLSL
Hook
Hylo
IL
ispc
Java
Julia
Kotlin
LLVM IR
LLVM MIR
Modula-2
Nim
Objective-C
Objective-C++
OCaml
Odin
OpenCL C
Pascal
Pony
Python
Racket
Ruby
Rust
Snowball
Scala
Slang
Solidity
Spice
SPIR-V
Swift
LLVM TableGen
Toit
TypeScript Native
V
Vala
Visual Basic
Vyper
WASM
Zig
Javascript
GIMPLE
Ygen
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 cc65 2.17
6502 cc65 2.18
6502 cc65 2.19
6502 cc65 trunk
ARM GCC 10.2.0 (linux)
ARM GCC 10.2.1 (none)
ARM GCC 10.3.0 (linux)
ARM GCC 10.3.1 (2021.07 none)
ARM GCC 10.3.1 (2021.10 none)
ARM GCC 10.5.0
ARM GCC 11.1.0 (linux)
ARM GCC 11.2.0 (linux)
ARM GCC 11.2.1 (none)
ARM GCC 11.3.0 (linux)
ARM GCC 11.4.0
ARM GCC 12.1.0 (linux)
ARM GCC 12.2.0 (linux)
ARM GCC 12.3.0
ARM GCC 12.4.0
ARM GCC 13.1.0 (linux)
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 14.1.0
ARM GCC 14.1.0 (unknown-eabi)
ARM GCC 14.2.0
ARM GCC 14.2.0 (unknown-eabi)
ARM GCC 4.5.4 (linux)
ARM GCC 4.6.4 (linux)
ARM GCC 5.4 (linux)
ARM GCC 5.4.1 (none)
ARM GCC 6.3.0 (linux)
ARM GCC 6.4.0 (linux)
ARM GCC 7.2.1 (none)
ARM GCC 7.3.0 (linux)
ARM GCC 7.5.0 (linux)
ARM GCC 8.2.0 (WinCE)
ARM GCC 8.2.0 (linux)
ARM GCC 8.3.1 (none)
ARM GCC 8.5.0 (linux)
ARM GCC 9.2.1 (none)
ARM GCC 9.3.0 (linux)
ARM GCC trunk (linux)
ARM msvc v19.0 (WINE)
ARM msvc v19.10 (WINE)
ARM msvc v19.14 (WINE)
ARM64 GCC 10.2.0
ARM64 GCC 10.3.0
ARM64 GCC 10.4.0
ARM64 GCC 10.5.0
ARM64 GCC 11.1.0
ARM64 GCC 11.2.0
ARM64 GCC 11.3.0
ARM64 GCC 11.4.0
ARM64 GCC 12.1.0
ARM64 GCC 12.2.0
ARM64 GCC 12.3.0
ARM64 GCC 12.4.0
ARM64 GCC 13.1.0
ARM64 GCC 13.2.0
ARM64 GCC 13.3.0
ARM64 GCC 14.1.0
ARM64 GCC 14.2.0
ARM64 GCC 4.9.4
ARM64 GCC 5.4
ARM64 GCC 5.5.0
ARM64 GCC 6.3
ARM64 GCC 6.4.0
ARM64 GCC 7.3.0
ARM64 GCC 7.5.0
ARM64 GCC 8.2.0
ARM64 GCC 8.5.0
ARM64 GCC 9.3.0
ARM64 GCC 9.4.0
ARM64 GCC 9.5.0
ARM64 GCC trunk
ARM64 Morello GCC 10.1.0 Alpha 1
ARM64 Morello GCC 10.1.2 Alpha 2
ARM64 msvc v19.14 (WINE)
AVR gcc 10.3.0
AVR gcc 11.1.0
AVR gcc 12.1.0
AVR gcc 12.2.0
AVR gcc 12.3.0
AVR gcc 12.4.0
AVR gcc 13.1.0
AVR gcc 13.2.0
AVR gcc 13.3.0
AVR gcc 14.1.0
AVR gcc 14.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 gcc 13.1.0
BPF gcc 13.2.0
BPF gcc 13.3.0
BPF gcc 14.1.0
BPF gcc 14.2.0
BPF gcc trunk
Chibicc 2020-12-07
FRC 2019
FRC 2020
FRC 2023
HPPA gcc 14.2.0
K1C gcc 7.4
K1C gcc 7.5
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)
LC3 (trunk)
M68K clang (trunk)
M68K gcc 13.1.0
M68K gcc 13.2.0
M68K gcc 13.3.0
M68K gcc 14.1.0
M68K gcc 14.2.0
MRISC32 gcc (trunk)
MSP430 gcc 12.1.0
MSP430 gcc 12.2.0
MSP430 gcc 12.3.0
MSP430 gcc 12.4.0
MSP430 gcc 13.1.0
MSP430 gcc 13.2.0
MSP430 gcc 13.3.0
MSP430 gcc 14.1.0
MSP430 gcc 14.2.0
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
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 13.1.0
POWER64 gcc 13.2.0
POWER64 gcc 13.3.0
POWER64 gcc 14.1.0
POWER64 gcc 14.2.0
POWER64 gcc trunk
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 13.1.0
RISC-V (32-bits) gcc 13.2.0
RISC-V (32-bits) gcc 13.3.0
RISC-V (32-bits) gcc 14.1.0
RISC-V (32-bits) gcc 14.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 13.1.0
RISC-V (64-bits) gcc 13.2.0
RISC-V (64-bits) gcc 13.3.0
RISC-V (64-bits) gcc 14.1.0
RISC-V (64-bits) gcc 14.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 9.0.0
RISC-V rv32gc clang 9.0.1
RISC-V rv64gc clang (trunk)
RISC-V rv64gc clang 10.0.0
RISC-V rv64gc clang 10.0.1
RISC-V rv64gc clang 11.0.0
RISC-V rv64gc clang 11.0.1
RISC-V rv64gc clang 12.0.0
RISC-V rv64gc clang 12.0.1
RISC-V rv64gc clang 13.0.0
RISC-V rv64gc clang 13.0.1
RISC-V rv64gc clang 14.0.0
RISC-V rv64gc clang 15.0.0
RISC-V rv64gc clang 16.0.0
RISC-V rv64gc clang 17.0.1
RISC-V rv64gc clang 18.1.0
RISC-V rv64gc clang 9.0.0
RISC-V rv64gc clang 9.0.1
Raspbian Buster
Raspbian Stretch
SDCC 4.0.0
SDCC 4.1.0
SDCC 4.2.0
SDCC 4.3.0
SDCC 4.4.0
SPARC LEON gcc 12.2.0
SPARC LEON gcc 12.3.0
SPARC LEON gcc 12.4.0
SPARC LEON gcc 13.1.0
SPARC LEON gcc 13.2.0
SPARC LEON gcc 13.3.0
SPARC LEON gcc 14.1.0
SPARC LEON gcc 14.2.0
SPARC gcc 12.2.0
SPARC gcc 12.3.0
SPARC gcc 12.4.0
SPARC gcc 13.1.0
SPARC gcc 13.2.0
SPARC gcc 13.3.0
SPARC gcc 14.1.0
SPARC gcc 14.2.0
SPARC64 gcc 12.2.0
SPARC64 gcc 12.3.0
SPARC64 gcc 12.4.0
SPARC64 gcc 13.1.0
SPARC64 gcc 13.2.0
SPARC64 gcc 13.3.0
SPARC64 gcc 14.1.0
SPARC64 gcc 14.2.0
TCC (trunk)
TCC 0.9.27
TI C6x gcc 12.2.0
TI C6x gcc 12.3.0
TI C6x gcc 12.4.0
TI C6x gcc 13.1.0
TI C6x gcc 13.2.0
TI C6x gcc 13.3.0
TI C6x gcc 14.1.0
TI C6x gcc 14.2.0
TI CL430 21.6.1
VAX gcc NetBSDELF 10.4.0
VAX gcc NetBSDELF 10.5.0 (Nov 15 03:50:22 2023)
WebAssembly clang (trunk)
Xtensa ESP32 gcc 11.2.0 (2022r1)
Xtensa ESP32 gcc 12.2.0 (20230208)
Xtensa ESP32 gcc 8.2.0 (2019r2)
Xtensa ESP32 gcc 8.2.0 (2020r1)
Xtensa ESP32 gcc 8.2.0 (2020r2)
Xtensa ESP32 gcc 8.4.0 (2020r3)
Xtensa ESP32 gcc 8.4.0 (2021r1)
Xtensa ESP32 gcc 8.4.0 (2021r2)
Xtensa ESP32-S2 gcc 11.2.0 (2022r1)
Xtensa ESP32-S2 gcc 12.2.0 (20230208)
Xtensa ESP32-S2 gcc 8.2.0 (2019r2)
Xtensa ESP32-S2 gcc 8.2.0 (2020r1)
Xtensa ESP32-S2 gcc 8.2.0 (2020r2)
Xtensa ESP32-S2 gcc 8.4.0 (2020r3)
Xtensa ESP32-S2 gcc 8.4.0 (2021r1)
Xtensa ESP32-S2 gcc 8.4.0 (2021r2)
Xtensa ESP32-S3 gcc 11.2.0 (2022r1)
Xtensa ESP32-S3 gcc 12.2.0 (20230208)
Xtensa ESP32-S3 gcc 8.4.0 (2020r3)
Xtensa ESP32-S3 gcc 8.4.0 (2021r1)
Xtensa ESP32-S3 gcc 8.4.0 (2021r2)
arm64 msvc v19.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.latest
armv7-a clang (trunk)
armv7-a clang 10.0.0
armv7-a clang 10.0.1
armv7-a clang 11.0.0
armv7-a clang 11.0.1
armv7-a clang 12.0.0
armv7-a clang 12.0.1
armv7-a clang 13.0.0
armv7-a clang 13.0.1
armv7-a clang 14.0.0
armv7-a clang 15.0.0
armv7-a clang 16.0.0
armv7-a clang 17.0.1
armv7-a clang 18.1.0
armv7-a clang 9.0.0
armv7-a clang 9.0.1
armv8-a clang (all architectural features, trunk)
armv8-a clang (trunk)
armv8-a clang 10.0.0
armv8-a clang 10.0.1
armv8-a clang 11.0.0
armv8-a clang 11.0.1
armv8-a clang 12.0.0
armv8-a clang 12.0.1
armv8-a clang 13.0.0
armv8-a clang 13.0.1
armv8-a clang 14.0.0
armv8-a clang 15.0.0
armv8-a clang 16.0.0
armv8-a clang 17.0.1
armv8-a clang 18.1.0
armv8-a clang 9.0.0
armv8-a clang 9.0.1
clang 12 for DPU (rel 2023.2.0)
cproc-master
llvm-mos commander X16
llvm-mos commodore 64
llvm-mos mega65
llvm-mos nes-cnrom
llvm-mos nes-mmc1
llvm-mos nes-mmc3
llvm-mos nes-nrom
llvm-mos osi-c1p
loongarch64 gcc 12.2.0
loongarch64 gcc 12.3.0
loongarch64 gcc 12.4.0
loongarch64 gcc 13.1.0
loongarch64 gcc 13.2.0
loongarch64 gcc 13.3.0
loongarch64 gcc 14.1.0
loongarch64 gcc 14.2.0
mips (el) gcc 12.1.0
mips (el) gcc 12.2.0
mips (el) gcc 12.3.0
mips (el) gcc 12.4.0
mips (el) gcc 13.1.0
mips (el) gcc 13.2.0
mips (el) gcc 13.3.0
mips (el) gcc 14.1.0
mips (el) gcc 14.2.0
mips (el) gcc 4.9.4
mips (el) gcc 5.4
mips (el) gcc 5.5.0
mips (el) gcc 9.5.0
mips clang 13.0.0
mips clang 14.0.0
mips clang 15.0.0
mips clang 16.0.0
mips clang 17.0.1
mips clang 18.1.0
mips gcc 11.2.0
mips gcc 12.1.0
mips gcc 12.2.0
mips gcc 12.3.0
mips gcc 12.4.0
mips gcc 13.1.0
mips gcc 13.2.0
mips gcc 13.3.0
mips gcc 14.1.0
mips gcc 14.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 13.1.0
mips64 (el) gcc 13.2.0
mips64 (el) gcc 13.3.0
mips64 (el) gcc 14.1.0
mips64 (el) gcc 14.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 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 13.1.0
mips64 gcc 13.2.0
mips64 gcc 13.3.0
mips64 gcc 14.1.0
mips64 gcc 14.2.0
mips64 gcc 4.9.4
mips64 gcc 5.4
mips64 gcc 5.5.0
mips64 gcc 9.5.0
mips64el clang 13.0.0
mips64el clang 14.0.0
mips64el clang 15.0.0
mips64el clang 16.0.0
mips64el clang 17.0.1
mips64el clang 18.1.0
mipsel clang 13.0.0
mipsel clang 14.0.0
mipsel clang 15.0.0
mipsel clang 16.0.0
mipsel clang 17.0.1
mipsel clang 18.1.0
movfuscator (trunk)
nanoMIPS gcc 6.3.0
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 13.1.0
power gcc 13.2.0
power gcc 13.3.0
power gcc 14.1.0
power gcc 14.2.0
power gcc 4.8.5
power64 AT12.0 (gcc8)
power64 AT13.0 (gcc9)
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 13.1.0
power64le gcc 13.2.0
power64le gcc 13.3.0
power64le gcc 14.1.0
power64le gcc 14.2.0
power64le gcc 6.3.0
power64le gcc trunk
powerpc64 clang (trunk)
ppci 0.5.5
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 13.1.0
s390x gcc 13.2.0
s390x gcc 13.3.0
s390x gcc 14.1.0
s390x gcc 14.2.0
sh gcc 12.2.0
sh gcc 12.3.0
sh gcc 12.4.0
sh gcc 13.1.0
sh gcc 13.2.0
sh gcc 13.3.0
sh gcc 14.1.0
sh gcc 14.2.0
sh gcc 4.9.4
sh gcc 9.5.0
vast (trunk)
x64 msvc v19.0 (WINE)
x64 msvc v19.10 (WINE)
x64 msvc v19.14 (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.latest
x86 CompCert 3.10
x86 CompCert 3.11
x86 CompCert 3.12
x86 CompCert 3.9
x86 gcc 1.27
x86 msvc v19.0 (WINE)
x86 msvc v19.10 (WINE)
x86 msvc v19.14 (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.latest
x86 nvc 24.11
x86 nvc 24.9
x86 tendra (trunk)
x86-64 clang (assertions trunk)
x86-64 clang (thephd.dev)
x86-64 clang (trunk)
x86-64 clang (widberg)
x86-64 clang 10.0.0
x86-64 clang 10.0.1
x86-64 clang 11.0.0
x86-64 clang 11.0.1
x86-64 clang 12.0.0
x86-64 clang 12.0.1
x86-64 clang 13.0.0
x86-64 clang 13.0.1
x86-64 clang 14.0.0
x86-64 clang 15.0.0
x86-64 clang 16.0.0
x86-64 clang 17.0.1
x86-64 clang 18.1.0
x86-64 clang 19.1.0
x86-64 clang 3.0.0
x86-64 clang 3.1
x86-64 clang 3.2
x86-64 clang 3.3
x86-64 clang 3.4.1
x86-64 clang 3.5
x86-64 clang 3.5.1
x86-64 clang 3.5.2
x86-64 clang 3.6
x86-64 clang 3.7
x86-64 clang 3.7.1
x86-64 clang 3.8
x86-64 clang 3.8.1
x86-64 clang 3.9.0
x86-64 clang 3.9.1
x86-64 clang 4.0.0
x86-64 clang 4.0.1
x86-64 clang 5.0.0
x86-64 clang 5.0.1
x86-64 clang 5.0.2
x86-64 clang 6.0.0
x86-64 clang 6.0.1
x86-64 clang 7.0.0
x86-64 clang 7.0.1
x86-64 clang 7.1.0
x86-64 clang 8.0.0
x86-64 clang 8.0.1
x86-64 clang 9.0.0
x86-64 clang 9.0.1
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 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 14.1
x86-64 gcc 14.1 (assertions)
x86-64 gcc 14.2
x86-64 gcc 14.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 6.1
x86-64 gcc 6.2
x86-64 gcc 6.3
x86-64 gcc 6.5
x86-64 gcc 7.1
x86-64 gcc 7.2
x86-64 gcc 7.3
x86-64 gcc 7.4
x86-64 gcc 7.5
x86-64 gcc 8.1
x86-64 gcc 8.2
x86-64 gcc 8.3
x86-64 gcc 8.4
x86-64 gcc 8.5
x86-64 gcc 9.1
x86-64 gcc 9.2
x86-64 gcc 9.3
x86-64 gcc 9.4
x86-64 gcc 9.5
x86-64 icc 13.0.1
x86-64 icc 16.0.3
x86-64 icc 17.0.0
x86-64 icc 18.0.0
x86-64 icc 19.0.0
x86-64 icc 19.0.1
x86-64 icc 2021.1.2
x86-64 icc 2021.10.0
x86-64 icc 2021.2.0
x86-64 icc 2021.3.0
x86-64 icc 2021.4.0
x86-64 icc 2021.5.0
x86-64 icc 2021.6.0
x86-64 icc 2021.7.0
x86-64 icc 2021.7.1
x86-64 icc 2021.8.0
x86-64 icc 2021.9.0
x86-64 icx (latest)
x86-64 icx 2021.1.2
x86-64 icx 2021.2.0
x86-64 icx 2021.3.0
x86-64 icx 2021.4.0
x86-64 icx 2022.0.0
x86-64 icx 2022.1.0
x86-64 icx 2022.2.0
x86-64 icx 2022.2.1
x86-64 icx 2023.0.0
x86-64 icx 2023.1.0
x86-64 icx 2024.0.0
x86_64 CompCert 3.10
x86_64 CompCert 3.11
x86_64 CompCert 3.12
x86_64 CompCert 3.9
z88dk 2.2
zig cc 0.10.0
zig cc 0.11.0
zig cc 0.12.0
zig cc 0.12.1
zig cc 0.13.0
zig cc 0.6.0
zig cc 0.7.0
zig cc 0.7.1
zig cc 0.8.0
zig cc 0.9.0
zig cc trunk
Options
Source code
#include <math.h> #include <stdbool.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> /* Const */ const double G = 6.67408 * 1e-11; const double EARTH_MASS = 5.97237 * 1e24; const double EARTH_RADIUS = 6.378 * 1e6; const double MOON_MASS = 7.342 * 1e22; // const double MOON_RADIUS = 1.737 * 1e6; const double EARTH_MOON_D = 3.84402 * 1e8; const double ANGULAR_SPEED = 2.0*M_PI / (27.3*86400.0); // rad/sec const double SIM_DURATION = 10.0*86400; // sec const double INJECTION_ALTITUDE = 185*1000; // m // 100 nm (Apollo 16-17: 90 nm) // 50 sec => 600 Ko / 13 k points => 500 km / 5 deg between 2 points around the Earth const uint64_t EXPORT_DT = 50; // sec /* Helpers */ double deg2rad(double angle) { return angle*M_PI/180.0; } void print_time(char *s) { static struct timespec last; static bool once = false; if (once == false) { clock_gettime(CLOCK_MONOTONIC_RAW, &last); once = true; } struct timespec cur; clock_gettime(CLOCK_MONOTONIC_RAW, &cur); double delta_ms = (cur.tv_sec - last.tv_sec) * 1000 + 1.0 * (cur.tv_nsec - last.tv_nsec) / (1000*1000); printf("print_time(): %s: %.3f ms\n", s, delta_ms); memcpy(&last, &cur, sizeof(struct timespec)); } /* Integrators */ typedef enum _Integrators { IntegratorsEulerNaive, IntegratorsEulerOptim, IntegratorsRk4Optim, } Integrators; char *Integrators_to_str(Integrators i) { switch (i) { case IntegratorsEulerNaive: return "IntegratorsEulerNaive"; break; case IntegratorsEulerOptim: return "IntegratorsEulerOptim"; break; case IntegratorsRk4Optim: return "IntegratorsRk4Optim"; break; } fprintf(stderr, "Error: Integrators_to_str(%d)\n", i); exit(1); } Integrators Integrators_from_str(char *s) { if (strcmp("euler-naive", s) == 0) return IntegratorsEulerNaive; if (strcmp("euler-optim", s) == 0) return IntegratorsEulerOptim; if (strcmp("rk4-optim", s) == 0) return IntegratorsRk4Optim; fprintf(stderr, "Error: Integrators_from_str(%s)\n", s); exit(1); } /* Vector */ typedef struct _Vector { double x, y; } Vector; Vector Vector_from_polar_rad(double magnitude, double angle) { return (Vector){ .x = magnitude*cos(angle), .y = magnitude*sin(angle), }; } double Vector_magnitude(Vector v) { return sqrt(v.x*v.x + v.y*v.y); } Vector Vector_rotate(Vector v, double rotation_matrix[2][2]) { return (Vector){ .x = rotation_matrix[0][0]*v.x + rotation_matrix[0][1]*v.y, .y = rotation_matrix[1][0]*v.x + rotation_matrix[1][1]*v.y, }; } Vector Vector_add(Vector v1, Vector v2) { return (Vector){ .x = v1.x + v2.x, .y = v1.y + v2.y, }; } void Vector_addAssign(Vector *v1, Vector v2) { v1->x += v2.x; v1->y += v2.y; } Vector Vector_sub(Vector v1, Vector v2) { return (Vector){ .x = v1.x - v2.x, .y = v1.y - v2.y, }; } Vector Vector_mul(Vector v1, double factor) { return (Vector){ .x = v1.x * factor, .y = v1.y * factor, }; } /* Body */ typedef struct _Body { double mass; double mu; // mass*G Vector pos; Vector vel; Vector acc; } Body; Vector Body_compute_acc_naive(Vector *pos, double mass, int others_len, Body *others) { Vector a = (Vector){.x = 0, .y = 0}; int i = 0; for (i = 0; i < others_len; ++i) { Vector dpos = Vector_sub(others[i].pos, *pos); // acc = force / mass = G*m1*m2/d**2 / m1 double grav_acc = (G * mass * others[i].mass) / pow(Vector_magnitude(dpos), 2) / mass; double angle = atan2(dpos.y, dpos.x); Vector_addAssign(&a, Vector_from_polar_rad(grav_acc, angle)); } return a; } Vector Body_compute_acc_optim(Vector *pos, double mass, int others_len, Body *others) { (void)mass; Vector a = (Vector){.x = 0, .y = 0}; int i = 0; for (i = 0; i < others_len; ++i) { Vector dpos = Vector_sub(others[i].pos, *pos); // x = grav_acc * dpos.x / mag(dpos) // = (mu / mag(dpos)**2) * dpos.x / mag(dpos) // = mu/mag(dpos)**3 * dpos.x double mag = Vector_magnitude(dpos); double mudist3 = others[i].mu / (mag*mag*mag); Vector_addAssign(&a, (Vector){ .x = mudist3*dpos.x, .y = mudist3*dpos.y, }); } return a; } void Body_update_acc_naive(Body *self, int others_len, Body *others) { self->acc = Body_compute_acc_naive(&self->pos, self->mass, others_len, others); } void Body_update_acc_optim(Body *self, int others_len, Body *others) { self->acc = Body_compute_acc_optim(&self->pos, self->mass, others_len, others); } void Body_update_state_euler(Body *self, int others_len, Body *others, double sim_dt) { (void)others_len; (void)others; Vector_addAssign(&self->vel, Vector_mul(self->acc, sim_dt)); Vector_addAssign(&self->pos, Vector_mul(self->vel, sim_dt)); } void Body_update_state_rk4(Body *self, int others_len, Body *others, double sim_dt) { Vector a1 = Body_compute_acc_optim(&self->pos, self->mass, others_len, others); Vector p2 = Vector_add(self->pos, Vector_mul(self->vel, sim_dt/2.0)); Vector a2 = Body_compute_acc_optim(&p2, self->mass, others_len, others); Vector v2 = Vector_add(self->vel, Vector_mul(a1, sim_dt/2.0)); Vector p3 = Vector_add(self->pos, Vector_mul(v2, sim_dt/2.0)); Vector a3 = Body_compute_acc_optim(&p3, self->mass, others_len, others); Vector v3 = Vector_add(self->vel, Vector_mul(a2, sim_dt/2.0)); Vector p4 = Vector_add(self->pos, Vector_mul(v3, sim_dt)); Vector a4 = Body_compute_acc_optim(&p4, self->mass, others_len, others); Vector v4 = Vector_add(self->vel, Vector_mul(a3, sim_dt)); Vector a = Vector_add( Vector_add( Vector_add( Vector_mul(a1, 1/6.0), Vector_mul(a2, 1/3.0) ), Vector_mul(a3, 1/3.0)), Vector_mul(a4, 1/6.0) ); Vector v = Vector_add( Vector_add( Vector_add( Vector_mul(self->vel, 1/6.0), Vector_mul(v2, 1/3.0) ), Vector_mul(v3, 1/3.0)), Vector_mul(v4, 1/6.0) ); Vector_addAssign(&self->vel, Vector_mul(a, sim_dt)); Vector_addAssign(&self->pos, Vector_mul(v, sim_dt)); } /* Funcs */ double barycenter_distance(double d, double m1, double m2) { return d * m2 / (m1 + m2); } double orbital_speed(double mass, double distance) { return sqrt(G*mass/abs(distance)); } /* Main */ int main(int ac, char **av) { const double EXPORT_DT_MS = EXPORT_DT*1000; // unit: ms print_time("pre (init)"); /* Args */ if (ac != 1+4) { printf("Usage: %s <injection_angle_deg:float> <injection_dv:float> <sim_step_per_sec:float> <integrator:euler-naive|euler-optim|rk4-optim>\n", av[0]); printf("Example: %s -123.7 3150 10 euler-naive\n", av[0]); exit(1); } double injection_angle_deg = atof(av[1]); double injection_dv = atof(av[2]); double sim_step_per_sec = atof(av[3]); Integrators flag_integrator = Integrators_from_str(av[4]); printf("Args\n"); printf("\tinjection_angle_deg: %.3f\n", injection_angle_deg); printf("\tinjection_dv: %.3f\n", injection_dv); printf("\tsim_step_per_sec: %.3f\n", sim_step_per_sec); printf("\tintegrator: %s\n", Integrators_to_str(flag_integrator)); printf("\n"); /* Vars */ double injection_angle = deg2rad(injection_angle_deg); Vector earth_pos = (Vector){.x=-barycenter_distance(EARTH_MOON_D, EARTH_MASS, MOON_MASS), .y=0.0}; Vector moon_pos = (Vector){.x=barycenter_distance(EARTH_MOON_D, MOON_MASS, EARTH_MASS), .y=0.0}; Vector spacecraft_pos = Vector_add(earth_pos, Vector_from_polar_rad(EARTH_RADIUS+INJECTION_ALTITUDE, injection_angle)); Vector earth_vel = Vector_from_polar_rad(Vector_magnitude(earth_pos)*ANGULAR_SPEED, -M_PI/2.0); Vector moon_vel = Vector_from_polar_rad(Vector_magnitude(moon_pos)*ANGULAR_SPEED, M_PI/2.0); double sc_s = orbital_speed(EARTH_MASS, Vector_magnitude(Vector_sub(spacecraft_pos, earth_pos))) + injection_dv; Vector spacecraft_vel = Vector_from_polar_rad(sc_s, injection_angle+M_PI/2.0); Body earthmoon[2] = { (Body){.mass=EARTH_MASS, .mu=G*EARTH_MASS, .pos=earth_pos, .vel=earth_vel, .acc=(Vector){.x=0, .y=0}}, (Body){.mass=MOON_MASS, .mu=G*MOON_MASS, .pos=moon_pos, .vel=moon_vel, .acc=(Vector){.x=0, .y=0}} }; Body *earth = &earthmoon[0]; Body *moon = &earthmoon[1]; Body *spacecraft = &(Body){ .mass=1.0, .mu=G*1.0, .pos=spacecraft_pos, .vel=spacecraft_vel, .acc=(Vector){.x=0, .y=0} }; double sim_dt = 1.0 / sim_step_per_sec; double sim_dt_ms = 1000.0 / sim_step_per_sec; double t_max_ms = SIM_DURATION * 1000.0; uint64_t export_ts = t_max_ms / EXPORT_DT_MS; Vector **poss = malloc(sizeof(Vector*)*export_ts); if (poss == NULL) { exit(43); } uint64_t i = 0; for (i = 0; i < export_ts; ++i) { poss[i] = malloc(sizeof(Vector)*3); if (poss[i] == NULL) { exit(44); } } if ( ! (EXPORT_DT_MS >= sim_dt_ms)) { fprintf(stderr, "Error: sim_dt_ms must be lower than EXPORT_DT_MS\n"); exit(1); } /* Prints */ printf("Pos\n"); printf("\tEarth: Vector(%f, %f)\n", earth->pos.x, earth->pos.y); printf("\tMoon: Vector(%f, %f)\n", moon->pos.x, moon->pos.y); printf("\tSpacecraft: Vector(%f, %f)\n", spacecraft->pos.x, spacecraft->pos.y); printf("Vel\n"); printf("\tEarth: Vector(%f, %f)\n", earth->vel.x, earth->vel.y); printf("\tMoon: Vector(%f, %f)\n", moon->vel.x, moon->vel.y); printf("\tSpacecraft: Vector(%f, %f)\n", spacecraft->vel.x, spacecraft->vel.y); // printf("Grav\n"); // printf("\tMoon <- Earth: Vector(%f, %f)\n", Body_grav_acc_from(&earth, &moon).x, Body_grav_acc_from(&earth, &moon).y); // printf("\tEarth <- Moon: Vector(%f, %f)\n", Body_grav_acc_from(&moon, &earth).x, Body_grav_acc_from(&moon, &earth).y); // printf("\tEarth <- Spacecraft: Vector(%f, %f)\n", Body_grav_acc_from(&spacecraft, &earth).x, Body_grav_acc_from(&spacecraft, &earth).y); // printf("\tMoon <- Spacecraft: Vector(%f, %f)\n", Body_grav_acc_from(&spacecraft, &moon).x, Body_grav_acc_from(&spacecraft, &moon).y); printf("\n"); printf("Miscs\n"); printf("\tsim_dt: %.3f\n", sim_dt); printf("\tsim_dt_ms: %.3f\n", sim_dt_ms); printf("\tt_max_ms: %.3f\n", t_max_ms); printf("\texport_ts: %lu\n", export_ts); printf("\n"); print_time("pre (var + prints)"); /* Sim */ double last_sim_ms = 0; double last_export_ms = 0; while (last_sim_ms < t_max_ms) { switch (flag_integrator) { case IntegratorsEulerNaive: Body_update_acc_naive(earth, 1, moon); Body_update_acc_naive(moon, 1, earth); Body_update_acc_naive(spacecraft, 2, earthmoon); break; case IntegratorsEulerOptim: case IntegratorsRk4Optim: Body_update_acc_optim(earth, 1, moon); Body_update_acc_optim(moon, 1, earth); Body_update_acc_optim(spacecraft, 2, earthmoon); break; } switch (flag_integrator) { case IntegratorsEulerNaive: case IntegratorsEulerOptim: Body_update_state_euler(earth, 1, moon, sim_dt); Body_update_state_euler(moon, 1, earth, sim_dt); Body_update_state_euler(spacecraft, 2, earthmoon, sim_dt); break; case IntegratorsRk4Optim: Body_update_state_rk4(earth, 1, moon, sim_dt); Body_update_state_rk4(moon, 1, earth, sim_dt); Body_update_state_rk4(spacecraft, 2, earthmoon, sim_dt); break; } if (last_sim_ms >= last_export_ms) { double alpha = -ANGULAR_SPEED * last_sim_ms/1000.0; double r[2][2] = { {cos(alpha), -sin(alpha)}, {sin(alpha), cos(alpha)}, }; uint64_t index = last_export_ms/EXPORT_DT_MS; poss[index][0] = Vector_rotate(earth->pos, r); poss[index][1] = Vector_rotate(moon->pos, r); poss[index][2] = Vector_rotate(spacecraft->pos, r); last_export_ms += EXPORT_DT_MS; } last_sim_ms += sim_dt_ms; } print_time("sim"); printf("poss size: %lu\n", export_ts); Vector scp = poss[export_ts-1][2]; printf("spacecraft pos %.0f %.0f\n", scp.x, scp.y); /* Export */ FILE *f = NULL; f = fopen("out.txt", "w"); if (f == NULL) { exit(45); } fprintf(f, "t earth_x earth_y moon_x moon_y spacecraft_x spacecraft_y\n"); for (i = 0; i < export_ts; ++i) { Vector ep = poss[i][0]; Vector mp = poss[i][1]; Vector scp = poss[i][2]; fprintf(f, "%lu %.0f %.0f %.0f %.0f %.0f %.0f\n", i * EXPORT_DT, ep.x, ep.y, mp.x, mp.y, scp.x, scp.y ); // format: 1 sec // writeln : 2 secs // total: 4 secs } fclose(f); print_time("post (export)"); return 0; }
Become a Patron
Sponsor on GitHub
Donate via PayPal
Source on GitHub
Mailing list
Installed libraries
Wiki
Report an issue
How it works
Contact the author
CE on Mastodon
CE on Bluesky
About the author
Statistics
Changelog
Version tree