curve: move taia related functions into own header
[netsniff-ng.git] / taia.h
blob227321d33f49e69aee9f2d392415ef2c0c6b621d
1 #ifndef TAIA_H
2 #define TAIA_H
4 #include <stdint.h>
6 struct tai {
7 uint64_t x;
8 };
10 struct taia {
11 struct tai sec;
12 uint32_t nano;
13 uint32_t atto;
16 static inline void tai_pack(unsigned char *s, struct tai *t)
18 uint64_t x;
20 x = t->x;
22 s[7] = x & 255; x >>= 8;
23 s[6] = x & 255; x >>= 8;
24 s[5] = x & 255; x >>= 8;
25 s[4] = x & 255; x >>= 8;
26 s[3] = x & 255; x >>= 8;
27 s[2] = x & 255; x >>= 8;
28 s[1] = x & 255; x >>= 8;
29 s[0] = x;
32 static inline void tai_unpack(unsigned char *s, struct tai *t)
34 uint64_t x;
36 x = (unsigned char) s[0];
37 x <<= 8; x += (unsigned char) s[1];
38 x <<= 8; x += (unsigned char) s[2];
39 x <<= 8; x += (unsigned char) s[3];
40 x <<= 8; x += (unsigned char) s[4];
41 x <<= 8; x += (unsigned char) s[5];
42 x <<= 8; x += (unsigned char) s[6];
43 x <<= 8; x += (unsigned char) s[7];
45 t->x = x;
48 static inline void taia_pack(unsigned char *s, struct taia *t)
50 unsigned long x;
52 tai_pack(s, &t->sec);
54 s += 8;
56 x = t->atto;
58 s[7] = x & 255; x >>= 8;
59 s[6] = x & 255; x >>= 8;
60 s[5] = x & 255; x >>= 8;
61 s[4] = x;
63 x = t->nano;
65 s[3] = x & 255; x >>= 8;
66 s[2] = x & 255; x >>= 8;
67 s[1] = x & 255; x >>= 8;
68 s[0] = x;
71 static inline void taia_unpack(unsigned char *s, struct taia *t)
73 unsigned long x;
75 tai_unpack(s, &t->sec);
77 s += 8;
79 x = (unsigned char) s[4];
80 x <<= 8; x += (unsigned char) s[5];
81 x <<= 8; x += (unsigned char) s[6];
82 x <<= 8; x += (unsigned char) s[7];
84 t->atto = x;
86 x = (unsigned char) s[0];
87 x <<= 8; x += (unsigned char) s[1];
88 x <<= 8; x += (unsigned char) s[2];
89 x <<= 8; x += (unsigned char) s[3];
91 t->nano = x;
94 #define tai_unix(t, u) ((void) ((t)->x = 4611686018427387914ULL + (uint64_t) (u)))
96 static inline void taia_now(struct taia *t)
98 struct timeval now;
100 gettimeofday(&now, NULL);
102 tai_unix(&t->sec, now.tv_sec);
103 t->nano = 1000 * now.tv_usec + 500;
104 t->atto = secrand();
107 static inline void taia_sub(struct taia *res, const struct taia *u,
108 const struct taia *v)
110 unsigned long unano = u->nano;
111 unsigned long uatto = u->atto;
113 res->sec.x = u->sec.x - v->sec.x;
114 res->nano = unano - v->nano;
115 res->atto = uatto - v->atto;
117 if (res->atto > uatto) {
118 res->atto += 1000000000UL;
119 --res->nano;
122 if (res->nano > unano) {
123 res->nano += 1000000000UL;
124 --res->sec.x;
128 static inline void taia_add(struct taia *res, const struct taia *u,
129 const struct taia *v)
131 res->sec.x = u->sec.x + v->sec.x;
132 res->nano = u->nano + v->nano;
133 res->atto = u->atto + v->atto;
135 if (res->atto > 999999999UL) {
136 res->atto -= 1000000000UL;
137 ++res->nano;
140 if (res->nano > 999999999UL) {
141 res->nano -= 1000000000UL;
142 ++res->sec.x;
146 static inline int taia_less(const struct taia *t, const struct taia *u)
148 if (t->sec.x < u->sec.x)
149 return 1;
150 if (t->sec.x > u->sec.x)
151 return 0;
152 if (t->nano < u->nano)
153 return 1;
154 if (t->nano > u->nano)
155 return 0;
156 return t->atto < u->atto;
159 #endif /* TAIA_H */