1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
|
// SPDX-License-Identifier: MPL-2.0
// (c) Hare authors <https://harelang.org>
use math;
// Adds a [[duration]] to an [[instant]], returning an instant further in the
// future (given a positive duration), or further in the past (given a negative
// duration).
export fn add(i: instant, x: duration) instant = {
if (x == 0) {
return i;
} else if (x > 0) {
return instant {
sec = i.sec + (i.nsec + x) / SECOND,
nsec = (i.nsec + x) % SECOND,
};
} else {
return instant {
sec = i.sec + (i.nsec + x - SECOND + NANOSECOND) / SECOND,
nsec = (i.nsec + (x % SECOND) + SECOND) % SECOND,
};
};
};
// Returns the [[duration]] from [[instant]] "a" to [[instant]] "b".
export fn diff(a: instant, b: instant) duration = {
return ((b.sec - a.sec) * SECOND) + (b.nsec - a.nsec);
};
// Returns -1 if a precedes b, 0 if a and b are simultaneous, or +1 if b
// precedes a.
export fn compare(a: instant, b: instant) i8 = {
return if (a.sec < b.sec) -1
else if (a.sec > b.sec) 1
else if (a.nsec < b.nsec) -1
else if (a.nsec > b.nsec) 1
else 0;
};
// Scales the given [[instant]]'s scalar value by a factor 'f'. Make sure to
// know what epoch you're dealing with.
export fn mult(i: instant, f: f64) instant = {
// use positive numbers for convenience
const positive = if (i.sec < 0 ^^ f < 0.0) false else true;
const f = if (f < 0.0) -f else f;
const asec: i64 = if (i.sec < 0) -i.sec - 1 else i.sec;
const ansec: i64 = if (i.sec < 0) SECOND - i.nsec else i.nsec;
// initial multiply
const fsec = (asec: f64 * f);
const bsec = fsec: i64;
const fnsec = (ansec: f64 * f);
const bnsec = fnsec: i64;
// get seconds overflow (nsec remainder)
const secrem = math::modf64(fsec, 1.0);
const addnsec = (secrem * SECOND: f64): i64;
// add overflows
const b = instant {
sec = bsec,
nsec = ansec,
};
const b = add(b, bnsec - ansec); // add nsec overflow
const b = add(b, addnsec); // add sec overflow
// switch back to original sign
const b = if (positive) {
yield b;
} else {
yield instant {
sec = -b.sec - 1,
nsec = SECOND - b.nsec,
};
};
return b;
};
@test fn add() void = {
const cases = [
// instant a duration x instant b
( 0, 0, -2000000001, -3, 999999999),
( 0, 0, -2000000000, -2, 0),
( 0, 0, -1999999999, -2, 1),
( 0, 0, -1000000001, -2, 999999999),
( 0, 0, -1000000000, -1, 0),
( 0, 0, -999999999, -1, 1),
( 0, 0, -1, -1, 999999999),
( 0, 0, 0, 0, 0),
( 0, 0, 1, 0, 1),
( 0, 0, 999999999, 0, 999999999),
( 0, 0, 1000000000, 1, 0),
( 0, 0, 1000000001, 1, 1),
( 0, 0, 1999999999, 1, 999999999),
( 0, 0, 2000000000, 2, 0),
( 0, 0, 2000000001, 2, 1),
( 0, 1, -2000000001, -2, 0),
( 0, 1, -2000000000, -2, 1),
( 0, 1, -1999999999, -2, 2),
( 0, 1, -1000000001, -1, 0),
( 0, 1, -1000000000, -1, 1),
( 0, 1, -999999999, -1, 2),
( 0, 1, -1, 0, 0),
( 0, 1, 0, 0, 1),
( 0, 1, 1, 0, 2),
( 0, 1, 999999999, 1, 0),
( 0, 1, 1000000000, 1, 1),
( 0, 1, 1000000001, 1, 2),
( 0, 1, 1999999999, 2, 0),
( 0, 1, 2000000000, 2, 1),
( 0, 1, 2000000001, 2, 2),
(-1, 999999999, -2000000001, -3, 999999998),
(-1, 999999999, -2000000000, -3, 999999999),
(-1, 999999999, -1999999999, -2, 0),
(-1, 999999999, -1000000001, -2, 999999998),
(-1, 999999999, -1000000000, -2, 999999999),
(-1, 999999999, - 999999999, -1, 0),
(-1, 999999999, -1, -1, 999999998),
(-1, 999999999, 0, -1, 999999999),
(-1, 999999999, 1, 0, 0),
(-1, 999999999, 999999999, 0, 999999998),
(-1, 999999999, 1000000000, 0, 999999999),
(-1, 999999999, 1000000001, 1, 0),
(-1, 999999999, 1999999999, 1, 999999998),
(-1, 999999999, 2000000000, 1, 999999999),
(-1, 999999999, 2000000001, 2, 0),
( 0, 999999999, -2000000001, -2, 999999998),
( 0, 999999999, -2000000000, -2, 999999999),
( 0, 999999999, -1999999999, -1, 0),
( 0, 999999999, -1000000001, -1, 999999998),
( 0, 999999999, -1000000000, -1, 999999999),
( 0, 999999999, -999999999, 0, 0),
( 0, 999999999, -1, 0, 999999998),
( 0, 999999999, 0, 0, 999999999),
( 0, 999999999, 1, 1, 0),
( 0, 999999999, 999999999, 1, 999999998),
( 0, 999999999, 1000000000, 1, 999999999),
( 0, 999999999, 1000000001, 2, 0),
( 0, 999999999, 1999999999, 2, 999999998),
( 0, 999999999, 2000000000, 2, 999999999),
( 0, 999999999, 2000000001, 3, 0),
];
for (let i = 0z; i < len(cases); i += 1) {
const C = cases[i];
const a = instant { sec = C.0, nsec = C.1 };
const x = C.2;
const b = instant { sec = C.3, nsec = C.4 };
const B = add(a, x);
assert(B.sec == b.sec, "time::add() .sec error");
assert(B.nsec == b.nsec, "time::add() .nsec error");
};
};
@test fn compare() void = {
let a = now(clock::MONOTONIC);
sleep(1 * MILLISECOND);
let b = now(clock::MONOTONIC);
assert(compare(a, b) < 0);
assert(compare(b, a) > 0);
assert(compare(a, a) == 0);
};
@test fn mult() void = {
const cases = [
// instant a factor f instant b interpretations
( 0, 0, 0.000, 0, 0), // 0.000000000
( 9, 0, 0.000, 0, 0), // 0.000000000
( 0, 999999999, 0.000, 0, 0), // 0.000000000
( 9, 999999999, 0.000, 0, 0), // 0.000000000
( 1, 0, 1.000, 1, 0), // 1.000000000
( 9, 0, 1.000, 9, 0), // 9.000000000
( 1, 999999999, 1.000, 1, 999999999), // 1.999999999
( 9, 999999999, 1.000, 9, 999999999), // 9.999999999
( 1, 0, 0.001, 0, 1000000), // 0.001000000
( 1, 0, 0.010, 0, 10000000), // 0.010000000
( 1, 0, 0.100, 0, 100000000), // 0.100000000
(-1, 0, 0.001, -1, 999000000), // -0.001000000
(-1, 0, 0.010, -1, 990000000), // -0.010000000
(-1, 0, 0.100, -1, 900000000), // -0.100000000
(-1, 0, 1.000, -1, 0), // -1.000000000
( 0, 500000000, 0.001, 0, 500000), // 0.005000000
( 0, 500000000, 0.010, 0, 5000000), // 0.050000000
( 0, 500000000, 0.100, 0, 50000000), // 0.500000000
( 2, 0, 0.001, 0, 2000000), // 0.002000000
( 2, 0, 0.010, 0, 20000000), // 0.020000000
( 2, 0, 0.100, 0, 200000000), // 0.200000000
( 3, 141592653, 3.141, 9, 867742523), // 9.867742523073
( 2, 718281828, 2.718, 7, 388290007), // 7.388290008504 (rounds down?)
( 1, 414213562, 1.414, 1, 999697975), // 1.999697976668 (rounds down?)
( 3, 141592653, -3.141, -10, 132257477), // -9.867742523073
( 2, 718281828, -2.718, -8, 611709993), // -7.388290008504
( 1, 414213562, -1.414, -2, 302025), // -1.999697976668
(-4, 858407347, 3.141, -10, 132257477), // -9.867742523073
(-3, 281718172, 2.718, -8, 611709993), // -7.388290008504
(-2, 585786438, 1.414, -2, 302025), // -1.999697976668
(-4, 858407347, -3.141, 9, 867742523), // 9.867742523073
(-3, 281718172, -2.718, 7, 388290007), // 7.388290008504
(-2, 585786438, -1.414, 1, 999697975), // 1.999697976668
];
for (let i = 0z; i < len(cases); i += 1) {
const C = cases[i];
const a = instant { sec = C.0, nsec = C.1 };
const f = C.2;
const b = instant { sec = C.3, nsec = C.4 };
const B = mult(a, f);
assert(B.sec == b.sec, "time::mult() .sec error");
assert(B.nsec == b.nsec, "time::mult() .nsec error");
};
};
|