A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
fix(abi): the C half's trig coefficients come from the one author
esengine1cd94db2014b
diff --git a/compiler/src/codegen.ts b/compiler/src/codegen.tsindex 17effda1f..acdd7c1b7 100644--- a/compiler/src/codegen.ts+++ b/compiler/src/codegen.ts@@ -28,6 +28,7 @@* than what JS computes, and more accurate is still different.*/import { resourceMethodBit } from '../../sdk/src/ecs/resourceShapes';+import { EXACT_COEFFICIENTS as EX } from '../../sdk/src/math/exact';import {CMD_WORDS, QUERYROWS_WORDS, SYSCTX_WORDS, EVENT_OUT_WORDS, CMD_DESPAWN, CMD_REMOVE,abiHandshake, planFor,@@ -261,24 +262,28 @@ static inline double es_sqrt(double x) { return sqrt(x); }These are the same range reduction, the same polynomial and the same ORDER assdk/src/math/exact.ts, line for line. Reordering either side is a change tothe result, and a differential compares them bit for bit.++ The COEFFICIENTS come from that file, written in below: what is implemented+ twice here is the structure, which a differential can compare, and not+ fifteen decimals, which retyping only puts at risk.--------------------------------------------------------------------------- */-#define ES_PIO2_HI 1.5707963267341256-#define ES_PIO2_LO 6.077100506506192e-11-#define ES_TWO_OVER_PI 0.6366197723675814+#define ES_PIO2_HI ${num(EX.PIO2_HI)}+#define ES_PIO2_LO ${num(EX.PIO2_LO)}+#define ES_TWO_OVER_PI ${num(EX.TWO_OVER_PI)}static inline double es_kernel_sin(double x) {double z = x * x;- double r = -1.6666666666666632e-01 + z * (8.333333333324894e-03- + z * (-1.984126982985795e-04 + z * (2.755731370707007e-06- + z * (-2.505076025340686e-08 + z * 1.5896909952115501e-10))));+ double r = ${num(EX.S1)} + z * (${num(EX.S2)}+ + z * (${num(EX.S3)} + z * (${num(EX.S4)}+ + z * (${num(EX.S5)} + z * ${num(EX.S6)}))));return x + x * z * r;}static inline double es_kernel_cos(double x) {double z = x * x;- double r = 4.1666666666666602e-02 + z * (-1.3888888888874109e-03- + z * (2.4801587289476730e-05 + z * (-2.7557314351390663e-07- + z * (2.0875723212981748e-09 + z * -1.1359647557788195e-11))));+ double r = ${num(EX.C1)} + z * (${num(EX.C2)}+ + z * (${num(EX.C3)} + z * (${num(EX.C4)}+ + z * (${num(EX.C5)} + z * ${num(EX.C6)}))));return 1.0 - 0.5 * z + z * z * r;}diff --git a/compiler/tests/exact-trig.test.ts b/compiler/tests/exact-trig.test.tsindex 86e45f811..5a6b21343 100644--- a/compiler/tests/exact-trig.test.ts+++ b/compiler/tests/exact-trig.test.ts@@ -110,6 +110,42 @@ describe('the engine\'s trigonometry, on both sides of the compiler', () => {});});+/**+ * Where the C half's numbers come from. What is implemented twice is the+ * STRUCTURE, which the differential above compares; the coefficients are data,+ * and a retyped copy of them is a different curve with no error and — on a+ * machine with no compiler — nothing above running to notice.+ */+describe('the specified trigonometry has one author for its constants', () => {+ /** Not coefficients: the parts of the ALGORITHM that happen to be numbers —+ * the Taylor head, the rounding rule, the four quadrants. */+ const STRUCTURAL = new Set([0, 0.5, 1, 2, 4]);++ /** The trig section of the emitted header, by markers that must be found:+ * a moved marker has to fail here, not quietly scan an empty string. */+ function trigSection(): string {+ const from = RUNTIME_H.indexOf('#define ES_PIO2_HI');+ const to = RUNTIME_H.indexOf('/* Math.sign');+ expect([from, to].some((i) => i < 0), 'the trig section markers moved').toBe(false);+ return RUNTIME_H.slice(from, to);+ }++ /** A numeric literal, not the digits inside an identifier like `ES_PIO2_HI`. */+ const LITERAL = /(?<![A-Za-z0-9_.])-?\d+\.?\d*(?:[eE][-+]?\d+)?/g;++ /**+ * In ORDER, and every one of them: a set comparison passes a copy that put+ * S4's digits where S3 goes, which is a different curve that only a host+ * with a compiler would have caught.+ */+ it('emits the authored constants, all of them, in the authored order', () => {+ const coefficients = [...trigSection().matchAll(LITERAL)]+ .map((m) => Number(m[0]))+ .filter((v) => !STRUCTURAL.has(v));+ expect(coefficients).toEqual([...EXACT_CONSTANTS]);+ });+});+/*** A compiled module carries ITS polynomial inside it, so a host running another* one makes the same system answer differently depending on whether it wasdiff --git a/sdk/src/math/exact.ts b/sdk/src/math/exact.tsindex 83a517446..33ae6647e 100644--- a/sdk/src/math/exact.ts+++ b/sdk/src/math/exact.ts@@ -44,18 +44,28 @@ const C5 = 2.0875723212981748e-09;const C6 = -1.1359647557788195e-11;/**- * Every constant the algorithm IS, in one place a digest can take exactly.- * Sampling answers is not enough on its own: a 1-ulp nudge to `S3` moved none- * of 250 probed answers (measured), while still making two builds different- * arithmetic that a long enough run would disagree on.+ * Every constant the algorithm IS, under the name both sides call it. The+ * compiler writes the C half's coefficients from here: what is implemented+ * twice is the STRUCTURE, which a differential compares. Retyping a 17-digit+ * decimal is not a second implementation — it is a copy that drifts in silence.** @internal*/-export const EXACT_CONSTANTS: readonly number[] = [+export const EXACT_COEFFICIENTS = {PIO2_HI, PIO2_LO, TWO_OVER_PI,S1, S2, S3, S4, S5, S6,C1, C2, C3, C4, C5, C6,-];+} as const;++/**+ * The same, as a list the digest takes exactly — the ORDER is in the hash, so+ * reordering the record above is an ABI change and not a tidy-up. Sampling+ * answers alone would not do: a 1-ulp `S3` moved none of 250 probed answers+ * (measured) while still being arithmetic a long run disagrees on.+ *+ * @internal+ */+export const EXACT_CONSTANTS: readonly number[] = Object.values(EXACT_COEFFICIENTS);/*** `Math.round`'s rule — ties toward +Infinity — spelled out, because C'sdiff --git a/src/esengine/aot/estella_abi.h b/src/esengine/aot/estella_abi.hindex a91311558..81dd353ac 100644--- a/src/esengine/aot/estella_abi.h+++ b/src/esengine/aot/estella_abi.h@@ -155,6 +155,10 @@ static inline double es_sqrt(double x) { return sqrt(x); }These are the same range reduction, the same polynomial and the same ORDER assdk/src/math/exact.ts, line for line. Reordering either side is a change tothe result, and a differential compares them bit for bit.++ The COEFFICIENTS come from that file, written in below: what is implemented+ twice here is the structure, which a differential can compare, and not+ fifteen decimals, which retyping only puts at risk.--------------------------------------------------------------------------- */#define ES_PIO2_HI 1.5707963267341256#define ES_PIO2_LO 6.077100506506192e-11@@ -162,17 +166,17 @@ static inline double es_sqrt(double x) { return sqrt(x); }static inline double es_kernel_sin(double x) {double z = x * x;- double r = -1.6666666666666632e-01 + z * (8.333333333324894e-03- + z * (-1.984126982985795e-04 + z * (2.755731370707007e-06- + z * (-2.505076025340686e-08 + z * 1.5896909952115501e-10))));+ double r = -0.16666666666666632 + z * (0.008333333333324894+ + z * (-0.0001984126982985795 + z * (0.000002755731370707007+ + z * (-2.505076025340686e-8 + z * 1.58969099521155e-10))));return x + x * z * r;}static inline double es_kernel_cos(double x) {double z = x * x;- double r = 4.1666666666666602e-02 + z * (-1.3888888888874109e-03- + z * (2.4801587289476730e-05 + z * (-2.7557314351390663e-07- + z * (2.0875723212981748e-09 + z * -1.1359647557788195e-11))));+ double r = 0.0416666666666666 + z * (-0.001388888888887411+ + z * (0.00002480158728947673 + z * (-2.7557314351390663e-7+ + z * (2.087572321298175e-9 + z * -1.1359647557788195e-11))));return 1.0 - 0.5 * z + z * z * r;}diff --git a/tools/lib/contractFacts.mjs b/tools/lib/contractFacts.mjsindex 463be08d6..739826bf2 100644--- a/tools/lib/contractFacts.mjs+++ b/tools/lib/contractFacts.mjs@@ -143,6 +143,9 @@ export const FACTS = [id: 'mathPolicy',what: 'The numeric subset a compiled system may use, specified to the bit because ECMAScript does not specify trigonometry.',surface: 'runtime-aot-abi',+ // Two implementations of the ALGORITHM over one author for the fifteen+ // constants, which the C half is written from: retyping a coefficient+ // is a copy that drifts, not a second implementation of anything.authors: [{ path: 'sdk/src/math/exact.ts', probe: /function kernelSin/, kind: 'implementation' },{ path: 'compiler/src/codegen.ts', probe: /es_kernel_sin/, kind: 'implementation' },@@ -150,12 +153,16 @@ export const FACTS = [projections: ['src/esengine/aot/estella_abi.h'],verification: [{ path: 'compiler/tests/exact-trig.test.ts', how: 'test', probe: /bit for bit/i },+ // The half that needs no compiler: every constant the C half emits+ // is the author's, in the author's order.+ { path: 'compiler/tests/exact-trig.test.ts', how: 'test', probe: /authored order/ },],digest: { name: 'engine-abi', path: 'sdk/src/ecs/aot/abiDigest.ts', probe: /trig=/ },- owed: 'the digest is taken of the TS side, so it refuses a module built against'- + ' different arithmetic — but a change to the C half ALONE moves nothing, and'- + ' only the differential sees it. That needs a host C compiler, so a checkout'- + ' without one verifies the two implementations against nothing.',+ owed: 'the constants are one author and therefore in the digest, but the STRUCTURE'+ + ' is still written twice and only the differential compares it — and that'+ + ' needs a host C compiler. A checkout without one holds the reduction, the'+ + ' quadrant table and the Horner order against nothing, and says so in a'+ + ' warning inside a test that passes.',},{id: 'tweenWireEnums',