5 #include <ntp_assert.h>
8 * we want to test a refid format of:
11 * where x.y.z are 24 bits containing 2 (signed) integer bits
12 * and 22 fractional bits.
14 * we want functions to convert to/from this format, with unit tests.
16 * Interesting test cases include:
26 l_fp convertRefIDToLFP(uint32_t r);
27 uint32_t convertLFPToRefID(l_fp num);
31 * The smear data in the refid is the bottom 3 bytes of the refid,
36 convertRefIDToLFP(uint32_t r)
42 printf("%03d %08x: ", (r >> 24) & 0xFF, (r & 0x00FFFFFF) );
44 temp.l_uf = (r << 10); /* 22 fractional bits */
46 temp.l_ui = (r >> 22) & 0x3;
47 temp.l_ui |= ~(temp.l_ui & 2) + 1;
54 convertLFPToRefID(l_fp num)
58 /* round the input with the highest bit to shift out from the
59 * fraction, then keep just two bits from the integral part.
61 * TODO: check for overflows; should we clamp/saturate or just
67 /* combine integral and fractional part to 24 bits */
68 temp = (num.l_ui << 22) | (num.l_uf >> 10);
70 /* put in the leading 254.0.0.0 */
71 temp |= UINT32_C(0xFE000000);
73 printf("%03d %08x: ", (temp >> 24) & 0xFF, (temp & 0x00FFFFFF) );
78 /* Tests start here */
80 void rtol(uint32_t r);
89 l = convertRefIDToLFP(htonl(r));
90 printf("refid %#x, smear %s\n", r, lfptoa(&l, 8));
96 void rtoltor(uint32_t r);
104 l = convertRefIDToLFP(htonl(r));
106 r = convertLFPToRefID(l);
107 printf("smear %s, refid %#.8x\n", lfptoa(&l, 8), ntohl(r));
122 r = convertLFPToRefID(l);
123 printf("smear %s, refid %#.8x\n", lfptoa(&l, 8), ntohl(r));
173 rc = atolfp("-.932087", &l);
178 printf("%x -> %d.%d.%d.%d\n",