5 type Fn_flt__flt = struct
6 f32 : (x : flt32 -> flt32)
7 f64 : (x : flt64 -> flt64)
10 type Fn_flt_flt__flt = struct
11 f32 : (x : flt32, y : flt32 -> flt32)
12 f64 : (x : flt64, y : flt64 -> flt64)
15 type Fn_flt_flt_flt__flt = struct
16 f32 : (x : flt32, y : flt32, z : flt32 -> flt32)
17 f64 : (x : flt64, y : flt64, z : flt64 -> flt64)
24 `Flt_flt__flt Fn_flt_flt__flt
25 `Flt_flt_flt__flt Fn_flt_flt_flt__flt
34 var available_fns : fn_desc[:] = [][:]
36 generic id : (a : @a -> @a) = {x; -> x}
38 const main = {args : byte[:][:]
40 [.name = "id", .f = `Flt__flt [ .f32 = id, .f64 = id]],
41 [.name = "ceil", .f = `Flt__flt [ .f32 = math.ceil, .f64 = math.ceil]],
42 [.name = "cos", .f = `Flt__flt [ .f32 = math.cos, .f64 = math.cos]],
43 [.name = "exp", .f = `Flt__flt [ .f32 = math.exp, .f64 = math.exp]],
44 [.name = "expm1", .f = `Flt__flt [ .f32 = math.expm1, .f64 = math.expm1]],
45 [.name = "floor", .f = `Flt__flt [ .f32 = math.floor, .f64 = math.floor]],
46 [.name = "fma", .f = `Flt_flt_flt__flt [ .f32 = math.fma, .f64 = math.fma]],
47 [.name = "log", .f = `Flt__flt [ .f32 = math.log, .f64 = math.log]],
48 [.name = "log1p", .f = `Flt__flt [ .f32 = math.log1p, .f64 = math.log1p]],
49 [.name = "powr", .f = `Flt_flt__flt [ .f32 = math.powr, .f64 = math.powr]],
50 [.name = "sqrt", .f = `Flt__flt [ .f32 = math.sqrt, .f64 = math.sqrt]],
51 [.name = "sin", .f = `Flt__flt [ .f32 = math.sin, .f64 = math.sin]],
52 [.name = "trunc", .f = `Flt__flt [ .f32 = math.trunc, .f64 = math.trunc]],
55 var p : flt_prec = `Single
56 var f : fn_desc = available_fns[0]
59 (p, f, n) = read_args(args)
64 const read_args = {args : byte[:][:]
65 var p : flt_prec = `Single
67 var fname : byte[:] = ""
68 var fn : fn_desc = available_fns[0]
69 var cmd = std.optparse(args, &[
72 [.opt = 's', .desc = "use single precision (default)"],
73 [.opt = 'd', .desc = "use double precision"],
74 [.opt = 'n', .arg = "N", .desc = "read/write ‘N’ entries at a time"],
75 [.opt = 'f', .arg = "func", .desc = "use function ‘f’"],
81 | ('s', _): p = `Single
82 | ('d', _): p = `Double
84 match std.intparse(ns)
85 | `std.Some np: n = np
87 std.put("impl-myrddin: unparsable number “{}”\n", ns)
90 | ('f', fs): fname = fs
91 | _ : std.die("impl-myrddin: impossible\n")
95 var good_fn : bool = false
97 if std.eq(f.name, fname)
105 std.put("impl-myrddin: unknown function “{}”\n", fname)
110 std.put("impl-myrddin: positive number of entries required\n")
118 const io_loop = {p : flt_prec, fn : fn_desc, n : std.size
119 var input_sz : std.size = 0
120 var output_sz : std.size = 0
121 var in_buf : byte[:] = [][:]
122 var out_buf : byte[:] = [][:]
123 var w = prec_width(p)
125 (input_sz, output_sz) = io_widths(p, fn)
127 if (((input_sz * n) / input_sz) != n) || (((output_sz * n) / output_sz) != n)
128 std.put("impl-myrddin: overflow in i/o buffer size\n")
132 in_buf = std.slalloc(input_sz * n)
133 out_buf = std.slalloc(output_sz * n)
136 match std.readall(0, in_buf)
139 std.put("impl-myrddin: std.readall(): {}\n", e)
143 for var j = 0; j < n; ++j
144 var ib : byte[:] = in_buf[j * input_sz:(j + 1) * input_sz]
145 var ob : byte[:] = out_buf[j * output_sz:(j + 1) * output_sz]
147 | (`Single, `Flt__flt f):
148 var x : flt32 = std.flt32frombits(std.getle32(ib))
149 std.putle32(ob, std.flt32bits(f.f32(x)))
150 | (`Double, `Flt__flt f):
151 var x : flt64 = std.flt64frombits(std.getle64(ib))
152 std.putle64(ob, std.flt64bits(f.f64(x)))
153 | (`Single, `Flt_flt__flt f):
154 var x1 : flt32 = std.flt32frombits(std.getle32(ib[0: 4]))
155 var x2 : flt32 = std.flt32frombits(std.getle32(ib[4: 8]))
156 std.putle32(ob, std.flt32bits(f.f32(x1, x2)))
157 | (`Double, `Flt_flt__flt f):
158 var x1 : flt64 = std.flt64frombits(std.getle64(ib[ 0: 8]))
159 var x2 : flt64 = std.flt64frombits(std.getle64(ib[ 8:16]))
160 std.putle64(ob, std.flt64bits(f.f64(x1, x2)))
161 | (`Single, `Flt_flt_flt__flt f):
162 var x1 : flt32 = std.flt32frombits(std.getle32(ib[0: 4]))
163 var x2 : flt32 = std.flt32frombits(std.getle32(ib[4: 8]))
164 var x3 : flt32 = std.flt32frombits(std.getle32(ib[8:12]))
165 std.putle32(ob, std.flt32bits(f.f32(x1, x2, x3)))
166 | (`Double, `Flt_flt_flt__flt f):
167 var x1 : flt64 = std.flt64frombits(std.getle64(ib[ 0: 8]))
168 var x2 : flt64 = std.flt64frombits(std.getle64(ib[ 8:16]))
169 var x3 : flt64 = std.flt64frombits(std.getle64(ib[16:24]))
170 std.putle64(ob, std.flt64bits(f.f64(x1, x2, x3)))
174 match std.writeall(1, out_buf)
177 std.put("impl-myrddin: std.writeall(): {}\n", e)
183 const prec_width = {p : flt_prec
190 const io_widths = {p : flt_prec, fn : fn_desc
191 var w : std.size = prec_width(p)
194 | `Flt__flt _ : -> (w, w)
195 | `Flt_flt__flt _ : -> (2*w, w)
196 | `Flt_flt_flt__flt _ : -> (3*w, w)