raz / commit 37e1ec36

commit
37e1ec362a686083c4fe3a7201e5eeb48cd58fcd
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
1e15c9a2
Step 12: F32, and real constants that take F32 or F64

F32 arithmetic, comparisons and conversions use the single precision
SSE instructions; F32 goes to and from C and code procedures in xmm
registers. A real literal is an untyped constant, folded in double
precision and rounded once to the type it takes. An F32 given to ...
is an error.

patch · log

14 files changed, +227 -50

CLAUDE.md modified +5 -3

@@ -38,9 +38,8 @@
3838 ## Where things stand
3939
4040 Stages 1 and 2 are done (steps 1-10). Stage 3 (steps 11-14) is under
41-way: its report changes are written and approved, and step 11 is done.
42-Next is step 12, `F32`, which also makes an `F32` given to `...` an
43-error.
41+way: its report changes are written and approved, and steps 11 and 12
42+are done. Next is step 13, threads and a thread-safe heap.
4443
4544 `test/libc` runs its programs over `lib/Sys.jed`, the C one, through
4645 a link in its `mods`. A test or example without `.expect` is only
@@ -78,6 +77,9 @@
7877 name like `Rt.exit`, before any module body runs.
7978 - Past an extern's parameters, a string goes as a C string and an
8079 integer constant as an `I64`.
80+- A real constant is held as an F64; one typed `F32` holds a value an
81+ F32 can hold. An `F32` in a register is its low 32 bits; the upper
82+ ones are left as they fall.
8183 - Every failed check says "index out of range"; some errors point at
8284 the symbol after the offending expression. Error messages are to be
8385 improved later, all together.

PLAN.md (history) modified +1 -1

@@ -112,7 +112,7 @@
112112 at each newline when standard output is a terminal, when full,
113113 by `Out.flush`, and at exit. The report says how to mix `Out`
114114 with C's `stdio`: flush one before using the other.
115-- [ ] 12. `F32`: arithmetic, conversions, calls; real literals become
115+- [x] 12. `F32`: arithmetic, conversions, calls; real literals become
116116 untyped constants that take `F32` or `F64`.
117117 - [ ] 13. A thread-safe `lib/Heap.jed`: its bad sizes refused, and a
118118 marker in each block's header so that freeing a block twice, or

src/gen.pas modified +68 -28

