raz / commit eae54d46

commit
eae54d46b7e5a3ac43ab7c8415b6896e02a0e403
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
f28bc869
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) <noreply@anthropic.com>

patch · log

6 files changed, +217 -13

doc/compiler.md (history) modified +12 -10

@@ -8,15 +8,15 @@
88
99 | File | Lines | Code | What |
1010 |---|---|---|---|
11-| `src/diag.pas` | 274 | 197 | Messages: positions, source excerpts, colour, counts |
12-| `src/scan.pas` | 706 | 568 | Symbols, literals, comments, positions |
13-| `src/sym.pas` | 593 | 493 | Types, objects, scopes, layout |
14-| `src/x64.pas` | 693 | 556 | Code buffer, chunks, fixups, places, instruction encoder |
15-| `src/gen.pas` | 1,596 | 1,378 | Items: loading, storing, copying, frames, calls |
16-| `src/parse.pas` | 2,443 | 2,201 | Declarations, statements, expressions, built-ins, recovery, warnings |
17-| `src/link.pas` | 180 | 138 | Tree shaking, layout, patching, the places |
18-| `src/elf.pas` | 425 | 351 | Static and dynamic ELF, symbol table, line numbers |
19-| `src/raz.pas` | 227 | 178 | The command line, the entry points |
11+| `src/diag.pas` | 274 | 198 | Messages: positions, source excerpts, colour, counts |
12+| `src/scan.pas` | 851 | 722 | Symbols, literals, comments, positions |
13+| `src/sym.pas` | 593 | 494 | Types, objects, scopes, layout |
14+| `src/x64.pas` | 693 | 557 | Code buffer, chunks, fixups, places, instruction encoder |
15+| `src/gen.pas` | 1,596 | 1,379 | Items: loading, storing, copying, frames, calls |
16+| `src/parse.pas` | 2,443 | 2,202 | Declarations, statements, expressions, built-ins, recovery, warnings |
17+| `src/link.pas` | 180 | 139 | Tree shaking, layout, patching, the places |
18+| `src/elf.pas` | 425 | 352 | Static and dynamic ELF, symbol table, line numbers |
19+| `src/raz.pas` | 227 | 179 | The command line, the entry points |
2020
2121 Every file opens with a comment that says what it is, what it depends
2222 on and what it leaves out. Read those first; they are the map.
@@ -132,7 +132,9 @@
132132 Read `Next` first: it skips white space and comments (`Comment`, which
133133 counts nesting), then dispatches on the first character. `Number`
134134 handles decimal, `0FFH`-style hexadecimal and reals; `LitChar` handles
135-one character of a literal, escapes included.
135+one character of a literal, escapes included. `ReadReal` turns a real's
136+digits into the nearest `F64` exactly, as a fraction of two big
137+integers (`TBig`), since Free Pascal's `Val` can be one bit off.
136138
137139 Identifiers are *interned*: `Intern` puts each distinct name once in a
138140 pool and hands back a small integer, so every later comparison of names

src/scan.pas modified +148 -3

