Skip to content

Commit

Permalink
/ issue #60: A basic bit depth of integer arithmetic for Component Pa…
Browse files Browse the repository at this point in the history
…scal.
  • Loading branch information
Oleg-N-Cher committed Mar 13, 2019
1 parent aa07cb1 commit 93a3b4b
Show file tree
Hide file tree
Showing 22 changed files with 568 additions and 483 deletions.
Binary file modified Bin/ofront+.exe
Binary file not shown.
8 changes: 4 additions & 4 deletions Lib/Mod/Files.Mod
Expand Up @@ -380,7 +380,7 @@ MODULE Files; (* J. Templ 1.12. 89/12.4.95 Oberon files mapped onto Unix files
ELSE buf := f.bufs[i]
END
ELSE
f.swapper := (f.swapper + 1) MOD nofbufs;
f.swapper := SHORT((f.swapper + 1) MOD nofbufs);
buf := f.bufs[f.swapper];
Flush(buf)
END;
Expand Down Expand Up @@ -612,7 +612,7 @@ Especially Length would become fairly complex.
IF SIZE(SHORTINT) = 1 THEN
x := ORD(b[0])
ELSE
x := ORD(b[0]) + ORD(b[1])*256
x := SHORT(ORD(b[0]) + ORD(b[1])*256)
END
END ReadSInt;

Expand Down Expand Up @@ -677,8 +677,8 @@ Especially Length would become fairly complex.
PROCEDURE ReadNum* (VAR R: Rider; VAR x: LONGINT);
VAR s: INTEGER; ch: SHORTCHAR;
BEGIN s := 0; x := 0; ReadChar(R, ch);
WHILE ORD(ch) >= 128 DO INC(x, ASH(LONG(LONG(ORD(ch) - 128)), s) ); INC(s, 7); ReadChar(R, ch) END;
INC(x, ASH(LONG(LONG(ORD(ch) MOD 64 - ORD(ch) DIV 64 * 64)), s) )
WHILE ORD(ch) >= 128 DO INC(x, ASH(LONG(ORD(ch) - 128), s) ); INC(s, 7); ReadChar(R, ch) END;
INC(x, ASH(LONG(ORD(ch) MOD 64 - ORD(ch) DIV 64 * 64), s) )
END ReadNum;

PROCEDURE WriteBool* (VAR R: Rider; x: BOOLEAN);
Expand Down
2 changes: 1 addition & 1 deletion Lib/Mod/Graph.Mod
Expand Up @@ -1397,7 +1397,7 @@ END Random;
PROCEDURE Uniform*(): SHORTREAL;
BEGIN
NextRND;
RETURN (randomSeed - 1) * (1 / (randomModulo - 1))
RETURN SHORT((randomSeed - 1) * (1 / (randomModulo - 1)))
END Uniform;

PROCEDURE Randomize*;
Expand Down
18 changes: 9 additions & 9 deletions Lib/Mod/Math.Mod
Expand Up @@ -163,7 +163,7 @@ PROCEDURE exponent*(x: SHORTREAL): SHORTINT;
BEGIN
IF x = ZERO THEN RETURN 0 (* NOTE: x=0.0 should raise exception *)
ELSE
RETURN SHORT(SYSTEM.LSH(SYSTEM.VAL(INTEGER, x), -23) MOD 256) - 127
RETURN SHORT(SYSTEM.LSH(SYSTEM.VAL(INTEGER, x), -23) MOD 256 - 127)
END
END exponent;

Expand Down Expand Up @@ -201,7 +201,7 @@ PROCEDURE ulp*(x: SHORTREAL): SHORTREAL;
value exists; otherwise an exception shall occur and may be raised.
*)
BEGIN
RETURN scale(ONE, exponent(x)-places+1)
RETURN scale(ONE, SHORT(exponent(x)-places+1))
END ulp;

PROCEDURE succ*(x: SHORTREAL): SHORTREAL;
Expand Down Expand Up @@ -291,7 +291,7 @@ BEGIN
IF x < ZERO THEN ErrorHandler(IllegalRoot); x := -x END;