@@ -154,12 +154,28 @@
154154 x.val := v;
155155 end;
156156
157+{ A real constant: untyped, unless typed later. Its value is an F64,
158+ which for an F32 is one an F32 can hold. }
157159 procedure MakeReal(var x: TItem; r: double);
158160 begin
159- MakeConst(x, tReal, 0);
161+ MakeConst(x, tRConst, 0);
160162 Move(r, x.val, 8);
161163 end;
162164
165+function IsF32(t: PType): boolean;
166+begin
167+ IsF32 := (t^.form = fReal) and (t^.size = 4);
168+end;
169+
170+{ Gives a real constant the real type t, rounded to it. }
171+procedure RealTo(var x: TItem; t: PType);
172+var r: double;
173+begin
174+ r := RealOf(x);
175+ if IsF32(t) then MakeReal(x, single(r)) else MakeReal(x, r);
176+ x.typ := t;
177+end;
178+
163179 function RealOf(const x: TItem): double;
164180 var r: double;
165181 begin
@@ -233,9 +249,13 @@
233249
234250 { A constant into r: an error constant is the address of its chunk. }
235251 procedure ConstTo(r: longint; const x: TItem);
252+var f: single;
236253 begin
237254 if (x.typ^.form = fError) and (x.val > 0) then Lea(r, MemRip(x.val - 1, 0))
238- else MovRI(r, x.val);
255+ else if IsF32(x.typ) then begin
256+ f := RealOf(x);
257+ MovRI(r, PLongword(@f)^);
258+ end else MovRI(r, x.val);
239259 end;
240260
241261 function MemOf(var x: TItem; r: longint): TMem;
@@ -259,7 +279,7 @@
259279
260280 function ScalarSize(t: PType): longint;
261281 begin
262- if t^.form in [fInt, fBool, fEnum, fSet] then ScalarSize := t^.size
282+ if t^.form in [fInt, fBool, fEnum, fSet, fReal] then ScalarSize := t^.size
263283 else ScalarSize := 8;
264284 end;
265285
@@ -427,7 +447,8 @@
427447 end;
428448 if x.mode in [mType, mBuiltin, mNone, mCase] then Mark('not a value');
429449 if Same(s, t) then exit;
430- if (s^.form = fConst) and (t^.form = fInt) then begin
450+ if (s = tRConst) and (t^.form = fReal) then RealTo(x, t)
451+ else if (s^.form = fConst) and (t^.form = fInt) then begin
431452 if (x.mode = mConst) and not Fits(x.val, t) then Mark('constant doesn''t fit ' + TypeName(t));
432453 x.typ := t;
433454 end else if (s^.form = fNil) and (t^.form in [fPtr, fProc, fError]) then
@@ -605,7 +626,7 @@
605626 begin
606627 if x.typ^.form = fReal then begin
607628 if x.mode = mConst then x.val := x.val xor low(int64)
608- else begin Value(x); Btc(RAX, 63) end;
629+ else begin Value(x); if IsF32(x.typ) then Btc(RAX, 31) else Btc(RAX, 63) end;
609630 end else if IsInt(x.typ) then begin
610631 if x.mode = mConst then x.val := Cut(-x.val, x.typ)
611632 else begin Value(x); Unary(3, RAX); Norm(x.typ) end;
@@ -706,7 +727,9 @@
706727 end;
707728 end;
708729
709-function FoldReal(op: TSym; const x, y: TItem; var r: TItem): boolean;
730+{ Folds x op y, of the real type t: an F32 rounded at each step, as
731+ the machine would. }
732+function FoldReal(op: TSym; const x, y: TItem; t: PType; var r: TItem): boolean;
710733 var a, b: double;
711734 begin
712735 a := RealOf(x); b := RealOf(y);
@@ -724,6 +747,7 @@
724747 sGeq: MakeConst(r, tBool, ord(a >= b));
725748 else FoldReal := false;
726749 end;
750+ if r.typ^.form = fReal then RealTo(r, t);
727751 end;
728752
729753 procedure Op2(op: TSym; var x, y: TItem; L: longint);
@@ -732,7 +756,7 @@
732756 CcSigned: array[sEql..sGeq] of longint = (ccE, ccNE, ccL, ccLE, ccG, ccGE);
733757 CcUnsigned: array[sEql..sGeq] of longint = (ccE, ccNE, ccB, ccBE, ccA, ccAE);
734758 var
735- xt, yt, t: PType; r: TItem; scale, sh: longint; isPtr, unsign: boolean; a, b: int64;
759+ xt, yt, t: PType; r: TItem; scale, sh: longint; isPtr, unsign, f: boolean; a, b: int64;
736760 begin
737761 xt := x.typ; yt := y.typ;
738762 if y.mode in [mType, mBuiltin, mNone, mCase] then Mark('not a value');
@@ -768,7 +792,7 @@
768792 { the type of the result, and the checks }
769793 t := nil; scale := 1; isPtr := false;
770794 if IsInt(xt) and IsInt(yt) then begin
771- if op = sSlash then Mark('/ is for F64; integers use div');
795+ if op = sSlash then Mark('/ is for reals; integers use div');
772796 if op in [sShl, sShr] then begin
773797 if xt^.form = fConst then t := tConst else t := xt
774798 end else if (xt^.form = fConst) and (yt^.form = fConst) then t := tConst
@@ -781,8 +805,12 @@
781805 end else if Same(xt, yt) then t := xt
782806 else Mark('different types: ' + TypeName(xt) + ' and ' + TypeName(yt));
783807 end else if (xt^.form = fReal) and (yt^.form = fReal) then begin
784- if not (op in Rel + [sPlus, sMinus, sTimes, sSlash]) then Mark('not an F64 operator');
785- t := tReal;
808+ if not (op in Rel + [sPlus, sMinus, sTimes, sSlash]) then Mark('not a real operator');
809+ if xt = tRConst then t := yt
810+ else if (yt = tRConst) or Same(xt, yt) then t := xt
811+ else Mark('different types: ' + TypeName(xt) + ' and ' + TypeName(yt));
812+ if (x.mode = mConst) and (x.typ <> t) then RealTo(x, t);
813+ if (y.mode = mConst) and (y.typ <> t) then RealTo(y, t);
786814 end else if (xt^.form = fBool) and (yt^.form = fBool) then begin
787815 if not (op in [sXor, sEql, sNeq]) then Mark('not a BOOL operator');
788816 t := tBool;
@@ -815,7 +843,7 @@
815843 { constants }
816844 if (x.mode = mConst) and (y.mode = mConst) and not isPtr then begin
817845 if t^.form = fReal then begin
818- if not FoldReal(op, x, y, r) then Mark('not an F64 operator');
846+ if not FoldReal(op, x, y, t, r) then Mark('not a real operator');
819847 x := r;
820848 end else if t^.form = fSet then begin
821849 a := x.val; b := y.val;
@@ -848,17 +876,18 @@
848876 if t^.form = fReal then begin
849877 MovqXR(0, RAX);
850878 MovqXR(1, RCX);
879+ f := IsF32(t);
851880 case op of
852- sPlus: Sse($58, 0, 1);
853- sMinus: Sse($5C, 0, 1);
854- sTimes: Sse($59, 0, 1);
855- sSlash: Sse($5E, 0, 1);
856- sEql: begin Ucomisd(0, 1); SetCC(ccE, RAX); SetCC(ccNP, RCX); Alu(aluAnd, RAX, RCX) end;
857- sNeq: begin Ucomisd(0, 1); SetCC(ccNE, RAX); SetCC(ccP, RCX); Alu(aluOr, RAX, RCX) end;
858- sLss: begin Ucomisd(1, 0); SetCC(ccA, RAX) end;
859- sLeq: begin Ucomisd(1, 0); SetCC(ccAE, RAX) end;
860- sGtr: begin Ucomisd(0, 1); SetCC(ccA, RAX) end;
861- sGeq: begin Ucomisd(0, 1); SetCC(ccAE, RAX) end;
881+ sPlus: Sse($58, 0, 1, f);
882+ sMinus: Sse($5C, 0, 1, f);
883+ sTimes: Sse($59, 0, 1, f);
884+ sSlash: Sse($5E, 0, 1, f);
885+ sEql: begin Ucomis(0, 1, f); SetCC(ccE, RAX); SetCC(ccNP, RCX); Alu(aluAnd, RAX, RCX) end;
886+ sNeq: begin Ucomis(0, 1, f); SetCC(ccNE, RAX); SetCC(ccP, RCX); Alu(aluOr, RAX, RCX) end;
887+ sLss: begin Ucomis(1, 0, f); SetCC(ccA, RAX) end;
888+ sLeq: begin Ucomis(1, 0, f); SetCC(ccAE, RAX) end;
889+ sGtr: begin Ucomis(0, 1, f); SetCC(ccA, RAX) end;
890+ sGeq: begin Ucomis(0, 1, f); SetCC(ccAE, RAX) end;
862891 end;
863892 if not (op in Rel) then MovqRX(RAX, 0);
864893 end else if t^.form = fSet then begin
@@ -915,7 +944,7 @@
915944 end;
916945
917946 procedure Convert(var x: TItem; t: PType);
918-var s: PType; r: double; L1, L2: longint;
947+var s: PType; r: double; L1, L2: longint; f: boolean;
919948 begin
920949 s := x.typ;
921950 if x.mode in [mType, mBuiltin, mNone, mCase, mStr] then Mark('can''t convert this');
@@ -935,9 +964,11 @@
935964 if t^.len < 64 then begin MovRI(RCX, (int64(1) shl t^.len) - 1); Alu(aluAnd, RAX, RCX) end;
936965 end;
937966 end else if IsInt(s) and (t^.form = fReal) then begin
967+ f := IsF32(t);
938968 if x.mode = mConst then begin
939969 if (s^.form = fInt) and not s^.signed then r := qword(x.val) else r := x.val;
940970 MakeReal(x, r);
971+ RealTo(x, t);
941972 end else begin
942973 Value(x);
943974 if (s^.form = fInt) and not s^.signed and (s^.size = 8) then begin
@@ -945,19 +976,25 @@
945976 L1 := NoLabel; L2 := NoLabel;
946977 TestRR(RAX, RAX);
947978 Jcc(ccS, L1);
948- Cvtsi2sd(0, RAX);
979+ Cvtsi2s(0, RAX, f);
949980 Jmp(L2);
950981 FixLink(L1);
951982 MovRR(RCX, RAX);
952983 ShiftI(shShr, RCX, 1);
953984 AluI(aluAnd, RAX, 1);
954985 Alu(aluOr, RCX, RAX);
955- Cvtsi2sd(0, RCX);
956- Sse($58, 0, 0);
986+ Cvtsi2s(0, RCX, f);
987+ Sse($58, 0, 0, f);
957988 FixLink(L2);
958- end else Cvtsi2sd(0, RAX);
989+ end else Cvtsi2s(0, RAX, f);
959990 MovqRX(RAX, 0);
960991 end;
992+ end else if (s^.form = fReal) and (t^.form = fReal) then begin
993+ { F32 to F64, F64 to F32, or a real constant to either }
994+ if x.mode = mConst then RealTo(x, t)
995+ else begin
996+ Value(x); MovqXR(0, RAX); Sse($5A, 0, 0, IsF32(s)); MovqRX(RAX, 0)
997+ end;
961998 end else if (s^.form = fReal) and (t^.form = fInt) then begin
962999 if x.mode = mConst then begin
9631000 r := RealOf(x);
@@ -965,7 +1002,7 @@
9651002 x.val := Cut(low(int64), t)
9661003 else x.val := Cut(trunc(r), t);
9671004 end else begin
968- Value(x); MovqXR(0, RAX); Cvttsd2si(RAX, 0); Norm(t)
1005+ Value(x); MovqXR(0, RAX); Cvtts2si(RAX, 0, IsF32(s)); Norm(t)
9691006 end;
9701007 end else if (IsInt(s) or (s^.form in [fPtr, fProc, fNil])) and (t^.form in [fPtr, fProc]) then begin
9711008 if x.mode <> mConst then Value(x);
@@ -1000,10 +1037,13 @@
10001037 if c.n > High(c.slots) then Mark('too many arguments');
10011038 if p = nil then begin
10021039 { past the parameters of a C procedure that ends in ...: a string
1003- goes as a C string, an integer constant as an I64 }
1040+ goes as a C string, an integer constant as an I64, a real
1041+ constant as an F64; C takes no F32 there }
10041042 if not c.sig^.variadic then Mark('too many arguments');
10051043 if a.mode = mStr then Coerce(a, tPtrU8)
10061044 else if a.typ^.form = fConst then Coerce(a, tI64)
1045+ else if a.typ = tRConst then Coerce(a, tReal)
1046+ else if IsF32(a.typ) then Mark('an F32 goes to ... as F64(x)')
10071047 else if not IsScalar(a.typ) then Mark('only a single value goes to ...');
10081048 c.isF[c.n] := a.typ^.form = fReal;
10091049 c.slots[c.n] := ToSlot(a);

