Loading...
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 | /*
* Copyright (c) 2019 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <ztest.h>
#include <zephyr/types.h>
#include <sys/time_units.h>
#include <random/rand32.h>
#define NUM_RANDOM 100
enum units { UNIT_ticks, UNIT_cyc, UNIT_ms, UNIT_us, UNIT_ns };
enum round { ROUND_floor, ROUND_ceil, ROUND_near };
static const char *const round_s[] = {
[ROUND_floor] = "floor",
[ROUND_ceil] = "ceil",
[ROUND_near] = "near",
};
struct test_rec {
enum units src;
enum units dst;
int precision; /* 32 or 64 */
enum round round;
void *func;
};
#define TESTREC(src, dst, round, prec) { \
UNIT_##src, UNIT_##dst, prec, ROUND_##round, \
(void *)k_##src##_to_##dst##_##round##prec \
} \
static struct test_rec tests[] = {
TESTREC(ms, cyc, floor, 32),
TESTREC(ms, cyc, floor, 64),
TESTREC(ms, cyc, near, 32),
TESTREC(ms, cyc, near, 64),
TESTREC(ms, cyc, ceil, 32),
TESTREC(ms, cyc, ceil, 64),
TESTREC(ms, ticks, floor, 32),
TESTREC(ms, ticks, floor, 64),
TESTREC(ms, ticks, near, 32),
TESTREC(ms, ticks, near, 64),
TESTREC(ms, ticks, ceil, 32),
TESTREC(ms, ticks, ceil, 64),
TESTREC(us, cyc, floor, 32),
TESTREC(us, cyc, floor, 64),
TESTREC(us, cyc, near, 32),
TESTREC(us, cyc, near, 64),
TESTREC(us, cyc, ceil, 32),
TESTREC(us, cyc, ceil, 64),
TESTREC(us, ticks, floor, 32),
TESTREC(us, ticks, floor, 64),
TESTREC(us, ticks, near, 32),
TESTREC(us, ticks, near, 64),
TESTREC(us, ticks, ceil, 32),
TESTREC(us, ticks, ceil, 64),
TESTREC(cyc, ms, floor, 32),
TESTREC(cyc, ms, floor, 64),
TESTREC(cyc, ms, near, 32),
TESTREC(cyc, ms, near, 64),
TESTREC(cyc, ms, ceil, 32),
TESTREC(cyc, ms, ceil, 64),
TESTREC(cyc, us, floor, 32),
TESTREC(cyc, us, floor, 64),
TESTREC(cyc, us, near, 32),
TESTREC(cyc, us, near, 64),
TESTREC(cyc, us, ceil, 32),
TESTREC(cyc, us, ceil, 64),
TESTREC(cyc, ticks, floor, 32),
TESTREC(cyc, ticks, floor, 64),
TESTREC(cyc, ticks, near, 32),
TESTREC(cyc, ticks, near, 64),
TESTREC(cyc, ticks, ceil, 32),
TESTREC(cyc, ticks, ceil, 64),
TESTREC(ticks, ms, floor, 32),
TESTREC(ticks, ms, floor, 64),
TESTREC(ticks, ms, near, 32),
TESTREC(ticks, ms, near, 64),
TESTREC(ticks, ms, ceil, 32),
TESTREC(ticks, ms, ceil, 64),
TESTREC(ticks, us, floor, 32),
TESTREC(ticks, us, floor, 64),
TESTREC(ticks, us, near, 32),
TESTREC(ticks, us, near, 64),
TESTREC(ticks, us, ceil, 32),
TESTREC(ticks, us, ceil, 64),
TESTREC(ticks, cyc, floor, 32),
TESTREC(ticks, cyc, floor, 64),
TESTREC(ticks, cyc, near, 32),
TESTREC(ticks, cyc, near, 64),
TESTREC(ticks, cyc, ceil, 32),
TESTREC(ticks, cyc, ceil, 64),
TESTREC(ns, cyc, floor, 32),
TESTREC(ns, cyc, floor, 64),
TESTREC(ns, cyc, near, 32),
TESTREC(ns, cyc, near, 64),
TESTREC(ns, cyc, ceil, 32),
TESTREC(ns, cyc, ceil, 64),
TESTREC(ns, ticks, floor, 32),
TESTREC(ns, ticks, floor, 64),
TESTREC(ns, ticks, near, 32),
TESTREC(ns, ticks, near, 64),
TESTREC(ns, ticks, ceil, 32),
TESTREC(ns, ticks, ceil, 64),
TESTREC(cyc, ns, floor, 32),
TESTREC(cyc, ns, floor, 64),
TESTREC(cyc, ns, near, 32),
TESTREC(cyc, ns, near, 64),
TESTREC(cyc, ns, ceil, 32),
TESTREC(cyc, ns, ceil, 64),
TESTREC(ticks, ns, floor, 32),
TESTREC(ticks, ns, floor, 64),
TESTREC(ticks, ns, near, 32),
TESTREC(ticks, ns, near, 64),
TESTREC(ticks, ns, ceil, 32),
TESTREC(ticks, ns, ceil, 64),
};
uint32_t get_hz(enum units u)
{
if (u == UNIT_ticks) {
return CONFIG_SYS_CLOCK_TICKS_PER_SEC;
} else if (u == UNIT_cyc) {
return sys_clock_hw_cycles_per_sec();
} else if (u == UNIT_ms) {
return 1000;
} else if (u == UNIT_us) {
return 1000000;
} else if (u == UNIT_ns) {
return 1000000000;
}
__ASSERT(0, "");
return 0;
}
void test_conversion(struct test_rec *t, uint64_t val)
{
uint32_t from_hz = get_hz(t->src), to_hz = get_hz(t->dst);
uint64_t result;
if (t->precision == 32) {
uint32_t (*convert)(uint32_t) = (uint32_t (*)(uint32_t)) t->func;
result = convert((uint32_t) val);
/* If the input value legitimately overflows, then
* there is nothing to test
*/
if ((val * to_hz) >= ((((uint64_t)from_hz) << 32))) {
return;
}
} else {
uint64_t (*convert)(uint64_t) = (uint64_t (*)(uint64_t)) t->func;
result = convert(val);
}
/* We expect the ideal result to be equal to "val * to_hz /
* from_hz", but that division is the source of precision
* issues. So reexpress our equation as:
*
* val * to_hz ==? result * from_hz
* 0 ==? val * to_hz - result * from_hz
*
* The difference is allowed to be in the range [0:from_hz) if
* we are rounding down, from (-from_hz:0] if we are rounding
* up, or [-from_hz/2:from_hz/2] if we are rounding to the
* nearest.
*/
int64_t diff = (int64_t)(val * to_hz - result * from_hz);
int64_t maxdiff, mindiff;
if (t->round == ROUND_floor) {
maxdiff = from_hz - 1;
mindiff = 0;
} else if (t->round == ROUND_ceil) {
maxdiff = 0;
mindiff = -(int64_t)(from_hz-1);
} else {
maxdiff = from_hz/2;
mindiff = -(int64_t)(from_hz/2);
}
zassert_true(diff <= maxdiff && diff >= mindiff,
"Convert %llu (%llx) from %u Hz to %u Hz %u-bit %s\n"
"result %llu (%llx) diff %lld (%llx) should be in [%lld:%lld]",
val, val, from_hz, to_hz, t->precision, round_s[t->round],
result, result, diff, diff, mindiff, maxdiff);
}
void test_time_conversions(void)
{
for (int i = 0; i < ARRAY_SIZE(tests); i++) {
test_conversion(&tests[i], 0);
test_conversion(&tests[i], 1);
test_conversion(&tests[i], 0x7fffffff);
test_conversion(&tests[i], 0x80000000);
test_conversion(&tests[i], 0xfffffff0);
if (tests[i].precision == 64) {
test_conversion(&tests[i], 0xffffffff);
test_conversion(&tests[i], 0x100000000ULL);
}
for (int j = 0; j < NUM_RANDOM; j++) {
test_conversion(&tests[i], sys_rand32_get());
}
}
}
|