2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
10 #include <afs/param.h>
11 #include <afsconfig.h>
16 #include <sys/types.h>
17 #include <sys/param.h>
20 #include <sys/socket.h>
21 #include <netinet/in.h>
22 #include <arpa/inet.h>
25 #include <sys/syslog.h>
37 * The nice thing here is that the quantity is NEVER signed.
44 #ifdef GENERIC_UNS_BUG
47 i = ntohl(t->fraction);
48 a = (afs_int32)((i >> 1) & 0x7fffffff);
52 a = a / (4.294967296e9); /* shift dec point over by 32 bits */
53 i = ntohl(t->int_part);
54 b = (afs_int32)((i >> 1) & 0x7fffffff);
58 #else /* GENERIC_UNS_BUG */
59 a = (afs_uint32) ntohl(t->fraction);
60 #ifdef VAX_COMPILER_FLT_BUG
61 if (a < 0.0) a += 4.294967296e9;
63 a = a / (4.294967296e9);/* shift dec point over by 32 bits */
64 b = (afs_uint32) ntohl(t->int_part);
65 #ifdef VAX_COMPILER_FLT_BUG
66 if (b < 0.0) b += 4.294967296e9;
68 #endif /* GENERIC_UNS_BUG */
73 * Here we have to worry about the high order bit being signed
83 if (ntohl(t->int_part) & 0x80000000) {
84 a = ntohl(~t->fraction);
85 #ifdef VAX_COMPILER_FLT_BUG
86 if (a < 0.0) a += 4.294967296e9;
88 a = a / (4.294967296e9);
89 b = ntohl(~t->int_part);
90 #ifdef VAX_COMPILER_FLT_BUG
91 if (b < 0.0) b += 4.294967296e9;
96 a = ntohl(t->fraction);
97 #ifdef VAX_COMPILER_FLT_BUG
98 if (a < 0.0) a += 4.294967296e9;
100 a = a / (4.294967296e9);
101 b = ntohl(t->int_part);
102 #ifdef VAX_COMPILER_FLT_BUG
103 if (b < 0.0) b += 4.294967296e9;
112 * Here we have to worry about the high order bit being signed
120 if (ntohs(t->int_part) & 0x8000) {
121 a = ntohs(~t->fraction & 0xFFFF);
122 a = a / 65536.0; /* shift dec point over by 16 bits */
123 a += ntohs(~t->int_part & 0xFFFF);
126 a = ntohs(t->fraction);
127 a = a / 65536.0; /* shift dec point over by 16 bits */
128 a += ntohs(t->int_part);
134 double_to_l_fixed(t, value)
140 if (value >= (double) 0.0) {
142 temp = value - t->int_part;
143 temp *= 4.294967296e9;
145 t->int_part = htonl(t->int_part);
146 t->fraction = htonl(t->fraction);
150 temp = value - t->int_part;
151 temp *= 4.294967296e9;
153 t->int_part = htonl(~t->int_part);
154 t->fraction = htonl(~t->fraction);
159 double_to_s_fixed(t, value)
165 if (value >= (double) 0.0) {
167 temp = value - t->int_part;
170 t->int_part = htons(t->int_part);
171 t->fraction = htons(t->fraction);
175 temp = value - t->int_part;
178 t->int_part = htons(~t->int_part);
179 t->fraction = htons(~t->fraction);
183 in the sun, trying to assign a float between 2^31 and 2^32
184 results in the value 2^31. Neither 4.2bsd nor VMS have this
185 problem. Reported it to Bob O'Brien of SMI
187 #ifndef AFS_SUN58_ENV
190 struct l_fixedpt *stampp;
196 stampp->int_part = ntohl(JAN_1970 + tvp->tv_sec);
197 dd = (float) tvp->tv_usec / 1000000.0;
198 tt = dd * 2147483648.0;
199 stampp->fraction = ntohl((tt << 1));
203 struct l_fixedpt *stampp;
206 stampp->int_part = ntohl((afs_uint32) (JAN_1970 + tvp->tv_sec));
207 stampp->fraction = ntohl((afs_uint32) ((float) tvp->tv_usec * 4294.967295));
210 #endif /* AFS_SUN58_ENV */
213 * ntoa is similar to inet_ntoa, but cycles through a set of 8 buffers
214 * so it can be invoked several times in a function parameter list.
219 struct in_addr in_addr;
222 static char bufs[8][20];
223 unsigned char *p = (unsigned char *) &in_addr.s_addr;
225 i = (i + 1) % (sizeof bufs / sizeof bufs[0]);
226 sprintf (bufs[i], "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
230 /* calculate effective precision, but repeated calls to gettimeofday */
232 int MeasurePrecision (intervalP)
235 #if defined(AFS_SUN5_ENV)
236 #define MAXTIMEDIFFS 100
238 #define MAXTIMEDIFFS 10
241 int diff[MAXTIMEDIFFS];
248 gettimeofday (&tv0, 0);
250 while (nDiff < MAXTIMEDIFFS) {
251 struct timeval tv, ntv;
252 int counting = 2; /* a counting kernel */
253 gettimeofday (&tv, 0);
255 gettimeofday (&ntv, 0);
258 * Bail if we are taking too long -- 30 seconds is arbitrary,
259 * but better than the previous approach, which was to bail
260 * after an arbitrary count of calls to gettimeofday(). This
261 * caused problems because the machines kept getting faster
262 * and the count kept getting exceeded.
264 if (ntv.tv_sec - tv0.tv_sec > 30) return 0;
266 interval = (ntv.tv_sec - tv.tv_sec)*1000000 +
267 ntv.tv_usec - tv.tv_usec;
268 if (interval <= counting) counting = interval+2;
269 } while (interval <= counting); /* RT & sun4/280 kernels count */
270 if (interval < 0) return 0; /* shouldn't happen but who knows... */
271 if (interval > 0) diff[nDiff++] = interval;
274 /* find average interval */
276 for (i=0; i<MAXTIMEDIFFS; i++)
278 interval = (interval + (MAXTIMEDIFFS/2)) / MAXTIMEDIFFS; /* round */
279 if (interval == 0) return 0; /* some problem... */
280 if (intervalP) *intervalP = interval;
282 /* calculate binary exponent of interval in seconds */
285 while (q > interval) {