@ac-kit/test-util 0.3.1 → 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.
Files changed (42) hide show
  1. package/CHANGELOG.md +109 -0
  2. package/README.md +159 -1
  3. package/dist/exact-dot.d.ts +87 -0
  4. package/dist/exact-dot.d.ts.map +1 -0
  5. package/dist/exact-dot.js +237 -0
  6. package/dist/exact-dot.js.map +1 -0
  7. package/dist/expect-alias-safe.d.ts +31 -0
  8. package/dist/expect-alias-safe.d.ts.map +1 -0
  9. package/dist/expect-alias-safe.js +80 -0
  10. package/dist/expect-alias-safe.js.map +1 -0
  11. package/dist/fake-wall-clock.d.ts.map +1 -1
  12. package/dist/fake-wall-clock.js +2 -2
  13. package/dist/fake-wall-clock.js.map +1 -1
  14. package/dist/index.d.ts +3 -0
  15. package/dist/index.d.ts.map +1 -1
  16. package/dist/index.js +3 -0
  17. package/dist/index.js.map +1 -1
  18. package/dist/out-first-case.d.ts +116 -0
  19. package/dist/out-first-case.d.ts.map +1 -0
  20. package/dist/out-first-case.js +113 -0
  21. package/dist/out-first-case.js.map +1 -0
  22. package/dist/v8/_collect.d.ts +25 -0
  23. package/dist/v8/_collect.d.ts.map +1 -0
  24. package/dist/v8/_collect.js +94 -0
  25. package/dist/v8/_collect.js.map +1 -0
  26. package/dist/v8/allocated-bytes.d.ts +97 -0
  27. package/dist/v8/allocated-bytes.d.ts.map +1 -0
  28. package/dist/v8/allocated-bytes.js +148 -0
  29. package/dist/v8/allocated-bytes.js.map +1 -0
  30. package/dist/v8/allocation-sites.d.ts +92 -0
  31. package/dist/v8/allocation-sites.d.ts.map +1 -0
  32. package/dist/v8/allocation-sites.js +131 -0
  33. package/dist/v8/allocation-sites.js.map +1 -0
  34. package/dist/v8/elements-kind.d.ts +50 -0
  35. package/dist/v8/elements-kind.d.ts.map +1 -0
  36. package/dist/v8/elements-kind.js +81 -0
  37. package/dist/v8/elements-kind.js.map +1 -0
  38. package/dist/v8/index.d.ts +4 -0
  39. package/dist/v8/index.d.ts.map +1 -0
  40. package/dist/v8/index.js +4 -0
  41. package/dist/v8/index.js.map +1 -0
  42. package/package.json +17 -6
package/CHANGELOG.md CHANGED
@@ -1,5 +1,114 @@
1
1
  # @ac-kit/test-util
2
2
 
