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
const std = @import("std"); // Type your code here, or load an example. export fn square(num: i32) i32 { return num * num; } // Fundamental types pub const F32x2 = @Vector(2, f32); pub const F32x4 = @Vector(4, f32); pub const F32x8 = @Vector(8, f32); pub const F32x16 = @Vector(16, f32); pub const Boolx4 = @Vector(4, bool); pub const Boolx8 = @Vector(8, bool); pub const Boolx16 = @Vector(16, bool); // "Higher-level" aliases pub const Vec = F32x4; pub const Mat = [4]F32x4; pub const Quat = F32x4; pub const Vec2 = F32x2; pub const Mat2 = [2]F32x2; pub inline fn f32x2(e0: f32, e1: f32) F32x2 { return .{ e0, e1 }; } pub inline fn f32x4(e0: f32, e1: f32, e2: f32, e3: f32) F32x4 { return .{ e0, e1, e2, e3 }; } pub const F32x4Component = enum { x, y, z, w }; pub inline fn swizzle( v: F32x4, comptime x: F32x4Component, comptime y: F32x4Component, comptime z: F32x4Component, comptime w: F32x4Component, ) F32x4 { return @shuffle(f32, v, undefined, [4]i32{ @intFromEnum(x), @intFromEnum(y), @intFromEnum(z), @intFromEnum(w) }); } pub const F32x2Component = enum { x, y }; pub inline fn swizzle2( v: F32x2, comptime x: F32x2Component, comptime y: F32x2Component, ) F32x2 { return @shuffle(f32, v, undefined, [2]i32{ @intFromEnum(x), @intFromEnum(y) }); } pub fn dot4Old(v0: Vec, v1: Vec) F32x4 { var xmm0 = v0 * v1; // | x0*x1 | y0*y1 | z0*z1 | w0*w1 | //var xmm1 = swizzle(xmm0, .y, .x, .w, .z); // | y0*y1 | -- | w0*w1 | -- | var xmm1 = @shuffle(f32, xmm0, undefined, [4]i32{1, 0, 3, 2}); // | y0*y1 | -- | w0*w1 | -- | xmm1 = xmm0 + xmm1; // | x0*x1 + y0*y1 | x0*x1 + y0*y1 | z0*z1 + w0*w1 | z0*z1 + w0*w1 | xmm0 = swizzle(xmm1, .w, .z, .y, .x); // | z0*z1 + w0*w1 | z0*z1 + w0*w1 | x0*x1 + y0*y1 | x0*x1 + y0*y1 | xmm0 = xmm0 + xmm1; return xmm0; } pub fn dot4(v0: Vec, v1: Vec) F32x4 { const xmm0 = v0 * v1; // | x0*x1 | y0*y1 | z0*z1 | w0*w1 | // var xmm1 = swizzle(xmm0, .y, .x, .w, .z); // | y0*y1 | -- | w0*w1 | -- | // xmm1 = xmm0 + xmm1; // | x0*x1 + y0*y1 | x0*x1 + y0*y1 | z0*z1 + w0*w1 | z0*z1 + w0*w1 | // xmm0 = swizzle(xmm1, .w, .z, .y, .x); // | z0*z1 + w0*w1 | z0*z1 + w0*w1 | x0*x1 + y0*y1 | x0*x1 + y0*y1 | // xmm0 = xmm0 + xmm1; return @splat(xmm0[0] + xmm0[1] + xmm0[2] + xmm0[3]); } pub fn dot2Vec2(v0: Vec2, v1: Vec2) F32x2 { var xmm0 = v0 * v1; // | x0*x1 | y0*y1 | const xmm1 = swizzle2(xmm0, .y, .x); // | y0*y1 | -- | xmm0 = f32x2(xmm0[0] + xmm1[0], xmm0[1]); // | x0*x1 + y0*y1 | -- | return swizzle2(xmm0, .x, .x); } pub fn dot2Vec2Better(v0: Vec2, v1: Vec2) F32x2 { const mul = v0 * v1; return @splat(mul[0] + mul[1]); } pub fn main() !void { // Prints to stderr (it's a shortcut based on `std.io.getStdErr()`) std.debug.print("All your {s} are belong to us.\n", .{"codebase"}); const vec = f32x2(1, 2); const vec2 = f32x2(3, 4); const dot1 = dot2Vec2(vec, vec2); const dot2 = dot2Vec2Better(vec, vec2); const vec3 = f32x4(1, 2, 3, 4); const vec4 = f32x4(5, 6, 7, 8); const dot3 = dot4Old(vec3, vec4); const dot4s = dot4(vec3, vec4); std.debug.print("results {d} {d} {d} {d}", .{dot1, dot2, dot3, dot4s}); }
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