src/parse.pas modified +2 -2

@@ -1283,7 +1283,7 @@
12831283 q := sig^.fields;
12841284 while q <> nil do begin
12851285 if not q^.isVar and not IsScalar(q^.typ) then
1286- Mark('a code procedure takes integers, BOOL, F64, pointers and procedures');
1286+ Mark('a code procedure takes integers, BOOL, reals, pointers and procedures');
12871287 if (q^.typ^.form = fReal) and not q^.isVar then inc(nf) else inc(ni);
12881288 q := q^.next;
12891289 end;
@@ -1362,7 +1362,7 @@
13621362 q := t^.fields;
13631363 while q <> nil do begin
13641364 if q^.isVar or not IsScalar(q^.typ) then
1365- Mark('C takes integers, BOOL, F64, pointers and procedures');
1365+ Mark('C takes integers, BOOL, reals, pointers and procedures');
13661366 q := q^.next;
13671367 end;
13681368 if (t^.base <> nil) and not IsScalar(t^.base) then Mark('C returns a single value');

src/sym.pas modified +6 -2

@@ -23,7 +23,8 @@
2323 TForm = (fNone, fBool, fInt, fReal, fConst, fNil, fStr, fPtr, fArray,
2424 fSlice, fRecord, fVariant, fProc, fEnum, fSet, fError);
2525 { fConst is the type of an untyped integer constant; fStr of a string
26- literal, whose length the literal carries }
26+ literal, whose length the literal carries. F32 and F64 are fReal,
27+ told apart by size, and so is tRConst, an untyped real constant. }
2728
2829 TClass = (oConst, oType, oVar, oProc, oModule, oField, oCase, oBuiltin);
2930
@@ -120,7 +121,7 @@
120121
121122 var
122123 tI8, tI16, tI32, tI64, tU8, tU16, tU32, tU64: PType;
123- tBool, tReal, tError, tConst, tNil, tStr, tNone, tPtrU8, tBytes: PType;
124+ tBool, tF32, tReal, tRConst, tError, tConst, tNil, tStr, tNone, tPtrU8, tBytes: PType;
124125 curModule: PModule;
125126
126127 function NewType(form: TForm; size, align: int64): PType;
@@ -372,6 +373,7 @@
372373 end;
373374 case t^.form of
374375 fConst: TypeName := 'an integer constant';
376+ fReal: TypeName := 'a real constant';
375377 fNil: TypeName := 'nil';
376378 fStr: TypeName := 'a string';
377379 fPtr: if t^.vol then TypeName := '^VOLATILE ' + TypeName(t^.base)
@@ -473,7 +475,9 @@
473475 tU32 := Basic('U32', fInt, 4, false);
474476 tU64 := Basic('U64', fInt, 8, false);
475477 tBool := Basic('BOOL', fBool, 1, false);
478+ tF32 := Basic('F32', fReal, 4, false);
476479 tReal := Basic('F64', fReal, 8, false);
480+ tRConst := NewType(fReal, 8, 8);
477481 tError := Basic('ERROR', fError, 8, false);
478482 tConst := NewType(fConst, 8, 8);
479483 tConst^.signed := true;