3
+ ## 0.5.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 0b4fe43: `@ac-kit/test-util/v8` exports `allocatedBytesPerCall(body, options?)`: the
8
+ mean bytes one call of `body` allocates once warmed up: every byte of the
9
+ JavaScript heap's object spaces — garbage freed by collections inside the
10
+ measurement, and what the engine allocates outside any JavaScript function,
11
+ the code it compiles aside — plus every off-heap `ArrayBuffer` backing store
12
+ allocated, at its byte length. The measurement's own cost and 256 bytes a
13
+ window of engine noise are removed, so an allocation-free body reads 0 and an
14
+ allocating one at most `256 / calls` bytes low. `warmUpCalls`,
15
+ `calls` and `windows` (the least window is kept) tune it; a `RangeError`
16
+ refuses a non-integer or out-of-range count. Node only; leaves V8's
17
+ array-buffer sweeping synchronous for the rest of the process.
18
+ - 0b4fe43: `@ac-kit/test-util/v8` exports `allocatedMemoryPerCall(body, options?)`: the
19
+ measurement `allocatedBytesPerCall` makes, split into `heapBytes` (JavaScript
20
+ heap objects, garbage included) and `externalBytes` (off-heap `ArrayBuffer`
21
+ backing stores, each counted once at its byte length, freed in the measurement
22
+ or not), with their sum, `bytes`. A body that allocates no buffer reads
23
+ `externalBytes` 0, so a gate tells a typed array allocated per call from a
24
+ stray object.
25
+ - 0b4fe43: `@ac-kit/test-util/v8` exports `allocationSitesPerCall(body, options?)`: the
26
+ mean bytes one call of `body` allocates once warmed up, and the functions that
27
+ allocated them, largest first, from V8's sampling heap profiler. Garbage counts
28
+ as much as what survives, the profiler's own cost is removed, and `include`
29
+ restricts the figure to the scripts under test; what a builtin allocates is its
30
+ calling function's, and what the optimiser inlined is its caller's.
31
+ `externalBytesPerCall` adds the off-heap `ArrayBuffer` backing stores per call,
32
+ which no sample sees, outside the sites and `include`. Node only; leaves V8's
33
+ array-buffer sweeping synchronous for the rest of the process.
34
+ - 0b4fe43: `exactDotScaled(a, b)` is the reference for a dot product beyond the doubles or
35
+ below them: `Σ aᵢ·bᵢ` computed exactly in integers and rounded once to 53
36
+ significant bits, ties to even, with no bound on the exponent, as `{
37
+ significand, exponent }` (significand in `±[1, 2)`, or `0` and `0` for an
38
+ exact zero). It shares no code with what it checks, as `exactDot`.
39
+
40
+ ### Patch Changes
41
+
42
+ - Updated dependencies [0b4fe43]
43
+ - Updated dependencies [0b4fe43]
44
+ - Updated dependencies [0b4fe43]
45
+ - Updated dependencies [0b4fe43]
46
+ - Updated dependencies [0b4fe43]
47
+ - Updated dependencies [0b4fe43]
48
+ - Updated dependencies [0b4fe43]
49
+ - Updated dependencies [0b4fe43]
50
+ - Updated dependencies [0b4fe43]
51
+ - Updated dependencies [0b4fe43]
52
+ - Updated dependencies [0b4fe43]
53
+ - Updated dependencies [0b4fe43]
54
+ - Updated dependencies [0b4fe43]
55
+ - Updated dependencies [0b4fe43]
56
+ - Updated dependencies [0b4fe43]
57
+ - Updated dependencies [0b4fe43]
58
+ - Updated dependencies [0b4fe43]
59
+ - @ac-kit/core@0.13.0
60
+
61
+ ## 0.4.0
62
+
63
+ ### Minor Changes
64
+
65
+ - 34e8cfa: `exactDot(a, b)` is a dot product rounded once from its exact value and
66
+ `exactDotSign(a, b)` the sign of that value, for any finite entries —
67
+ references computed in integers with no floating-point error-free
68
+ transformation, so a test never checks a kernel against the arithmetic it is
69
+ built on; `absoluteDot` is the scale such errors are measured against.
70
+ `defineOutFirstCase` and `expectAliasSafe` check functions that write their
71
+ result into a first `out` argument: with `out` as each input in turn, and as
72
+ every input at once, the result is the one a separate `out` gets, bit for bit,
73
+ and the function returns `out`. `samplerOf` replays fixed inputs. `vitest` is a
74
+ peer dependency, which the `expect…` assertions already needed.
75
+ - 34e8cfa: `@ac-kit/test-util/v8` exports `v8ElementsKind`, an array's V8 elements kind,
76
+ for tests run with `--allow-natives-syntax`; a proxy is `"other"` and a value
77
+ that is not an object throws a `TypeError`, where V8 itself would abort the
78
+ process.
79
+
80
+ ### Patch Changes
81
+
82
+ - 34e8cfa: An error message that quotes an argument of the wrong kind or value describes
83
+ it with `@ac-kit/core`'s `describeValue`, so building the message can no
84
+ longer throw on a symbol or run a hostile `toString`. The text changes: a
85
+ string is JSON-quoted (`got "sideways"`), `-0` keeps its sign, and an object is
86
+ named by its kind. Affected: `bitAddressFromBigInt`, `ipv4FromUint32`,
87
+ `toPortNumber`, `splitBitPrefixInto` and `printIpv6Address`' form, the HTTP
88
+ cookie jar's limits, the fetch, redirect and content-decoding limits and the
89
+ HTTP/1, HTTP/2 and HTTP/3 servers' response statuses, `OscPeer`'s
90
+ `maxPacketSize`, the QUIC `maxUdpPayloadSize`, `oshash`'s `size`,
91
+ `hammingDistancesInto`' query, the luma grid's `width` and `height`,
92
+ `phash`'s variant, `FakeWallClock`'s milliseconds, the piano partials, input-latency, chord-verifier and
93
+ tick-synthesis arguments of `@ac-kit/media-piano-audio`, and `@ac-kit/model-music`'s reference
94
+ pitch, tempo map and time signatures.
95
+ - Updated dependencies [34e8cfa]
96
+ - Updated dependencies [34e8cfa]
97
+ - Updated dependencies [34e8cfa]
98
+ - Updated dependencies [34e8cfa]
99
+ - Updated dependencies [34e8cfa]
100
+ - Updated dependencies [34e8cfa]
101
+ - Updated dependencies [34e8cfa]
102
+ - Updated dependencies [34e8cfa]
103
+ - Updated dependencies [34e8cfa]
104
+ - Updated dependencies [34e8cfa]
105
+ - Updated dependencies [34e8cfa]
106
+ - Updated dependencies [34e8cfa]
107
+ - Updated dependencies [34e8cfa]
108
+ - Updated dependencies [34e8cfa]
109
+ - Updated dependencies [34e8cfa]
110
+ - @ac-kit/core@0.12.0
111
+
3
112
  ## 0.3.1
4
113
 
5
114
  ### Patch Changes
package/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  # @ac-kit/test-util
2
2
 
3
- Test-harness primitives with no dependency beyond `@ac-kit/core`.
3
+ Test-harness primitives with no dependency beyond `@ac-kit/core`. The
4
+ `expect…` assertions call Vitest's `expect`, so `vitest` is a peer dependency.
4
5
 
5
6
  Being dependency-free is the design constraint, not an accident: any package's
6
7
  test suite may need these, so pulling in a heavier dependency here would force
@@ -42,6 +43,163 @@ a temporary file tree, a loopback HTTP server), see
42
43
  heavier dependencies, precisely because its own consumers are few, while this
43
44
  one stays portable.
44
45
 
