Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
estella_abi.h
Go to the documentation of this file.
1/* GENERATED by @estella/compiler — do not edit. */
2/* The Estella ABI, C side. */
3#ifndef ESTELLA_ABI_H
4#define ESTELLA_ABI_H
5
6#include <stddef.h>
7#include <stdint.h>
8#include <string.h>
9#include <math.h>
10
11/* A fused multiply-add is MORE accurate than the double rounding JS does, and
12 more accurate is still a different number. GCC does not implement this pragma
13 and warns about it, so the flag in CFLAGS is the one that has to be there. */
14#if defined(__clang__)
15#pragma STDC FP_CONTRACT OFF
16#endif
17
18/* An address is a POINTER, and on wasm32 a pointer IS a 32-bit offset into
19 linear memory — which is why one emitted source serves both back ends. The
20 OFFSETS below are the contract; the address width is the machine, and it is
21 mixed into the handshake so a 32-bit artifact cannot load into a 64-bit host.
22
23 ES_ADDR_BASE is the other model: a host that owns one block and addresses it
24 by offset. A wasm2c deployment is one, and so is the differential
25 harness. Same structs, same offsets, one word wider or narrower. */
26#if defined(ES_ADDR_BASE)
27typedef uint32_t es_addr_t;
28extern unsigned char *es_memory;
29#define ES_PTR(a) (es_memory + (a))
30#else
31typedef uintptr_t es_addr_t;
32#define ES_PTR(a) ((unsigned char *)(a))
33#endif
34
35/* A host that LOADS this asks the platform for a symbol by name, and on
36 Windows nothing is exported from a DLL unless it says so — the module
37 builds, loads, and answers null to every lookup. A wasm link names its
38 exports on the command line instead, so this is inert there. */
39#if defined(_WIN32)
40#define ES_EXPORT __declspec(dllexport)
41#elif defined(__GNUC__)
42#define ES_EXPORT __attribute__((visibility("default")))
43#else
44#define ES_EXPORT
45#endif
46
47/* The packed rows the host materialised: for each row, the entity and
48 then one base address per component the query named, in that order. */
53
54/* A command: four u32, sixteen bytes, at every address width. */
55typedef struct EsCmd {
56 uint32_t kind;
57 uint32_t a;
58 uint32_t b;
59 uint32_t c;
61
62#define ES_CMD_DESPAWN 1u
63#define ES_CMD_REMOVE 2u
64
65/* 6 addresses, and a number that needs a reason to grow. */
74
75/* What a host needs in order to CALL a system without glue written by hand.
76 The compiler knows a system's queries and resources -- that is what _params
77 is -- so it says so here rather than leaving each host to be told twice. */
78/* Where a system APPENDS events: f64 records, because a payload is fields and
79 not four words. A record is [writer slot, ...fields], and how long one is
80 comes from the manifest — the host declared it.
81
82 Its own queue rather than a wider EsCmd: fixed-width records and
83 variable-width ones cannot share a buffer without stride guesswork.
84
85 3 addresses. A host lays this block out from that same
86 number, so a field appended here without it is a header one side walks past. */
87typedef struct EsEventOut {
88 es_addr_t buf; /* double * */
89 es_addr_t cap; /* capacity, in doubles */
90 es_addr_t count; /* address of the used-doubles word */
92
93typedef struct EsQueryDecl {
94 const char *const *comps; /* component names, in the order the query names them */
95 const unsigned char *mut; /* 1 where the system may write that component */
96 uint32_t count;
98
99typedef struct EsSystemDecl {
100 const char *name;
101 void (*fn)(es_addr_t);
103 const char *const *resources;
104 /* 1 where the system declared ResMut: a host that MIRRORS a resource into
105 the block has to write that block back, or the write is dropped. */
106 const unsigned char *resourcesMut;
107 uint32_t queryCount;
110
111/* The C and TS sides of these structs have ONE author, and a side that drifts
112 from it must fail to compile. The numbers below are written by that author
113 (sdk/src/ecs/aot/abiDigest.ts, through compiler/src/abi.ts) into this file;
114 these four declarations are how C says no if a struct above ever stops
115 matching them. Every struct a host addresses by word owes one: EsEventOut
116 went without for as long as it existed, and a fourth field would have been
117 read as the next struct by whichever side had not been told. */
118typedef char es_check_sysctx[sizeof(EsSysCtx) == 6 * sizeof(es_addr_t) ? 1 : -1];
119typedef char es_check_rows[sizeof(EsQueryRows) == 2 * sizeof(es_addr_t) ? 1 : -1];
120typedef char es_check_cmd[sizeof(EsCmd) == 4 * 4 ? 1 : -1];
121typedef char es_check_eventout[sizeof(EsEventOut) == 3 * sizeof(es_addr_t) ? 1 : -1];
122
123/* memcpy, not a cast through a typed pointer: a component base is whatever the
124 host handed over, and a wider load through an under-aligned pointer is
125 undefined even where the hardware would have allowed it. */
126static inline double es_f32(const unsigned char *p) { float v; memcpy(&v, p, 4); return (double)v; }
127static inline void es_set_f32(unsigned char *p, double v) { float f = (float)v; memcpy(p, &f, 4); }
128static inline double es_f64(const unsigned char *p) { double v; memcpy(&v, p, 8); return v; }
129static inline void es_set_f64(unsigned char *p, double v) { memcpy(p, &v, 8); }
130
131/* A bool in the C++ structs is ONE byte. Reading it as a float would read three
132 bytes of whatever follows it, which is a value rather than an error. */
133static inline int es_bool(const unsigned char *p) { return *p != 0; }
134/* One question of a service, as the host mirrored it: a byte and a mask. */
135static inline int es_bit(const unsigned char *p, unsigned bit) { return (*p >> bit) & 1u; }
136static inline void es_set_bool(unsigned char *p, int v) { *p = (unsigned char)(v ? 1 : 0); }
137
138/* ---------------------------------------------------------------------------
139 Math. Only the operations ECMAScript specifies EXACTLY are in the subset, but
140 "exactly specified" is not the same as "what libm does" — three of them
141 differ, and each difference is one bit of one pixel that would take a day to
142 find. So each is written out rather than forwarded.
143 --------------------------------------------------------------------------- */
144static inline double es_abs(double x) { return fabs(x); }
145static inline double es_floor(double x) { return floor(x); }
146static inline double es_ceil(double x) { return ceil(x); }
147static inline double es_trunc(double x) { return trunc(x); }
148static inline double es_sqrt(double x) { return sqrt(x); }
149
150/* ---------------------------------------------------------------------------
151 Trigonometry, SPECIFIED. Math.sin is implementation-defined, so an
152 interpreted build and a compiled one may both be right and disagree — which
153 for a differential pixel gate is a red frame with no bug behind it.
154
155 These are the same range reduction, the same polynomial and the same ORDER as
156 sdk/src/math/exact.ts, line for line. Reordering either side is a change to
157 the result, and a differential compares them bit for bit.
158
159 The COEFFICIENTS come from that file, written in below: what is implemented
160 twice here is the structure, which a differential can compare, and not
161 fifteen decimals, which retyping only puts at risk.
162 --------------------------------------------------------------------------- */
163#define ES_PIO2_HI 1.5707963267341256
164#define ES_PIO2_LO 6.077100506506192e-11
165#define ES_TWO_OVER_PI 0.6366197723675814
166
167static inline double es_kernel_sin(double x) {
168 double z = x * x;
169 double r = -0.16666666666666632 + z * (0.008333333333324894
170 + z * (-0.0001984126982985795 + z * (0.000002755731370707007
171 + z * (-2.505076025340686e-8 + z * 1.58969099521155e-10))));
172 return x + x * z * r;
173}
174
175static inline double es_kernel_cos(double x) {
176 double z = x * x;
177 double r = 0.0416666666666666 + z * (-0.001388888888887411
178 + z * (0.00002480158728947673 + z * (-2.7557314351390663e-7
179 + z * (2.087572321298175e-9 + z * -1.1359647557788195e-11))));
180 return 1.0 - 0.5 * z + z * z * r;
181}
182
183/* Math.round's rule, as floor(x + 0.5) rather than es_round: the TS side
184 reduces with that same expression, and the two must agree on a tie. */
185static inline double es_trig_quadrant(double x, double *r) {
186 double n = floor(x * ES_TWO_OVER_PI + 0.5);
187 *r = (x - n * ES_PIO2_HI) - n * ES_PIO2_LO;
188 double q = fmod(n, 4.0);
189 if (q < 0.0) q += 4.0;
190 return q;
191}
192
193static inline double es_exact_sin(double x) {
194 if (!isfinite(x)) return (double)NAN;
195 double r;
196 double q = es_trig_quadrant(x, &r);
197 if (q == 0.0) return es_kernel_sin(r);
198 if (q == 1.0) return es_kernel_cos(r);
199 if (q == 2.0) return -es_kernel_sin(r);
200 return -es_kernel_cos(r);
201}
202
203static inline double es_exact_cos(double x) {
204 if (!isfinite(x)) return (double)NAN;
205 double r;
206 double q = es_trig_quadrant(x, &r);
207 if (q == 0.0) return es_kernel_cos(r);
208 if (q == 1.0) return -es_kernel_sin(r);
209 if (q == 2.0) return -es_kernel_cos(r);
210 return es_kernel_sin(r);
211}
212
213/* Math.sign returns x itself for NaN and for both zeroes, so the sign of zero
214 survives — and the sign of zero is a distinct bit pattern in the image. */
215static inline double es_sign(double x) {
216 if (isnan(x) || x == 0.0) return x;
217 return x > 0.0 ? 1.0 : -1.0;
218}
219
220/* C's round() breaks ties AWAY from zero; ECMAScript breaks them toward +Inf.
221 And floor(x + 0.5) — the usual fix — is wrong for the double just below 0.5,
222 where the addition itself rounds up to 1. Subtracting the floor is exact for
223 every finite x, so the comparison sees the true fraction. */
224static inline double es_round(double x) {
225 if (!isfinite(x) || x == 0.0) return x;
226 double f = floor(x);
227 double r = (x - f) < 0.5 ? f : f + 1.0;
228 /* Math.round(-0.2) is -0, and -0 is not the same bytes as +0. */
229 return (r == 0.0 && x < 0.0) ? -0.0 : r;
230}
231
232/* fmin/fmax return the non-NaN operand and are free to pick either zero;
233 Math.min/max propagate NaN and distinguish -0 from +0. */
234static inline double es_min(double a, double b) {
235 if (isnan(a) || isnan(b)) return (double)NAN;
236 if (a == b) return signbit(a) ? a : b;
237 return a < b ? a : b;
238}
239static inline double es_max(double a, double b) {
240 if (isnan(a) || isnan(b)) return (double)NAN;
241 if (a == b) return signbit(a) ? b : a;
242 return a > b ? a : b;
243}
244
245/* JS % on doubles is the truncated remainder, which is fmod. */
246static inline double es_rem(double a, double b) { return fmod(a, b); }
247
248#endif /* ESTELLA_ABI_H */
static double es_max(double a, double b)
char es_check_sysctx[sizeof(EsSysCtx)==6 *sizeof(es_addr_t) ? 1 :-1]
static double es_sign(double x)
static double es_min(double a, double b)
static double es_kernel_sin(double x)
static void es_set_f64(unsigned char *p, double v)
#define ES_PIO2_HI
char es_check_cmd[sizeof(EsCmd)==4 *4 ? 1 :-1]
char es_check_eventout[sizeof(EsEventOut)==3 *sizeof(es_addr_t) ? 1 :-1]
static double es_abs(double x)
static void es_set_bool(unsigned char *p, int v)
char es_check_rows[sizeof(EsQueryRows)==2 *sizeof(es_addr_t) ? 1 :-1]
#define ES_TWO_OVER_PI
static double es_exact_cos(double x)
static double es_kernel_cos(double x)
static void es_set_f32(unsigned char *p, double v)
#define ES_PIO2_LO
static int es_bit(const unsigned char *p, unsigned bit)
static double es_ceil(double x)
static double es_f64(const unsigned char *p)
static double es_rem(double a, double b)
static int es_bool(const unsigned char *p)
static double es_trunc(double x)
static double es_trig_quadrant(double x, double *r)
static double es_f32(const unsigned char *p)
uintptr_t es_addr_t
Definition estella_abi.h:31
static double es_exact_sin(double x)
static double es_floor(double x)
static double es_round(double x)
static double es_sqrt(double x)
uint32_t c
Definition estella_abi.h:59
uint32_t b
Definition estella_abi.h:58
uint32_t a
Definition estella_abi.h:57
uint32_t kind
Definition estella_abi.h:56
es_addr_t cap
Definition estella_abi.h:89
es_addr_t count
Definition estella_abi.h:90
es_addr_t buf
Definition estella_abi.h:88
const char *const * comps
Definition estella_abi.h:94
const unsigned char * mut
Definition estella_abi.h:95
uint32_t count
Definition estella_abi.h:96
es_addr_t count
Definition estella_abi.h:51
es_addr_t rows
Definition estella_abi.h:50
es_addr_t events
Definition estella_abi.h:72
es_addr_t cmdCap
Definition estella_abi.h:70
es_addr_t resources
Definition estella_abi.h:68
es_addr_t cmdBuf
Definition estella_abi.h:69
es_addr_t cmdCount
Definition estella_abi.h:71
es_addr_t queries
Definition estella_abi.h:67
const unsigned char * resourcesMut
const char * name
void(* fn)(es_addr_t)
uint32_t resourceCount
const char *const * resources
const EsQueryDecl * queries
uint32_t queryCount