summaryrefslogtreecommitdiffstats
path: root/sys/dev/patm/genrtab/genrtab.c
blob: eef0749fc8c4230fa06475b629754939b1640049 (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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/*
 * Copyright (c) 2003
 *	Fraunhofer Institute for Open Communication Systems (FhG Fokus).
 * 	All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * Author: Hartmut Brandt <harti@freebsd.org>
 *
 * This program is used to generate the different rate tables for the IDT77252
 * driver. The generated tables are slightly different from those in the
 * IDT manual.
 */
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
#include <math.h>
#include <ieeefp.h>

/* verbosity flag */
static int verbose;

/* number of table entries */
static const u_int tsize = 256;

/* number of rate difference tables to create */
static const u_int ndtables = 16;

/* cell rate offset for log 0 */
static const double offset = 10.0;

/*
 * Make an internal form of the interval and be sure to round down.
 */
static u_int
d2interval(double d)
{
	fp_rnd_t r;
	u_int s, id;

	r = fpsetround(FP_RZ);
	id = (u_int)rint(32 * d);
	fpsetround(r);

	s = 0;
	while (id >= 32 * 32) {
		s++;
		id >>= 1;
	}
	return ((s << 10) | (id));
}

/*
 * Convert an internal interval back to a real one.
 */
static double
interval2d(u_int id)
{
	return ((1 << ((id >> 10) & 0xf)) * ((id & 0x3ff) / 32.0));
}

/*
 * Convert double to ATM-Forum format. Make sure to round up.
 */
static u_int
cps2atmf(double cps)
{
	fp_rnd_t r;
	u_int s, id;

	if (cps < 1.0)
		return (0);

	s = 0;
	while (cps >= 2.0) {
		s++;
		cps /= 2;
	}
	r = fpsetround(FP_RP);
	id = (u_int)rint(512 * cps);
	fpsetround(r);

	return ((1 << 14) | (s << 9) | (id & 0x1ff));
}

/*
 * Convert ATM forum format to double
 */
static double
atmf2cps(u_int atmf)
{
	return (((atmf >> 14) & 1) * (1 << ((atmf >> 9) & 0x1f)) *
	    ((512 + (atmf & 0x1ff)) / 512.0));
}

/*
 * A cell rate to the logarithmic one
 */
static double
cps2log(u_int alink, double lg)
{
	if (lg <= offset)
		return (0);
	if (lg >= alink)
		return (tsize - 1);

	return ((tsize - 1) * (1 - log(alink / lg) / log(alink / offset)));
}

/*
 * Convert log to cell rate
 */
static double
log2cps(u_int alink, u_int lg)
{
	return (alink / pow(alink / offset,
	    (double)(tsize - lg - 1) / (tsize - 1)));
}

/*
 * Convert a double to an internal scaled double
 */
static u_int
d2ifp(double fp)
{
	fp_rnd_t r;
	u_int s, ifp;

	fp *= (1 << 16);

	r = fpsetround(FP_RN);
	ifp = (u_int)rint(fp);
	fpsetround(r);

	s = 0;
	while (ifp >= 1024) {
		s++;
		ifp >>= 1;
	}
	return ((s << 10) | (ifp));
}

/*
 * Convert internal scaled float to double
 */
static double
ifp2d(u_int p)
{
	return ((p & 0x3ff) * (1 << ((p >> 10) & 0xf)) / 65536.0);
}

/*
 * Generate log to rate conversion table
 */
static void
gen_log2rate(u_int alink)
{
	u_int i, iinterval, atmf, n, nrm;
	double rate, interval, xinterval, cps, xcps;

	for (i = 0; i < 256; i++) {
		/* get the desired rate */
		rate = alink / pow(alink / offset,
		    (double)(tsize - i - 1) / (tsize - 1));

		/* convert this to an interval */
		interval = alink / rate;

		/* make the internal form of this interval, be sure to
		 * round down */
		iinterval = d2interval(interval);

		/* now convert back */
		xinterval = interval2d(iinterval);

		/* make a cps from this interval */
		cps = alink / xinterval;

		/* convert this to its ATM forum format */
		atmf = cps2atmf(cps);

		/* and back */
		xcps = atmf2cps(atmf);

		/* decide on NRM */
		if (xcps < 40.0) {
			nrm = 0;
			n = 3;
		} else if (xcps < 80.0) {
			nrm = 1;
			n = 4;
		} else if (xcps < 160.0) {
			nrm = 2;
			n = 8;
		} else if (xcps < 320.0) {
			nrm = 3;
			n = 16;
		} else {
			nrm = 4;
			n = 32;
		}

		/* print */
		if (verbose)
			printf(" 0x%08x, /* %03u: cps=%f nrm=%u int=%f */\n",
			    (atmf << 17) | (nrm << 14) | iinterval, i,
			    xcps, n, xinterval);
		else
			printf("0x%08x,\n", (atmf << 17) | (nrm << 14) |
			    iinterval);
	}
}

/*
 * Generate rate to log conversion table
 */
static void
gen_rate2log(u_int alink)
{
	u_int i, atmf, val, ilcr;
	double cps, lcr;
	fp_rnd_t r;

	val = 0;
	for (i = 0; i < 512; i++) {
		/* make ATM Forum CPS from index */
		atmf = (((i & 0x1f0) >> 4) << 9) |
		    ((i & 0xf) << 5) | (1 << 14);

		/* make cps */
		cps = atmf2cps(atmf);

		/* convert to log */
		lcr = cps2log(alink, cps);

		r = fpsetround(FP_RN);
		ilcr = (u_int)rint(lcr);
		fpsetround(r);

		/* put together */
		val |= ilcr << (8 * (i % 4));

		/* print */
		if (i % 4 == 3) {
			if (verbose)
				printf(" 0x%08x,\t", val);
			else
				printf("0x%08x,\n", val);
			val = 0;
		} else if (verbose)
			printf("\t\t");
		if (verbose)
			printf("/* %03u: %f -> %f */\n", i,
			    cps, log2cps(alink, ilcr));
	}
}

/*
 * Generate one entry into the global table
 */
static void
gen_glob_entry(u_int alink, u_int fill, u_int ci, u_int ni)
{
	if (verbose)
		printf("  0x%08x,	/* %2u/32 %8.6f, %6u, ci=%u, ni=%u */\n",
		    cps2atmf(alink * fill / 32.0) | (ci << 17) | (ni << 16),
		    fill, fill / 32.0, alink * fill / 32, ci, ni);
	else
		printf("0x%08x,\n",
		    cps2atmf(alink * fill / 32.0) | (ci << 17) | (ni << 16));
}

/*
 * Generate global parameter table
 */
static void
gen_glob(u_int alink)
{
	u_int i;

	gen_glob_entry(alink, 32, 0, 0);
	gen_glob_entry(alink, 16, 0, 0);
	gen_glob_entry(alink,  8, 0, 1);
	gen_glob_entry(alink,  4, 0, 1);
	gen_glob_entry(alink,  2, 1, 1);
	gen_glob_entry(alink,  1, 1, 1);
	gen_glob_entry(alink,  0, 1, 1);
	gen_glob_entry(alink,  0, 1, 1);

	for (i = 0; i < tsize/2 - 8; i++) {
		if (i % 16 == 0)
			printf(" ");
		printf(" 0,");
		if (i % 16 == 15)
			printf("\n");
	}
	printf("\n");
}

/*
 * Generate additive rate increase tables
 */
static void
gen_air(u_int alink)
{
	u_int t, i;
	double diff;	/* cell rate to increase by */
	double cps;
	double add;
	u_int val, a;

	for (t = 0; t < ndtables; t++) {
		diff = (double)alink / (1 << t);
		printf("/* AIR %u: diff=%f */\n", t, diff);
		val = 0;
		for (i = 0; i < tsize; i++) {
			cps = log2cps(alink, i);
			cps += diff;
			if (cps > alink)
				cps = alink;

			add = cps2log(alink, cps) - i;

			a = d2ifp(add);

			if (i % 2) {
				val |= a << 16;
				if (verbose)
					printf("  0x%08x,\t", val);
				else
					printf("0x%08x,\n", val);
			} else {
				val = a;
				if (verbose)
					printf("\t\t");
			}
			if (verbose)
				printf("/* %3u: %f */\n", i, ifp2d(add));
		}
	}
}

/*
 * Generate rate decrease table
 */
static void
gen_rdf(u_int alink)
{
	double d;
	u_int t, i, f, val, diff;

	for (t = 0; t < ndtables; t++) {
		/* compute the log index difference */
		if (t == 0) {
			d = tsize - 1;
		} else {
			f = 1 << t;
			d = (tsize - 1) / log(alink / offset);
			d *= log((double)f / (f - 1));
		}
		printf(" /* RDF %u: 1/%u: %f */\n", t, 1 << t, d);
		val = 0;
		for (i = 0; i < tsize; i++) {
			if (i < d)
				diff = d2ifp(i);
			else
				diff = d2ifp(d);
			if (i % 2) {
				val |= diff << 16;
				if (verbose)
					printf("  0x%08x,\t", val);
				else
					printf("0x%08x,\n", val);
			} else {
				val = diff;
				if (verbose)
					printf("\t\t");
			}
			if (verbose)
				printf("/* %3u: %f */\n", i, ifp2d(diff));
		}
	}
}

/*
 * Create all the tables for a given link cell rate and link bit rate.
 * The link bit rate is only used to name the table.
 */
static void
gen_tables(u_int alink, u_int mbps)
{
	printf("\n");
	printf("/*\n");
	printf(" * Tables for %ucps and %uMbps\n", alink, mbps);
	printf(" */\n");
	printf("const uint32_t patm_rtables%u[128 * (4 + 2 * %u)] = {\n",
	    mbps, ndtables);

	gen_log2rate(alink);
	gen_rate2log(alink);
	gen_glob(alink);
	gen_air(alink);
	gen_rdf(alink);

	printf("};\n");
}

int
main(int argc, char *argv[])
{
	int opt;

	while ((opt = getopt(argc, argv, "v")) != -1)
		switch (opt) {

		  case 'v':
			verbose = 1;
			break;
		}

	printf("/*\n");
	printf(" * This file was generated by `%s'\n", argv[0]);
	printf(" */\n");
	printf("\n");
	printf("#include <sys/cdefs.h>\n");
	printf("__FBSDID(\"$FreeBSD$\");\n");
	printf("\n");
	printf("#include <sys/types.h>\n");
	printf("\n");
	printf("const u_int patm_rtables_size = 128 * (4 + 2 * %u);\n",
	    ndtables);
	printf("const u_int patm_rtables_ntab = %u;\n", ndtables);
	gen_tables(352768, 155);
	gen_tables( 59259,  25);
	return (0);
}
OpenPOWER on IntegriCloud