1 /** $MirOS: src/usr.sbin/ntpd/util.c,v 1.6 2011/11/21 20:49:38 tg Exp $ */
2 /* $OpenBSD: util.c,v 1.10 2004/12/08 15:47:38 mickey Exp $ */
3
4 /*
5 * Copyright (c) 2004, 2007, 2011
6 * Thorsten "mirabilos" Glaser <tg@mirbsd.org>
7 * Copyright (c) 2004 Alexander Guy <alexander.guy@andern.org>
8 *
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
18 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
19 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 */
21
22 #include <sys/types.h>
23 #include <limits.h>
24
25 #include "ntpd.h"
26
27 __RCSID("$MirOS: src/usr.sbin/ntpd/util.c,v 1.6 2011/11/21 20:49:38 tg Exp $");
28
29 void
d_to_tv(double d,struct timeval * tv)30 d_to_tv(double d, struct timeval *tv)
31 {
32 tv->tv_sec = (long)d;
33 tv->tv_usec = (d - tv->tv_sec) * 1000000;
34 }
35
36 double
lfp_to_d(struct l_fixedpt lfp)37 lfp_to_d(struct l_fixedpt lfp)
38 {
39 double ret;
40
41 lfp.int_partl = ntohl(lfp.int_partl);
42 lfp.fractionl = ntohl(lfp.fractionl);
43
44 ret = (double)(lfp.int_partl) + ((double)lfp.fractionl / UINT_MAX);
45
46 return (ret);
47 }
48
49 struct l_fixedpt
d_to_lfp(double d)50 d_to_lfp(double d)
51 {
52 struct l_fixedpt lfp;
53
54 lfp.int_partl = htonl((u_int32_t)d);
55 lfp.fractionl = htonl((u_int32_t)((d - (u_int32_t)d) * UINT_MAX));
56
57 return (lfp);
58 }
59
60 double
sfp_to_d(struct s_fixedpt sfp)61 sfp_to_d(struct s_fixedpt sfp)
62 {
63 double ret;
64
65 sfp.int_parts = ntohs(sfp.int_parts);
66 sfp.fractions = ntohs(sfp.fractions);
67
68 ret = (double)(sfp.int_parts) + ((double)sfp.fractions / USHRT_MAX);
69
70 return (ret);
71 }
72
73 struct s_fixedpt
d_to_sfp(double d)74 d_to_sfp(double d)
75 {
76 struct s_fixedpt sfp;
77
78 sfp.int_parts = htons((u_int16_t)d);
79 sfp.fractions = htons((u_int16_t)((d - (u_int16_t)d) * USHRT_MAX));
80
81 return (sfp);
82 }
83