@@ -529,8 +529,154 @@
529529 until depth = 0;
530530 end;
531531
532+{ Reading a decimal real exactly: the F64 nearest to digits times 10 to
533+ an exponent, ties to the even one, which a library's Val doesn't
534+ promise. The number is a fraction of two big integers, num / den,
535+ scaled by 2^e until its whole part has 53 or 54 bits; that part and
536+ the remainder give the bits and the rounding. }
537+type
538+ TBig = record
539+ n: longint; { words in use; 0 is zero }
540+ w: array[0..95] of longword; { least significant first }
541+ end;
542+
543+{ a := a * m + add }
544+procedure BigMul(var a: TBig; m, add: longword);
545+var i: longint; c: qword;
546+begin
547+ c := add;
548+ for i := 0 to a.n - 1 do begin
549+ c := qword(a.w[i]) * m + c;
550+ a.w[i] := longword(c);
551+ c := c shr 32;
552+ end;
553+ if c <> 0 then begin a.w[a.n] := longword(c); inc(a.n) end;
554+end;
555+
556+procedure BigShl(var a: TBig; k: longint);
557+var i, words, bits: longint;
558+begin
559+ if a.n = 0 then exit;
560+ words := k div 32; bits := k mod 32;
561+ a.w[a.n + words] := 0;
562+ for i := a.n - 1 downto 0 do begin
563+ if bits > 0 then a.w[i + words + 1] := a.w[i + words + 1] or (a.w[i] shr (32 - bits));
564+ a.w[i + words] := a.w[i] shl bits;
565+ end;
566+ for i := 0 to words - 1 do a.w[i] := 0;
567+ inc(a.n, words + 1);
568+ while (a.n > 0) and (a.w[a.n - 1] = 0) do dec(a.n);
569+end;
570+
571+procedure BigShr1(var a: TBig);
572+var i: longint;
573+begin
574+ for i := 0 to a.n - 1 do begin
575+ a.w[i] := a.w[i] shr 1;
576+ if i + 1 < a.n then a.w[i] := a.w[i] or (a.w[i + 1] shl 31);
577+ end;
578+ while (a.n > 0) and (a.w[a.n - 1] = 0) do dec(a.n);
579+end;
580+
581+function BigCmp(const a, b: TBig): longint;
582+var i: longint;
583+begin
584+ if a.n <> b.n then exit(ord(a.n > b.n) * 2 - 1);
585+ for i := a.n - 1 downto 0 do
586+ if a.w[i] <> b.w[i] then exit(ord(a.w[i] > b.w[i]) * 2 - 1);
587+ BigCmp := 0;
588+end;
589+
590+{ a := a - b, b not above a }
591+procedure BigSub(var a: TBig; const b: TBig);
592+var i: longint; d: int64;
593+begin
594+ d := 0;
595+ for i := 0 to a.n - 1 do begin
596+ d := d + a.w[i];
597+ if i < b.n then d := d - b.w[i];
598+ a.w[i] := longword(d);
599+ d := -ord(d < 0); { a borrow }
600+ end;
601+ while (a.n > 0) and (a.w[a.n - 1] = 0) do dec(a.n);
602+end;
603+
604+function BigBits(const a: TBig): longint;
605+var t: longword;
606+begin
607+ if a.n = 0 then exit(0);
608+ BigBits := (a.n - 1) * 32;
609+ t := a.w[a.n - 1];
610+ while t <> 0 do begin inc(BigBits); t := t shr 1 end;
611+end;
612+
613+{ s is digits, a point, digits, and maybe E, a sign and digits. False if
614+ the value is past the largest F64; one below the smallest is 0. }
615+function ReadReal(const s: shortstring; out x: double): boolean;
616+var
617+ num, den, d: TBig; i, digits, e, ex: longint; neg, half, sticky: boolean;
618+ q, b: qword;
619+begin
620+ num.n := 0; den.n := 1; den.w[0] := 1;
621+ digits := 0; e := 0; i := 1;
622+ while (i <= length(s)) and (s[i] <> 'E') do begin
623+ if s[i] = '.' then e := 1
624+ else begin
625+ BigMul(num, 10, ord(s[i]) - ord('0'));
626+ if num.n > 0 then inc(digits); { counted from the first that isn't 0 }
627+ if e > 0 then inc(e);
628+ end;
629+ inc(i);
630+ end;
631+ if e > 0 then e := 1 - e; { minus the digits after the point }
632+ ex := 0; neg := false;
633+ if i <= length(s) then begin
634+ inc(i);
635+ neg := s[i] = '-';
636+ if s[i] in ['+', '-'] then inc(i);
637+ for i := i to length(s) do
638+ if ex < 100000 then ex := ex * 10 + ord(s[i]) - ord('0');
639+ end;
640+ if neg then ex := -ex;
641+ inc(e, ex);
642+ x := 0.0;
643+ if num.n = 0 then exit(true);
644+ if digits + e > 310 then exit(false); { 10^310 is past every F64 }
645+ if digits + e < -330 then exit(true); { and 10^-330 below half the least }
646+ for i := 1 to e do BigMul(num, 10, 0);
647+ for i := 1 to -e do BigMul(den, 10, 0);
648+ { num / den 2^-e from 2^52 to 2^54, but not past the least exponent }
649+ e := BigBits(num) - BigBits(den) - 53;
650+ if e < -1074 then e := -1074;
651+ if e < 0 then BigShl(num, -e) else BigShl(den, e);
652+ { q, its whole part, by long division; num is left the remainder }
653+ d := den; BigShl(d, 53);
654+ q := 0;
655+ for i := 53 downto 0 do begin
656+ if BigCmp(num, d) >= 0 then begin BigSub(num, d); q := q or (qword(1) shl i) end;
657+ BigShr1(d);
658+ end;
659+ if q >= qword(1) shl 53 then begin { a bit too many: it rounds }
660+ half := odd(q); sticky := num.n > 0;
661+ q := q shr 1; inc(e);
662+ end else begin { the remainder against half of den }
663+ BigShl(num, 1);
664+ i := BigCmp(num, den);
665+ half := i >= 0; sticky := i > 0;
666+ end;
667+ if half and (sticky or odd(q)) then begin
668+ inc(q);
669+ if q = qword(1) shl 53 then begin q := q shr 1; inc(e) end;
670+ end;
671+ if e > 971 then exit(false);
672+ if q >= qword(1) shl 52 then b := qword(e + 1075) shl 52 or (q and (qword(1) shl 52 - 1))
673+ else b := q; { below the least normal, e is -1074 }
674+ Move(b, x, 8);
675+ ReadReal := true;
676+end;
677+
532678 procedure Number;
533-var start, i, d: longint; allDec: boolean; v: qword; s: shortstring; code: word;
679+var start, i, d: longint; allDec: boolean; v: qword; s: shortstring;
534680 begin
535681 start := cur.pos;
536682 allDec := true;
@@ -560,8 +706,7 @@
560706 if cur.pos - start > 255 then LexError('a number has at most 255 characters');
561707 SetLength(s, cur.pos - start);
562708 Move(cur.buf[start], s[1], cur.pos - start);
563- Val(s, rval, code);
564- if code <> 0 then LexError(Text(start, cur.pos) + ' is out of the range of F64');
709+ if not ReadReal(s, rval) then LexError(Text(start, cur.pos) + ' is out of the range of F64');
565710 tok := sReal;
566711 end else begin
567712 if not allDec then LexError('a hex number ends with H');

test/err/ScanRealBig.err added +4 -0

@@ -0,0 +1,4 @@
1+test/err/ScanRealBig.raz:5:10: error: 1.8E308 is out of the range of F64
2+ 5 | x := 1.8E308;
3+ | ^^^^^^^
4+raz: 1 error

test/err/ScanRealBig.raz added +7 -0

@@ -0,0 +1,7 @@
1+module ScanRealBig;
2+proc main*;
3+var x: F64;
4+begin
5+ x := 1.8E308;
6+end;
7+end ScanRealBig.

test/run/RealRead.expect added +12 -0

@@ -0,0 +1,12 @@
1+7506AC5B262CA1FF
2+3FB999999999999A
3+FFFFFFFFFFFFF
4+1
5+1
6+0
7+0
8+7FEFFFFFFFFFFFFF
9+4340000000000000
10+4340000000000002
11+3FF0000000000000
12+3FF0000000000001

test/run/RealRead.raz added +34 -0

@@ -0,0 +1,34 @@
1+(* Real literals read to the nearest F64, ties to the even one: their
2+ bits, for numbers a reader of decimals easily gets one off. *)
3+module RealRead;
4+import Fmt;
5+
6+proc bits(x: F64): U64;
7+var p: ^U64;
8+begin
9+ p := ^U64(ADR(x));
10+ return p^;
11+end;
12+
13+proc show(x: F64);
14+begin
15+ Fmt.print("{}\n", [Fmt.hex(bits(x))]);
16+end;
17+
18+proc main*;
19+begin
20+ show(5.319372648326541E255);
21+ show(0.1);
22+ show(2.2250738585072011E-308); (* just below the least normal *)
23+ show(4.9E-324); (* the least F64 *)
24+ show(2.4703282292062328E-324); (* just over half of it: up *)
25+ show(2.4703282292062327E-324); (* just under: 0 *)
26+ show(1.0E-400);
27+ show(1.7976931348623157E308); (* the largest *)
28+ show(9007199254740993.0); (* 2^53 + 1, a tie: down to even *)
29+ show(9007199254740995.0); (* 2^53 + 3, a tie: up to even *)
30+ show(1.00000000000000011102230246251565404236316680908203125); (* 1 + 2^-53, a tie *)
31+ show(1.00000000000000011102230246251565404236316680908203126);
32+end;
33+
34+end RealRead.