5 type Fn_flt__flt = struct
6 f32 : (f : flt32 -> flt32)
7 f64 : (f : flt64 -> flt64)
22 var available_fns : fn_desc[:] = [][:]
24 generic id : (a : @a -> @a) = {x; -> x}
26 const main = {args : byte[:][:]
28 [.name = "id", .f = `Flt__flt [ .f32 = id, .f64 = id]],
29 [.name = "trunc", .f = `Flt__flt [ .f32 = math.trunc, .f64 = id]],
32 var p : flt_prec = `Single
33 var f : fn_desc = available_fns[0]
36 (p, f, n) = read_args(args)
41 const read_args = {args : byte[:][:]
42 var p : flt_prec = `Single
44 var fname : byte[:] = ""
45 var fn : fn_desc = available_fns[0]
46 var cmd = std.optparse(args, &[
49 [.opt = 's', .desc = "use single precision (default)"],
50 [.opt = 'd', .desc = "use double precision"],
51 [.opt = 'n', .arg = "N", .desc = "read/write ‘N’ entries at a time"],
52 [.opt = 'f', .arg = "func", .desc = "use function ‘f’"],
58 | ('s', _): p = `Single
59 | ('d', _): p = `Double
61 match std.intparse(ns)
62 | `std.Some np: n = np
64 std.put("impl-myrddin: unparsable number “{}”\n", ns)
67 | ('f', fs): fname = fs
68 | _ : std.die("impl-myrddin: impossible\n")
72 var good_fn : bool = false
74 if std.eq(f.name, fname)
82 std.put("impl-myrddin: unknown function “{}”\n", fname)
87 std.put("impl-myrddin: positive number of entries required\n")
95 const io_loop = {p : flt_prec, fn : fn_desc, n : std.size
96 var input_sz : std.size = 0
97 var output_sz : std.size = 0
98 var in_buf : byte[:] = [][:]
99 var out_buf : byte[:] = [][:]
100 var w = prec_width(p)
102 (input_sz, output_sz) = io_widths(p, fn)
104 if (((input_sz * n) / input_sz) != n) || (((output_sz * n) / output_sz) != n)
105 std.put("impl-myrddin: overflow in i/o buffer size\n")
109 in_buf = std.slalloc(input_sz * n)
110 out_buf = std.slalloc(output_sz * n)
113 match std.readall(0, in_buf)
116 std.put("impl-myrddin: std.readall(): {}\n", e)
121 | (`Single, `Flt__flt f):
122 for var j = 0; j < n; ++j
123 var ib : byte[:] = in_buf[j * w:(j + 1) * w]
124 var ob : byte[:] = out_buf[j * w:(j + 1) * w]
125 var x : flt32 = std.flt32frombits(std.getle32(ib))
126 std.putle32(ob, std.flt32bits(f.f32(x)))
128 | (`Double, `Flt__flt f):
129 for var j = 0; j < n; ++j
130 var ib : byte[:] = in_buf[j * w:(j + 1) * w]
131 var ob : byte[:] = out_buf[j * w:(j + 1) * w]
132 var x : flt64 = std.flt64frombits(std.getle64(ib))
133 std.putle64(ob, std.flt64bits(f.f64(x)))
137 match std.writeall(1, out_buf)
140 std.put("impl-myrddin: std.writeall(): {}\n", e)
146 const prec_width = {p : flt_prec
153 const io_widths = {p : flt_prec, fn : fn_desc
154 var w : std.size = prec_width(p)
157 | `Flt__flt _ : -> (w, w)