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