raz / test / run / Single.raz

85 lines · 2.1 KiB · raw · history · last change 264716cb

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