aboutsummaryrefslogtreecommitdiff
path: root/chad/timespec.h
blob: 6fbf5aa61c3f6e5efd6bdbebe97261a849c56011 (plain) (blame)
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
#ifndef TIMESPEC_H
#define TIMESPEC_H
/* ripped partly from glibc */
#include <time.h>
#include "macros.h"
#include "terry.h"

/* -- pontentially over designed and undertested. */
/* -- consider this "LIKELY TO BE REVISED OR REMOVED!" */
/* -- ESPECIALLY the generics and the from group */

long TIMESPEC_HZ = 1000000000L;

typedef struct timespec timespec_t;

static const timespec_t one_second = {1, 0}, zero_seconds = {0, 0};

always_inline f64 timespec_to_f64(timespec_t ts)
{ return (f64) ts.tv_sec + ((f64) ts.tv_nsec / TIMESPEC_HZ); }

always_inline f32 timespec_to_f32(timespec_t ts)
{ return (f32) ts.tv_sec + ((f32) ts.tv_nsec / TIMESPEC_HZ); }

#if defined(TERRY_SMALL_FLOAT_IMPRECISE) && !defined(NO_ALIASES)
always_inline double timespec_to_double(timespec_t ts) alias("timespec_to_f64");
always_inline float timespec_to_float(timespec_t ts) alias("timespec_to_f32");
#else
always_inline double timespec_to_double(timespec_t ts)
{ return (double) ts.tv_sec + ((double) ts.tv_nsec / TIMESPEC_HZ); }
always_inline float timespec_to_float(timespec_t ts)
{ return (float) ts.tv_sec + ((float) ts.tv_nsec / TIMESPEC_HZ); }
#endif


#ifdef TERRY_SMALL_FLOAT_IMPRECISE
#define _from_timespec(ts,to) _Generic((to), double *: double_from_timespec, float *: float_from_timespec)(ts,to)
#else
#define _from_timespec(ts,to) _Generic((to), f64 *: f64_from_timespec, f32 *: f32_from_timespec, double *: double_from_timespec, float *: float_from_timespec)(ts,to)
#endif

always_inline void from_timespec(timespec_t ts, )

always_inline void f64_from_timespec(timespec_t ts, f64 * r)
{ *r = (f64) ts.tv_sec + ((f64) ts.tv_nsec / TIMESPEC_HZ); }

always_inline void f32_from_timespec(timespec_t ts, f32 * r)
{ *r = (f32) ts.tv_sec + ((f32) ts.tv_nsec / TIMESPEC_HZ); }

#if defined(TERRY_SMALL_FLOAT_IMPRECISE) && !defined(NO_ALIASES)
always_inline void double_from_timespec(timespec_t ts, double * r) alias("timespec_to_f64");
always_inline void float_from_timespec(timespec_t ts, float * r) alias("timespec_to_f32");
#else
always_inline void double_from_timespec(timespec_t ts, double * r)
{ *r = (double) ts.tv_sec + ((double) ts.tv_nsec / TIMESPEC_HZ); }
always_inline void float_from_timespec(timespec_t ts, float * r)
{ *r = (float) ts.tv_sec + ((float) ts.tv_nsec / TIMESPEC_HZ); }
#endif

always_inline timespec_t timespec_add(timespec_t a, timespec_t b) {
  a.tv_sec += b.tv_sec;
  a.tv_nsec += b.tv_nsec;
  if (a.tv_nsec >= TIMESPEC_HZ) {
    a.tv_sec++;
    a.tv_nsec -= TIMESPEC_HZ;
  }
  return a;
}

always_inline timespec_t timespec_sub(timespec_t a, timespec_t b) {
  a.tv_sec -= b.tv_sec;
  a.tv_nsec -= b.tv_nsec;
  if (a.tv_nsec < 0) {
    a.tv_sec--;
    a.tv_nsec += TIMESPEC_HZ;
  }
  return a;
}

always_inline int timespec_cmp(timespec_t a, timespec_t b) {
  return a.tv_sec > b.tv_sec ?
    (1) :
    a.tv_sec < b.tv_sec ?
    (-1) :
    (
      a.tv_nsec > b.tv_nsec ?
      (1) :
      a.tv_nsec < b.tv_nsec ?
      (-1) : 0
      );
}

always_inline timespec_t timespec_max(timespec_t a, timespec_t b) {
  return a.tv_sec > b.tv_sec ?
    a :
    a.tv_sec < b.tv_sec ?
    b :
    (
      a.tv_nsec > b.tv_nsec ?
      a : b
      );
}

always_inline timespec_t timespec_min(timespec_t a, timespec_t b) {
  return a.tv_sec < b.tv_sec ?
    a :
    a.tv_sec > b.tv_sec ?
    b :
    (
      a.tv_nsec < b.tv_nsec ?
      a : b
      );
}

#endif /* TIMESPEC_H */