(* reduce the input number to the range 0.5 <= x <= 1.0 *)
xMant := fraction(x) * HALF; xExp := exponent(x) + 1;
xMant := fraction(x) * HALF; xExp := SHORT(exponent(x) + 1);

(* initial estimate of the square root *)
yEst := P0 + P1 * xMant;
Expand All @@ -303,7 +303,7 @@ BEGIN
IF ODD(xExp) THEN yEst := yEst * sqrtHalf; INC(xExp) END;

(* single Newtonian iteration to produce real number accuracy *)
RETURN scale(yEst, xExp DIV 2)
RETURN scale(yEst, SHORT(xExp DIV 2))
END sqrt;

PROCEDURE exp* (x: SHORTREAL): SHORTREAL;
Expand Down Expand Up @@ -339,7 +339,7 @@ BEGIN
IF x <= ZERO THEN ErrorHandler(IllegalLog); RETURN -large END;

(* reduce the range of the input *)
f := fraction(x) * HALF; n := exponent(x) + 1;
f := fraction(x) * HALF; n := SHORT(exponent(x) + 1);
IF f > sqrtHalf THEN zn := (f - HALF) - HALF; zd := f * HALF + HALF
ELSE zn := f - HALF; zd := zn * HALF + HALF; DEC(n)
END;
Expand Down Expand Up @@ -526,7 +526,7 @@ BEGIN

(* extract the exponent of base to m and clear exponent of base in g *)
g := fraction(base) * HALF;
m := exponent(base) + 1;
m := SHORT(exponent(base) + 1);

(* determine p table offset with an unrolled binary search *)
p := 1;
Expand Down Expand Up @@ -600,17 +600,17 @@ BEGIN
END;

(* trap potential overflows and underflows *)
Exp := (exponent(x) + 1) * base; y := LnInfinity * ln2Inv;
Exp := SHORT((exponent(x) + 1) * base); y := LnInfinity * ln2Inv;
IF Exp > y THEN ErrorHandler(Overflow); RETURN Adjust(large)
ELSIF Exp< - y THEN RETURN ZERO
END;

(* compute x * *base using an optimised algorithm from Knuth, slightly
altered : p442, The Art Of Computer Programming, Vol 2 *)
y := ONE; IF base < 0 THEN neg := TRUE; base := -base ELSE neg:= FALSE END;
y := ONE; IF base < 0 THEN neg := TRUE; base := SHORT(-base) ELSE neg:= FALSE END;
LOOP
IF ODD(base) THEN y := y * x END;
base := base DIV 2; IF base = 0 THEN EXIT END;
base := SHORT(base DIV 2); IF base = 0 THEN EXIT END;
x := x * x;
END;
IF neg THEN RETURN ONE/y ELSE RETURN y END
Expand Down
4 changes: 2 additions & 2 deletions Lib/Mod/MathL.Mod
Expand Up @@ -522,10 +522,10 @@ BEGIN

(* compute x**base using an optimised algorithm from Knuth, slightly
altered : p442, The Art Of Computer Programming, Vol 2 *)
y := ONE; IF base < 0 THEN neg := TRUE; base := -base ELSE neg:= FALSE END;
y := ONE; IF base < 0 THEN neg := TRUE; base := SHORT(-base) ELSE neg:= FALSE END;
LOOP
IF ODD(base) THEN y := y*x END;
base := base DIV 2; IF base=0 THEN EXIT END;
base := SHORT(base DIV 2); IF base=0 THEN EXIT END;
x := x*x;
END;
IF neg THEN RETURN ONE/y ELSE RETURN y END
Expand Down
42 changes: 21 additions & 21 deletions Lib/Mod/Out.Mod
Expand Up @@ -114,7 +114,7 @@ VAR r, power: LONGREAL;
BEGIN r := 1.0D0; power := 1.0D1;
WHILE e > 0 DO
IF ODD(e) THEN r := r*power END;
power := power*power; e := e DIV 2
power := power*power; e := SHORT(e DIV 2)
END;
RETURN r
END Ten;
Expand Down Expand Up @@ -156,13 +156,13 @@ BEGIN
number of digits to generate. *)
IF long THEN
el := 3;
dr := n-6; (* Leave room for dp and '+D000' *)
dr := SHORT(n-6); (* Leave room for dp and '+D000' *)
IF dr > 17 THEN dr := 17 END; (* Limit to max useful significant digits *)
d := dr; (* Number of digits to generate *)
IF d < 15 THEN d := 15 END (* Generate enough digits to do trailing zero supporession *)
ELSE
el := 2;
dr := n-5; (* Leave room for dp and '+E00' *)
dr := SHORT(n-5); (* Leave room for dp and '+E00' *)
IF dr > 9 THEN dr := 9 END; (* Limit to max useful significant digits *)
d := dr; (* Number of digits to generate *)
IF d < 6 THEN d := 6 END (* Generate enough digits to do trailing zero supporession *)
Expand All @@ -176,18 +176,18 @@ BEGIN
IF nn THEN x := -x END;

