summaryrefslogtreecommitdiffstats
path: root/contrib/ntp/tests/sandbox/smeartest.c
blob: a8ee2102e8742310793f91fd99f4f238e7862ddf (plain)
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <config.h>

#include <ntp.h>
#include <ntp_fp.h>

/*
 * we want to test a refid format of:
 * 254.x.y.x
 *
 * where x.y.z are 24 bits containing 2 (signed) integer bits
 * and 22 fractional bits.
 *
 * we want functions to convert to/from this format, with unit tests.
 *
 * Interesting test cases include:
 * 254.0.0.0
 * 254.0.0.1
 * 254.127.255.255
 * 254.128.0.0
 * 254.255.255.255
 */

char *progname = "";

l_fp convertRefIDToLFP(uint32_t r);
uint32_t convertLFPToRefID(l_fp num);


/*
 * The smear data in the refid is the bottom 3 bytes of the refid,
 * 2 bits of integer
 * 22 bits of fraction
 */
l_fp
convertRefIDToLFP(uint32_t r)
{
	l_fp temp;

	r = ntohl(r);

	printf("%03d %08x: ", (r >> 24) & 0xFF, (r & 0x00FFFFFF) );

	temp.l_uf = (r << 10);	/* 22 fractional bits */

	temp.l_ui = (r >> 22) & 0x3;
	temp.l_ui |= ~(temp.l_ui & 2) + 1;

	return temp;
}


uint32_t
convertLFPToRefID(l_fp num)
{
	uint32_t temp;

	/* round the input with the highest bit to shift out from the
	 * fraction, then keep just two bits from the integral part.
	 *
	 * TODO: check for overflows; should we clamp/saturate or just
	 * complain?
	 */
	L_ADDUF(&num, 0x200);
	num.l_ui &= 3;

	/* combine integral and fractional part to 24 bits */
	temp  = (num.l_ui << 22) | (num.l_uf >> 10);

	/* put in the leading 254.0.0.0 */
	temp |= UINT32_C(0xFE000000);

	printf("%03d %08x: ", (temp >> 24) & 0xFF, (temp & 0x00FFFFFF) );

	return htonl(temp);
}

/* Tests start here */

void rtol(uint32_t r);

void
rtol(uint32_t r)
{
	l_fp l;

	printf("rtol: ");

	l = convertRefIDToLFP(htonl(r));
	printf("refid %#x, smear %s\n", r, lfptoa(&l, 8));

	return;
}


void rtoltor(uint32_t r);

void
rtoltor(uint32_t r)
{
	l_fp l;

	printf("rtoltor: ");
	l = convertRefIDToLFP(htonl(r));

	r = convertLFPToRefID(l);
	printf("smear %s, refid %#.8x\n", lfptoa(&l, 8), ntohl(r));

	return;
}


void ltor(l_fp l);

void
ltor(l_fp l)
{
	uint32_t r;

	printf("ltor: ");

	r = convertLFPToRefID(l);
	printf("smear %s, refid %#.8x\n", lfptoa(&l, 8), ntohl(r));

	return;
}


main()
{
	l_fp l;
	int rc;

	rtol(0xfe800000);
	rtol(0xfe800001);
	rtol(0xfe8ffffe);
	rtol(0xfe8fffff);
	rtol(0xfef00000);
	rtol(0xfef00001);
	rtol(0xfefffffe);
	rtol(0xfeffffff);

	rtol(0xfe000000);
	rtol(0xfe000001);
	rtol(0xfe6ffffe);
	rtol(0xfe6fffff);
	rtol(0xfe700000);
	rtol(0xfe700001);
	rtol(0xfe7ffffe);
	rtol(0xfe7fffff);

	rtoltor(0xfe800000);
	rtoltor(0xfe800001);
	rtoltor(0xfe8ffffe);
	rtoltor(0xfe8fffff);
	rtoltor(0xfef00000);
	rtoltor(0xfef00001);
	rtoltor(0xfefffffe);
	rtoltor(0xfeffffff);

	rtoltor(0xfe000000);
	rtoltor(0xfe000001);
	rtoltor(0xfe6ffffe);
	rtoltor(0xfe6fffff);
	rtoltor(0xfe700000);
	rtoltor(0xfe700001);
	rtoltor(0xfe7ffffe);
	rtoltor(0xfe7fffff);

	rc = atolfp("-.932087", &l);
	ltor(l);
	rtol(0xfec458b0);
	printf("%x -> %d.%d.%d.%d\n",
		0xfec458b0,
		0xfe,
		  0xc4,
		    0x58,
		      0xb0);

	return 0;
}
OpenPOWER on IntegriCloud