8 type impl_prog = struct
20 /* Flt is ``whatever precision we're testing''. */
36 type exactness = union
41 /* (name, number-of-flt-args, constant-extra-bytes) */
42 var available_fns : fn_desc[:] = [][:]
46 const main = {args : byte[:][:]
48 [.name = "id", .inputs = [`Flt][:], .outputs = [`Flt][:]],
49 [.name = "ceil", .inputs = [`Flt][:], .outputs = [`Flt][:]],
50 [.name = "cos", .inputs = [`Flt][:], .outputs = [`Flt][:]],
51 [.name = "exp", .inputs = [`Flt][:], .outputs = [`Flt][:]],
52 [.name = "expm1", .inputs = [`Flt][:], .outputs = [`Flt][:]],
53 [.name = "floor", .inputs = [`Flt][:], .outputs = [`Flt][:]],
54 [.name = "fma", .inputs = [`Flt, `Flt, `Flt][:], .outputs = [`Flt][:]],
55 [.name = "log", .inputs = [`Flt][:], .outputs = [`Flt][:]],
56 [.name = "log1p", .inputs = [`Flt][:], .outputs = [`Flt][:]],
57 [.name = "powr", .inputs = [`Flt, `Flt][:], .outputs = [`Flt][:]],
58 [.name = "sin", .inputs = [`Flt][:], .outputs = [`Flt][:]],
59 [.name = "sincos",.inputs = [`Flt][:], .outputs = [`Flt, `Flt][:]],
60 [.name = "sqrt", .inputs = [`Flt][:], .outputs = [`Flt][:]],
61 [.name = "trunc", .inputs = [`Flt][:], .outputs = [`Flt][:]],
66 .handler = (nop : byte#),
67 .flags = sys.Saresethand,
69 sys.sigaction(sys.Sigpipe, &sa, &old)
71 var fn : fn_desc = available_fns[0]
72 var next_bits_fn : (b : byte[:], n : std.size -> bool) = next_rand
74 var precision : flt_prec = `Single
75 var exactness : exactness = `Exact
76 var impls : impl_prog#[:]
77 rng = std.mksrng((std.now() : uint32))
79 (precision, exactness, fn, next_bits_fn) = read_args(args)
81 var input_sz = args_width(fn.inputs, precision)
82 var num_inputs = (1 << 18)
83 var buf_sz = num_inputs * input_sz
85 impls = start_impls([prec_arg(precision), "-f", fn.name, "-n", std.fmt("{}", num_inputs)][:])
87 io_loop(impls, precision, exactness, num_inputs, fn, next_bits_fn)
105 const args_width = {ts : fp_type[:], p : flt_prec
110 | `Flt: w += prec_width(p)
117 const read_args = {args : byte[:][:]
118 var exactness : exactness = `Exact
119 var precision : flt_prec = `Single
120 var fn_name : byte[:] = "UNSPECIFIED"
121 var fn : fn_desc = available_fns[0]
122 var next_bits_fn : (b : byte[:], n : std.size -> bool) = next_rand
124 var cmd = std.optparse(args, &[
127 [.opt = 's', .desc = "use single precision (default)"],
128 [.opt = 'd', .desc = "use double precision"],
129 [.opt = 'l', .desc = "list available functions"],
130 [.opt = 'f', .arg = "f", .desc = "test function ‘f’"],
131 [.opt = 'r', .arg = "s", .desc = "choose inputs randomly, with seed ‘s’"],
132 [.opt = 'e', .desc = "exhaust input space"],
133 [.opt = 'i', .arg = "i", .desc = "allow inexact matches (by ‘i’, bitwise)"],
144 match std.intparse(arg)
147 exactness = `Inexact (n : uint)
149 std.put("unacceptable inexactness “{}”\n", arg)
152 std.put("cannot parse inexactness “{}”\n", arg)
158 | ('f', arg): fn_name = arg
160 next_bits_fn = next_rand
161 match std.intparse(arg)
165 std.put("cannot parse seed “{}”\n", arg)
168 | ('e', _): next_bits_fn = next_exhaust
169 | _: std.die("impossible\n")
173 var good_fn : bool = false
174 for f : available_fns
175 if std.eq(f.name, fn_name)
183 std.put("unknown function “{}”\n", fn_name)
187 -> (precision, exactness, fn, next_bits_fn)
190 const io_loop = {impls : impl_prog#[:], p : flt_prec, x : exactness, num_inputs : std.size, fn : fn_desc, next_bits_fn : (b : byte[:], n : std.size -> bool)
191 var input_sz = args_width(fn.inputs, p)
192 var output_sz = args_width(fn.outputs, p)
193 var draw_line : bool = false
195 var bits : byte[:] = std.slalloc(num_inputs * input_sz)
196 var last_bits : byte[:] = std.slalloc(num_inputs * input_sz)
198 std.slfill(last_bits, 0)
200 i.output_bits = std.slalloc(num_inputs * output_sz)
201 std.slfill(i.output_bits, 0)
204 /* Now, loop perhaps infinitely with the comparisons */
210 match std.writeall(i.stdin, bits)
213 std.put("CRASH: {w=20} [{w=6}] failed to receive data\n", i.name, i.pid)
220 for var j = 0; j < impls.len; ++j
224 impls[j] = impls[impls.len - 1]
225 std.slgrow(&impls, impls.len - 1)
228 /* Gather consensus on last time's answers */
229 consensus(last_bits, num_inputs, fn, p, x, impls)
232 std.put("\x1b[1G\x1b[0K")
238 std.slcp(last_bits, bits)
240 /* Receive new answers */
246 match std.readall(i.stdout, i.output_bits)
249 std.put("CRASH: {w=20} [{w=6}] failed to send data\n", i.name, i.pid)
257 std.put("Less than 2 implementations left. Consensus impossible.\n")
258 std.put("----------\n")
263 std.put("----------\n")
268 if next_bits_fn(bits, num_inputs)
273 const next_rand = { b : byte[:], n : std.size
274 std.rngrandbytes(rng, b)
278 const next_exhaust = { b : byte[:], n : std.size
279 var one_arg : byte[:] = std.slalloc(b.len / n)
280 std.slcp(one_arg, b[b.len - one_arg.len:])
281 var finished : bool = false
283 /* n is the number of total argument groups */
284 for var i = 0; i < n; ++i
285 /* Increment this particular argument */
286 var j = one_arg.len - 1
295 finished = finished || j < 0
296 var z = one_arg.len * i
297 std.slcp(b[z:z + one_arg.len], one_arg)
303 const list_functions = {
304 std.put("Available functions:\n")
305 std.put("--------------------\n")
306 for f : available_fns
311 const start_impls = {opts : byte[:][:]
312 var cmd : byte[:][:] = std.slalloc(opts.len + 1)
313 var nice_name : byte[:] = [][:]
314 var started_impls : impl_prog#[:] = [][:]
315 var survived_impls : impl_prog#[:] = [][:]
317 for var j = 0; j < opts.len; ++j
321 /* Start everything */
322 for f : fileutil.bywalk(".")
323 match std.strrfind(f, "/")
324 | `std.Some j: nice_name = std.sldup(f[j+1:])
325 | `std.None: nice_name = std.sldup(f)
328 if nice_name.len < 5 || !std.eq(nice_name[:5], "impl-") || \
329 std.eq(nice_name[nice_name.len - 2:nice_name.len - 1], ".")
330 std.slfree(nice_name)
336 | `std.Ok (p, fi, fo) :
337 std.slpush(&started_impls, std.mk([
344 | `std.Err e: std.slfree(nice_name)
348 /* Give them a bit of time to die */
351 /* Reap the zombies */
354 var WNOHANG = 1 /* HACK */
355 while ((z = sys.waitpid(-1, &l, WNOHANG)) > 0)
356 match sys.waitstatus(l)
360 | `sys.Waitstop _: continue
362 for i : started_impls
363 if i.pid == (z : std.pid)
370 for i : started_impls
375 std.slpush(&survived_impls, i)
378 match survived_impls.len
380 std.put("No implementations found. Try running from fpmath-consensus root dir.\n")
383 std.put("Only one implementation found. Comparisons will be impossible.\n")
388 std.put("Executing:\n")
389 std.put("----------\n")
390 for i : survived_impls
391 std.put(" [{w=6}] {w=20}", i.pid, i.name)
397 std.put("----------\n")
399 std.slfree(started_impls)
404 const consensus = { input : byte[:], num_inputs : std.size, fn : fn_desc, p : flt_prec, x : exactness, impls : impl_prog#[:]
405 var all_agree : bool = true
406 var flt_sz : std.size = prec_width(p)
407 var inputs_sz = args_width(fn.inputs, p)
408 var outputs_sz = args_width(fn.outputs, p)
410 for var z = 0; z < num_inputs; ++z
413 /* The input is possibly multiple entries */
414 var i_start = z * inputs_sz
415 var i_end = (z + 1) * inputs_sz
417 /* The output might also be strange */
418 var a_start = z * outputs_sz
419 var a_end = (z + 1) * outputs_sz
421 for var j = 0; j + 1 < impls.len; ++j
424 if std.sleq(impls[j].output_bits[a_start:a_end], impls[j+1].output_bits[a_start:a_end])
428 /* The memory patterns don't agree. But perhaps this is due to NaN? */
429 if detailed_eq(impls[j].output_bits[a_start:a_end], impls[j+1].output_bits[a_start:a_end], fn.outputs, p, 0)
433 var good : bool = true
435 for var k = j + 1; k < impls.len; ++k
436 good = good && detailed_eq(impls[j].output_bits[a_start:a_end], impls[k].output_bits[a_start:a_end], fn.outputs, p, i)
453 std.put("For input: ")
454 extract_and_describe(input[i_start:i_end], fn.inputs, p)
457 std.put(" [{w=6}] {w=20}: ", i.pid, i.name)
458 extract_and_describe(i.output_bits[a_start:a_end], fn.outputs, p)
460 std.put("----------\n")
464 const extract_and_describe = {bits : byte[:], ts : fp_type[:], p : flt_prec
465 var w : std.size = prec_width(p)
480 var u = std.getle32(bits[:w])
481 std.put("0x{w=8,p=0,x} ({})\n", u, std.flt32frombits(u))
483 var u = std.getle64(bits[:w])
484 std.put("0x{w=16,p=0,x} ({})\n", u, std.flt64frombits(u))
492 const detailed_eq = {a : byte[:], b : byte[:], ts : fp_type[:], p : flt_prec, i : uint
493 var w : std.size = prec_width(p)
500 var u1 = std.getle32(a[:w])
501 var u2 = std.getle32(b[:w])
502 var f1 = std.flt32frombits(u1)
503 var f2 = std.flt32frombits(u2)
504 if !((f1 == f2) || (std.isnan(f1) && std.isnan(f2)))
505 if i == 0 || ((u1 - u2 > (i : uint32)) && (u2 - u1 > (i : uint32)))
510 var u1 = std.getle64(a[:w])
511 var u2 = std.getle64(b[:w])
512 var f1 = std.flt64frombits(u1)
513 var f2 = std.flt64frombits(u2)
514 if !((f1 == f2) || (std.isnan(f1) && std.isnan(f2)))
515 if i == 0 || ((u1 - u2 > (i : uint64)) && (u2 - u1 > (i : uint64)))