/src/ntp-dev/libntp/timespecops.c
Line | Count | Source |
1 | | /* |
2 | | * timespecops.c -- calculations on 'struct timespec' values |
3 | | * |
4 | | * Written by Juergen Perlinger (perlinger@ntp.org) for the NTP project. |
5 | | * The contents of 'html/copyright.html' apply. |
6 | | * |
7 | | */ |
8 | | |
9 | | #include "config.h" |
10 | | |
11 | | #include <sys/types.h> |
12 | | #include <stdio.h> |
13 | | #include <math.h> |
14 | | |
15 | | #include "ntp.h" |
16 | | #include "timetoa.h" |
17 | | #include "timespecops.h" |
18 | | |
19 | | |
20 | | /* nanoseconds per second */ |
21 | 196 | #define NANOSECONDS 1000000000 |
22 | | |
23 | | /* conversion between l_fp fractions and nanoseconds */ |
24 | | #ifdef HAVE_U_INT64 |
25 | | # define FTOTVN(tsf) \ |
26 | 0 | ((int32) \ |
27 | 0 | (((u_int64)(tsf) * NANOSECONDS + 0x80000000) >> 32)) |
28 | | # define TVNTOF(tvu) \ |
29 | 14 | ((u_int32) \ |
30 | 14 | ((((u_int64)(tvu) << 32) + NANOSECONDS / 2) / \ |
31 | 14 | NANOSECONDS)) |
32 | | #else |
33 | | # define NSECFRAC (FRAC / NANOSECONDS) |
34 | | # define FTOTVN(tsf) \ |
35 | | ((int32)((tsf) / NSECFRAC + 0.5)) |
36 | | # define TVNTOF(tvu) \ |
37 | | ((u_int32)((tvu) * NSECFRAC + 0.5)) |
38 | | #endif |
39 | | |
40 | | |
41 | | |
42 | | /* make sure nanoseconds are in nominal range */ |
43 | | struct timespec |
44 | | normalize_tspec( |
45 | | struct timespec x |
46 | | ) |
47 | 42 | { |
48 | 42 | #if SIZEOF_LONG > 4 |
49 | 42 | long z; |
50 | | |
51 | | /* |
52 | | * tv_nsec is of type 'long', and on a 64-bit machine using only |
53 | | * loops becomes prohibitive once the upper 32 bits get |
54 | | * involved. On the other hand, division by constant should be |
55 | | * fast enough; so we do a division of the nanoseconds in that |
56 | | * case. The floor adjustment step follows with the standard |
57 | | * normalisation loops. And labs() is intentionally not used |
58 | | * here: it has implementation-defined behaviour when applied |
59 | | * to LONG_MIN. |
60 | | */ |
61 | 42 | if (x.tv_nsec < -3l * NANOSECONDS || |
62 | 42 | x.tv_nsec > 3l * NANOSECONDS) { |
63 | 0 | z = x.tv_nsec / NANOSECONDS; |
64 | 0 | x.tv_nsec -= z * NANOSECONDS; |
65 | 0 | x.tv_sec += z; |
66 | 0 | } |
67 | 42 | #endif |
68 | | /* since 10**9 is close to 2**32, we don't divide but do a |
69 | | * normalisation in a loop; this takes 3 steps max, and should |
70 | | * outperform a division even if the mul-by-inverse trick is |
71 | | * employed. */ |
72 | 42 | if (x.tv_nsec < 0) |
73 | 0 | do { |
74 | 0 | x.tv_nsec += NANOSECONDS; |
75 | 0 | x.tv_sec--; |
76 | 0 | } while (x.tv_nsec < 0); |
77 | 42 | else if (x.tv_nsec >= NANOSECONDS) |
78 | 0 | do { |
79 | 0 | x.tv_nsec -= NANOSECONDS; |
80 | 0 | x.tv_sec++; |
81 | 0 | } while (x.tv_nsec >= NANOSECONDS); |
82 | | |
83 | 42 | return x; |
84 | 42 | } |
85 | | |
86 | | /* x = abs(a) */ |
87 | | struct timespec |
88 | | abs_tspec( |
89 | | struct timespec a |
90 | | ) |
91 | 0 | { |
92 | 0 | struct timespec c; |
93 | |
|
94 | 0 | c = normalize_tspec(a); |
95 | 0 | if (c.tv_sec < 0) { |
96 | 0 | if (c.tv_nsec != 0) { |
97 | 0 | c.tv_sec = -c.tv_sec - 1; |
98 | 0 | c.tv_nsec = NANOSECONDS - c.tv_nsec; |
99 | 0 | } else { |
100 | 0 | c.tv_sec = -c.tv_sec; |
101 | 0 | } |
102 | 0 | } |
103 | |
|
104 | 0 | return c; |
105 | 0 | } |
106 | | |
107 | | /* |
108 | | * compare previously-normalised a and b |
109 | | * return 1 / 0 / -1 if a < / == / > b |
110 | | */ |
111 | | int |
112 | | cmp_tspec( |
113 | | struct timespec a, |
114 | | struct timespec b |
115 | | ) |
116 | 28 | { |
117 | 28 | int r; |
118 | | |
119 | 28 | r = (a.tv_sec > b.tv_sec) - (a.tv_sec < b.tv_sec); |
120 | 28 | if (0 == r) |
121 | 26 | r = (a.tv_nsec > b.tv_nsec) - |
122 | 26 | (a.tv_nsec < b.tv_nsec); |
123 | | |
124 | 28 | return r; |
125 | 28 | } |
126 | | |
127 | | /* |
128 | | * test previously-normalised a |
129 | | * return 1 / 0 / -1 if a < / == / > 0 |
130 | | */ |
131 | | int |
132 | | test_tspec( |
133 | | struct timespec a |
134 | | ) |
135 | 0 | { |
136 | 0 | int r; |
137 | |
|
138 | 0 | r = (a.tv_sec > 0) - (a.tv_sec < 0); |
139 | 0 | if (r == 0) |
140 | 0 | r = (a.tv_nsec > 0); |
141 | | |
142 | 0 | return r; |
143 | 0 | } |
144 | | |
145 | | /* |
146 | | * convert to l_fp type, relative and absolute |
147 | | */ |
148 | | |
149 | | /* convert from timespec duration to l_fp duration */ |
150 | | l_fp |
151 | | tspec_intv_to_lfp( |
152 | | struct timespec x |
153 | | ) |
154 | 14 | { |
155 | 14 | struct timespec v; |
156 | 14 | l_fp y; |
157 | | |
158 | 14 | v = normalize_tspec(x); |
159 | 14 | y.l_uf = TVNTOF(v.tv_nsec); |
160 | 14 | y.l_i = (int32)v.tv_sec; |
161 | | |
162 | 14 | return y; |
163 | 14 | } |
164 | | |
165 | | /* convert from l_fp type, relative signed/unsigned and absolute */ |
166 | | struct timespec |
167 | | lfp_intv_to_tspec( |
168 | | l_fp x |
169 | | ) |
170 | 0 | { |
171 | 0 | struct timespec out; |
172 | 0 | l_fp absx; |
173 | 0 | int neg; |
174 | | |
175 | 0 | neg = L_ISNEG(&x); |
176 | 0 | absx = x; |
177 | 0 | if (neg) { |
178 | 0 | L_NEG(&absx); |
179 | 0 | } |
180 | 0 | out.tv_nsec = FTOTVN(absx.l_uf); |
181 | 0 | out.tv_sec = absx.l_i; |
182 | 0 | if (neg) { |
183 | 0 | out.tv_sec = -out.tv_sec; |
184 | 0 | out.tv_nsec = -out.tv_nsec; |
185 | 0 | out = normalize_tspec(out); |
186 | 0 | } |
187 | |
|
188 | 0 | return out; |
189 | 0 | } |
190 | | |
191 | | struct timespec |
192 | | lfp_uintv_to_tspec( |
193 | | l_fp x |
194 | | ) |
195 | 0 | { |
196 | 0 | struct timespec out; |
197 | | |
198 | 0 | out.tv_nsec = FTOTVN(x.l_uf); |
199 | 0 | out.tv_sec = x.l_ui; |
200 | |
|
201 | 0 | return out; |
202 | 0 | } |
203 | | |
204 | | /* |
205 | | * absolute (timestamp) conversion. Input is time in NTP epoch, output |
206 | | * is in UN*X epoch. The NTP time stamp will be expanded around the |
207 | | * pivot time *p or the current time, if p is NULL. |
208 | | */ |
209 | | struct timespec |
210 | | lfp_stamp_to_tspec( |
211 | | l_fp x, |
212 | | const time_t * p |
213 | | ) |
214 | 0 | { |
215 | 0 | struct timespec out; |
216 | 0 | vint64 sec; |
217 | |
|
218 | 0 | sec = ntpcal_ntp_to_time(x.l_ui, p); |
219 | 0 | out.tv_nsec = FTOTVN(x.l_uf); |
220 | | |
221 | | /* copying a vint64 to a time_t needs some care... */ |
222 | | #if SIZEOF_TIME_T <= 4 |
223 | | out.tv_sec = (time_t)sec.d_s.lo; |
224 | | #elif defined(HAVE_INT64) |
225 | | out.tv_sec = (time_t)sec.q_s; |
226 | | #else |
227 | | out.tv_sec = ((time_t)sec.d_s.hi << 32) | sec.d_s.lo; |
228 | | #endif |
229 | | |
230 | 0 | return out; |
231 | 0 | } |
232 | | |
233 | | /* -*-EOF-*- */ |