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
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.15.2
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
export fn absdiff1(a: usize, b: usize) usize { return @max(a, b) - @min(a, b); } export fn absdiff2(a: usize, b: usize) usize { if (a > b) { return a - b; } return b - a; } export fn absdiff3(a: usize, b: usize) usize { return @max(a -| b, b -| a); } export fn absdiff4(a: usize, b: usize) usize { return @min(a -% b, b -% a); } // Only works for numbers with known size. // From mentioned versions it is the only one which uses `a` and `b` expressions once so it is theoretically usable as inline expression. export fn absdiff5(a: u32, b: u32) u32 { return @intCast(@abs(@as(i33, a) - @as(i33, b))); } const builtin = @import("builtin"); export fn absdiff_asm(a: usize, b: usize) usize { switch (builtin.cpu.arch) { .aarch64 => { return asm volatile ( \\ subs x0, x0, x1 \\ cneg x0, x0, cc : [ret] "={x0}" (-> usize), : [inp_a] "{x0}" (a), [inp_b] "{x1}" (b), : .{ .nzcv = true }); }, .arm => { return asm volatile ( \\ subs r0, r0, r1 \\ rsbcc r0, r0, #0 : [ret] "={r0}" (-> usize), : [inp_a] "{r0}" (a), [inp_b] "{r1}" (b), : .{ .apsr = true }); }, .x86_64 => { // mov rax, rsi // sub rax, rdi // sub rdi, rsi // cmovae rax, rdi return asm volatile ( \\ movq %%rsi, %%rax \\ subq %%rdi, %%rax \\ subq %%rsi, %%rdi \\ cmovae %%rdi, %%rax : [ret] "={rax}" (-> usize), : [inp_a] "{rdi}" (a), [inp_b] "{rsi}" (b), : .{ .cc = true }); }, .x86 => { // ecx = a - b // eax = b - a // mov ecx, dword ptr [ebp + 8] // mov eax, dword ptr [ebp + 12] // sub eax, dword ptr [ebp + 8] // sub ecx, dword ptr [ebp + 12] // cmovb eax, ecx return asm volatile ( \\ mov 8(%%ebp), %%ecx \\ mov 12(%%ebp), %%eax \\ sub 8(%%ebp), %%eax \\ sub 12(%%ebp), %%ecx \\ cmovc %%ecx, %[ret] : [ret] "={eax}" (-> usize), : : .{ .cc = true, .ecx = true }); }, else => @compileError("TODO: add support for arch: " ++ @tagName(builtin.cpu.arch)), } }
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