@danielsimonjr/mathts-wasm 0.4.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -41,6 +41,74 @@ console.log(sin_f64(Math.PI / 2)); // 1
41
41
  `matrix` and `functions` load the same binary as `mathts-as.wasm` via their own
42
42
  loaders and SHA-384 verification (`core/src/wasm-loader.ts`).
43
43
 
44
+ ### Entry points
45
+
46
+ | Specifier | Kind | Content |
47
+ | ------------------------------------- | ------ | -------------------------------------------------------------------- |
48
+ | `@danielsimonjr/mathts-wasm` | module | The glue of the release build (`build/mathts.js`). |
49
+ | `@danielsimonjr/mathts-wasm/debug.js` | module | The glue of the debug build. It checks the input parameters (below). |
50
+ | `@danielsimonjr/mathts-wasm/wasm` | file | The release binary `mathts.wasm`. It has no import. |
51
+ | `@danielsimonjr/mathts-wasm/debug` | file | The debug binary `mathts-debug.wasm`. It has no import. |
52
+
53
+ The two binaries need no host function, so any runtime can instantiate them with an empty import
54
+ object. Read the bytes of the file that the subpath names:
55
+
56
+ ```typescript node
57
+ import { readFile } from 'node:fs/promises';
58
+
59
+ const bytes = await readFile(new URL(import.meta.resolve('@danielsimonjr/mathts-wasm/wasm')));
60
+ const { instance } = await WebAssembly.instantiate(bytes, {});
61
+ const add = instance.exports.add_f64 as (a: number, b: number) => number;
62
+ console.log(add(2, 3)); // 5
63
+ ```
64
+
65
+ Node also loads a binary as a WebAssembly ES module: `await import('@danielsimonjr/mathts-wasm/wasm')`
66
+ returns its exports (pointer ABI, without the glue). Node 22.12 needs the flag
67
+ `--experimental-wasm-modules`; newer versions (measured on 22.23, 24.22 and 26.13) need none.
68
+ Bun has no loader for a WebAssembly module: `import()` of a binary subpath returns the file path
69
+ as `default`.
70
+
71
+ A binary has no `abort` import. An AssemblyScript abort (a failed assertion, or the
72
+ `RangeError` that a kernel throws above its order limit) stores its message, file, line and
73
+ column in globals and traps as `WebAssembly.RuntimeError` (message `unreachable` in V8). The glue
74
+ catches the trap and throws `Error('<message> in <file>:<line>:<column>')`, for example
75
+ `Index out of range in ~lib/typedarray.ts:1457:64`.
76
+
77
+ A host that calls a binary directly reads the record with these exports:
78
+
79
+ - `abort_take()` returns 1 when an abort is pending, and clears the flag. A trap that is not an
80
+ abort (a memory access out of bounds) leaves the flag clear. Call it first, in the `catch` of
81
+ every call into the binary (a runtime helper such as `__new` can abort too). An abort that the
82
+ host does not read stays pending.
83
+ - `abort_message()` and `abort_file()` return pointers to AssemblyScript strings (UTF-16 text,
84
+ the byte length is at pointer - 4).
85
+ - `abort_line()` and `abort_column()` return numbers.
86
+
87
+ `guardWasmAborts` of `@danielsimonjr/mathts-core/internal` does this for the loaders of `matrix`
88
+ and `functions`. Each loader throws the `Error` text that it threw when the binary had the
89
+ import (`WASM abort`, and `AS WASM abort` for `WASMBackend`). The `cause` of that `Error` is an
90
+ `Error` with the text `<message> in <file>:<line>:<column>`.
91
+
92
+ ### Output parameters
93
+
94
+ The glue copies each typed-array argument into WebAssembly memory before the call. After the
95
+ call, it copies the arguments that the kernel writes back into your array: an output buffer, an
96
+ in-place buffer or a scratch buffer. It never writes an input argument back. A plain Array works
97
+ as an input and as an output.
98
+
99
+ ```typescript node
100
+ import { matrix_multiply } from '@danielsimonjr/mathts-wasm';
101
+
102
+ const result = new Float64Array(4);
103
+ matrix_multiply(new Float64Array([1, 2, 3, 4]), 2, 2, new Float64Array([5, 6, 7, 8]), 2, result);
104
+ console.log(Array.from(result)); // [19, 22, 43, 50]
105
+ ```
106
+
107
+ `scripts/patch-glue.mjs` holds the class of every parameter (`OUTPUT_PARAMS`: `in` or `out`) and
108
+ runs as a step of `gen:manifest`. The build fails when an export or a parameter is missing from
109
+ that list. The debug glue (`@danielsimonjr/mathts-wasm/debug.js`) throws `mathts glue: <export> wrote its input
110
+ parameter <param>` when a kernel writes a parameter that the list marks `in`.
111
+
44
112
  ### Dense decompositions (AS exports)
45
113
 
46
114
  From `algebra/decomposition.ts` (used by matrix `WASMBackend` tests, not public
@@ -79,6 +147,8 @@ assembly/
79
147
  - Hyperbolic: `sinh_f64`, `cosh_f64`, `tanh_f64`, `asinh_f64`, `acosh_f64`, `atanh_f64`
80
148
  - Rounding: `abs_f64`, `floor_f64`, `ceil_f64`, `round_f64`, `trunc_f64`
81
149
 
150
+ `round_f64` rounds a tie away from zero on the exact double: `round_f64(0.5)` is 1 and `round_f64(-2.5)` is -3. A zero input gives +0, and `round_f64(-0.4)` gives -0. NaN gives NaN, and an infinity gives itself. The kernel has no configuration and applies no tolerance. The `round` of `@danielsimonjr/mathts-functions` applies its `relTol` first: it reads `0.49999999999999994` as the tie 0.5 and gives 1, and `round_f64(0.49999999999999994)` gives 0. The WebAssembly instruction `nearest` rounds a tie to the even integer, and the kernel does not use it.
151
+
82
152
  ### Array Operations
83
153
 
84
154
  - Reductions: `array_sum`, `array_product`, `array_mean`, `array_variance`, `array_min`, `array_max`
@@ -14,6 +14,31 @@ export declare const __rtti_base: number;
14
14
  * src/heap/heap_reset
15
15
  */
16
16
  export declare function heap_reset(): void;
17
+ /**
18
+ * src/abort/abort_take
19
+ * @returns `bool`
20
+ */
21
+ export declare function abort_take(): boolean;
22
+ /**
23
+ * src/abort/abort_message
24
+ * @returns `usize`
25
+ */
26
+ export declare function abort_message(): number;
27
+ /**
28
+ * src/abort/abort_file
29
+ * @returns `usize`
30
+ */
31
+ export declare function abort_file(): number;
32
+ /**
33
+ * src/abort/abort_line
34
+ * @returns `u32`
35
+ */
36
+ export declare function abort_line(): number;
37
+ /**
38
+ * src/abort/abort_column
39
+ * @returns `u32`
40
+ */
41
+ export declare function abort_column(): number;
17
42
  /**
18
43
  * src/types/complex/complex
19
44
  * @param re `f64`