Thanks for using Compiler Explorer
Sponsors
Jakt
C++
Ada
Algol68
Analysis
Android Java
Android Kotlin
Assembly
C
C3
Carbon
C with Coccinelle
C++ with Coccinelle
C++ (Circle)
CIRCT
Clean
CMake
CMakeScript
COBOL
C++ for OpenCL
MLIR
Cppx
Cppx-Blue
Cppx-Gold
Cpp2-cppfront
Crystal
C#
CUDA C++
D
Dart
Elixir
Erlang
Fortran
F#
GLSL
Go
Haskell
HLSL
Hook
Hylo
IL
ispc
Java
Julia
Kotlin
LLVM IR
LLVM MIR
Modula-2
Mojo
Nim
Numba
Nix
Objective-C
Objective-C++
OCaml
Odin
OpenCL C
Pascal
Pony
PTX
Python
Racket
Raku
Ruby
Rust
Sail
Snowball
Scala
Slang
Solidity
Spice
SPIR-V
Swift
LLVM TableGen
Toit
Triton
TypeScript Native
V
Vala
Visual Basic
Vyper
WASM
Zig
Javascript
GIMPLE
Ygen
sway
zig 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
zig 0.10.0
zig 0.11.0
zig 0.12.0
zig 0.12.1
zig 0.13.0
zig 0.14.0
zig 0.14.1
zig 0.15.1
zig 0.2.0
zig 0.3.0
zig 0.4.0
zig 0.5.0
zig 0.6.0
zig 0.7.0
zig 0.7.1
zig 0.8.0
zig 0.9.0
zig trunk
Options
Source code
// Type your code here, or load an example. const std = @import("std"); pub fn main() void { // The highest possible dec value. const a0: i128 = 170141183460469231731687303715884105727; // b is 2*pi as a dec. // That said, b1 is fraction and reprensents 64 bits of precision past what dec normally supports. const b0: u64 = 0b0101011100110010010110111011111101000100011001001001001110101100; const b1: u64 = 0b1110110011011111100101111111000111001010111000100101011111110111; std.debug.print("b0: {}\n", .{b0}); std.debug.print("b1: {}\n", .{b1}); // This can be converted into a multiplication with 1/(b0+1). // That may be faster, but might not due to needing to emulate 256 bit math. const q0 = @divTrunc(a0, b0+1); std.debug.print("q0: {}\n", .{q0}); const upper = q0*b0; var lower: u128 = undefined; const overflow = @mulWithOverflow(u128, q0, b1, &lower); std.debug.print("{s}\n", .{if(overflow) "overflow" else "nope"}); const full = upper + @intCast(i128, lower>>64) + (if (overflow) 1<<64 else 0); std.debug.print("{}\n", .{@intCast(i128, lower>>64) + (if (overflow) 1<<64 else 0)}); std.debug.print("{}\n", .{upper}); std.debug.print("{}\n", .{full}); // Is this final mod needed? I think not, but am not 100% convinced. // Also, is the intCast to u64 always correct, if so, it makes the final mod faster. // Again, I think so, but am not 100% sure. std.debug.print("{} vs {} \n", .{@mod(@intCast(u64, a0 - full), b0), @mod(a0, b0-1)}); std.debug.print("{} vs {} \n", .{a0 - full, @mod(a0, b0-1)}); }
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