summaryrefslogtreecommitdiff
path: root/src/tc-time.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/tc-time.c')
-rw-r--r--src/tc-time.c48
1 files changed, 29 insertions, 19 deletions
diff --git a/src/tc-time.c b/src/tc-time.c
index e842e77..5097cde 100644
--- a/src/tc-time.c
+++ b/src/tc-time.c
@@ -1,10 +1,5 @@
-/*
- * Copyright Jan Engelhardt, 2012
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the WTF Public License version 2 or
- * (at your option) any later version.
- */
+// SPDX-License-Identifier: MIT
+#include <assert.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
@@ -26,7 +21,7 @@ static const int NANOSECOND = 1000000000;
static const long long NANOSECOND_LL = 1000000000;
static const unsigned int clock_id = CLOCK_THREAD_CPUTIME_ID;
static const unsigned int step = 1000;
-static const unsigned int step_mul = 10000000;
+static const long step_mul = 10000000;
static const struct timespec pairs[] = {
{-1, 700000000}, {-1, 400000000}, {-1, 0},
@@ -36,10 +31,12 @@ static const struct timespec pairs[] = {
};
/*
- * Variant that uses full 64 bit division and is thus slower on
- * a handful of hardware.
+ * The playing fields it not level at all!
+ * HX_timespec_add_DIVQ benefits from inlining and near jumps,
+ * while HX_timespec_add has to go via PLT, and also has the PIC tax.
+ * It is actually worse by 7% on i7-8250U/-m64.
*/
-static struct timespec *HX_timespec_add_FDIV(struct timespec *r,
+static struct timespec *HX_timespec_add_DIVQ(struct timespec *r,
const struct timespec *a, const struct timespec *b)
{
long long p, q;
@@ -105,6 +102,20 @@ HX_timespec_mulf_S(struct timespec *r, const struct timespec *a, double f)
return r;
}
+static void test_basic(void)
+{
+ struct timeval a = {1, 769298}, b = {2, 430520}, c;
+ struct timespec p = {1, 769298000}, q = {2, 430520000}, r;
+ HX_timeval_sub(&c, &b, &a);
+ HX_timespec_sub(&r, &q, &p);
+ printf(HX_TIMEVAL_FMT "\n", HX_TIMEVAL_EXP(&c));
+ printf(HX_TIMESPEC_FMT "\n", HX_TIMESPEC_EXP(&r));
+ assert(c.tv_sec == 0);
+ assert(c.tv_usec == 661222);
+ assert(r.tv_sec == 0);
+ assert(r.tv_nsec == 661222000);
+}
+
static void test_same(void)
{
static const struct timespec zero = {0, 0};
@@ -205,9 +216,9 @@ static void test_add(void)
for (a = pairs; a < pairs + ARRAY_SIZE(pairs); ++a) {
for (b = pairs; b < pairs + ARRAY_SIZE(pairs); ++b) {
HX_timespec_add(&r, a, b);
- print_op2(&r, a, "+N", b);
- HX_timespec_add_FDIV(&s, a, b);
- print_op2(&r, a, "+F", b);
+ print_op2(&r, a, "+L", b);
+ HX_timespec_add_DIVQ(&s, a, b);
+ print_op2(&r, a, "+Q", b);
if (r.tv_sec != s.tv_sec || r.tv_nsec != s.tv_nsec)
abort();
HX_timespec_sub(&r, a, b);
@@ -288,7 +299,7 @@ static void test_adds(void)
{
printf("# Test addition speed\n");
test_adds_1("normal: ", HX_timespec_add);
- test_adds_1("fulldiv: ", HX_timespec_add_FDIV);
+ test_adds_1("div64: ", HX_timespec_add_DIVQ);
printf("\n");
}
@@ -335,11 +346,10 @@ static void test_mul(void)
static void test_muls_1i(const char *text, mul_func_t fn)
{
struct timespec r, s, start, delta;
- unsigned int i;
printf("%s", text);
clock_gettime(clock_id, &start);
- for (i = 0; i < step_mul; ++i) {
+ for (time_t i = 0; i < step_mul; ++i) {
r.tv_sec = -i;
r.tv_nsec = -i / 4;
(*fn)(&s, &r, 7);
@@ -352,11 +362,10 @@ static void test_muls_1i(const char *text, mul_func_t fn)
static void test_muls_1f(const char *text, mulf_func_t fn)
{
struct timespec r, s, start, delta;
- unsigned int i;
printf("%s", text);
clock_gettime(clock_id, &start);
- for (i = 0; i < step_mul; ++i) {
+ for (time_t i = 0; i < step_mul; ++i) {
r.tv_sec = -i;
r.tv_nsec = -i / 4;
(*fn)(&s, &r, 7);
@@ -382,6 +391,7 @@ int main(void)
if (HX_init() <= 0)
abort();
+ test_basic();
test_same();
test_neg();
test_add();