src/x64.pas modified +16 -12

@@ -135,10 +135,11 @@
135135 procedure MovqRX(r, x: longint);
136136 procedure MovsdXM(x: longint; const m: TMem);
137137 procedure MovsdMX(const m: TMem; x: longint);
138-procedure Sse(op, x, y: longint); { F2 0F op: 58 add, 59 mul, 5C sub, 5E div }
139-procedure Ucomisd(x, y: longint);
140-procedure Cvtsi2sd(x, r: longint);
141-procedure Cvttsd2si(r, x: longint);
138+{ The real instructions take single: F32 (ss) rather than F64 (sd) }
139+procedure Sse(op, x, y: longint; single: boolean); { 58 add, 59 mul, 5C sub, 5E div, 5A convert }
140+procedure Ucomis(x, y: longint; single: boolean);
141+procedure Cvtsi2s(x, r: longint; single: boolean);
142+procedure Cvtts2si(r, x: longint; single: boolean);
142143 procedure Btc(r, bit: longint);
143144
144145 procedure Jmp(var L: longint);
@@ -554,24 +555,27 @@
554555 OpM($F2, false, $110F, x, m);
555556 end;
556557
557-procedure Sse(op, x, y: longint);
558+const
559+ RealPfx: array[boolean] of longint = ($F2, $F3);
560+
561+procedure Sse(op, x, y: longint; single: boolean);
558562 begin
559- OpR($F2, false, $0F or op shl 8, x, y);
563+ OpR(RealPfx[single], false, $0F or op shl 8, x, y);
560564 end;
561565
562-procedure Ucomisd(x, y: longint);
566+procedure Ucomis(x, y: longint; single: boolean);
563567 begin
564- OpR($66, false, $2E0F, x, y);
568+ if single then OpR(0, false, $2E0F, x, y) else OpR($66, false, $2E0F, x, y);
565569 end;
566570
567-procedure Cvtsi2sd(x, r: longint);
571+procedure Cvtsi2s(x, r: longint; single: boolean);
568572 begin
569- OpR($F2, true, $2A0F, x, r);
573+ OpR(RealPfx[single], true, $2A0F, x, r);
570574 end;
571575
572-procedure Cvttsd2si(r, x: longint);
576+procedure Cvtts2si(r, x: longint; single: boolean);
573577 begin
574- OpR($F2, true, $2C0F, r, x);
578+ OpR(RealPfx[single], true, $2C0F, r, x);
575579 end;
576580
577581 procedure Btc(r, bit: longint);

