aboutsummaryrefslogtreecommitdiff
path: root/chad/experimental/timespec.c
blob: dbcca824b0ff46293a8fee7246fd3cbb33c79f4a (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
#include <chad/experimental/timespec.h>

/* --- conversion --- */

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

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

double timespec_to_double(timespec_t ts)
{ return (double) ts.tv_sec + ((double) ts.tv_nsec / TIMESPEC_HZ); }

float timespec_to_float(timespec_t ts)
{ return (float) ts.tv_sec + ((float) ts.tv_nsec / TIMESPEC_HZ); }

/* --- from --- */

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

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

void double_from_timespec(timespec_t ts, double * r)
{ *r = (double) ts.tv_sec + ((double) ts.tv_nsec / TIMESPEC_HZ); }

void float_from_timespec(timespec_t ts, float * r)
{ *r = (float) ts.tv_sec + ((float) ts.tv_nsec / TIMESPEC_HZ); }

/* --- math --- */

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;
}

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;
}

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
      );
}

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
      );
}

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
      );
}