+/* --- @tvec_claimeq_float@ --- *
+ *
+ * Arguments: @struct tvec_state *tv@ = test-vector state
+ * @double f0, f1@ = two floating-point numbers
+ * @const char *file@, @unsigned @lno@ = calling file and line
+ * @const char *expr@ = the expression to quote on failure
+ *
+ * Returns: Nonzero if @f0@ and @u1@ are identical, otherwise zero.
+ *
+ * Use: Check that values of @f0@ and @f1@ are identical. The
+ * function is exactly equivalent to @tvec_claimeqish_float@
+ * with @f == TVFF_EXACT@.
+ */
+
+int tvec_claimeq_float(struct tvec_state *tv,
+ double f0, double f1,
+ const char *file, unsigned lno,
+ const char *expr)
+{
+ return (tvec_claimeqish_float(tv, f0, f1, TVFF_EXACT, 0.0,
+ file, lno, expr));
+}
+
+/*----- Durations ---------------------------------------------------------*/
+
+/* A duration is a floating-point number of seconds. Initialization and
+ * teardown, equality comparison, and serialization are as for floating-point
+ * values.
+ */
+
+static const struct duration_unit {
+ const char *unit;
+ double scale;
+ unsigned f;
+#define DUF_PREFER 1u
+} duration_units[] = {
+ { "Ys", 1e+24, 0 },
+ { "Zs", 1e+21, 0 },
+ { "Es", 1e+18, 0 },
+ { "Ps", 1e+15, 0 },
+ { "Ts", 1e+12, 0 },
+ { "Gs", 1e+9, 0 },
+ { "Ms", 1e+6, 0 },
+ { "ks", 1e+3, 0 },
+ { "hs", 1e+2, 0 },
+ { "das", 1e+1, 0 },
+
+ { "yr", 31557600.0, DUF_PREFER },
+ { "y", 31557600.0, 0 },
+ { "day", 86400.0, DUF_PREFER },
+ { "dy", 86400.0, 0 },
+ { "d", 86400.0, 0 },
+ { "hr", 3600.0, DUF_PREFER },
+ { "hour", 3600.0, 0 },
+ { "h", 3600.0, 0 },
+ { "min", 60.0, DUF_PREFER },
+ { "m", 60.0, 0 },
+
+ { "s", 1.0, DUF_PREFER },
+ { "sec", 1.0, 0 },
+
+ { "ds", 1e-1, 0 },
+ { "cs", 1e-2, 0 },
+ { "ms", 1e-3, DUF_PREFER },
+ { "µs", 1e-6, DUF_PREFER },
+ { "ns", 1e-9, DUF_PREFER },
+ { "ps", 1e-12, DUF_PREFER },
+ { "fs", 1e-15, DUF_PREFER },
+ { "as", 1e-18, DUF_PREFER },
+ { "zs", 1e-21, DUF_PREFER },
+ { "ys", 1e-24, DUF_PREFER },
+
+ { 0 }
+};
+
+/* --- @tvec_parsedurunit@ --- *
+ *
+ * Arguments: @double *scale_out@ = where to leave the scale
+ * @const char **p_inout@ = input unit string, updated
+ *
+ * Returns: Zero on success, %$-1$% on error.
+ *
+ * Use: If @*p_inout@ begins with a unit string followed by the end
+ * of the string or some non-alphanumeric character, then store
+ * the corresponding scale factor in @*scale_out@, advance
+ * @*p_inout@ past the unit string, and return zero. Otherwise,
+ * return %$-1$%.
+ */
+
+int tvec_parsedurunit(double *scale_out, const char **p_inout)
+{
+ const char *p = *p_inout, *q;
+ const struct duration_unit *u;
+ size_t n;
+
+ while (ISSPACE(*p)) p++;
+ for (q = p; *q && ISALNUM(*q); q++);
+ n = q - p; if (!n) { *scale_out = 1.0; return (0); }
+
+ for (u = duration_units; u->unit; u++)
+ if (STRNCMP(p, ==, u->unit, n) && !u->unit[n])
+ { *scale_out = u->scale; *p_inout = q; return (0); }
+ return (-1);
+}
+
+/* --- @parse_duration@ --- *
+ *
+ * Arguments: @union tvec_regval *rv@ = register value
+ * @const struct tvec_regdef *rd@ = register definition
+ * @struct tvec_state *tv@ = test-vector state
+ *
+ * Returns: Zero on success, %$-1$% on error.
+ *
+ * Use: Parse a register value from an input file.
+ *
+ * Duration values are finite nonnegative floating-point
+ * numbers in @strtod@ syntax, optionally followed by a unit .
+ */
+
+static int parse_duration(union tvec_regval *rv,
+ const struct tvec_regdef *rd,
+ struct tvec_state *tv)
+{
+ const struct duration_unit *u;
+ const char *q;
+ dstr d = DSTR_INIT;
+ double t;
+ int rc;
+
+ if (tvec_readword(tv, &d, 0, ";", "duration")) { rc = -1; goto end; }
+ if (parse_floating(&t, &q, d.buf,
+ rd->arg.p ? rd->arg.p : &tvflt_nonneg, tv))
+ { rc = -1; goto end; }
+
+ if (!*q) tvec_readword(tv, &d, &q, ";", 0);
+ if (*q) {
+ for (u = duration_units; u->unit; u++)
+ if (STRCMP(q, ==, u->unit)) { t *= u->scale; goto found_unit; }
+ rc = tvec_syntax(tv, *q, "end-of-line"); goto end;
+ found_unit:;
+ }
+
+ if (tvec_flushtoeol(tv, 0)) { rc = -1; goto end; }
+ rv->f = t; rc = 0;
+end:
+ dstr_destroy(&d);
+ return (rc);
+}
+
+/* --- @dump_duration@ --- *
+ *
+ * Arguments: @const union tvec_regval *rv@ = register value
+ * @const struct tvec_regdef *rd@ = register definition
+ * @unsigned style@ = output style (@TVSF_...@)
+ * @const struct gprintf_ops *gops@, @void *gp@ = format output
+ *
+ * Returns: ---
+ *
+ * Use: Dump a register value to the format output.
+ *
+ * Durations are dumped as a human-palatable scaled value with
+ * unit, and, if compact style is not requested, as a raw number
+ * of seconds at full precision as a comment.
+ */
+
+static void dump_duration(const union tvec_regval *rv,
+ const struct tvec_regdef *rd,
+ unsigned style,
+ const struct gprintf_ops *gops, void *go)
+{
+ const struct duration_unit *u;
+ double t = rv->f;
+
+ if (!t) u = 0;
+ else {
+ for (u = duration_units; u->scale > t && u[1].unit; u++);
+ t /= u->scale;
+ }
+
+ gprintf(gops, go, "%.4g %s", t, u ? u->unit : "s");
+ if (!(style&TVSF_COMPACT)) {
+ gprintf(gops, go, "; = ");
+ format_floating(gops, go, rv->f);
+ gprintf(gops, go, " s");
+ }
+}
+
+/* Duration type definition. */
+const struct tvec_regty tvty_duration = {
+ init_float, trivial_release, eq_float,
+ tobuf_float, frombuf_float,
+ parse_duration, dump_duration
+};
+