46
+ ## Exact dot product
47
+
48
+ `exactDot(a, b)` is `Σ aᵢ·bᵢ` computed exactly in integers and rounded once to
49
+ the nearest binary64 (ties to even), `exactDotSign(a, b)` is the sign of that
50
+ exact value, and `absoluteDot(a, b)` is `Σ |aᵢ·bᵢ|` in plain arithmetic, the
51
+ scale an error bound is stated against. They are the references a dot product —
52
+ compensated, exactly signed, or the error of a product — is checked against,
53
+ for any finite entries: cancellation to nothing, subnormal results and ties,
54
+ overflow.
55
+
56
+ ```ts
57
+ import { absoluteDot, exactDot, exactDotSign } from "@ac-kit/test-util";
58
+
59
+ exactDot([1e17, 1, -1e17], [1, 1, 1]); // 1, where the plain sum is 0
60
+ exactDotSign([1e17, -1, -1e17], [1, 1, 1]); // -1
61
+ // The rounding error of a product, rounded once: a·b − product.
62
+ exactDot([a, product], [b, -1]);
63
+
64
+ // An error bound stated against the scale of the terms.
65
+ expect(Math.abs(result - exactDot(a, b))).toBeLessThanOrEqual(
66
+ 4 * Number.EPSILON * absoluteDot(a, b),
67
+ );
68
+ ```
69
+
70
+ `exactDotScaled(a, b)` rounds the same exact sum to 53 significant bits with no
71
+ bound on the exponent, as `{ significand, exponent }` (significand in `±[1, 2)`,
72
+ or `0` and `0` for an exact zero): the reference for a sum beyond
73
+ `Number.MAX_VALUE` or below the subnormals, such as a cross product of huge or
74
+ tiny coordinates.
75
+
76
+ An exact zero, or empty input, gives `+0`; a non-zero sum below half the
77
+ smallest subnormal gives a zero of its own sign; past `Number.MAX_VALUE` after
78
+ rounding, `±Infinity`. The functions throw a `RangeError` when the lengths
79
+ differ, and `exactDot` and `exactDotSign` when an entry is `NaN` or infinite: a
80
+ reference must not answer a mistake in the test with `NaN`. `absoluteDot` lets
81
+ non-finite values propagate, as the dot products it measures do.
82
+
83
+ They share no code with what they check. The decomposition is their own integer
84
+ code over `@ac-kit/core`'s `float64ToBits` and `bitsToFloat64`, which read and
85
+ write a bit pattern and compute nothing; nothing from `float64ToParts`,
86
+ `PreciseSum`, an error-free transformation or a `math-*` package is used, so a
87
+ defect in those cannot cancel against the same defect in the reference. They
88
+ allocate and cost O(n) `bigint` operations: a reference, not a kernel.
89
+
90
+ ## Alias safety
91
+
92
+ A function that writes its result into a first `out` argument must give the
93
+ same result whichever of its inputs `out` is, and when every input is `out` at
94
+ once. `defineOutFirstCase` describes such a function, `expectAliasSafe` runs it
95
+ with a fresh `out`, with `out` as each input in turn and as every input at
96
+ once, and fails unless each result matches the one a separate `out` gets, bit
97
+ for bit (`NaN` and `-0` count) and the function returns `out`.
98
+
99
+ ```ts
100
+ import {
101
+ defineOutFirstCase,
102
+ expectAliasSafe,
103
+ samplerOf,
104
+ } from "@ac-kit/test-util";
105
+
106
+ const vec2AddCase = defineOutFirstCase({
107
+ name: "vec2Add",
108
+ // A fresh `out` from the value's own creator, and the arguments after it.
109
+ create: () => vec2Create(),
110
+ aliasable: [0, 1], // the arguments `out` may be
111
+ makeArguments: (next) => [vec2Of(next(), next()), vec2Of(next(), next())],
112
+ call: (out, [a, b]) => vec2Add(out, a, b),
113
+ });
114
+
115
+ expectAliasSafe(vec2AddCase, samplerOf([1, 2, 3, 4]));
116
+ ```
117
+
118
+ A package keeps the table of its own functions — which take `out` first, and
119
+ which argument positions may be `out` — and loops `expectAliasSafe` over it.
120
+ `samplerOf(values)` replays fixed numbers on every pass; any
121
+ `() => () => number` (a seeded generator that restarts) serves as a
122
+ `NumberSampler`. A function that returns something other than `out` — a success
123
+ flag, nothing — is declared `returns: "value"`, and the check then requires
124
+ that value to be the same wherever `out` is. `case.run(placement, sampler)` and
125
+ `case.aliasable` serve other checks over the same table. The harness never
126
+ builds a value it passes to the function: `create` and `makeArguments` do, so a
127
+ branded value always comes from its own creator, and bulk kernels with a
128
+ `Float64Array` `out` work in place the same way.
129
+
130
+ ## `@ac-kit/test-util/v8`
131
+
132
+ `allocationSitesPerCall` measures what one call of a hot path allocates, by
133
+ the function that allocated it, from V8's sampling heap profiler: garbage
134
+ included, the profiler's own cost removed, and optionally restricted to the
135
+ scripts under test (a function the optimiser inlined counts as its caller's).
136
+
137
+ ```ts
138
+ import { allocationSitesPerCall } from "@ac-kit/test-util/v8";
139
+
140
+ declare const renderFrame: () => void;
141
+ const { bytesPerCall, sites } = await allocationSitesPerCall(renderFrame);
142
+ // bytesPerCall: 0 for an allocation-free path; sites: who allocates, largest first
143
+ ```
144
+
145
+ `allocatedBytesPerCall` counts everything one call allocates: the growth of the
146
+ heap's object spaces plus what every collection in the window freed, what no
147
+ JavaScript function allocated (the engine's own bookkeeping) included but not
148
+ the code the engine compiles, and the off-heap backing stores of the
149
+ `ArrayBuffer`s it allocated (a typed array over more than 64 bytes), each
150
+ counted once at its byte length, collected in the window or not. Its own cost
151
+ and 256 bytes a window of engine noise are removed, so an allocation-free body
152
+ reads 0; measure a control loop of the same shape alongside to tell the
153
+ engine's share from the code's. `allocatedMemoryPerCall` gives the same figure
154
+ split into `heapBytes` and `externalBytes`, and `allocationSitesPerCall`
155
+ reports the backing stores it cannot attribute to a site as
156
+ `externalBytesPerCall`.
157
+
158
+ ```ts
159
+ import { allocatedBytesPerCall } from "@ac-kit/test-util/v8";
160
+ import { allocatedMemoryPerCall } from "@ac-kit/test-util/v8";
161
+
162
+ declare const renderFrame: () => void;
163
+ const bytes = allocatedBytesPerCall(renderFrame, { calls: 50_000 });
164
+ const { heapBytes, externalBytes } = allocatedMemoryPerCall(renderFrame);
165
+ ```
166
+
167
+ Both make V8 free dead backing stores inside the collection that finds them
168
+ (`--no-concurrent-array-buffer-sweeping`) and expose `gc`, for the rest of the
169
+ process.
170
+
171
+ `v8ElementsKind` reports the elements kind V8 holds for an array
172
+ (`"packed-smi"`, `"packed-double"`, `"holey-object"`, `"dictionary"`, …), to test
173
+ that a creator or a hot loop keeps a `number[]` on the compact kind its speed
174
+ depends on. It is behind a subpath because it belongs to one engine and needs a
175
+ runtime flag, `--allow-natives-syntax`; importing the subpath never throws, and
176
+ the call throws an `Error` naming the flag when V8 lacks it (or the engine is
177
+ not V8).
178
+
179
+ ```ts
180
+ import { v8ElementsKind } from "@ac-kit/test-util/v8";
181
+
182
+ v8ElementsKind([1.5, 2.5]); // "packed-double"
183
+ v8ElementsKind([1, 2]); // "packed-smi"
184
+ v8ElementsKind(new Array(3)); // "holey-smi"
185
+ ```
186
+
187
+ With Vitest, give the project forked workers with the flag, so that the arrays
188
+ a test provokes cannot reach another file's type feedback:
189
+
190
+ ```ts
191
+ // vitest.config.ts
192
+ import { defineProject } from "vitest/config";
193
+
194
+ export default defineProject({
195
+ test: {
196
+ pool: "forks",
197
+ isolate: true,
198
+ execArgv: ["--allow-natives-syntax"],
199
+ },
200
+ });
201
+ ```
202
+
45
203
  See the
46
204
  [generated API docs](https://anthochamp.pages.grenadine.io/node-essentials/api/test-util/)
47
205
  for the full reference.
@@ -0,0 +1,87 @@
1
+ /**
2
+ * `Σ aᵢ·bᵢ` computed exactly in integers and rounded once to the nearest
3
+ * binary64, ties to even: the reference a dot product, compensated or not, is
4
+ * checked against. An exact zero, or empty input, gives `+0`; a non-zero sum
5
+ * below half the smallest subnormal gives a zero of its own sign; a sum past
6
+ * `Number.MAX_VALUE` after rounding gives `±Infinity`.
7
+ *
8
+ * It shares no code with `@ac-kit/math-scalar`, `@ac-kit/core`'s `PreciseSum`
9
+ * or any error-free transformation, so a defect in those cannot cancel against
10
+ * the same defect here. The decomposition is its own integer code over core's
11
+ * bit-pattern conversions.
12
+ *
13
+ * Time complexity: O(n) `bigint` operations on integers of up to about 4,200
14
+ * bits, plus one rounding. It allocates: it is a reference, not a kernel.
15
+ *
16
+ * @example
17
+ * // 1e17 + 1 − 1e17: the plain sum loses the 1.
18
+ * exactDot([1e17, 1, -1e17], [1, 1, 1]); // 1
19
+ *
20
+ * @param a - Left factors.
21
+ * @param b - Right factors, paired by index.
22
+ * @returns The exact sum, rounded once.
23
+ * @throws {RangeError} When `a` and `b` differ in length, or an entry is `NaN`
24
+ * or infinite: a reference must not answer a test's mistake with `NaN`.
25
+ */
26
+ export declare function exactDot(a: ArrayLike<number>, b: ArrayLike<number>): number;
27
+ /** {@link exactDotScaled}'s result: `significand · 2^exponent`. */
28
+ export type ScaledFloat = {
29
+ /** In `±[1, 2)`, or `0` for an exact zero. */
30
+ readonly significand: number;
31
+ /** An integer, `0` for an exact zero. */
32
+ readonly exponent: number;
33
+ };
34
+ /**
35
+ * `Σ aᵢ·bᵢ` computed exactly in integers and rounded once to 53 significant
36
+ * bits, ties to even, with no bound on the exponent: the reference for a sum
37
+ * beyond `Number.MAX_VALUE` or below the subnormals, which {@link exactDot}
38
+ * would give as `±Infinity` or a rounded subnormal. Its independence is
39
+ * {@link exactDot}'s.
40
+ *
41
+ * Time complexity: O(n) `bigint` operations, as {@link exactDot}.
42
+ *
43
+ * @example
44
+ * exactDotScaled([Number.MAX_VALUE], [4]); // { significand: 2 - 2 ** -52, exponent: 1025 }
45
+ * exactDotScaled([2 ** -1074], [2 ** -1074]); // { significand: 1, exponent: -2148 }
46
+ *
47
+ * @param a - Left factors.
48
+ * @param b - Right factors, paired by index.
49
+ * @returns The exact sum, rounded once, as significand and exponent.
50
+ * @throws {RangeError} When `a` and `b` differ in length, or an entry is `NaN`
51
+ * or infinite.
52
+ */
53
+ export declare function exactDotScaled(a: ArrayLike<number>, b: ArrayLike<number>): ScaledFloat;
54
+ /**
55
+ * The sign of the exact `Σ aᵢ·bᵢ`: `-1`, `0` (only an exactly zero sum, which
56
+ * empty input is) or `1`. The reference `dotSign` is checked against, with the
57
+ * same independence as {@link exactDot}.
58
+ *
59
+ * Time complexity: O(n) `bigint` operations, as {@link exactDot}.
60
+ *
61
+ * @example
62
+ * exactDotSign([1e17, -1, -1e17], [1, 1, 1]); // -1
63
+ *
64
+ * @param a - Left factors.
65
+ * @param b - Right factors, paired by index.
66
+ * @returns The sign of the exact sum.
67
+ * @throws {RangeError} When `a` and `b` differ in length, or an entry is `NaN`
68
+ * or infinite.
69
+ */
70
+ export declare function exactDotSign(a: ArrayLike<number>, b: ArrayLike<number>): -1 | 0 | 1;
71
+ /**
72
+ * `Σ |aᵢ·bᵢ|` in plain arithmetic: the scale a dot product's error bound is
73
+ * stated against (`|computed − exact| ≤ k·ε·Σ|aᵢbᵢ|`). Non-finite entries
74
+ * propagate, as they do in the dot products it measures.
75
+ *
76
+ * Time complexity: O(n).
77
+ *
78
+ * @example
79
+ * absoluteDot([1, -2], [3, 4]); // 11
80
+ *
81
+ * @param a - Left factors.
82
+ * @param b - Right factors, paired by index.
83
+ * @returns The sum of the absolute products, rounded as it goes.
84
+ * @throws {RangeError} When `a` and `b` differ in length.
85
+ */
86
+ export declare function absoluteDot(a: ArrayLike<number>, b: ArrayLike<number>): number;
87
+ //# sourceMappingURL=exact-dot.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"exact-dot.d.ts","sourceRoot":"","sources":["../src/exact-dot.ts"],"names":[],"mappings":"AA2FA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,QAAQ,CAAC,CAAC,EAAE,SAAS,CAAC,MAAM,CAAC,EAAE,CAAC,EAAE,SAAS,CAAC,MAAM,CAAC,GAAG,MAAM,CAoC3E;AAED,mEAAmE;AACnE,MAAM,MAAM,WAAW,GAAG;IACzB,8CAA8C;IAC9C,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7B,yCAAyC;IACzC,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;CAC1B,CAAC;AAEF;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,cAAc,CAC7B,CAAC,EAAE,SAAS,CAAC,MAAM,CAAC,EACpB,CAAC,EAAE,SAAS,CAAC,MAAM,CAAC,GAClB,WAAW,CA2Cb;AA+BD;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,YAAY,CAC3B,CAAC,EAAE,SAAS,CAAC,MAAM,CAAC,EACpB,CAAC,EAAE,SAAS,CAAC,MAAM,CAAC,GAClB,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,CAQZ;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,WAAW,CAC1B,CAAC,EAAE,SAAS,CAAC,MAAM,CAAC,EACpB,CAAC,EAAE,SAAS,CAAC,MAAM,CAAC,GAClB,MAAM,CAcR"}
@@ -0,0 +1,237 @@
1
+ import { bitsToFloat64, float64ToBits } from "@ac-kit/core";
2
+ /*
3
+ * Exact references for the dot product, computed in integers. A binary64 is
4
+ * `mantissa · 2^exponent`, so the product of two of them is an integer times a
5
+ * power of two, and any number of such products add exactly once they share the
6
+ * smallest exponent. The sum is rounded to binary64 once, at the end, in
7
+ * integer code.
8
+ *
9
+ * Independence is the point (CONVENTIONS §15): what these check is built on
10
+ * `twoSumError`, `twoProductError`, `PreciseSum` and `float64ToParts`, so they
11
+ * use none of them. The decomposition below duplicates core's on purpose, and
12
+ * the only core functions called are `float64ToBits` and `bitsToFloat64`,
13
+ * which read and write the IEEE 754 bit pattern and compute nothing. Never call
14
+ * `float64ToParts`, `partsToFloat64`, `scaleByPowerOfTwo`, `PreciseSum`,
15
+ * `sumPrecise` or anything from a `math-*` package here.
16
+ */
17
+ const FRACTION_BITS_ = 52n;
18
+ const FRACTION_MASK_ = (1n << FRACTION_BITS_) - 1n;
19
+ const HIDDEN_BIT_ = 1n << FRACTION_BITS_;
20
+ const SIGN_BIT_ = 1n << 63n;
21
+ /** The exponent of the unit in the last place of the smallest subnormal. */
22
+ const SUBNORMAL_EXPONENT_ = -1074;
23
+ /** The biased exponent field of the infinities and `NaN`. */
24
+ const SPECIAL_EXPONENT_ = 0x7ff;
25
+ /** A finite binary64 as `mantissa · 2^exponent`, with an integer mantissa. */
26
+ function decompose_(value, name) {
27
+ const bits = float64ToBits(value);
28
+ const biased = Number((bits >> FRACTION_BITS_) & 0x7ffn);
29
+ if (biased === SPECIAL_EXPONENT_) {
30
+ throw new RangeError(`${name} must be finite, got ${value}`);
31
+ }
32
+ const fraction = bits & FRACTION_MASK_;
33
+ const magnitude = biased === 0 ? fraction : fraction | HIDDEN_BIT_;
34
+ return {
35
+ mantissa: (bits & SIGN_BIT_) === 0n ? magnitude : -magnitude,
36
+ exponent: biased === 0 ? SUBNORMAL_EXPONENT_ : biased - 1075,
37
+ };
38
+ }
39
+ /** The exact sum `units · 2^exponent` of the products, of an integer `units`. */
40
+ function exactSum_(a, b, caller) {
41
+ if (a.length !== b.length) {
42
+ throw new RangeError(`${caller}: a and b must have the same length, got ${a.length} and ${b.length}`);
43
+ }
44
+ const terms = [];
45
+ for (let index = 0; index < a.length; index++) {
46
+ const left = decompose_(a[index], `${caller}: a[${index}]`);
47
+ const right = decompose_(b[index], `${caller}: b[${index}]`);
48
+ if (left.mantissa !== 0n && right.mantissa !== 0n) {
49
+ terms.push({
50
+ mantissa: left.mantissa * right.mantissa,
51
+ exponent: left.exponent + right.exponent,
52
+ });
53
+ }
54
+ }
55
+ let exponent = Number.POSITIVE_INFINITY;
56
+ for (const term of terms) {
57
+ exponent = Math.min(exponent, term.exponent);
58
+ }
59
+ let units = 0n;
60
+ for (const term of terms) {
61
+ units += term.mantissa << BigInt(term.exponent - exponent);
62
+ }
63
+ return { mantissa: units, exponent: terms.length === 0 ? 0 : exponent };
64
+ }
65
+ /**
66
+ * `Σ aᵢ·bᵢ` computed exactly in integers and rounded once to the nearest
67
+ * binary64, ties to even: the reference a dot product, compensated or not, is
68
+ * checked against. An exact zero, or empty input, gives `+0`; a non-zero sum
69
+ * below half the smallest subnormal gives a zero of its own sign; a sum past
70
+ * `Number.MAX_VALUE` after rounding gives `±Infinity`.
71
+ *
72
+ * It shares no code with `@ac-kit/math-scalar`, `@ac-kit/core`'s `PreciseSum`
73
+ * or any error-free transformation, so a defect in those cannot cancel against
74
+ * the same defect here. The decomposition is its own integer code over core's
75
+ * bit-pattern conversions.
76
+ *
77
+ * Time complexity: O(n) `bigint` operations on integers of up to about 4,200
78
+ * bits, plus one rounding. It allocates: it is a reference, not a kernel.
79
+ *
80
+ * @example
81
+ * // 1e17 + 1 − 1e17: the plain sum loses the 1.
82
+ * exactDot([1e17, 1, -1e17], [1, 1, 1]); // 1
83
+ *
84
+ * @param a - Left factors.
85
+ * @param b - Right factors, paired by index.
86
+ * @returns The exact sum, rounded once.
87
+ * @throws {RangeError} When `a` and `b` differ in length, or an entry is `NaN`
88
+ * or infinite: a reference must not answer a test's mistake with `NaN`.
89
+ */
90
+ export function exactDot(a, b) {
91
+ const { mantissa: units, exponent } = exactSum_(a, b, "exactDot");
92
+ if (units === 0n) {
93
+ return 0;
94
+ }
95
+ const isNegative = units < 0n;
96
+ const magnitude = isNegative ? -units : units;
97
+ // The unit of the last place of the result: 53 significant bits, but never
98
+ // finer than the smallest subnormal.
99
+ const bitLength = magnitude.toString(2).length;
100
+ const resultExponent = Math.max(bitLength - 53 + exponent, SUBNORMAL_EXPONENT_);
101
+ const dropped = resultExponent - exponent;
102
+ let kept;
103
+ if (dropped <= 0) {
104
+ kept = magnitude << BigInt(-dropped);
105
+ }
106
+ else {
107
+ const shift = BigInt(dropped);
108
+ kept = magnitude >> shift;
109
+ const remainder = magnitude - (kept << shift);
110
+ const half = 1n << (shift - 1n);
111
+ if (remainder > half || (remainder === half && (kept & 1n) === 1n)) {
112
+ kept += 1n;
113
+ }
114
+ }
115
+ return assemble_(isNegative, kept, resultExponent);
116
+ }
117
+ /**
118
+ * `Σ aᵢ·bᵢ` computed exactly in integers and rounded once to 53 significant
119
+ * bits, ties to even, with no bound on the exponent: the reference for a sum
120
+ * beyond `Number.MAX_VALUE` or below the subnormals, which {@link exactDot}
121
+ * would give as `±Infinity` or a rounded subnormal. Its independence is
122
+ * {@link exactDot}'s.
123
+ *
124
+ * Time complexity: O(n) `bigint` operations, as {@link exactDot}.
125
+ *
126
+ * @example
127
+ * exactDotScaled([Number.MAX_VALUE], [4]); // { significand: 2 - 2 ** -52, exponent: 1025 }
128
+ * exactDotScaled([2 ** -1074], [2 ** -1074]); // { significand: 1, exponent: -2148 }
129
+ *
130
+ * @param a - Left factors.
131
+ * @param b - Right factors, paired by index.
132
+ * @returns The exact sum, rounded once, as significand and exponent.
133
+ * @throws {RangeError} When `a` and `b` differ in length, or an entry is `NaN`
134
+ * or infinite.
135
+ */
136
+ export function exactDotScaled(a, b) {
137
+ const { mantissa: units, exponent } = exactSum_(a, b, "exactDotScaled");
138
+ if (units === 0n) {
139
+ return { significand: 0, exponent: 0 };
140
+ }
141
+ const isNegative = units < 0n;
142
+ const magnitude = isNegative ? -units : units;
143
+ const bitLength = magnitude.toString(2).length;
144
+ // The unit of the last place of the 53 bits kept.
145
+ let unitExponent = bitLength - 53 + exponent;
146
+ const dropped = bitLength - 53;
147
+ let kept;
148
+ if (dropped <= 0) {
149
+ kept = magnitude << BigInt(-dropped);
150
+ }
151
+ else {
152
+ const shift = BigInt(dropped);
153
+ kept = magnitude >> shift;
154
+ const remainder = magnitude - (kept << shift);
155
+ const half = 1n << (shift - 1n);
156
+ if (remainder > half || (remainder === half && (kept & 1n) === 1n)) {
157
+ kept += 1n;
158
+ }
159
+ }
160
+ if (kept === HIDDEN_BIT_ << 1n) {
161
+ kept = HIDDEN_BIT_;
162
+ unitExponent += 1;
163
+ }
164
+ // `kept` is below 2⁵³, so its conversion and the scaling are exact.
165
+ const significand = Number(kept) / 2 ** 52;
166
+ return {
167
+ significand: isNegative ? -significand : significand,
168
+ exponent: unitExponent + 52,
169
+ };
170
+ }
171
+ /** `kept · 2^exponent`, with `kept` at most `2⁵³`, as a binary64's bits. */
172
+ function assemble_(isNegative, kept, exponent) {
173
+ let significand = kept;
174
+ let unitExponent = exponent;
175
+ if (significand === HIDDEN_BIT_ << 1n) {
176
+ significand = HIDDEN_BIT_;
177
+ unitExponent += 1;
178
+ }
179
+ let bits = significand;
180
+ if (significand >= HIDDEN_BIT_) {
181
+ const biased = unitExponent + 1075;
182
+ if (biased >= SPECIAL_EXPONENT_) {
183
+ return isNegative ? Number.NEGATIVE_INFINITY : Number.POSITIVE_INFINITY;
184
+ }
185
+ bits = (BigInt(biased) << FRACTION_BITS_) | (significand & FRACTION_MASK_);
186
+ }
187
+ return bitsToFloat64(isNegative ? bits | SIGN_BIT_ : bits);
188
+ }
189
+ /**
190
+ * The sign of the exact `Σ aᵢ·bᵢ`: `-1`, `0` (only an exactly zero sum, which
191
+ * empty input is) or `1`. The reference `dotSign` is checked against, with the
192
+ * same independence as {@link exactDot}.
193
+ *
194
+ * Time complexity: O(n) `bigint` operations, as {@link exactDot}.
195
+ *
196
+ * @example
197
+ * exactDotSign([1e17, -1, -1e17], [1, 1, 1]); // -1
198
+ *
199
+ * @param a - Left factors.
200
+ * @param b - Right factors, paired by index.
201
+ * @returns The sign of the exact sum.
202
+ * @throws {RangeError} When `a` and `b` differ in length, or an entry is `NaN`
203
+ * or infinite.
204
+ */
205
+ export function exactDotSign(a, b) {
206
+ const { mantissa: units } = exactSum_(a, b, "exactDotSign");
207
+ if (units === 0n) {
208
+ return 0;
209
+ }
210
+ return units > 0n ? 1 : -1;
211
+ }
212
+ /**
213
+ * `Σ |aᵢ·bᵢ|` in plain arithmetic: the scale a dot product's error bound is
214
+ * stated against (`|computed − exact| ≤ k·ε·Σ|aᵢbᵢ|`). Non-finite entries
215
+ * propagate, as they do in the dot products it measures.
216
+ *
217
+ * Time complexity: O(n).
218
+ *
219
+ * @example
220
+ * absoluteDot([1, -2], [3, 4]); // 11
221
+ *
222
+ * @param a - Left factors.
223
+ * @param b - Right factors, paired by index.
224
+ * @returns The sum of the absolute products, rounded as it goes.
225
+ * @throws {RangeError} When `a` and `b` differ in length.
226
+ */
227
+ export function absoluteDot(a, b) {
228
+ if (a.length !== b.length) {
229
+ throw new RangeError(`absoluteDot: a and b must have the same length, got ${a.length} and ${b.length}`);
230
+ }
231
+ let total = 0;
232
+ for (let index = 0; index < a.length; index++) {
233
+ total += Math.abs(a[index] * b[index]);
234
+ }
235
+ return total;
236
+ }
237
+ //# sourceMappingURL=exact-dot.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"exact-dot.js","sourceRoot":"","sources":["../src/exact-dot.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAE5D;;;;;;;;;;;;;;GAcG;AAEH,MAAM,cAAc,GAAG,GAAG,CAAC;AAC3B,MAAM,cAAc,GAAG,CAAC,EAAE,IAAI,cAAc,CAAC,GAAG,EAAE,CAAC;AACnD,MAAM,WAAW,GAAG,EAAE,IAAI,cAAc,CAAC;AACzC,MAAM,SAAS,GAAG,EAAE,IAAI,GAAG,CAAC;AAC5B,4EAA4E;AAC5E,MAAM,mBAAmB,GAAG,CAAC,IAAI,CAAC;AAClC,6DAA6D;AAC7D,MAAM,iBAAiB,GAAG,KAAK,CAAC;AAOhC,8EAA8E;AAC9E,SAAS,UAAU,CAAC,KAAa,EAAE,IAAY;IAC9C,MAAM,IAAI,GAAG,aAAa,CAAC,KAAK,CAAC,CAAC;IAClC,MAAM,MAAM,GAAG,MAAM,CAAC,CAAC,IAAI,IAAI,cAAc,CAAC,GAAG,MAAM,CAAC,CAAC;IAEzD,IAAI,MAAM,KAAK,iBAAiB,EAAE,CAAC;QAClC,MAAM,IAAI,UAAU,CAAC,GAAG,IAAI,wBAAwB,KAAK,EAAE,CAAC,CAAC;IAC9D,CAAC;IAED,MAAM,QAAQ,GAAG,IAAI,GAAG,cAAc,CAAC;IACvC,MAAM,SAAS,GAAG,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,QAAQ,GAAG,WAAW,CAAC;IAEnE,OAAO;QACN,QAAQ,EAAE,CAAC,IAAI,GAAG,SAAS,CAAC,KAAK,EAAE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EAAE,EAAE,CAAC;QAC/C,KAAK,IAAI,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,KAAK,CAAE,GAAG,CAAC,CAAC,KAAK,CAAE,CAAC,CAAC;IAC1C,CAAC;IAED,OAAO,KAAK,CAAC;AACd,CAAC"}
@@ -0,0 +1,31 @@
1
+ import type { NumberSampler, OutFirstCase } from "./out-first-case.js";
2
+ /**
3
+ * Asserts the alias rule of a function that writes into a first `out`:
4
+ * whichever input `out` is, and even when every input of its type is the same
5
+ * array, the result is the one a separate `out` gives, bit for bit, and the
6
+ * function returns `out` (or, for a case declared `returns: "value"`, the same
7
+ * value as with a separate `out`).
8
+ *
9
+ * It runs the case (see {@link defineOutFirstCase}) with:
10
+ *
11
+ * 1. A fresh `out`;
12
+ * 2. `out` set to each aliasable argument in turn;
13
+ * 3. When two or more arguments are aliasable, every one of them set to the same
14
+ * array, with `out` that array, against the same inputs with a fresh `out`.
15
+ *
16
+ * Every comparison uses `Object.is` per element, so `NaN` and `-0` count. A
17
+ * failure names the case, the placement and the first element that differs.
18
+ *
19
+ * Time complexity: 1 + k calls of the function, plus 2 more when k ≥ 2, each
20
+ * with an O(length) comparison, where k is the number of aliasable arguments.
21
+ *
22
+ * @example
23
+ * expectAliasSafe(vec2AddCase, samplerOf([1, 2, 3, 4]));
24
+ *
25
+ * @param testCase - The function's case.
26
+ * @param sampler - The numbers its arguments are built from; every run starts
27
+ * it again.
28
+ * @throws {AssertionError} On the first placement that differs.
29
+ */
30
+ export declare function expectAliasSafe(testCase: OutFirstCase, sampler: NumberSampler): void;
31
+ //# sourceMappingURL=expect-alias-safe.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"expect-alias-safe.d.ts","sourceRoot":"","sources":["../src/expect-alias-safe.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EACX,aAAa,EACb,YAAY,EAGZ,MAAM,qBAAqB,CAAC;AAiD7B;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,wBAAgB,eAAe,CAC9B,QAAQ,EAAE,YAAY,EACtB,OAAO,EAAE,aAAa,GACpB,IAAI,CAuCN"}