This commit was generated by cvs2svn to compensate for changes in r1650,
[iproute2.git] / netem / paretonormal.c
1 /*
2  * Paretoormal distribution table generator
3  *
4  * This distribution is simply .25*normal + .75*pareto; a combination
5  * which seems to match experimentally observed distributions reasonably
6  *  well, but is computationally easy to handle.
7  * The entries represent a scaled inverse of the cumulative distribution
8  * function.
9  *
10  * Taken from the uncopyrighted NISTnet code.
11  */
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <math.h>
17 #include <limits.h>
18 #include <malloc.h>
19
20 #include <linux/types.h>
21 #include <linux/pkt_sched.h>
22
23 #define TABLESIZE       16384
24 #define TABLEFACTOR     NETEM_DIST_SCALE
25
26 static double
27 normal(double x, double mu, double sigma)
28 {
29         return .5 + .5*erf((x-mu)/(sqrt(2.0)*sigma));
30 }
31
32
33 static const double a=3.0;
34
35 static int
36 paretovalue(int i)
37 {
38         double dvalue;
39
40         i = 65536-4*i;
41         dvalue = (double)i/(double)65536;
42         dvalue = 1.0/pow(dvalue, 1.0/a);
43         dvalue -= 1.5;
44         dvalue *= (4.0/3.0)*(double)TABLEFACTOR;
45         if (dvalue > 32767)
46                 dvalue = 32767;
47         return (int)rint(dvalue);
48 }       
49
50 int
51 main(int argc, char **argv)
52 {
53         double x;
54         double *table;
55         int i,n;
56
57         table = calloc(TABLESIZE+1, sizeof(double));
58         if (!table) {
59                 fprintf(stderr, "Out of memory!\n");
60                 exit(1);
61         }
62
63         for (x = -10.0; x < 10.05; x += .00005) {
64                 i = (int)rint(TABLESIZE*normal(x, 0.0, 1.0));
65                 table[i] = x;
66         }
67         printf(
68 "# This is the distribution table for the paretonormal distribution.\n"
69         );
70
71         for (i = n = 0; i < TABLESIZE; i += 4) {
72                 int normvalue, parvalue, value;
73
74                 normvalue = (int) rint(table[i]*TABLEFACTOR);
75                 parvalue = paretovalue(i);
76
77                 value = (normvalue+3*parvalue)/4;
78                 if (value < SHRT_MIN) value = SHRT_MIN;
79                 if (value > SHRT_MAX) value = SHRT_MAX;
80
81                 printf(" %d", value);
82                 if (++n == 8) {
83                         putchar('\n');
84                         n = 0;
85                 }
86         }
87         free(table);
88
89         return 0;
90 }