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
OpenCL C
Pascal
Pony
Python
Racket
Ruby
Rust
Snowball
Scala
Solidity
Spice
SPIR-V
Swift
LLVM TableGen
Toit
TypeScript Native
V
Vala
Visual Basic
WASM
Zig
Javascript
GIMPLE
Ygen
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.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
pub fn Aecs( /// Model description of the form: /// ```zig /// struct { /// archetype_a: [capacity_a]struct { /// component_a: A, /// component_b: B, /// component_c: C, /// }, /// archetype_b: [capacity_b]struct { /// component_a: A, /// component_b: B, /// component_d: D, /// component_e: E, /// }; /// } /// ``` /// Where: /// - each top-level field names an archetype, /// - each field declared directly under an archetype is a component of that archetype, /// - and the capacity is the maximum number of entities of this kind which can be allocated. comptime config: type, ) type { const M, const O, const I = comptime typ: { const info = @typeInfo(config).@"struct"; var archetypes: [info.fields.len]StructField = undefined; for (&archetypes, info.fields) |*archetype, source| { const array = @typeInfo(source.type).array; const fields = @typeInfo(array.child).@"struct".fields; var components: [fields.len]StructField = undefined; for (&components, fields) |*component, field| { component.* = .{ .name = field.name, .type = [array.len]field.type, .default_value = null, .alignment = @alignOf([array.len]field.type), .is_comptime = false, }; } const Component = @Type(.{ .@"struct" = .{ .layout = .@"extern", .fields = &components, .decls = &.{}, .is_tuple = false, } }); archetype.* = .{ .name = source.name, .type = Component, .default_value = null, .alignment = @alignOf(Component), .is_comptime = false, }; } const M = @Type(.{ .@"struct" = .{ .layout = .@"extern", .fields = &archetypes, .decls = &.{}, .is_tuple = false, } }); var offset_fields: [info.fields.len]StructField = undefined; for (&offset_fields, archetypes) |*offset, archetype| { offset.* = .{ .name = archetype.name, .type = u16, .default_value = &@as(u16, 0), .alignment = 2, .is_comptime = false, }; } const last = archetypes[archetypes.len - 1]; const fields = @typeInfo(last.type).@"struct".fields; const distance = @offsetOf(M, last.name) + @offsetOf(last.type, fields[fields.len - 1].name); const I = if (distance > 0xffff) u32 else u16; const O = @Type(.{ .@"struct" = .{ .layout = .@"extern", .fields = &offset_fields, .decls = &.{}, .is_tuple = false, } }); break :typ .{ M, O, I }; }; return struct { len: Offset, pub const empty: @This() = .{ .len = mem.zeroes(Offset) }; pub const Model = extern struct { data: M, pub const Data = M; pub const empty: Model = .{ .data = mem.zeroes(M) }; }; pub const Offset = O; pub fn apply(aecs: *const @This(), model: *Model, context: anytype) void { const info = @typeInfo(@TypeOf(context)); const Context = info.pointer.child; const parameters = @typeInfo(Context.spec).@"struct".fields; const model_info = @typeInfo(Model.Data).@"struct"; const Tuple = comptime typ: { var fields: [parameters.len + 1]StructField = undefined; fields[0] = .{ .name = "0", .type = *Context, .default_value = null, .alignment = @alignOf(*Context), .is_comptime = false, }; for (fields[1..], parameters, 1..) |*field, param, i| field.* = .{ .name = std.fmt.comptimePrint("{d}", .{i}), .type = param.type, .default_value = null, .alignment = @alignOf(param.type), .is_comptime = false, }; break :typ @Type(.{ .@"struct" = .{ .layout = .auto, .fields = &fields, .decls = &.{}, .is_tuple = true, } }); }; const indices, const offsets = comptime data: { var indices: [model_info.fields.len]u16 = undefined; var offsets: [model_info.fields.len][parameters.len]I = undefined; var found: u16 = 0; search: for (model_info.fields, 0..) |archetype, index| { const base = @offsetOf(Model.Data, archetype.name); for (&offsets[found], parameters) |*offset, param| { if (!@hasField(archetype.type, param.name)) continue :search; offset.* = base + @offsetOf(archetype.type, param.name); } indices[found] = index; found += 1; } const ind = indices[0..found].*; const off = offsets[0..found].*; break :data .{ ind, off }; }; var length: [indices.len]u16 = undefined; const len_info: [*]const u16 = @ptrCast(&aecs.len); const bytes: [*]u8 = @ptrCast(model); for (&length, indices) |*len, index| len.* = len_info[index]; for (offsets, length) |base, len| { var tuple: Tuple = undefined; tuple[0] = context; inline for (base, 1..) |offset, index| { tuple[index].ptr = @alignCast(@ptrCast(bytes + offset)); tuple[index].len = len; } @call(.auto, Context.update, tuple); } } }; } const Ecs = Aecs(struct { player: [40]extern struct { x: f32, y: f32, z: f32, r: f32, pid: u32, }, enemy: [40]extern struct { x: f32, y: f32, z: f32, r: f32, foo: u32, }, coin: [10]extern struct { value: u16, }, mob: [20]extern struct { x: f32, y: f32, z: f32, r: f32, }, }); const System = struct { pub const deps = .{ .damage, .move }; pub const spec = struct { x: []const f32, y: []const f32, z: []const f32, r: []f32, }; pub fn update(_: *@This(), noalias xs: []const f32, noalias ys: []const f32, noalias zs: []const f32, noalias rs: []f32) void { for (rs, xs, ys, zs) |*r, x, y, z| { r.* = x + y + z; } } }; //export fn example(ecs: *Ecs, model: *Ecs.Model, system: *System) void { // ecs.apply(model, system); //} export fn table(ecs: *Ecs, model: *Ecs.Model, system: *System) void { ecs.apply(model, system); } const std = @import("std"); const mem = std.mem; const testing = std.testing; const StructField = std.builtin.Type.StructField;
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
About the author
Statistics
Changelog
Version tree