test/enctest.pas modified +3 -1

@@ -24,7 +24,9 @@
2424 Push(RAX); Push(12); Pop(RCX); Pop(R11); CallR(R11); CallM(Mem(RCX, 8)); Ret; Leave; Syscall; Mfence; Ud2; RepMovsb; RepStosb;
2525 LockCmpxchg(Mem(RCX, 0), RDX); Xchg(Mem(RCX, 0), RAX); LockXadd(Mem(RCX, 0), RAX);
2626 MovqXR(0, RAX); MovqXR(1, RCX); MovqRX(RAX, 0); MovsdXM(2, Mem(RBP, -8)); MovsdMX(Mem(RBP, -8), 7);
27- Sse($58, 0, 1); Sse($5E, 0, 1); Ucomisd(0, 1); Cvtsi2sd(0, RAX); Cvttsd2si(RAX, 0); Btc(RAX, 63);
27+ Sse($58, 0, 1, false); Sse($5E, 0, 1, true); Sse($5A, 0, 0, false); Sse($5A, 0, 0, true);
28+ Ucomis(0, 1, false); Ucomis(0, 1, true); Cvtsi2s(0, RAX, false); Cvtsi2s(0, RAX, true);
29+ Cvtts2si(RAX, 0, false); Cvtts2si(RAX, 0, true); Btc(RAX, 63);
2830 L := NoLabel; Jcc(ccE, L); Jmp(L); FixLink(L); JmpBack(chunks^[e].start); JccBack(ccNE, chunks^[e].start);
2931 EndChunk(e);
3032 WriteElf(ParamStr(1), e);