(* Scale e to be an exponent of 10 rather than 2 *)
e := SHORT(LONG(e - 1023) * 77 DIV 256);
IF e >= 0 THEN x := x / Ten(e) ELSE x := Ten(-e) * x END ;
e := SHORT(SHORT(LONG(e - 1023) * 77 DIV 256));
IF e >= 0 THEN x := x / Ten(e) ELSE x := Ten(SHORT(-e)) * x END ;
IF x >= 10.0D0 THEN x := 0.1D0 * x; INC(e) END;

(* Generate the exponent digits *)
en := e < 0; IF en THEN e := - e END;
WHILE el > 0 DO digit(e, s, i); e := e DIV 10; DEC(el) END;
en := e < 0; IF en THEN e := SHORT(- e) END;
WHILE el > 0 DO digit(e, s, i); e := SHORT(e DIV 10); DEC(el) END;
DEC(i); IF en THEN s[i] := "-" ELSE s[i] := "+" END;

(* Scale x to enough significant digits to reliably test for trailing
zeroes or to the amount of space available, if greater. *)
x0 := Ten(d-1);
x0 := Ten(SHORT(d-1));
x := x0 * x;
x := x + 0.5D0; (* Do not combine with previous line as doing so
introduces a least significant bit difference
Expand All @@ -209,7 +209,7 @@ BEGIN
END;

(* Generate leading padding *)
DEC(n, LEN(s)-i); WHILE n > 0 DO Char(" "); DEC(n) END;
DEC(n, SHORT(LEN(s)-i)); WHILE n > 0 DO Char(" "); DEC(n) END;

(* Render prepared number from right end of buffer s *)
IF nn THEN Char("-") END;
Expand Down Expand Up @@ -259,7 +259,7 @@ PROCEDURE Expo*(x: REAL): INTEGER;
VAR i: INTEGER;
BEGIN
SYSTEM.GET(SYSTEM.ADR(x)+2, i);
RETURN (i DIV 128) MOD 256
RETURN SHORT((i DIV 128) MOD 256)
END Expo;

PROCEDURE RealFix* (x: REAL; n, k: INTEGER);
Expand All @@ -280,28 +280,28 @@ PROCEDURE RealFix* (x: REAL; n, k: INTEGER);

BEGIN e := Expo(x);
IF k < 0 THEN k := 0 END;
IF e = 0 THEN seq(" ", n-k-2); Char("0"); seq(" ", k+1)
ELSIF e = 255 THEN String(" NaN"); seq(" ", n-4)
ELSE e := (e - 127) * 77 DIV 256;
IF e = 0 THEN seq(" ", SHORT(n-k-2)); Char("0"); seq(" ", SHORT(k+1))
ELSIF e = 255 THEN String(" NaN"); seq(" ", SHORT(n-4))
ELSE e := SHORT((e - 127) * 77 DIV 256);
IF x < 0 THEN sign := "-"; x := -x ELSE sign := " " END;
IF e >= 0 THEN (*x >= 1.0, 77/256 = log 2*) x := SHORT(x/Ten(e))
ELSE (*x < 1.0*) x := SHORT(Ten(-e) * x)
ELSE (*x < 1.0*) x := SHORT(Ten(SHORT(-e)) * x)
END;
IF x >= 10.0 THEN x := 0.1*x; INC(e) END;
(* 1 <= x < 10 *)
IF k+e >= maxD-1 THEN k := maxD-1-e
ELSIF k+e < 0 THEN k := -e; x := 0.0
IF k+e >= maxD-1 THEN k := SHORT(maxD-1-e)
ELSIF k+e < 0 THEN k := SHORT(-e); x := 0.0
END;
x0 := SHORT(Ten(k+e)); x := x0*x + 0.5;
x0 := SHORT(Ten(SHORT(k+e))); x := x0*x + 0.5;
IF x >= 10.0*x0 THEN INC(e) END;
(*e = no. of digits before decimal point*)
INC(e); i := k+e; ConvertL(x, i, d);
INC(e); i := SHORT(k+e); ConvertL(x, i, d);
IF e > 0 THEN
seq(" ", n-e-k-2); Char(sign); dig(e);
seq(" ", SHORT(n-e-k-2)); Char(sign); dig(e);
Char("."); dig(k)
ELSE seq(" ", n-k-3);
ELSE seq(" ", SHORT(n-k-3));
Char(sign); Char("0"); Char(".");
seq("0", -e); dig(k+e)
seq("0", SHORT(-e)); dig(SHORT(k+e))
END
END
END RealFix;
Expand Down
12 changes: 6 additions & 6 deletions Lib/Mod/Reals.Mod
Expand Up @@ -23,7 +23,7 @@ MODULE Reals;
power := 10.0;
WHILE e > 0 DO
IF ODD(e) THEN r := r * power END ;
power := power * power; e := e DIV 2
power := power * power; e := SHORT(e DIV 2)
END ;
RETURN SHORT(r)
END Ten;
Expand All @@ -34,7 +34,7 @@ MODULE Reals;
power := 10.0;
LOOP
IF ODD(e) THEN r := r * power END ;
e := e DIV 2;
e := SHORT(e DIV 2);
IF e <= 0 THEN RETURN r END ;
power := power * power
END
Expand Down Expand Up @@ -91,10 +91,10 @@ MODULE Reals;
VAR i, k: SHORTINT; len: LONGINT;
BEGIN i := 0; len := LEN(b);
WHILE i < len DO
k := SHORT(ORD(S.VAL(CHAR, b[i])) DIV 16);
IF k > 9 THEN d[i*2] := k + 55 ELSE d[i*2] := k + 48 END ;
k := SHORT(ORD(S.VAL(CHAR, b[i])) MOD 16);
IF k > 9 THEN d[i*2+1] := k + 55 ELSE d[i*2+1] := k + 48 END ;
k := SHORT(SHORT(ORD(S.VAL(CHAR, b[i])) DIV 16));
IF k > 9 THEN d[i*2] := SHORT(SHORT(k + 55)) ELSE d[i*2] := SHORT(SHORT(k + 48)) END;
k := SHORT(SHORT(ORD(S.VAL(CHAR, b[i])) MOD 16));
IF k > 9 THEN d[i*2+1] := SHORT(SHORT(k + 55)) ELSE d[i*2+1] := SHORT(SHORT(k + 48)) END;
INC(i)
END
END Unpack;
Expand Down
46 changes: 23 additions & 23 deletions Lib/Mod/Texts.Mod
Expand Up @@ -412,7 +412,7 @@ MODULE Texts; (** CAS/HM 23.9.93 -- interface based on Texts by JG/NW 6.12.91**
IF ch = "+" THEN Read(S, ch) END
END;
WHILE ("0" <= ch) & (ch <= "9") DO
e := e*10 + ORD(ch) - 30H; Read(S, ch)
e := SHORT(e*10 + ORD(ch) - 30H); Read(S, ch)
END
END ReadScaleFactor;

Expand All @@ -435,7 +435,7 @@ MODULE Texts; (** CAS/HM 23.9.93 -- interface based on Texts by JG/NW 6.12.91**
WHILE (ch # 22X) & (ch >= " ") & (i # 63) DO (* << *)
S.s[i] := ch; INC(i); Read(S, ch)
END;
S.s[i] := 0X; S.len := i+1; Read(S, ch); S.class := 2
S.s[i] := 0X; S.len := SHORT(SHORT(i+1)); Read(S, ch); S.class := 2
ELSE
IF ch = "-" THEN neg := TRUE; Read(S, ch) ELSE neg := FALSE END ;
IF ("0" <= ch) & (ch <= "9") THEN (*number*)
Expand All @@ -451,7 +451,7 @@ MODULE Texts; (** CAS/HM 23.9.93 -- interface based on Texts by JG/NW 6.12.91**
END;
IF ch = "H" THEN (*hex number*)
Read(S, ch); S.class := 3;
IF i-j > 8 THEN j := i-8 END ;
IF i-j > 8 THEN j := SHORT(SHORT(i-8)) END;
k := ORD(d[j]) - 30H; INC(j);
IF (i-j = 7) & (k >= 8) THEN DEC(k, 16) END ;
WHILE j < i DO k := k*10H + (ORD(d[j]) - 30H); INC(j) END ;
Expand Down Expand Up @@ -636,16 +636,16 @@ MODULE Texts; (** CAS/HM 23.9.93 -- interface based on Texts by JG/NW 6.12.91**
REPEAT Write(W, " "); DEC(n) UNTIL n <= 8;
(*there are 2 < n <= 8 digits to be written*)
IF x < 0.0 THEN Write(W, "-"); x := -x ELSE Write(W, " ") END;
e := (e - 127) * 77 DIV 256;
IF e >= 0 THEN x := x / Reals.Ten(e) ELSE x := Reals.Ten(-e) * x END;
e := SHORT((e - 127) * 77 DIV 256);
IF e >= 0 THEN x := x / Reals.Ten(e) ELSE x := Reals.Ten(SHORT(-e)) * x END;
IF x >= 10.0 THEN x := 0.1*x; INC(e) END;
x0 := Reals.Ten(n-1); x := x0*x + 0.5;
x0 := Reals.Ten(SHORT(n-1)); x := x0*x + 0.5;
IF x >= 10.0*x0 THEN x := x*0.1; INC(e) END;
Reals.Convert(x, n, d);
DEC(n); Write(W, d[n]); Write(W, ".");
REPEAT DEC(n); Write(W, d[n]) UNTIL n = 0;
Write(W, "E");
IF e < 0 THEN Write(W, "-"); e := -e ELSE Write(W, "+") END;
IF e < 0 THEN Write(W, "-"); e := SHORT(-e) ELSE Write(W, "+") END;
Write(W, CHR(e DIV 10 + 30H)); Write(W, CHR(e MOD 10 + 30H))
END
END WriteReal;
Expand All @@ -667,28 +667,28 @@ MODULE Texts; (** CAS/HM 23.9.93 -- interface based on Texts by JG/NW 6.12.91**

BEGIN e := Reals.Expo(x);
IF k < 0 THEN k := 0 END;
IF e = 0 THEN seq(" ", n-k-2); Write(W, "0"); seq(" ", k+1)
ELSIF e = 255 THEN WriteString(W, " NaN"); seq(" ", n-4)
ELSE e := (e - 127) * 77 DIV 256;
IF e = 0 THEN seq(" ", SHORT(n-k-2)); Write(W, "0"); seq(" ", SHORT(k+1))
ELSIF e = 255 THEN WriteString(W, " NaN"); seq(" ", SHORT(n-4))
ELSE e := SHORT((e - 127) * 77 DIV 256);
IF x < 0 THEN sign := "-"; x := -x ELSE sign := " " END;
IF e >= 0 THEN (*x >= 1.0, 77/256 = log 2*) x := x/Reals.Ten(e)
ELSE (*x < 1.0*) x := Reals.Ten(-e) * x
ELSE (*x < 1.0*) x := Reals.Ten(SHORT(-e)) * x
END;
IF x >= 10.0 THEN x := 0.1*x; INC(e) END;
(* 1 <= x < 10 *)
IF k+e >= maxD-1 THEN k := maxD-1-e
ELSIF k+e < 0 THEN k := -e; x := 0.0
IF k+e >= maxD-1 THEN k := SHORT(maxD-1-e)
ELSIF k+e < 0 THEN k := SHORT(-e); x := 0.0
END;
x0 := Reals.Ten(k+e); x := x0*x + 0.5;
x0 := Reals.Ten(SHORT(k+e)); x := x0*x + 0.5;
IF x >= 10.0*x0 THEN INC(e) END;
(*e = no. of digits before decimal point*)
INC(e); i := k+e; Reals.Convert(x, i, d);
INC(e); i := SHORT(k+e); Reals.Convert(x, i, d);
IF e > 0 THEN
seq(" ", n-e-k-2); Write(W, sign); dig(e);
seq(" ", SHORT(n-e-k-2)); Write(W, sign); dig(e);
Write(W, "."); dig(k)
ELSE seq(" ", n-k-3);
ELSE seq(" ", SHORT(n-k-3));
Write(W, sign); Write(W, "0"); Write(W, ".");
seq("0", -e); dig(k+e)
seq("0", SHORT(-e)); dig(SHORT(k+e))
END
END
END WriteRealFix;
Expand Down Expand Up @@ -718,12 +718,12 @@ MODULE Texts; (** CAS/HM 23.9.93 -- interface based on Texts by JG/NW 6.12.91**
IF x < 0 THEN Write(W, "-"); x := -x ELSE Write(W, " ") END;

(* Scale e to be an exponent of 10 rather than 2 *)
e := SHORT(LONG(e - 1023) * 77 DIV 256);
IF e >= 0 THEN x := x / Reals.TenL(e) ELSE x := Reals.TenL(-e) * x END ;
e := SHORT(SHORT(LONG(e - 1023) * 77 DIV 256));
IF e >= 0 THEN x := x / Reals.TenL(e) ELSE x := Reals.TenL(SHORT(-e)) * x END ;
IF x >= 10.0D0 THEN x := 0.1D0 * x; INC(e) END;

(* Scale x to the number of digits requested *)
x0 := Reals.TenL(n-1); x := x0*x + 0.5D0;
x0 := Reals.TenL(SHORT(n-1)); x := x0*x + 0.5D0;
IF x >= 10.0D0*x0 THEN x := 0.1D0 * x; INC(e) END ;

(* Generate the mantissa digits of x *)
Expand All @@ -733,8 +733,8 @@ MODULE Texts; (** CAS/HM 23.9.93 -- interface based on Texts by JG/NW 6.12.91**
REPEAT DEC(n); Write(W, d[n]) UNTIL n = 0;

Write(W, "D");
IF e < 0 THEN Write(W, "-"); e := -e ELSE Write(W, "+") END;
Write(W, CHR(e DIV 100 + 30H)); e := e MOD 100;
IF e < 0 THEN Write(W, "-"); e := SHORT(-e) ELSE Write(W, "+") END;
Write(W, CHR(e DIV 100 + 30H)); e := SHORT(e MOD 100);
Write(W, CHR(e DIV 10 + 30H));
Write(W, CHR(e MOD 10 + 30H))
END
Expand Down
2 changes: 1 addition & 1 deletion Lib/Mod/ooc2RandomNumbers.Mod
Expand Up @@ -61,7 +61,7 @@ PROCEDURE Random*() : SHORTREAL;
(**Calculates a number @samp{x} with @samp{0.0 <= x < 1.0}. *)
BEGIN
NextRND;
RETURN (z-1)*(1 / (modulo-1))
RETURN SHORT((z-1)*(1 / (modulo-1)))
END Random;

BEGIN
Expand Down
Binary file modified Mod/OPB.odc
Binary file not shown.
Binary file modified Mod/OPP.odc
Binary file not shown.
Binary file modified Mod/OPS.odc
Binary file not shown.

0 comments on commit 93a3b4b

Please sign in to comment.