Merge master.kernel.org:/pub/scm/linux/kernel/git/paulus/powerpc
[sfrench/cifs-2.6.git] / net / dccp / ccids / lib / tfrc_equation.c
1 /*
2  *  net/dccp/ccids/lib/tfrc_equation.c
3  *
4  *  Copyright (c) 2005 The University of Waikato, Hamilton, New Zealand.
5  *  Copyright (c) 2005 Ian McDonald <ian.mcdonald@jandi.co.nz>
6  *  Copyright (c) 2005 Arnaldo Carvalho de Melo <acme@conectiva.com.br>
7  *  Copyright (c) 2003 Nils-Erik Mattsson, Joacim Haggmark, Magnus Erixzon
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or
12  *  (at your option) any later version.
13  */
14
15 #include <linux/module.h>
16 #include <asm/div64.h>
17 #include "../../dccp.h"
18 #include "tfrc.h"
19
20 #define TFRC_CALC_X_ARRSIZE 500
21 #define TFRC_CALC_X_SPLIT   50000       /* 0.05 * 1000000, details below */
22 #define TFRC_SMALLEST_P     (TFRC_CALC_X_SPLIT/TFRC_CALC_X_ARRSIZE)
23
24 /*
25   TFRC TCP Reno Throughput Equation Lookup Table for f(p)
26
27   The following two-column lookup table implements a part of the TCP throughput
28   equation from [RFC 3448, sec. 3.1]:
29
30                                      s
31   X_calc  =  --------------------------------------------------------------
32              R * sqrt(2*b*p/3) + (3 * t_RTO * sqrt(3*b*p/8) * (p + 32*p^3))
33
34   Where:
35         X      is the transmit rate in bytes/second
36         s      is the packet size in bytes
37         R      is the round trip time in seconds
38         p      is the loss event rate, between 0 and 1.0, of the number of loss
39                       events as a fraction of the number of packets transmitted
40         t_RTO  is the TCP retransmission timeout value in seconds
41         b      is the number of packets acknowledged by a single TCP ACK
42
43   We can assume that b = 1 and t_RTO is 4 * R. The equation now becomes:
44
45                                      s
46   X_calc  =  -------------------------------------------------------
47              R * sqrt(p*2/3) + (12 * R * sqrt(p*3/8) * (p + 32*p^3))
48
49   which we can break down into:
50
51                       s
52         X_calc  =  ---------
53                     R * f(p)
54
55   where f(p) is given for 0 < p <= 1 by:
56
57         f(p)  =  sqrt(2*p/3) + 12 * sqrt(3*p/8) *  (p + 32*p^3)
58
59   Since this is kernel code, floating-point arithmetic is avoided in favour of
60   integer arithmetic. This means that nearly all fractional parameters are
61   scaled by 1000000:
62     * the parameters p and R
63     * the return result f(p)
64   The lookup table therefore actually tabulates the following function g(q):
65
66         g(q)  =  1000000 * f(q/1000000)
67
68   Hence, when p <= 1, q must be less than or equal to 1000000. To achieve finer
69   granularity for the practically more relevant case of small values of p (up to
70   5%), the second column is used; the first one ranges up to 100%.  This split
71   corresponds to the value of q = TFRC_CALC_X_SPLIT. At the same time this also
72   determines the smallest resolution possible with this lookup table:
73
74     TFRC_SMALLEST_P   =  TFRC_CALC_X_SPLIT / TFRC_CALC_X_ARRSIZE
75
76   The entire table is generated by:
77     for(i=0; i < TFRC_CALC_X_ARRSIZE; i++) {
78         lookup[i][0]  =  g((i+1) * 1000000/TFRC_CALC_X_ARRSIZE);
79         lookup[i][1]  =  g((i+1) * TFRC_CALC_X_SPLIT/TFRC_CALC_X_ARRSIZE);
80     }
81
82   With the given configuration, we have, with M = TFRC_CALC_X_ARRSIZE-1,
83     lookup[0][0]  =  g(1000000/(M+1))            =  1000000 * f(0.2%)
84     lookup[M][0]  =  g(1000000)                  =  1000000 * f(100%)
85     lookup[0][1]  =  g(TFRC_SMALLEST_P)           = 1000000 * f(0.01%)
86     lookup[M][1]  =  g(TFRC_CALC_X_SPLIT)        =  1000000 * f(5%)
87
88   In summary, the two columns represent f(p) for the following ranges:
89     * The first column is for   0.002  <= p <= 1.0
90     * The second column is for  0.0001 <= p <= 0.05
91   Where the columns overlap, the second (finer-grained) is given preference,
92   i.e. the first column is used only for p >= 0.05.
93  */
94 static const u32 tfrc_calc_x_lookup[TFRC_CALC_X_ARRSIZE][2] = {
95         {     37172,   8172 },
96         {     53499,  11567 },
97         {     66664,  14180 },
98         {     78298,  16388 },
99         {     89021,  18339 },
100         {     99147,  20108 },
101         {    108858,  21738 },
102         {    118273,  23260 },
103         {    127474,  24693 },
104         {    136520,  26052 },
105         {    145456,  27348 },
106         {    154316,  28589 },
107         {    163130,  29783 },
108         {    171919,  30935 },
109         {    180704,  32049 },
110         {    189502,  33130 },
111         {    198328,  34180 },
112         {    207194,  35202 },
113         {    216114,  36198 },
114         {    225097,  37172 },
115         {    234153,  38123 },
116         {    243294,  39055 },
117         {    252527,  39968 },
118         {    261861,  40864 },
119         {    271305,  41743 },
120         {    280866,  42607 },
121         {    290553,  43457 },
122         {    300372,  44293 },
123         {    310333,  45117 },
124         {    320441,  45929 },
125         {    330705,  46729 },
126         {    341131,  47518 },
127         {    351728,  48297 },
128         {    362501,  49066 },
129         {    373460,  49826 },
130         {    384609,  50577 },
131         {    395958,  51320 },
132         {    407513,  52054 },
133         {    419281,  52780 },
134         {    431270,  53499 },
135         {    443487,  54211 },
136         {    455940,  54916 },
137         {    468635,  55614 },
138         {    481581,  56306 },
139         {    494785,  56991 },
140         {    508254,  57671 },
141         {    521996,  58345 },
142         {    536019,  59014 },
143         {    550331,  59677 },
144         {    564939,  60335 },
145         {    579851,  60988 },
146         {    595075,  61636 },
147         {    610619,  62279 },
148         {    626491,  62918 },
149         {    642700,  63553 },
150         {    659253,  64183 },
151         {    676158,  64809 },
152         {    693424,  65431 },
153         {    711060,  66050 },
154         {    729073,  66664 },
155         {    747472,  67275 },
156         {    766266,  67882 },
157         {    785464,  68486 },
158         {    805073,  69087 },
159         {    825103,  69684 },
160         {    845562,  70278 },
161         {    866460,  70868 },
162         {    887805,  71456 },
163         {    909606,  72041 },
164         {    931873,  72623 },
165         {    954614,  73202 },
166         {    977839,  73778 },
167         {   1001557,  74352 },
168         {   1025777,  74923 },
169         {   1050508,  75492 },
170         {   1075761,  76058 },
171         {   1101544,  76621 },
172         {   1127867,  77183 },
173         {   1154739,  77741 },
174         {   1182172,  78298 },
175         {   1210173,  78852 },
176         {   1238753,  79405 },
177         {   1267922,  79955 },
178         {   1297689,  80503 },
179         {   1328066,  81049 },
180         {   1359060,  81593 },
181         {   1390684,  82135 },
182         {   1422947,  82675 },
183         {   1455859,  83213 },
184         {   1489430,  83750 },
185         {   1523671,  84284 },
186         {   1558593,  84817 },
187         {   1594205,  85348 },
188         {   1630518,  85878 },
189         {   1667543,  86406 },
190         {   1705290,  86932 },
191         {   1743770,  87457 },
192         {   1782994,  87980 },
193         {   1822973,  88501 },
194         {   1863717,  89021 },
195         {   1905237,  89540 },
196         {   1947545,  90057 },
197         {   1990650,  90573 },
198         {   2034566,  91087 },
199         {   2079301,  91600 },
200         {   2124869,  92111 },
201         {   2171279,  92622 },
202         {   2218543,  93131 },
203         {   2266673,  93639 },
204         {   2315680,  94145 },
205         {   2365575,  94650 },
206         {   2416371,  95154 },
207         {   2468077,  95657 },
208         {   2520707,  96159 },
209         {   2574271,  96660 },
210         {   2628782,  97159 },
211         {   2684250,  97658 },
212         {   2740689,  98155 },
213         {   2798110,  98651 },
214         {   2856524,  99147 },
215         {   2915944,  99641 },
216         {   2976382, 100134 },
217         {   3037850, 100626 },
218         {   3100360, 101117 },
219         {   3163924, 101608 },
220         {   3228554, 102097 },
221         {   3294263, 102586 },
222         {   3361063, 103073 },
223         {   3428966, 103560 },
224         {   3497984, 104045 },
225         {   3568131, 104530 },
226         {   3639419, 105014 },
227         {   3711860, 105498 },
228         {   3785467, 105980 },
229         {   3860253, 106462 },
230         {   3936229, 106942 },
231         {   4013410, 107422 },
232         {   4091808, 107902 },
233         {   4171435, 108380 },
234         {   4252306, 108858 },
235         {   4334431, 109335 },
236         {   4417825, 109811 },
237         {   4502501, 110287 },
238         {   4588472, 110762 },
239         {   4675750, 111236 },
240         {   4764349, 111709 },
241         {   4854283, 112182 },
242         {   4945564, 112654 },
243         {   5038206, 113126 },
244         {   5132223, 113597 },
245         {   5227627, 114067 },
246         {   5324432, 114537 },
247         {   5422652, 115006 },
248         {   5522299, 115474 },
249         {   5623389, 115942 },
250         {   5725934, 116409 },
251         {   5829948, 116876 },
252         {   5935446, 117342 },
253         {   6042439, 117808 },
254         {   6150943, 118273 },
255         {   6260972, 118738 },
256         {   6372538, 119202 },
257         {   6485657, 119665 },
258         {   6600342, 120128 },
259         {   6716607, 120591 },
260         {   6834467, 121053 },
261         {   6953935, 121514 },
262         {   7075025, 121976 },
263         {   7197752, 122436 },
264         {   7322131, 122896 },
265         {   7448175, 123356 },
266         {   7575898, 123815 },
267         {   7705316, 124274 },
268         {   7836442, 124733 },
269         {   7969291, 125191 },
270         {   8103877, 125648 },
271         {   8240216, 126105 },
272         {   8378321, 126562 },
273         {   8518208, 127018 },
274         {   8659890, 127474 },
275         {   8803384, 127930 },
276         {   8948702, 128385 },
277         {   9095861, 128840 },
278         {   9244875, 129294 },
279         {   9395760, 129748 },
280         {   9548529, 130202 },
281         {   9703198, 130655 },
282         {   9859782, 131108 },
283         {  10018296, 131561 },
284         {  10178755, 132014 },
285         {  10341174, 132466 },
286         {  10505569, 132917 },
287         {  10671954, 133369 },
288         {  10840345, 133820 },
289         {  11010757, 134271 },
290         {  11183206, 134721 },
291         {  11357706, 135171 },
292         {  11534274, 135621 },
293         {  11712924, 136071 },
294         {  11893673, 136520 },
295         {  12076536, 136969 },
296         {  12261527, 137418 },
297         {  12448664, 137867 },
298         {  12637961, 138315 },
299         {  12829435, 138763 },
300         {  13023101, 139211 },
301         {  13218974, 139658 },
302         {  13417071, 140106 },
303         {  13617407, 140553 },
304         {  13819999, 140999 },
305         {  14024862, 141446 },
306         {  14232012, 141892 },
307         {  14441465, 142339 },
308         {  14653238, 142785 },
309         {  14867346, 143230 },
310         {  15083805, 143676 },
311         {  15302632, 144121 },
312         {  15523842, 144566 },
313         {  15747453, 145011 },
314         {  15973479, 145456 },
315         {  16201939, 145900 },
316         {  16432847, 146345 },
317         {  16666221, 146789 },
318         {  16902076, 147233 },
319         {  17140429, 147677 },
320         {  17381297, 148121 },
321         {  17624696, 148564 },
322         {  17870643, 149007 },
323         {  18119154, 149451 },
324         {  18370247, 149894 },
325         {  18623936, 150336 },
326         {  18880241, 150779 },
327         {  19139176, 151222 },
328         {  19400759, 151664 },
329         {  19665007, 152107 },
330         {  19931936, 152549 },
331         {  20201564, 152991 },
332         {  20473907, 153433 },
333         {  20748982, 153875 },
334         {  21026807, 154316 },
335         {  21307399, 154758 },
336         {  21590773, 155199 },
337         {  21876949, 155641 },
338         {  22165941, 156082 },
339         {  22457769, 156523 },
340         {  22752449, 156964 },
341         {  23049999, 157405 },
342         {  23350435, 157846 },
343         {  23653774, 158287 },
344         {  23960036, 158727 },
345         {  24269236, 159168 },
346         {  24581392, 159608 },
347         {  24896521, 160049 },
348         {  25214642, 160489 },
349         {  25535772, 160929 },
350         {  25859927, 161370 },
351         {  26187127, 161810 },
352         {  26517388, 162250 },
353         {  26850728, 162690 },
354         {  27187165, 163130 },
355         {  27526716, 163569 },
356         {  27869400, 164009 },
357         {  28215234, 164449 },
358         {  28564236, 164889 },
359         {  28916423, 165328 },
360         {  29271815, 165768 },
361         {  29630428, 166208 },
362         {  29992281, 166647 },
363         {  30357392, 167087 },
364         {  30725779, 167526 },
365         {  31097459, 167965 },
366         {  31472452, 168405 },
367         {  31850774, 168844 },
368         {  32232445, 169283 },
369         {  32617482, 169723 },
370         {  33005904, 170162 },
371         {  33397730, 170601 },
372         {  33792976, 171041 },
373         {  34191663, 171480 },
374         {  34593807, 171919 },
375         {  34999428, 172358 },
376         {  35408544, 172797 },
377         {  35821174, 173237 },
378         {  36237335, 173676 },
379         {  36657047, 174115 },
380         {  37080329, 174554 },
381         {  37507197, 174993 },
382         {  37937673, 175433 },
383         {  38371773, 175872 },
384         {  38809517, 176311 },
385         {  39250924, 176750 },
386         {  39696012, 177190 },
387         {  40144800, 177629 },
388         {  40597308, 178068 },
389         {  41053553, 178507 },
390         {  41513554, 178947 },
391         {  41977332, 179386 },
392         {  42444904, 179825 },
393         {  42916290, 180265 },
394         {  43391509, 180704 },
395         {  43870579, 181144 },
396         {  44353520, 181583 },
397         {  44840352, 182023 },
398         {  45331092, 182462 },
399         {  45825761, 182902 },
400         {  46324378, 183342 },
401         {  46826961, 183781 },
402         {  47333531, 184221 },
403         {  47844106, 184661 },
404         {  48358706, 185101 },
405         {  48877350, 185541 },
406         {  49400058, 185981 },
407         {  49926849, 186421 },
408         {  50457743, 186861 },
409         {  50992759, 187301 },
410         {  51531916, 187741 },
411         {  52075235, 188181 },
412         {  52622735, 188622 },
413         {  53174435, 189062 },
414         {  53730355, 189502 },
415         {  54290515, 189943 },
416         {  54854935, 190383 },
417         {  55423634, 190824 },
418         {  55996633, 191265 },
419         {  56573950, 191706 },
420         {  57155606, 192146 },
421         {  57741621, 192587 },
422         {  58332014, 193028 },
423         {  58926806, 193470 },
424         {  59526017, 193911 },
425         {  60129666, 194352 },
426         {  60737774, 194793 },
427         {  61350361, 195235 },
428         {  61967446, 195677 },
429         {  62589050, 196118 },
430         {  63215194, 196560 },
431         {  63845897, 197002 },
432         {  64481179, 197444 },
433         {  65121061, 197886 },
434         {  65765563, 198328 },
435         {  66414705, 198770 },
436         {  67068508, 199213 },
437         {  67726992, 199655 },
438         {  68390177, 200098 },
439         {  69058085, 200540 },
440         {  69730735, 200983 },
441         {  70408147, 201426 },
442         {  71090343, 201869 },
443         {  71777343, 202312 },
444         {  72469168, 202755 },
445         {  73165837, 203199 },
446         {  73867373, 203642 },
447         {  74573795, 204086 },
448         {  75285124, 204529 },
449         {  76001380, 204973 },
450         {  76722586, 205417 },
451         {  77448761, 205861 },
452         {  78179926, 206306 },
453         {  78916102, 206750 },
454         {  79657310, 207194 },
455         {  80403571, 207639 },
456         {  81154906, 208084 },
457         {  81911335, 208529 },
458         {  82672880, 208974 },
459         {  83439562, 209419 },
460         {  84211402, 209864 },
461         {  84988421, 210309 },
462         {  85770640, 210755 },
463         {  86558080, 211201 },
464         {  87350762, 211647 },
465         {  88148708, 212093 },
466         {  88951938, 212539 },
467         {  89760475, 212985 },
468         {  90574339, 213432 },
469         {  91393551, 213878 },
470         {  92218133, 214325 },
471         {  93048107, 214772 },
472         {  93883493, 215219 },
473         {  94724314, 215666 },
474         {  95570590, 216114 },
475         {  96422343, 216561 },
476         {  97279594, 217009 },
477         {  98142366, 217457 },
478         {  99010679, 217905 },
479         {  99884556, 218353 },
480         { 100764018, 218801 },
481         { 101649086, 219250 },
482         { 102539782, 219698 },
483         { 103436128, 220147 },
484         { 104338146, 220596 },
485         { 105245857, 221046 },
486         { 106159284, 221495 },
487         { 107078448, 221945 },
488         { 108003370, 222394 },
489         { 108934074, 222844 },
490         { 109870580, 223294 },
491         { 110812910, 223745 },
492         { 111761087, 224195 },
493         { 112715133, 224646 },
494         { 113675069, 225097 },
495         { 114640918, 225548 },
496         { 115612702, 225999 },
497         { 116590442, 226450 },
498         { 117574162, 226902 },
499         { 118563882, 227353 },
500         { 119559626, 227805 },
501         { 120561415, 228258 },
502         { 121569272, 228710 },
503         { 122583219, 229162 },
504         { 123603278, 229615 },
505         { 124629471, 230068 },
506         { 125661822, 230521 },
507         { 126700352, 230974 },
508         { 127745083, 231428 },
509         { 128796039, 231882 },
510         { 129853241, 232336 },
511         { 130916713, 232790 },
512         { 131986475, 233244 },
513         { 133062553, 233699 },
514         { 134144966, 234153 },
515         { 135233739, 234608 },
516         { 136328894, 235064 },
517         { 137430453, 235519 },
518         { 138538440, 235975 },
519         { 139652876, 236430 },
520         { 140773786, 236886 },
521         { 141901190, 237343 },
522         { 143035113, 237799 },
523         { 144175576, 238256 },
524         { 145322604, 238713 },
525         { 146476218, 239170 },
526         { 147636442, 239627 },
527         { 148803298, 240085 },
528         { 149976809, 240542 },
529         { 151156999, 241000 },
530         { 152343890, 241459 },
531         { 153537506, 241917 },
532         { 154737869, 242376 },
533         { 155945002, 242835 },
534         { 157158929, 243294 },
535         { 158379673, 243753 },
536         { 159607257, 244213 },
537         { 160841704, 244673 },
538         { 162083037, 245133 },
539         { 163331279, 245593 },
540         { 164586455, 246054 },
541         { 165848586, 246514 },
542         { 167117696, 246975 },
543         { 168393810, 247437 },
544         { 169676949, 247898 },
545         { 170967138, 248360 },
546         { 172264399, 248822 },
547         { 173568757, 249284 },
548         { 174880235, 249747 },
549         { 176198856, 250209 },
550         { 177524643, 250672 },
551         { 178857621, 251136 },
552         { 180197813, 251599 },
553         { 181545242, 252063 },
554         { 182899933, 252527 },
555         { 184261908, 252991 },
556         { 185631191, 253456 },
557         { 187007807, 253920 },
558         { 188391778, 254385 },
559         { 189783129, 254851 },
560         { 191181884, 255316 },
561         { 192588065, 255782 },
562         { 194001698, 256248 },
563         { 195422805, 256714 },
564         { 196851411, 257181 },
565         { 198287540, 257648 },
566         { 199731215, 258115 },
567         { 201182461, 258582 },
568         { 202641302, 259050 },
569         { 204107760, 259518 },
570         { 205581862, 259986 },
571         { 207063630, 260454 },
572         { 208553088, 260923 },
573         { 210050262, 261392 },
574         { 211555174, 261861 },
575         { 213067849, 262331 },
576         { 214588312, 262800 },
577         { 216116586, 263270 },
578         { 217652696, 263741 },
579         { 219196666, 264211 },
580         { 220748520, 264682 },
581         { 222308282, 265153 },
582         { 223875978, 265625 },
583         { 225451630, 266097 },
584         { 227035265, 266569 },
585         { 228626905, 267041 },
586         { 230226576, 267514 },
587         { 231834302, 267986 },
588         { 233450107, 268460 },
589         { 235074016, 268933 },
590         { 236706054, 269407 },
591         { 238346244, 269881 },
592         { 239994613, 270355 },
593         { 241651183, 270830 },
594         { 243315981, 271305 }
595 };
596
597 /* return largest index i such that fval <= lookup[i][small] */
598 static inline u32 tfrc_binsearch(u32 fval, u8 small)
599 {
600         u32 try, low = 0, high = TFRC_CALC_X_ARRSIZE - 1;
601
602         while (low < high) {
603                 try = (low + high) / 2;
604                 if (fval <= tfrc_calc_x_lookup[try][small])
605                         high = try;
606                 else
607                         low  = try + 1;
608         }
609         return high;
610 }
611
612 /**
613  * tfrc_calc_x - Calculate the send rate as per section 3.1 of RFC3448
614  *
615  *  @s: packet size          in bytes
616  *  @R: RTT                  scaled by 1000000   (i.e., microseconds)
617  *  @p: loss ratio estimate  scaled by 1000000
618  *  Returns X_calc           in bytes per second (not scaled).
619  *
620  * Note: DO NOT alter this code unless you run test cases against it,
621  *       as the code has been optimized to stop underflow/overflow.
622  */
623 u32 tfrc_calc_x(u16 s, u32 R, u32 p)
624 {
625         int index;
626         u32 f;
627         u64 tmp1, tmp2;
628
629         /* check against invalid parameters and divide-by-zero   */
630         BUG_ON(p >  1000000);           /* p must not exceed 100%   */
631         BUG_ON(p == 0);                 /* f(0) = 0, divide by zero */
632         if (R == 0) {                   /* possible  divide by zero */
633                 DCCP_CRIT("WARNING: RTT is 0, returning maximum X_calc.");
634                 return ~0U;
635         }
636
637         if (p <= TFRC_CALC_X_SPLIT)             {     /* 0.0000 < p <= 0.05   */
638                 if (p < TFRC_SMALLEST_P) {            /* 0.0000 < p <  0.0001 */
639                         DCCP_WARN("Value of p (%d) below resolution. "
640                                   "Substituting %d\n", p, TFRC_SMALLEST_P);
641                         index = 0;
642                 } else                                /* 0.0001 <= p <= 0.05  */
643                         index =  p/TFRC_SMALLEST_P - 1;
644
645                 f = tfrc_calc_x_lookup[index][1];
646
647         } else {                                      /* 0.05   <  p <= 1.00  */
648                 index = p/(1000000/TFRC_CALC_X_ARRSIZE) - 1;
649
650                 f = tfrc_calc_x_lookup[index][0];
651         }
652
653         /* The following computes X = s/(R*f(p)) in bytes per second. Since f(p)
654          * and R are both scaled by 1000000, we need to multiply by 1000000^2.
655          * ==> DO NOT alter this unless you test against overflow on 32 bit   */
656         tmp1 = ((u64)s * 100000000);
657         tmp2 = ((u64)R * (u64)f);
658         do_div(tmp2, 10000);
659         do_div(tmp1, tmp2); 
660
661         return (u32)tmp1; 
662 }
663
664 EXPORT_SYMBOL_GPL(tfrc_calc_x);
665
666 /*
667  *  tfrc_calc_x_reverse_lookup  -  try to find p given f(p)
668  *
669  *  @fvalue: function value to match, scaled by 1000000
670  *  Returns closest match for p, also scaled by 1000000
671  */
672 u32 tfrc_calc_x_reverse_lookup(u32 fvalue)
673 {
674         int index;
675
676         if (fvalue == 0)        /* f(p) = 0  whenever  p = 0 */
677                 return 0;
678
679         /* Error cases. */
680         if (fvalue < tfrc_calc_x_lookup[0][1]) {
681                 DCCP_WARN("fvalue %d smaller than resolution\n", fvalue);
682                 return tfrc_calc_x_lookup[0][1];
683         }
684         if (fvalue > tfrc_calc_x_lookup[TFRC_CALC_X_ARRSIZE - 1][0]) {
685                 DCCP_WARN("fvalue %d exceeds bounds!\n", fvalue);
686                 return 1000000;
687         }
688
689         if (fvalue <= tfrc_calc_x_lookup[TFRC_CALC_X_ARRSIZE - 1][1]) {
690                 index = tfrc_binsearch(fvalue, 1);
691                 return (index + 1) * TFRC_CALC_X_SPLIT / TFRC_CALC_X_ARRSIZE;
692         }
693  
694         /* else ... it must be in the coarse-grained column */
695         index = tfrc_binsearch(fvalue, 0);
696         return (index + 1) * 1000000 / TFRC_CALC_X_ARRSIZE;
697 }
698
699 EXPORT_SYMBOL_GPL(tfrc_calc_x_reverse_lookup);