test/err/ExternRec.err (history) modified +1 -1

@@ -1 +1 @@
1-C takes integers, BOOL, F64, pointers and procedures
1+C takes integers, BOOL, reals, pointers and procedures

test/err/F32Dots.err (history) added +1 -0

@@ -0,0 +1 @@
1+an F32 goes to ... as F64(x)

test/err/F32Dots.jed added +11 -0

@@ -0,0 +1,11 @@
1+module F32Dots;
2+import C;
3+
4+proc main*;
5+var x: F32;
6+begin
7+ x := 1.5;
8+ C.printf("%f\n", x);
9+end;
10+
11+end F32Dots.

test/err/Mixed.err added +1 -0

@@ -0,0 +1 @@
1+different types: F32 and F64

test/err/Mixed.jed added +9 -0

@@ -0,0 +1,9 @@
1+module Mixed;
2+
3+proc main*;
4+var x: F32; y: F64;
5+begin
6+ y := x + y;
7+end;
8+
9+end Mixed.

test/run/Single.expect (history) added +22 -0

@@ -0,0 +1,22 @@
1+4 12
2+a+b 0.300000012
3+d+0.2 0.3
4+third 0.333333343
5+1/3 0.333333343
6+ten tenths 1.00000012
7+not one
8+near one
9+negated -1
10+I64(-2.75) -2
11+10000000000
12+U64 max 1.84467441e+19
13+I32 big 16777216
14+F32(1E40) inf
15+F32(1/3) 0.333333343
16+dot 7
17+sqrtf 1.41421354
18+fabsf 1.5
19+twice 2.5
20+mix 3.875
21+arr 1.5
22+0.500000 0.500000

