File: ntp.h

package info (click to toggle)
libprelude 0.9.18.1-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 17,480 kB
  • ctags: 11,663
  • sloc: ansic: 165,605; xml: 27,857; sh: 10,044; makefile: 413; awk: 341; yacc: 238; lex: 186; python: 9; perl: 2
file content (129 lines) | stat: -rw-r--r-- 4,293 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/**********************************************************************
* Copyright (c) David L. Mills 1992-2001                              *
*                                                                     *
* Permission to use, copy, modify, and distribute this software and   *
* its documentation for any purpose and without fee is hereby         *
* granted, provided that the above copyright notice appears in all    *
* copies and that both the copyright notice and this permission       *
* notice appear in supporting documentation, and that the name        *
* University of Delaware not be used in advertising or publicity      *
* pertaining to distribution of the software without specific,        *
* written prior permission. The University of Delaware makes no       *
* representations about the suitability this software for any         *
* purpose. It is provided "as is" without express or implied          *
* warranty.                                                           *
*                                                                     *
***********************************************************************/

#ifndef _LIBPRELUDE_NTP_H
#define _LIBPRELUDE_NTP_H

#define TS_MASK         0xfffff000      /* mask to usec, for time stamps */
#define TS_ROUNDBIT     0x00000800      /* round at this bit */
#define	JAN_1970	0x83aa7e80	/* 2208988800 1970 - 1900 in seconds */


typedef int32_t s_fp;


typedef struct {
        union {
                uint32_t Xl_ui;
                int32_t Xl_i;
        } Ul_i;
        union {
                uint32_t Xl_uf;
                int32_t Xl_f;
        } Ul_f;
} l_fp;

#define l_ui    Ul_i.Xl_ui              /* unsigned integral part */
#define l_i     Ul_i.Xl_i               /* signed integral part */
#define l_uf    Ul_f.Xl_uf              /* unsigned fractional part */
#define l_f     Ul_f.Xl_f               /* signed fractional part */



extern const unsigned long ustotslo[];
extern const unsigned long ustotsmid[];
extern const unsigned long ustotshi[];


#define M_NEG(v_i, v_f)         /* v = -v */ \
        do { \
                if ((v_f) == 0) \
                        (v_i) = -((s_fp)(v_i)); \
                else { \
                        (v_f) = -((s_fp)(v_f)); \
                        (v_i) = ~(v_i); \
                } \
        } while(0)



#define L_NEG(v)        M_NEG((v)->l_ui, (v)->l_uf)


#define TVUTOTSF(tvu, tsf) \
        (tsf) = ustotslo[(tvu) & 0xff] \
            + ustotsmid[((tvu) >> 8) & 0xff] \
            + ustotshi[((tvu) >> 16) & 0xf]


#define sTVTOTS(tv, ts) \
        do { \
                int isneg = 0; \
                long usec; \
                (ts)->l_ui = (tv)->tv_sec; \
                usec = (tv)->tv_usec; \
                if (((tv)->tv_sec < 0) || ((tv)->tv_usec < 0)) { \
                        usec = -usec; \
                        (ts)->l_ui = -(ts)->l_ui; \
                        isneg = 1; \
                } \
                TVUTOTSF(usec, (ts)->l_uf); \
                if (isneg) { \
                        L_NEG((ts)); \
                } \
        } while(0)

/* Taken from /usr/src/contrib/ntp/include/ntp_unixtime.h, FreeBSD 5.1-RELEASE */

/*
 * TV_SHIFT is used to turn the table result into a usec value.  To round,
 * add in TV_ROUNDBIT before shifting
 */
#define TV_SHIFT        3
#define TV_ROUNDBIT     0x4
    

/*
 * Convert a time stamp fraction to microseconds.  The time stamp
 * fraction is assumed to be unsigned.  To use this in a program, declare:
 */
extern const long tstouslo[];
extern const long tstousmid[];
extern const long tstoushi[];

#define TSFTOTVU(tsf, tvu) \
        (tvu) = (tstoushi[((tsf) >> 24) & 0xff] \
            + tstousmid[((tsf) >> 16) & 0xff] \
            + tstouslo[((tsf) >> 9) & 0x7f] \
            + TV_ROUNDBIT) >> TV_SHIFT
/*
 * Convert a time stamp to a struct timeval.  The time stamp
 * has to be positive.
 */
#define TSTOTV(ts, tv) \
        do { \
                (tv)->tv_sec = (ts)->l_ui; \
                TSFTOTVU((ts)->l_uf, (tv)->tv_usec); \
                if ((tv)->tv_usec == 1000000) { \
                        (tv)->tv_sec++; \
                        (tv)->tv_usec = 0; \
                } \
        } while (0)



#endif /* _LIBPRELUDE_NTP_H */