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.
| @@ -38,9 +38,8 @@ | ||
| 38 | 38 | ## Where things stand |
| 39 | 39 | |
| 40 | 40 | 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. | |
| 44 | 43 | |
| 45 | 44 | `test/libc` runs its programs over `lib/Sys.jed`, the C one, through |
| 46 | 45 | a link in its `mods`. A test or example without `.expect` is only |
| @@ -78,6 +77,9 @@ | ||
| 78 | 77 | name like `Rt.exit`, before any module body runs. |
| 79 | 78 | - Past an extern's parameters, a string goes as a C string and an |
| 80 | 79 | 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. | |
| 81 | 83 | - Every failed check says "index out of range"; some errors point at |
| 82 | 84 | the symbol after the offending expression. Error messages are to be |
| 83 | 85 | improved later, all together. |
| @@ -112,7 +112,7 @@ | ||
| 112 | 112 | at each newline when standard output is a terminal, when full, |
| 113 | 113 | by `Out.flush`, and at exit. The report says how to mix `Out` |
| 114 | 114 | 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 | |
| 116 | 116 | untyped constants that take `F32` or `F64`. |
| 117 | 117 | - [ ] 13. A thread-safe `lib/Heap.jed`: its bad sizes refused, and a |
| 118 | 118 | marker in each block's header so that freeing a block twice, or |
| @@ -154,12 +154,28 @@ | ||
| 154 | 154 | x.val := v; |
| 155 | 155 | end; |
| 156 | 156 | |
| 157 | +{ A real constant: untyped, unless typed later. Its value is an F64, | |
| 158 | + which for an F32 is one an F32 can hold. } | |
| 157 | 159 | procedure MakeReal(var x: TItem; r: double); |
| 158 | 160 | begin |
| 159 | - MakeConst(x, tReal, 0); | |
| 161 | + MakeConst(x, tRConst, 0); | |
| 160 | 162 | Move(r, x.val, 8); |
| 161 | 163 | end; |
| 162 | 164 | |
| 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 | + | |
| 163 | 179 | function RealOf(const x: TItem): double; |
| 164 | 180 | var r: double; |
| 165 | 181 | begin |
| @@ -233,9 +249,13 @@ | ||
| 233 | 249 | |
| 234 | 250 | { A constant into r: an error constant is the address of its chunk. } |
| 235 | 251 | procedure ConstTo(r: longint; const x: TItem); |
| 252 | +var f: single; | |
| 236 | 253 | begin |
| 237 | 254 | 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); | |
| 239 | 259 | end; |
| 240 | 260 | |
| 241 | 261 | function MemOf(var x: TItem; r: longint): TMem; |
| @@ -259,7 +279,7 @@ | ||
| 259 | 279 | |
| 260 | 280 | function ScalarSize(t: PType): longint; |
| 261 | 281 | 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 | |
| 263 | 283 | else ScalarSize := 8; |
| 264 | 284 | end; |
| 265 | 285 | |
| @@ -427,7 +447,8 @@ | ||
| 427 | 447 | end; |
| 428 | 448 | if x.mode in [mType, mBuiltin, mNone, mCase] then Mark('not a value'); |
| 429 | 449 | 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 | |
| 431 | 452 | if (x.mode = mConst) and not Fits(x.val, t) then Mark('constant doesn''t fit ' + TypeName(t)); |
| 432 | 453 | x.typ := t; |
| 433 | 454 | end else if (s^.form = fNil) and (t^.form in [fPtr, fProc, fError]) then |
| @@ -605,7 +626,7 @@ | ||
| 605 | 626 | begin |
| 606 | 627 | if x.typ^.form = fReal then begin |
| 607 | 628 | 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; | |
| 609 | 630 | end else if IsInt(x.typ) then begin |
| 610 | 631 | if x.mode = mConst then x.val := Cut(-x.val, x.typ) |
| 611 | 632 | else begin Value(x); Unary(3, RAX); Norm(x.typ) end; |
| @@ -706,7 +727,9 @@ | ||
| 706 | 727 | end; |
| 707 | 728 | end; |
| 708 | 729 | |
| 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; | |
| 710 | 733 | var a, b: double; |
| 711 | 734 | begin |
| 712 | 735 | a := RealOf(x); b := RealOf(y); |
| @@ -724,6 +747,7 @@ | ||
| 724 | 747 | sGeq: MakeConst(r, tBool, ord(a >= b)); |
| 725 | 748 | else FoldReal := false; |
| 726 | 749 | end; |
| 750 | + if r.typ^.form = fReal then RealTo(r, t); | |
| 727 | 751 | end; |
| 728 | 752 | |
| 729 | 753 | procedure Op2(op: TSym; var x, y: TItem; L: longint); |
| @@ -732,7 +756,7 @@ | ||
| 732 | 756 | CcSigned: array[sEql..sGeq] of longint = (ccE, ccNE, ccL, ccLE, ccG, ccGE); |
| 733 | 757 | CcUnsigned: array[sEql..sGeq] of longint = (ccE, ccNE, ccB, ccBE, ccA, ccAE); |
| 734 | 758 | 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; | |
| 736 | 760 | begin |
| 737 | 761 | xt := x.typ; yt := y.typ; |
| 738 | 762 | if y.mode in [mType, mBuiltin, mNone, mCase] then Mark('not a value'); |
| @@ -768,7 +792,7 @@ | ||
| 768 | 792 | { the type of the result, and the checks } |
| 769 | 793 | t := nil; scale := 1; isPtr := false; |
| 770 | 794 | 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'); | |
| 772 | 796 | if op in [sShl, sShr] then begin |
| 773 | 797 | if xt^.form = fConst then t := tConst else t := xt |
| 774 | 798 | end else if (xt^.form = fConst) and (yt^.form = fConst) then t := tConst |
| @@ -781,8 +805,12 @@ | ||
| 781 | 805 | end else if Same(xt, yt) then t := xt |
| 782 | 806 | else Mark('different types: ' + TypeName(xt) + ' and ' + TypeName(yt)); |
| 783 | 807 | 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); | |
| 786 | 814 | end else if (xt^.form = fBool) and (yt^.form = fBool) then begin |
| 787 | 815 | if not (op in [sXor, sEql, sNeq]) then Mark('not a BOOL operator'); |
| 788 | 816 | t := tBool; |
| @@ -815,7 +843,7 @@ | ||
| 815 | 843 | { constants } |
| 816 | 844 | if (x.mode = mConst) and (y.mode = mConst) and not isPtr then begin |
| 817 | 845 | 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'); | |
| 819 | 847 | x := r; |
| 820 | 848 | end else if t^.form = fSet then begin |
| 821 | 849 | a := x.val; b := y.val; |
| @@ -848,17 +876,18 @@ | ||
| 848 | 876 | if t^.form = fReal then begin |
| 849 | 877 | MovqXR(0, RAX); |
| 850 | 878 | MovqXR(1, RCX); |
| 879 | + f := IsF32(t); | |
| 851 | 880 | 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; | |
| 862 | 891 | end; |
| 863 | 892 | if not (op in Rel) then MovqRX(RAX, 0); |
| 864 | 893 | end else if t^.form = fSet then begin |
| @@ -915,7 +944,7 @@ | ||
| 915 | 944 | end; |
| 916 | 945 | |
| 917 | 946 | 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; | |
| 919 | 948 | begin |
| 920 | 949 | s := x.typ; |
| 921 | 950 | if x.mode in [mType, mBuiltin, mNone, mCase, mStr] then Mark('can''t convert this'); |
| @@ -935,9 +964,11 @@ | ||
| 935 | 964 | if t^.len < 64 then begin MovRI(RCX, (int64(1) shl t^.len) - 1); Alu(aluAnd, RAX, RCX) end; |
| 936 | 965 | end; |
| 937 | 966 | end else if IsInt(s) and (t^.form = fReal) then begin |
| 967 | + f := IsF32(t); | |
| 938 | 968 | if x.mode = mConst then begin |
| 939 | 969 | if (s^.form = fInt) and not s^.signed then r := qword(x.val) else r := x.val; |
| 940 | 970 | MakeReal(x, r); |
| 971 | + RealTo(x, t); | |
| 941 | 972 | end else begin |
| 942 | 973 | Value(x); |
| 943 | 974 | if (s^.form = fInt) and not s^.signed and (s^.size = 8) then begin |
| @@ -945,19 +976,25 @@ | ||
| 945 | 976 | L1 := NoLabel; L2 := NoLabel; |
| 946 | 977 | TestRR(RAX, RAX); |
| 947 | 978 | Jcc(ccS, L1); |
| 948 | - Cvtsi2sd(0, RAX); | |
| 979 | + Cvtsi2s(0, RAX, f); | |
| 949 | 980 | Jmp(L2); |
| 950 | 981 | FixLink(L1); |
| 951 | 982 | MovRR(RCX, RAX); |
| 952 | 983 | ShiftI(shShr, RCX, 1); |
| 953 | 984 | AluI(aluAnd, RAX, 1); |
| 954 | 985 | Alu(aluOr, RCX, RAX); |
| 955 | - Cvtsi2sd(0, RCX); | |
| 956 | - Sse($58, 0, 0); | |
| 986 | + Cvtsi2s(0, RCX, f); | |
| 987 | + Sse($58, 0, 0, f); | |
| 957 | 988 | FixLink(L2); |
| 958 | - end else Cvtsi2sd(0, RAX); | |
| 989 | + end else Cvtsi2s(0, RAX, f); | |
| 959 | 990 | MovqRX(RAX, 0); |
| 960 | 991 | 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; | |
| 961 | 998 | end else if (s^.form = fReal) and (t^.form = fInt) then begin |
| 962 | 999 | if x.mode = mConst then begin |
| 963 | 1000 | r := RealOf(x); |
| @@ -965,7 +1002,7 @@ | ||
| 965 | 1002 | x.val := Cut(low(int64), t) |
| 966 | 1003 | else x.val := Cut(trunc(r), t); |
| 967 | 1004 | 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) | |
| 969 | 1006 | end; |
| 970 | 1007 | end else if (IsInt(s) or (s^.form in [fPtr, fProc, fNil])) and (t^.form in [fPtr, fProc]) then begin |
| 971 | 1008 | if x.mode <> mConst then Value(x); |
| @@ -1000,10 +1037,13 @@ | ||
| 1000 | 1037 | if c.n > High(c.slots) then Mark('too many arguments'); |
| 1001 | 1038 | if p = nil then begin |
| 1002 | 1039 | { 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 } | |
| 1004 | 1042 | if not c.sig^.variadic then Mark('too many arguments'); |
| 1005 | 1043 | if a.mode = mStr then Coerce(a, tPtrU8) |
| 1006 | 1044 | 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)') | |
| 1007 | 1047 | else if not IsScalar(a.typ) then Mark('only a single value goes to ...'); |
| 1008 | 1048 | c.isF[c.n] := a.typ^.form = fReal; |
| 1009 | 1049 | c.slots[c.n] := ToSlot(a); |
| @@ -1283,7 +1283,7 @@ | ||
| 1283 | 1283 | q := sig^.fields; |
| 1284 | 1284 | while q <> nil do begin |
| 1285 | 1285 | 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'); | |
| 1287 | 1287 | if (q^.typ^.form = fReal) and not q^.isVar then inc(nf) else inc(ni); |
| 1288 | 1288 | q := q^.next; |
| 1289 | 1289 | end; |
| @@ -1362,7 +1362,7 @@ | ||
| 1362 | 1362 | q := t^.fields; |
| 1363 | 1363 | while q <> nil do begin |
| 1364 | 1364 | 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'); | |
| 1366 | 1366 | q := q^.next; |
| 1367 | 1367 | end; |
| 1368 | 1368 | if (t^.base <> nil) and not IsScalar(t^.base) then Mark('C returns a single value'); |
| @@ -23,7 +23,8 @@ | ||
| 23 | 23 | TForm = (fNone, fBool, fInt, fReal, fConst, fNil, fStr, fPtr, fArray, |
| 24 | 24 | fSlice, fRecord, fVariant, fProc, fEnum, fSet, fError); |
| 25 | 25 | { 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. } | |
| 27 | 28 | |
| 28 | 29 | TClass = (oConst, oType, oVar, oProc, oModule, oField, oCase, oBuiltin); |
| 29 | 30 | |
| @@ -120,7 +121,7 @@ | ||
| 120 | 121 | |
| 121 | 122 | var |
| 122 | 123 | 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; | |
| 124 | 125 | curModule: PModule; |
| 125 | 126 | |
| 126 | 127 | function NewType(form: TForm; size, align: int64): PType; |
| @@ -372,6 +373,7 @@ | ||
| 372 | 373 | end; |
| 373 | 374 | case t^.form of |
| 374 | 375 | fConst: TypeName := 'an integer constant'; |
| 376 | + fReal: TypeName := 'a real constant'; | |
| 375 | 377 | fNil: TypeName := 'nil'; |
| 376 | 378 | fStr: TypeName := 'a string'; |
| 377 | 379 | fPtr: if t^.vol then TypeName := '^VOLATILE ' + TypeName(t^.base) |
| @@ -473,7 +475,9 @@ | ||
| 473 | 475 | tU32 := Basic('U32', fInt, 4, false); |
| 474 | 476 | tU64 := Basic('U64', fInt, 8, false); |
| 475 | 477 | tBool := Basic('BOOL', fBool, 1, false); |
| 478 | + tF32 := Basic('F32', fReal, 4, false); | |
| 476 | 479 | tReal := Basic('F64', fReal, 8, false); |
| 480 | + tRConst := NewType(fReal, 8, 8); | |
| 477 | 481 | tError := Basic('ERROR', fError, 8, false); |
| 478 | 482 | tConst := NewType(fConst, 8, 8); |
| 479 | 483 | tConst^.signed := true; |
| @@ -135,10 +135,11 @@ | ||
| 135 | 135 | procedure MovqRX(r, x: longint); |
| 136 | 136 | procedure MovsdXM(x: longint; const m: TMem); |
| 137 | 137 | 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); | |
| 142 | 143 | procedure Btc(r, bit: longint); |
| 143 | 144 | |
| 144 | 145 | procedure Jmp(var L: longint); |
| @@ -554,24 +555,27 @@ | ||
| 554 | 555 | OpM($F2, false, $110F, x, m); |
| 555 | 556 | end; |
| 556 | 557 | |
| 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); | |
| 558 | 562 | begin |
| 559 | - OpR($F2, false, $0F or op shl 8, x, y); | |
| 563 | + OpR(RealPfx[single], false, $0F or op shl 8, x, y); | |
| 560 | 564 | end; |
| 561 | 565 | |
| 562 | -procedure Ucomisd(x, y: longint); | |
| 566 | +procedure Ucomis(x, y: longint; single: boolean); | |
| 563 | 567 | 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); | |
| 565 | 569 | end; |
| 566 | 570 | |
| 567 | -procedure Cvtsi2sd(x, r: longint); | |
| 571 | +procedure Cvtsi2s(x, r: longint; single: boolean); | |
| 568 | 572 | begin |
| 569 | - OpR($F2, true, $2A0F, x, r); | |
| 573 | + OpR(RealPfx[single], true, $2A0F, x, r); | |
| 570 | 574 | end; |
| 571 | 575 | |
| 572 | -procedure Cvttsd2si(r, x: longint); | |
| 576 | +procedure Cvtts2si(r, x: longint; single: boolean); | |
| 573 | 577 | begin |
| 574 | - OpR($F2, true, $2C0F, r, x); | |
| 578 | + OpR(RealPfx[single], true, $2C0F, r, x); | |
| 575 | 579 | end; |
| 576 | 580 | |
| 577 | 581 | procedure Btc(r, bit: longint); |
| @@ -24,7 +24,9 @@ | ||
| 24 | 24 | Push(RAX); Push(12); Pop(RCX); Pop(R11); CallR(R11); CallM(Mem(RCX, 8)); Ret; Leave; Syscall; Mfence; Ud2; RepMovsb; RepStosb; |
| 25 | 25 | LockCmpxchg(Mem(RCX, 0), RDX); Xchg(Mem(RCX, 0), RAX); LockXadd(Mem(RCX, 0), RAX); |
| 26 | 26 | 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); | |
| 28 | 30 | L := NoLabel; Jcc(ccE, L); Jmp(L); FixLink(L); JmpBack(chunks^[e].start); JccBack(ccNE, chunks^[e].start); |
| 29 | 31 | EndChunk(e); |
| 30 | 32 | WriteElf(ParamStr(1), e); |
| @@ -1 +1 @@ | ||
| 1 | -C takes integers, BOOL, F64, pointers and procedures | |
| 1 | +C takes integers, BOOL, reals, pointers and procedures |
| @@ -0,0 +1 @@ | ||
| 1 | +an F32 goes to ... as F64(x) |
| @@ -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. |
| @@ -0,0 +1 @@ | ||
| 1 | +different types: F32 and F64 |
| @@ -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. |
| @@ -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 |
| @@ -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. |