From eae54d46b7e5a3ac43ab7c8415b6896e02a0e403 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Sun, 11 Oct 2026 16:37:26 +0100 Subject: [PATCH] Reals read to the nearest F64, exactly FPC's Val could be an ulp off (5.319372648326541E255) and let 1.8E308 through as inf. ReadReal makes the digits a fraction of two big ints, scales it to 53-54 bits, divides, and rounds ties to even; subnormals, and below half the least is 0. Past the largest is the error it was meant to be. Matches Python on 6,000 literals. compiler.md's table refreshed: rloc counted one more line in each file. Co-Authored-By: Claude Opus 5.5 (1M context) --- diff --git a/doc/compiler.md b/doc/compiler.md index f9a4427..0afb0c1 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -8,15 +8,15 @@ | File | Lines | Code | What | |---|---|---|---| -| `src/diag.pas` | 274 | 197 | Messages: positions, source excerpts, colour, counts | -| `src/scan.pas` | 706 | 568 | Symbols, literals, comments, positions | -| `src/sym.pas` | 593 | 493 | Types, objects, scopes, layout | -| `src/x64.pas` | 693 | 556 | Code buffer, chunks, fixups, places, instruction encoder | -| `src/gen.pas` | 1,596 | 1,378 | Items: loading, storing, copying, frames, calls | -| `src/parse.pas` | 2,443 | 2,201 | Declarations, statements, expressions, built-ins, recovery, warnings | -| `src/link.pas` | 180 | 138 | Tree shaking, layout, patching, the places | -| `src/elf.pas` | 425 | 351 | Static and dynamic ELF, symbol table, line numbers | -| `src/raz.pas` | 227 | 178 | The command line, the entry points | +| `src/diag.pas` | 274 | 198 | Messages: positions, source excerpts, colour, counts | +| `src/scan.pas` | 851 | 722 | Symbols, literals, comments, positions | +| `src/sym.pas` | 593 | 494 | Types, objects, scopes, layout | +| `src/x64.pas` | 693 | 557 | Code buffer, chunks, fixups, places, instruction encoder | +| `src/gen.pas` | 1,596 | 1,379 | Items: loading, storing, copying, frames, calls | +| `src/parse.pas` | 2,443 | 2,202 | Declarations, statements, expressions, built-ins, recovery, warnings | +| `src/link.pas` | 180 | 139 | Tree shaking, layout, patching, the places | +| `src/elf.pas` | 425 | 352 | Static and dynamic ELF, symbol table, line numbers | +| `src/raz.pas` | 227 | 179 | The command line, the entry points | Every file opens with a comment that says what it is, what it depends on and what it leaves out. Read those first; they are the map. @@ -132,7 +132,9 @@ Read `Next` first: it skips white space and comments (`Comment`, which counts nesting), then dispatches on the first character. `Number` handles decimal, `0FFH`-style hexadecimal and reals; `LitChar` handles -one character of a literal, escapes included. +one character of a literal, escapes included. `ReadReal` turns a real's +digits into the nearest `F64` exactly, as a fraction of two big +integers (`TBig`), since Free Pascal's `Val` can be one bit off. Identifiers are *interned*: `Intern` puts each distinct name once in a pool and hands back a small integer, so every later comparison of names diff --git a/src/scan.pas b/src/scan.pas index e3ada9d..3f44fd0 100644 --- a/src/scan.pas +++ b/src/scan.pas @@ -529,8 +529,154 @@ until depth = 0; end; +{ Reading a decimal real exactly: the F64 nearest to digits times 10 to + an exponent, ties to the even one, which a library's Val doesn't + promise. The number is a fraction of two big integers, num / den, + scaled by 2^e until its whole part has 53 or 54 bits; that part and + the remainder give the bits and the rounding. } +type + TBig = record + n: longint; { words in use; 0 is zero } + w: array[0..95] of longword; { least significant first } + end; + +{ a := a * m + add } +procedure BigMul(var a: TBig; m, add: longword); +var i: longint; c: qword; +begin + c := add; + for i := 0 to a.n - 1 do begin + c := qword(a.w[i]) * m + c; + a.w[i] := longword(c); + c := c shr 32; + end; + if c <> 0 then begin a.w[a.n] := longword(c); inc(a.n) end; +end; + +procedure BigShl(var a: TBig; k: longint); +var i, words, bits: longint; +begin + if a.n = 0 then exit; + words := k div 32; bits := k mod 32; + a.w[a.n + words] := 0; + for i := a.n - 1 downto 0 do begin + if bits > 0 then a.w[i + words + 1] := a.w[i + words + 1] or (a.w[i] shr (32 - bits)); + a.w[i + words] := a.w[i] shl bits; + end; + for i := 0 to words - 1 do a.w[i] := 0; + inc(a.n, words + 1); + while (a.n > 0) and (a.w[a.n - 1] = 0) do dec(a.n); +end; + +procedure BigShr1(var a: TBig); +var i: longint; +begin + for i := 0 to a.n - 1 do begin + a.w[i] := a.w[i] shr 1; + if i + 1 < a.n then a.w[i] := a.w[i] or (a.w[i + 1] shl 31); + end; + while (a.n > 0) and (a.w[a.n - 1] = 0) do dec(a.n); +end; + +function BigCmp(const a, b: TBig): longint; +var i: longint; +begin + if a.n <> b.n then exit(ord(a.n > b.n) * 2 - 1); + for i := a.n - 1 downto 0 do + if a.w[i] <> b.w[i] then exit(ord(a.w[i] > b.w[i]) * 2 - 1); + BigCmp := 0; +end; + +{ a := a - b, b not above a } +procedure BigSub(var a: TBig; const b: TBig); +var i: longint; d: int64; +begin + d := 0; + for i := 0 to a.n - 1 do begin + d := d + a.w[i]; + if i < b.n then d := d - b.w[i]; + a.w[i] := longword(d); + d := -ord(d < 0); { a borrow } + end; + while (a.n > 0) and (a.w[a.n - 1] = 0) do dec(a.n); +end; + +function BigBits(const a: TBig): longint; +var t: longword; +begin + if a.n = 0 then exit(0); + BigBits := (a.n - 1) * 32; + t := a.w[a.n - 1]; + while t <> 0 do begin inc(BigBits); t := t shr 1 end; +end; + +{ s is digits, a point, digits, and maybe E, a sign and digits. False if + the value is past the largest F64; one below the smallest is 0. } +function ReadReal(const s: shortstring; out x: double): boolean; +var + num, den, d: TBig; i, digits, e, ex: longint; neg, half, sticky: boolean; + q, b: qword; +begin + num.n := 0; den.n := 1; den.w[0] := 1; + digits := 0; e := 0; i := 1; + while (i <= length(s)) and (s[i] <> 'E') do begin + if s[i] = '.' then e := 1 + else begin + BigMul(num, 10, ord(s[i]) - ord('0')); + if num.n > 0 then inc(digits); { counted from the first that isn't 0 } + if e > 0 then inc(e); + end; + inc(i); + end; + if e > 0 then e := 1 - e; { minus the digits after the point } + ex := 0; neg := false; + if i <= length(s) then begin + inc(i); + neg := s[i] = '-'; + if s[i] in ['+', '-'] then inc(i); + for i := i to length(s) do + if ex < 100000 then ex := ex * 10 + ord(s[i]) - ord('0'); + end; + if neg then ex := -ex; + inc(e, ex); + x := 0.0; + if num.n = 0 then exit(true); + if digits + e > 310 then exit(false); { 10^310 is past every F64 } + if digits + e < -330 then exit(true); { and 10^-330 below half the least } + for i := 1 to e do BigMul(num, 10, 0); + for i := 1 to -e do BigMul(den, 10, 0); + { num / den 2^-e from 2^52 to 2^54, but not past the least exponent } + e := BigBits(num) - BigBits(den) - 53; + if e < -1074 then e := -1074; + if e < 0 then BigShl(num, -e) else BigShl(den, e); + { q, its whole part, by long division; num is left the remainder } + d := den; BigShl(d, 53); + q := 0; + for i := 53 downto 0 do begin + if BigCmp(num, d) >= 0 then begin BigSub(num, d); q := q or (qword(1) shl i) end; + BigShr1(d); + end; + if q >= qword(1) shl 53 then begin { a bit too many: it rounds } + half := odd(q); sticky := num.n > 0; + q := q shr 1; inc(e); + end else begin { the remainder against half of den } + BigShl(num, 1); + i := BigCmp(num, den); + half := i >= 0; sticky := i > 0; + end; + if half and (sticky or odd(q)) then begin + inc(q); + if q = qword(1) shl 53 then begin q := q shr 1; inc(e) end; + end; + if e > 971 then exit(false); + if q >= qword(1) shl 52 then b := qword(e + 1075) shl 52 or (q and (qword(1) shl 52 - 1)) + else b := q; { below the least normal, e is -1074 } + Move(b, x, 8); + ReadReal := true; +end; + procedure Number; -var start, i, d: longint; allDec: boolean; v: qword; s: shortstring; code: word; +var start, i, d: longint; allDec: boolean; v: qword; s: shortstring; begin start := cur.pos; allDec := true; @@ -560,8 +706,7 @@ if cur.pos - start > 255 then LexError('a number has at most 255 characters'); SetLength(s, cur.pos - start); Move(cur.buf[start], s[1], cur.pos - start); - Val(s, rval, code); - if code <> 0 then LexError(Text(start, cur.pos) + ' is out of the range of F64'); + if not ReadReal(s, rval) then LexError(Text(start, cur.pos) + ' is out of the range of F64'); tok := sReal; end else begin if not allDec then LexError('a hex number ends with H'); diff --git a/test/err/ScanRealBig.err b/test/err/ScanRealBig.err new file mode 100644 index 0000000..265f29b --- /dev/null +++ b/test/err/ScanRealBig.err @@ -0,0 +1,4 @@ +test/err/ScanRealBig.raz:5:10: error: 1.8E308 is out of the range of F64 + 5 | x := 1.8E308; + | ^^^^^^^ +raz: 1 error diff --git a/test/err/ScanRealBig.raz b/test/err/ScanRealBig.raz new file mode 100644 index 0000000..5734c3e --- /dev/null +++ b/test/err/ScanRealBig.raz @@ -0,0 +1,7 @@ +module ScanRealBig; +proc main*; +var x: F64; +begin + x := 1.8E308; +end; +end ScanRealBig. diff --git a/test/run/RealRead.expect b/test/run/RealRead.expect new file mode 100644 index 0000000..9255818 --- /dev/null +++ b/test/run/RealRead.expect @@ -0,0 +1,12 @@ +7506AC5B262CA1FF +3FB999999999999A +FFFFFFFFFFFFF +1 +1 +0 +0 +7FEFFFFFFFFFFFFF +4340000000000000 +4340000000000002 +3FF0000000000000 +3FF0000000000001 diff --git a/test/run/RealRead.raz b/test/run/RealRead.raz new file mode 100644 index 0000000..3f71900 --- /dev/null +++ b/test/run/RealRead.raz @@ -0,0 +1,34 @@ +(* Real literals read to the nearest F64, ties to the even one: their + bits, for numbers a reader of decimals easily gets one off. *) +module RealRead; +import Fmt; + +proc bits(x: F64): U64; +var p: ^U64; +begin + p := ^U64(ADR(x)); + return p^; +end; + +proc show(x: F64); +begin + Fmt.print("{}\n", [Fmt.hex(bits(x))]); +end; + +proc main*; +begin + show(5.319372648326541E255); + show(0.1); + show(2.2250738585072011E-308); (* just below the least normal *) + show(4.9E-324); (* the least F64 *) + show(2.4703282292062328E-324); (* just over half of it: up *) + show(2.4703282292062327E-324); (* just under: 0 *) + show(1.0E-400); + show(1.7976931348623157E308); (* the largest *) + show(9007199254740993.0); (* 2^53 + 1, a tie: down to even *) + show(9007199254740995.0); (* 2^53 + 3, a tie: up to even *) + show(1.00000000000000011102230246251565404236316680908203125); (* 1 + 2^-53, a tie *) + show(1.00000000000000011102230246251565404236316680908203126); +end; + +end RealRead.