ifpps: Remove unused 'forks' member from struct ifstat
[netsniff-ng.git] / taia.h
blob7440418acf4a55355b8502feedf1ef8f481124e7
1 #ifndef TAIA_H
2 #define TAIA_H
4 #include <stdint.h>
5 #include <stdbool.h>
6 #include <stdio.h>
7 #include <sys/time.h>
9 #include "rnd.h"
11 struct tai {
12 uint64_t x;
15 struct taia {
16 struct tai sec;
17 uint32_t nano;
18 uint32_t atto;
21 static inline void tai_pack(unsigned char *s, struct tai *t)
23 uint64_t x;
25 x = t->x;
27 s[7] = x & 255; x >>= 8;
28 s[6] = x & 255; x >>= 8;
29 s[5] = x & 255; x >>= 8;
30 s[4] = x & 255; x >>= 8;
31 s[3] = x & 255; x >>= 8;
32 s[2] = x & 255; x >>= 8;
33 s[1] = x & 255; x >>= 8;
34 s[0] = x;
37 static inline void tai_unpack(unsigned char *s, struct tai *t)
39 uint64_t x;
41 x = (unsigned char) s[0];
42 x <<= 8; x += (unsigned char) s[1];
43 x <<= 8; x += (unsigned char) s[2];
44 x <<= 8; x += (unsigned char) s[3];
45 x <<= 8; x += (unsigned char) s[4];
46 x <<= 8; x += (unsigned char) s[5];
47 x <<= 8; x += (unsigned char) s[6];
48 x <<= 8; x += (unsigned char) s[7];
50 t->x = x;
53 static inline void taia_pack(unsigned char *s, struct taia *t)
55 unsigned long x;
57 tai_pack(s, &t->sec);
59 s += 8;
61 x = t->atto;
63 s[7] = x & 255; x >>= 8;
64 s[6] = x & 255; x >>= 8;
65 s[5] = x & 255; x >>= 8;
66 s[4] = x;
68 x = t->nano;
70 s[3] = x & 255; x >>= 8;
71 s[2] = x & 255; x >>= 8;
72 s[1] = x & 255; x >>= 8;
73 s[0] = x;
76 static inline void taia_unpack(unsigned char *s, struct taia *t)
78 unsigned long x;
80 tai_unpack(s, &t->sec);
82 s += 8;
84 x = (unsigned char) s[4];
85 x <<= 8; x += (unsigned char) s[5];
86 x <<= 8; x += (unsigned char) s[6];
87 x <<= 8; x += (unsigned char) s[7];
89 t->atto = x;
91 x = (unsigned char) s[0];
92 x <<= 8; x += (unsigned char) s[1];
93 x <<= 8; x += (unsigned char) s[2];
94 x <<= 8; x += (unsigned char) s[3];
96 t->nano = x;
99 #define tai_unix(t, u) ((void) ((t)->x = 4611686018427387914ULL + (uint64_t) (u)))
101 static inline void taia_now(struct taia *t)
103 struct timeval now;
105 gettimeofday(&now, NULL);
107 tai_unix(&t->sec, now.tv_sec);
108 t->nano = 1000 * now.tv_usec + 500;
109 t->atto = secrand();
112 static inline void taia_sub(struct taia *res, const struct taia *u,
113 const struct taia *v)
115 unsigned long unano = u->nano;
116 unsigned long uatto = u->atto;
118 res->sec.x = u->sec.x - v->sec.x;
119 res->nano = unano - v->nano;
120 res->atto = uatto - v->atto;
122 if (res->atto > uatto) {
123 res->atto += 1000000000UL;
124 --res->nano;
127 if (res->nano > unano) {
128 res->nano += 1000000000UL;
129 --res->sec.x;
133 static inline void taia_add(struct taia *res, const struct taia *u,
134 const struct taia *v)
136 res->sec.x = u->sec.x + v->sec.x;
137 res->nano = u->nano + v->nano;
138 res->atto = u->atto + v->atto;
140 if (res->atto > 999999999UL) {
141 res->atto -= 1000000000UL;
142 ++res->nano;
145 if (res->nano > 999999999UL) {
146 res->nano -= 1000000000UL;
147 ++res->sec.x;
151 static inline int taia_less(const struct taia *t, const struct taia *u)
153 if (t->sec.x < u->sec.x)
154 return 1;
155 if (t->sec.x > u->sec.x)
156 return 0;
157 if (t->nano < u->nano)
158 return 1;
159 if (t->nano > u->nano)
160 return 0;
161 return t->atto < u->atto;
164 extern bool taia_looks_good(struct taia *arr_taia, struct taia *pkt_taia);
166 #endif /* TAIA_H */