test/run/Single.jed added +81 -0

@@ -0,0 +1,81 @@
1+(* F32: arithmetic rounded at each step, conversions, real constants
2+ that take either type, calls to C and to code. *)
3+module Single;
4+import Out, C;
5+link "libm.so.6";
6+
7+const
8+ third = 1.0 / 3.0; (* in double precision, then rounded once *)
9+ tenth = 0.1;
10+
11+type
12+ Vec = record x, y, z: F32; end;
13+
14+extern proc sqrtf(x: F32): F32;
15+extern proc fabsf(x: F32): F32;
16+extern proc sqrt(x: F64): F64;
17+
18+(* addss xmm0, xmm0; ret *)
19+proc twice(x: F32): F32;
20+code 0F3H, 0FH, 58H, 0C0H, 0C3H;
21+
22+proc dot(a, b: Vec): F32;
23+begin
24+ return a.x * b.x + a.y * b.y + a.z * b.z;
25+end;
26+
27+proc mix(a: F32; n: I64; b: F64; c: F32): F64;
28+begin
29+ return F64(a) + F64(n) + b + F64(c);
30+end;
31+
32+proc show(name: ^U8; x: F64);
33+begin
34+ Out.flush;
35+ C.printf("%s %.9g\n", name, x);
36+ C.fflush(C.stdout);
37+end;
38+
39+proc main*;
40+var
41+ a, b, s: F32; d: F64; v, w: Vec; i: I64; big: U64; arr: [4]F32;
42+begin
43+ Out.int(SIZE(F32)); Out.char(' '); Out.int(SIZE(Vec)); Out.ln;
44+ a := tenth;
45+ b := 0.2;
46+ show("a+b", F64(a + b));
47+ d := tenth;
48+ show("d+0.2", d + 0.2);
49+ a := third;
50+ show("third", F64(a));
51+ a := 1.0;
52+ b := 3.0;
53+ show("1/3", F64(a / b));
54+ s := 0.0;
55+ for i := 1 to 10 do s := s + tenth; end;
56+ show("ten tenths", F64(s));
57+ if s # 1.0 then Out.str("not one\n"); end;
58+ if (s > 0.9) and (s <= 1.1) and not (s < 0.0) then Out.str("near one\n"); end;
59+ a := -a;
60+ show("negated", F64(a));
61+ show("I64(-2.75)", F64(I64(F32(-2.75))));
62+ Out.int(I64(F32(1.0E10))); Out.ln;
63+ big := 0FFFFFFFFFFFFFFFFH;
64+ show("U64 max", F64(F32(big)));
65+ show("I32 big", F64(F32(I32(16777217))));
66+ show("F32(1E40)", F64(F32(d * 1.0E41)));
67+ show("F32(1/3)", F64(F32(1.0 / 3.0)));
68+ v := {x: 1.0, y: 2.0, z: 3.0};
69+ w := {x: 0.5, y: 0.25, z: 2.0};
70+ show("dot", F64(dot(v, w)));
71+ show("sqrtf", F64(sqrtf(2.0)));
72+ show("fabsf", F64(fabsf(-1.5)));
73+ show("twice", F64(twice(1.25)));
74+ show("mix", mix(1.5, 2, 0.25, 0.125));
75+ for i := 0 to 3 do arr[i] := F32(i) * 0.5; end;
76+ show("arr", F64(arr[3]));
77+ C.printf("%f %f\n", F64(arr[1]), 0.5);
78+ C.fflush(C.stdout);
79+end;
80+
81+end Single.