// Pseudo for numerical work: sized types, FLOAT32, conversions, the numerical built-ins, USE IEEE, and what the // prover may and may not say about them. The corpus programs 49 to 52 show the same features in use; this file // pins each rule on its own. import test from 'node:test'; import assert from 'node:assert/strict'; import { studyOf, runPseudo, provePseudo } from '../dist/services/pseudo.js'; import { fma, exactDecimal, nextAfter, ulp, roundDyadic, wrap } from '../dist/engines/pseudo/numeric.js'; import { intRange, SIZED, formatFloat32 } from '../dist/engines/pseudo/lang.js'; const run = (src) => { const st = studyOf(src); assert.ok(st.ok, st.diagnostics.filter((d) => d.severity === 'error').map((d) => `L${d.line} ${d.id}: ${d.message}`).join(' | ')); return runPseudo(st, {}); }; const out = (src) => { const r = run(src); assert.equal(r.error, null, r.error?.message); return r.output; }; const errors = (src) => studyOf(src).diagnostics.filter((d) => d.severity === 'error').map((d) => d.id); test('the sized types have the ranges of their names (64-bit types cut at what a run holds exactly)', () => { assert.deepEqual(intRange(SIZED.INT8), [-128, 127]); assert.deepEqual(intRange(SIZED.UINT16), [0, 65535]); assert.deepEqual(intRange(SIZED.INT32), [-2147483648, 2147483647]); assert.deepEqual(intRange(SIZED.UINT32), [0, 4294967295]); assert.deepEqual(intRange(SIZED.INT64), [-Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER]); }); test('literals: underscores, hexadecimal and binary', () => { assert.deepEqual(out('OUTPUT 1_000_000, " ", 0xFF, " ", 0b1010, " ", 1_000.5'), ['1000000 255 10 1000.5']); assert.ok(errors('OUTPUT 1__0').includes('bad-number')); assert.ok(errors('OUTPUT 0xFG').includes('bad-number')); }); test('a sized result that leaves its range stops the run; WRAP wraps on purpose', () => { const r = run('DECLARE X : INT8 <- 127\nX <- X + 1\n'); assert.equal(r.error?.kind, 'overflow'); assert.equal(r.error.line, 2); assert.match(r.error.message, /INT8 holds -128 to 127/); assert.deepEqual(out('DECLARE X : INT8 <- 127\nOUTPUT WRAP(X + 1, INT8), " ", WRAP(-1, UINT8), " ", WRAP(70000, UINT16)'), ['-128 255 4464']); assert.equal(run('DECLARE U : UINT8 <- 0\nU <- U - 1\n').error?.kind, 'overflow'); assert.equal(run('DECLARE X : INT8\nX <- -128\nX <- -X\n').error?.kind, 'overflow'); }); test('storing, passing and returning check the range of the receiving type', () => { assert.equal(run('DECLARE N : INTEGER <- 300\nDECLARE B : UINT8\nB <- N\n').error?.kind, 'overflow'); const src = 'FUNCTION Id(X : UINT8) RETURNS UINT8\n RETURN X\nENDFUNCTION\nDECLARE N : INTEGER <- 256\nOUTPUT Id(N)\n'; const r = run(src); assert.equal(r.error?.kind, 'overflow'); assert.match(r.error.message, /Parameter X of Id/); assert.equal(run('FUNCTION Big(N : INTEGER) RETURNS INT16\n RETURN N * 1000\nENDFUNCTION\nOUTPUT Big(40)\n').error?.kind, 'overflow'); }); test('conversions: a FLOAT loses its fraction toward zero, and a value that does not fit stops the run', () => { assert.deepEqual(out('OUTPUT INT32(-2.9), " ", UINT8(200), " ", INT64(2.5e9), " ", FLOAT32(0.1) == FLOAT32(0.1)'), ['-2 200 2500000000 TRUE']); assert.deepEqual(errors('OUTPUT INT8(200)'), ['literal-range']); // a literal is checked before any run assert.equal(run('DECLARE N : INTEGER <- 200\nOUTPUT INT8(N)').error?.kind, 'overflow'); assert.equal(run('DECLARE F : FLOAT <- 3.0e9\nOUTPUT INT32(F)').error?.kind, 'overflow'); }); test('the checker: widths and precisions do not mix silently, and literals take the type beside them', () => { assert.deepEqual(errors('DECLARE A : INT8 <- 1\nDECLARE B : INT32 <- 2\nOUTPUT A + B'), ['mixed-width']); assert.deepEqual(errors('DECLARE A : FLOAT32 <- 1.0\nDECLARE B : FLOAT <- 2.0\nOUTPUT A * B'), ['mixed-precision']); assert.deepEqual(errors('DECLARE A : INT8\nA <- 300'), ['literal-range']); assert.deepEqual(errors('DECLARE A : INT8 <- 1\nDECLARE B : INT32 <- 2\nB <- A'), ['type']); assert.deepEqual(errors('DECLARE A : FLOAT32\nDECLARE D : FLOAT <- 0.5\nA <- D'), ['type']); assert.deepEqual(errors('DECLARE A : FLOAT32\nDECLARE D : FLOAT <- 0.5\nA <- FLOAT32(D)'), []); assert.deepEqual(errors('DECLARE A : FLOAT32\nA <- 0.1'), []); assert.deepEqual(errors('OUTPUT INT8'), ['type-as-value']); assert.deepEqual(errors('OUTPUT MAX_VALUE(INT64)'), ['exact-limit']); assert.deepEqual(errors('OUTPUT WRAP(5, UINT64)'), ['wrap-64']); assert.deepEqual(errors('DECLARE X : INTEGER <- 1\nOUTPUT EPSILON(INT32)'), ['type']); }); test('FLOAT32: results round to single precision, a literal next to one is a single, and it prints as the shortest decimal', () => { assert.deepEqual(out('DECLARE X : FLOAT32 <- 0.1\nOUTPUT X, " ", X == 0.1, " ", FLOAT64(X) == 0.1, " ", X + 0.2'), ['0.1 TRUE FALSE 0.3']); assert.deepEqual(out('DECLARE X : FLOAT32 <- 16777216.0\nOUTPUT X + 1.0, " ", FLOAT64(X) + 1.0'), ['16777216.0 16777217.0']); assert.equal(formatFloat32(Math.fround(0.1)), '0.1'); assert.equal(formatFloat32(Math.fround(1 / 3)), '0.33333334'); }); test('exact helpers: FMA rounds once, EXACT shows every digit, ULP and NEXT_AFTER step by one value', () => { assert.equal(fma(0.1, 10, -1), 5.551115123125783e-17); assert.equal(0.1 * 10 - 1, 0); assert.equal(fma(2, 3, 4), 10); assert.equal(fma(Math.fround(0.1), 10, -1, true), Math.fround(1.4901161193847656e-8)); assert.equal(exactDecimal(0.1), '0.1000000000000000055511151231257827021181583404541015625'); assert.equal(exactDecimal(-2.5), '-2.5'); assert.equal(exactDecimal(1e21), '1000000000000000000000.0'); assert.equal(exactDecimal(Number.MIN_VALUE).length, 1076); // 4.94...e-324 has 1074 digits after the point assert.equal(nextAfter(1, 2), 1 + Number.EPSILON); assert.equal(nextAfter(0, -1), -Number.MIN_VALUE); assert.equal(nextAfter(1, 2, true), 1 + 2 ** -23); assert.equal(ulp(1), Number.EPSILON); assert.equal(ulp(Number.MAX_VALUE), 2 ** 971); assert.equal(ulp(1, true), 2 ** -23); assert.equal(roundDyadic(3n, -1), 1.5); assert.equal(roundDyadic((1n << 54n) + 1n, 0), 2 ** 54); // ties and below round to even assert.equal(wrap(-129, 8, true), 127); assert.deepEqual(out('OUTPUT FMA(0.1, 10.0, -1.0), " ", ULP(1.0) == EPSILON(FLOAT64), " ", EXACT(0.5)'), ['5.551115123125783e-17 TRUE 0.5']); }); test('whole numbers bit by bit, two\'s complement for negative values', () => { assert.deepEqual(out('OUTPUT BIT_AND(-1, 255), " ", BIT_OR(-8, 1), " ", BIT_XOR(5, 3), " ", BIT_NOT(5), " ", BIT_NOT(UINT8(5))'), ['255 -7 6 -6 250']); assert.deepEqual(out('OUTPUT SHIFT_LEFT(3, 4), " ", SHIFT_RIGHT(-9, 1), " ", SHIFT_RIGHT(UINT8(255), 4), " ", GCD(-12, 18), " ", LCM(6, 8)'), ['48 -5 15 6 24']); assert.equal(run('DECLARE B : UINT8 <- 200\nOUTPUT SHIFT_LEFT(B, 1)').error?.kind, 'overflow'); }); test('the limits of each type', () => { assert.deepEqual(out('OUTPUT MAX_VALUE(INT8), " ", MIN_VALUE(INT16), " ", MAX_VALUE(UINT32), " ", MIN_VALUE(UINT64)'), ['127 -32768 4294967295 0']); assert.deepEqual(out('OUTPUT MAX_VALUE(FLOAT64) > 1e308, " ", EPSILON(FLOAT64), " ", MAX_VALUE(FLOAT32)'), ['TRUE 2.220446049250313e-16 3.4028235e38']); }); test('domains: without USE IEEE the run stops where IEEE 754 would give NaN or an infinity; with it, the values flow', () => { for (const [e, kind] of [['SQRT(-1.0)', 'value'], ['LOG(0.0)', 'value'], ['ASIN(2.0)', 'value'], ['1.0 / 0.0', 'divide'], ['MAX_VALUE(FLOAT64) * 2.0', 'overflow'], ['INF()', 'overflow'], ['LOG1P(-1.0)', 'value']]) { assert.equal(run(`OUTPUT ${e}`).error?.kind, kind, e); } assert.deepEqual(out('USE IEEE\nOUTPUT SQRT(-1.0), " ", 1.0 / 0.0, " ", -1.0 / 0.0, " ", IS_NAN(NAN()), " ", NAN() == NAN()'), ['not a number infinity -infinity TRUE FALSE']); // whole-number division by zero still stops: IEEE 754 is about FLOAT values assert.equal(run('USE IEEE\nOUTPUT 1 DIV 0').error?.kind, 'divide'); }); test('names: a program may still call its variable E or its function GCD, and its own definition wins', () => { assert.deepEqual(out('DECLARE E : FLOAT <- 2.0\nOUTPUT E * 2.0, " ", ROUND(E() * 100)'), ['4.0 272']); assert.deepEqual(out('FUNCTION GCD(A : INTEGER, B : INTEGER) RETURNS INTEGER\n RETURN 7\nENDFUNCTION\nOUTPUT GCD(12, 18)'), ['7']); assert.ok(errors('DECLARE LENGTH : INTEGER').includes('builtin-name')); assert.ok(errors('DECLARE INT8 : INTEGER').length > 0); }); test('the prover: sized arithmetic carries an overflow obligation; FLOAT32 is a function of its own; USE IEEE proves nothing', () => { const report = (src) => provePseudo(studyOf(src)).units.flatMap((u) => u.obligations.map((o) => ({ ...o, unit: u.name, notes: u.notes }))); const guarded = report('FUNCTION Inc(X : INT8) RETURNS INT8\n REQUIRE X < 127\n RETURN X + 1\nENDFUNCTION\n'); assert.deepEqual(guarded.filter((o) => o.kind === 'overflow').map((o) => o.status), ['proved']); const open = report('FUNCTION Inc(X : INT8) RETURNS INT8\n RETURN X + 1\nENDFUNCTION\n'); assert.deepEqual(open.filter((o) => o.kind === 'overflow').map((o) => o.status), ['broken']); // the fuzzer tries 127 const narrow = report('FUNCTION N(K : INTEGER) RETURNS UINT8\n REQUIRE 0 <= K AND K < 256\n RETURN K\nENDFUNCTION\n'); assert.deepEqual(narrow.filter((o) => o.kind === 'overflow').map((o) => o.status), ['proved']); const loop = report('FUNCTION Count(N : UINT8) RETURNS UINT8\n DECLARE C : UINT8 <- 0\n FOR I <- 0 TO N\n INVARIANT C == I\n C <- C + 1\n NEXT I\n RETURN C\nENDFUNCTION\n'); assert.ok(loop.filter((o) => o.kind === 'overflow').every((o) => o.status === 'proved'), JSON.stringify(loop.map((o) => [o.kind, o.status]))); const half = report('FUNCTION Half(X : FLOAT32) RETURNS FLOAT32\n ENSURE RESULT == X / 2.0\n RETURN X / 2.0\nENDFUNCTION\n'); assert.ok(half.find((o) => o.kind === 'ensure').status === 'proved'); const roots = report('FUNCTION R(X : FLOAT) RETURNS FLOAT\n RETURN SQRT(X)\nENDFUNCTION\n'); assert.deepEqual(roots.filter((o) => o.kind === 'argument').map((o) => o.status), ['broken']); const ieee = report('USE IEEE\nFUNCTION F(X : INTEGER) RETURNS INTEGER\n REQUIRE X > 0\n RETURN 10 DIV X\nENDFUNCTION\n'); assert.ok(ieee.length && ieee.every((o) => o.status !== 'proved')); assert.ok(ieee[0].notes.some((n) => /USE IEEE/.test(n))); }); test('the fuzzer reaches the edges of a sized type and generates FLOAT32 values', async () => { const { checkProperties } = await import('../dist/engines/pseudo/testing.js'); const st = studyOf('FUNCTION F(X : UINT8, Y : FLOAT32) RETURNS BOOL\n ASSERT X != 255\n ASSERT FLOAT32(Y) == Y\n RETURN TRUE\nENDFUNCTION\n'); const fails = checkProperties(st.program.program, 'F', { count: 300 }).failures; assert.ok(fails.some((f) => f.kind === 'assert' && f.line === 2), 'X = 255 is never tried'); assert.ok(!fails.some((f) => f.line === 3), 'a generated FLOAT32 is not a single'); });