include/punk_observe/po_nsarith.h

filter: Statementstmt 78.2% 18 / 23 Branchbran 47.8% 22 / 46 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 57.9% 40 / 69 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Punk/Observe/Store.pm ↑ Summary xs/ingest.xs »
1 /* po_nsarith.h - nanosecond arithmetic, done as integers.
2 *
3 * A NANOSECOND INSTANT DOES NOT FIT A DOUBLE, AND ON HALF THE SMOKERS IT DOES
4 * NOT FIT AN IV EITHER.
5 *
6 * 2026 is about 1.79e18 nanoseconds past the epoch. A double carries 53 bits
7 * of mantissa, so it starts losing the low digits somewhere around 9.0e15 -
8 * which is to say it has been wrong since 1970 plus a few months. The Perl
9 * this replaces handled that by doing DECIMAL STRING arithmetic: splitting
10 * both operands into digits, adding with a carry, joining back. Correct, and
11 * roughly two hundred times the cost of the thing it is emulating.
12 *
13 * It is emulating a uint64. This distribution has had one since phase 0.
14 *
15 * So the arithmetic is done in po_u64 and the strings exist only at the Perl
16 * boundary, where po_sv_to_u64 and po_u64_to_sv already handle the crossing:
17 * a UV where that is lossless, a decimal string where it is not.
18 *
19 * WHAT IS NOT ASSUMED: that every value fits. A caller can hand in something
20 * that is not a bare non-negative integer - an empty string, a float, a
21 * timestamp from a future format - and the answer has to be defined for those
22 * rather than merely fast for the rest. Anything that does not parse falls
23 * back to a comparison the caller can still reason about, and the fallback is
24 * reported rather than silent.
25 */
26 #ifndef PO_NSARITH_H
27 #define PO_NSARITH_H
28  
29 #include "punk_observe/po_compat.h"
30  
31 /* Compare two instants.
32 *
33 * `ok` is set when both parsed as integers. When either did not, the result
34 * is a plain byte comparison of what was there - which is at least a total
35 * order, so a sort using it still terminates and still produces a stable
36 * answer instead of undefined behaviour. */
37 546997 static int po_ns_cmp_str(const char *a, size_t alen,
38 const char *b, size_t blen, int *ok) {
39 546997 size_t ai = 0, bi = 0;
40 size_t an, bn;
41  
42 546997 ▶ if (ok) *ok = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (ok) *ok = 0; 468 546529 546997
43  
44 /* Leading zeroes are not significant, and a value written with them is
45 * the same instant. Skipping them here means "007" and "7" compare equal
46 * rather than sorting by width. */
47 546997 ▶ while (ai < alen && a[ai] == '0') ai++;
Branch: 2 branches
BranchTrueFalseTotal
while (ai < alen && a[ai] == '0') ai++; 546997 0 546997
while (ai < alen && a[ai] == '0') ai++; 0 546997 546997
48 546997 ▶ while (bi < blen && b[bi] == '0') bi++;
Branch: 2 branches
BranchTrueFalseTotal
while (bi < blen && b[bi] == '0') bi++; 546997 0 546997
while (bi < blen && b[bi] == '0') bi++; 0 546997 546997
49 546997 an = alen - ai;
50 546997 bn = blen - bi;
51  
52 { /* Both must be all digits for the numeric answer to mean anything. */
53 size_t i;
54 12032762 ▶ for (i = ai; i < alen; i++) if (a[i] < '0' || a[i] > '9') goto bytes;
Branch: 3 branches
BranchTrueFalseTotal
for (i = ai; i < alen; i++) if (a[i] < '0' || a[i] > '9') goto bytes; 11485765 0 11485765
for (i = ai; i < alen; i++) if (a[i] < '0' || a[i] > '9') goto bytes; 0 11485765 11485765
for (i = ai; i < alen; i++) if (a[i] < '0' || a[i] > '9') goto bytes; 11485765 546997 12032762
55 12032762 ▶ for (i = bi; i < blen; i++) if (b[i] < '0' || b[i] > '9') goto bytes;
Branch: 3 branches
BranchTrueFalseTotal
for (i = bi; i < blen; i++) if (b[i] < '0' || b[i] > '9') goto bytes; 11485765 0 11485765
for (i = bi; i < blen; i++) if (b[i] < '0' || b[i] > '9') goto bytes; 0 11485765 11485765
for (i = bi; i < blen; i++) if (b[i] < '0' || b[i] > '9') goto bytes; 11485765 546997 12032762
56 }
57  
58 546997 ▶ if (ok) *ok = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ok) *ok = 1; 468 546529 546997
59 /* MORE DIGITS IS LARGER, once the leading zeroes are gone. That single
60 * rule is what makes this exact for values of any width, including ones
61 * past a uint64 - which a parse-then-compare would silently clamp. */
62 546997 ▶ if (an != bn) return an < bn ? -1 : 1;
Branch: 2 branches
BranchTrueFalseTotal
if (an != bn) return an < bn ? -1 : 1; 0 546997 546997
if (an != bn) return an < bn ? -1 : 1; 0 0 0
63 546997 ▶ if (an == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (an == 0) return 0; 0 546997 546997
64 {
65 546997 int c = memcmp(a + ai, b + bi, an);
66 546997 ▶ return c < 0 ? -1 : (c > 0 ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
return c < 0 ? -1 : (c > 0 ? 1 : 0); 488601 58396 546997
67 }
68  
69 0 bytes:
70 {
71 0 size_t n = alen < blen ? alen : blen;
72 0 ▶ int c = n ? memcmp(a, b, n) : 0;
Branch: 1 branch
BranchTrueFalseTotal
int c = n ? memcmp(a, b, n) : 0; 0 0 0
73 0 ▶ if (c) return c < 0 ? -1 : 1;
Branch: 2 branches
BranchTrueFalseTotal
if (c) return c < 0 ? -1 : 1; 0 0 0
if (c) return c < 0 ? -1 : 1; 0 0 0
74 0 ▶ return alen == blen ? 0 : (alen < blen ? -1 : 1);
Branch: 2 branches
BranchTrueFalseTotal
return alen == blen ? 0 : (alen < blen ? -1 : 1); 0 0 0
return alen == blen ? 0 : (alen < blen ? -1 : 1); 0 0 0
75 }
76 }
77  
78 /* Add, saturating. Overflow is clamped rather than wrapped: a retention
79 * horizon that wrapped to zero would delete everything, and one that clamps
80 * to the top of the range keeps everything, which is the failure worth
81 * having. */
82 9774 static po_u64 po_ns_add(po_u64 a, po_u64 b) {
83 9774 ▶ return a > PO_U64_MAX - b ? PO_U64_MAX : a + b;
Branch: 1 branch
BranchTrueFalseTotal
return a > PO_U64_MAX - b ? PO_U64_MAX : a + b; 9774 0 9774
84 }
85  
86 /* Subtract, clamped at zero. An instant before the epoch is not a thing, and
87 * in a uint64 the difference is not a small negative number - it is 1.8e19,
88 * which as a time bound admits everything. */
89 584 static po_u64 po_ns_sub(po_u64 a, po_u64 b) {
90 584 ▶ return a < b ? 0 : a - b;
Branch: 1 branch
BranchTrueFalseTotal
return a < b ? 0 : a - b; 584 0 584
91 }
92  
93 #endif /* PO_NSARITH_H */
« blib/lib/Punk/Observe/Store.pm ↑ Summary xs/ingest.xs »