smeartest.c revision 290001
1#include <config.h>
2
3#include <ntp.h>
4#include <ntp_fp.h>
5
6/*
7 * we want to test a refid format of:
8 * 254.x.y.x
9 *
10 * where x.y.z are 24 bits containing 2 (signed) integer bits
11 * and 22 fractional bits.
12 *
13 * we want functions to convert to/from this format, with unit tests.
14 *
15 * Interesting test cases include:
16 * 254.0.0.0
17 * 254.0.0.1
18 * 254.127.255.255
19 * 254.128.0.0
20 * 254.255.255.255
21 */
22
23char *progname = "";
24
25l_fp convertRefIDToLFP(uint32_t r);
26uint32_t convertLFPToRefID(l_fp num);
27
28
29/*
30 * The smear data in the refid is the bottom 3 bytes of the refid,
31 * 2 bits of integer
32 * 22 bits of fraction
33 */
34l_fp
35convertRefIDToLFP(uint32_t r)
36{
37	l_fp temp;
38
39	r = ntohl(r);
40
41	printf("%03d %08x: ", (r >> 24) & 0xFF, (r & 0x00FFFFFF) );
42
43	temp.l_uf = (r << 10);	/* 22 fractional bits */
44
45	temp.l_ui = (r >> 22) & 0x3;
46	temp.l_ui |= ~(temp.l_ui & 2) + 1;
47
48	return temp;
49}
50
51
52uint32_t
53convertLFPToRefID(l_fp num)
54{
55	uint32_t temp;
56
57	/* round the input with the highest bit to shift out from the
58	 * fraction, then keep just two bits from the integral part.
59	 *
60	 * TODO: check for overflows; should we clamp/saturate or just
61	 * complain?
62	 */
63	L_ADDUF(&num, 0x200);
64	num.l_ui &= 3;
65
66	/* combine integral and fractional part to 24 bits */
67	temp  = (num.l_ui << 22) | (num.l_uf >> 10);
68
69	/* put in the leading 254.0.0.0 */
70	temp |= UINT32_C(0xFE000000);
71
72	printf("%03d %08x: ", (temp >> 24) & 0xFF, (temp & 0x00FFFFFF) );
73
74	return htonl(temp);
75}
76
77/* Tests start here */
78
79void rtol(uint32_t r);
80
81void
82rtol(uint32_t r)
83{
84	l_fp l;
85
86	printf("rtol: ");
87
88	l = convertRefIDToLFP(htonl(r));
89	printf("refid %#x, smear %s\n", r, lfptoa(&l, 8));
90
91	return;
92}
93
94
95void rtoltor(uint32_t r);
96
97void
98rtoltor(uint32_t r)
99{
100	l_fp l;
101
102	printf("rtoltor: ");
103	l = convertRefIDToLFP(htonl(r));
104
105	r = convertLFPToRefID(l);
106	printf("smear %s, refid %#.8x\n", lfptoa(&l, 8), ntohl(r));
107
108	return;
109}
110
111
112void ltor(l_fp l);
113
114void
115ltor(l_fp l)
116{
117	uint32_t r;
118
119	printf("ltor: ");
120
121	r = convertLFPToRefID(l);
122	printf("smear %s, refid %#.8x\n", lfptoa(&l, 8), ntohl(r));
123
124	return;
125}
126
127
128main()
129{
130	l_fp l;
131	int rc;
132
133	rtol(0xfe800000);
134	rtol(0xfe800001);
135	rtol(0xfe8ffffe);
136	rtol(0xfe8fffff);
137	rtol(0xfef00000);
138	rtol(0xfef00001);
139	rtol(0xfefffffe);
140	rtol(0xfeffffff);
141
142	rtol(0xfe000000);
143	rtol(0xfe000001);
144	rtol(0xfe6ffffe);
145	rtol(0xfe6fffff);
146	rtol(0xfe700000);
147	rtol(0xfe700001);
148	rtol(0xfe7ffffe);
149	rtol(0xfe7fffff);
150
151	rtoltor(0xfe800000);
152	rtoltor(0xfe800001);
153	rtoltor(0xfe8ffffe);
154	rtoltor(0xfe8fffff);
155	rtoltor(0xfef00000);
156	rtoltor(0xfef00001);
157	rtoltor(0xfefffffe);
158	rtoltor(0xfeffffff);
159
160	rtoltor(0xfe000000);
161	rtoltor(0xfe000001);
162	rtoltor(0xfe6ffffe);
163	rtoltor(0xfe6fffff);
164	rtoltor(0xfe700000);
165	rtoltor(0xfe700001);
166	rtoltor(0xfe7ffffe);
167	rtoltor(0xfe7fffff);
168
169	rc = atolfp("-.932087", &l);
170	ltor(l);
171	rtol(0xfec458b0);
172	printf("%x -> %d.%d.%d.%d\n",
173		0xfec458b0,
174		0xfe,
175		  0xc4,
176		    0x58,
177		      0xb0);
178
179	return 0;
180}
181