c05e2a7826b86f1ffd019922e071e544eb1340f6
[dpdk.git] / lib / librte_sched / rte_approx.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2013 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  * 
7  *   Redistribution and use in source and binary forms, with or without 
8  *   modification, are permitted provided that the following conditions 
9  *   are met:
10  * 
11  *     * Redistributions of source code must retain the above copyright 
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright 
14  *       notice, this list of conditions and the following disclaimer in 
15  *       the documentation and/or other materials provided with the 
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its 
18  *       contributors may be used to endorse or promote products derived 
19  *       from this software without specific prior written permission.
20  * 
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  * 
33  */
34
35 #include <stdlib.h>
36
37 #include "rte_approx.h"
38
39 /* 
40  * Based on paper "Approximating Rational Numbers by Fractions" by Michal 
41  * Forisek forisek@dcs.fmph.uniba.sk
42  *
43  * Given a rational number alpha with 0 < alpha < 1 and a precision d, the goal
44  * is to find positive integers p, q such that alpha - d < p/q < alpha + d, and
45  * q is minimal.
46  *
47  * http://people.ksp.sk/~misof/publications/2007approx.pdf
48  */
49
50 /* fraction comparison: compare (a/b) and (c/d) */
51 static inline uint32_t 
52 less(uint32_t a, uint32_t b, uint32_t c, uint32_t d)
53 {
54         return (a*d < b*c);
55 }
56
57 static inline uint32_t
58 less_or_equal(uint32_t a, uint32_t b, uint32_t c, uint32_t d)
59 {
60         return (a*d <= b*c);
61 }
62
63 /* check whether a/b is a valid approximation */
64 static inline uint32_t 
65 matches(uint32_t a, uint32_t b, 
66         uint32_t alpha_num, uint32_t d_num, uint32_t denum)
67 {
68         if (less_or_equal(a, b, alpha_num - d_num, denum))
69                 return 0;
70
71         if (less(a ,b, alpha_num + d_num, denum))
72                 return 1;
73         
74         return 0;
75 }
76
77 static inline void 
78 find_exact_solution_left(uint32_t p_a, uint32_t q_a, uint32_t p_b, uint32_t q_b, 
79         uint32_t alpha_num, uint32_t d_num, uint32_t denum, uint32_t *p, uint32_t *q)
80 {
81         uint32_t k_num = denum * p_b - (alpha_num + d_num) * q_b;
82         uint32_t k_denum = (alpha_num + d_num) * q_a - denum * p_a;
83         uint32_t k = (k_num / k_denum) + 1;
84         
85         *p = p_b + k * p_a;
86         *q = q_b + k * q_a;
87 }
88
89 static inline void
90 find_exact_solution_right(uint32_t p_a, uint32_t q_a, uint32_t p_b, uint32_t q_b,
91         uint32_t alpha_num, uint32_t d_num, uint32_t denum, uint32_t *p, uint32_t *q) 
92 {
93         uint32_t k_num = - denum * p_b + (alpha_num - d_num) * q_b;
94         uint32_t k_denum = - (alpha_num - d_num) * q_a + denum * p_a;
95         uint32_t k = (k_num / k_denum) + 1;
96         
97         *p = p_b + k * p_a;
98         *q = q_b + k * q_a;
99 }
100
101 static int 
102 find_best_rational_approximation(uint32_t alpha_num, uint32_t d_num, uint32_t denum, uint32_t *p, uint32_t *q)
103 {
104         uint32_t p_a, q_a, p_b, q_b;
105         
106         /* check assumptions on the inputs */
107         if (!((0 < d_num) && (d_num < alpha_num) && (alpha_num < denum) && (d_num + alpha_num < denum))) {
108                 return -1;
109         }
110         
111         /* set initial bounds for the search */
112         p_a = 0;
113         q_a = 1;
114         p_b = 1;
115         q_b = 1;
116
117         while (1) {
118                 uint32_t new_p_a, new_q_a, new_p_b, new_q_b;
119                 uint32_t x_num, x_denum, x;
120                 int aa, bb;
121                 
122                 /* compute the number of steps to the left */
123                 x_num = denum * p_b - alpha_num * q_b;
124                 x_denum = - denum * p_a + alpha_num * q_a;
125                 x = (x_num + x_denum - 1) / x_denum; /* x = ceil(x_num / x_denum) */
126                 
127                 /* check whether we have a valid approximation */
128                 aa = matches(p_b + x * p_a, q_b + x * q_a, alpha_num, d_num, denum);
129                 bb = matches(p_b + (x-1) * p_a, q_b + (x - 1) * q_a, alpha_num, d_num, denum);
130                 if (aa || bb) {
131                         find_exact_solution_left(p_a, q_a, p_b, q_b, alpha_num, d_num, denum, p, q);
132                         return 0;
133                 }
134                 
135                 /* update the interval */
136                 new_p_a = p_b + (x - 1) * p_a ;
137                 new_q_a = q_b + (x - 1) * q_a;
138                 new_p_b = p_b + x * p_a ;
139                 new_q_b = q_b + x * q_a;
140
141                 p_a = new_p_a ;
142                 q_a = new_q_a;
143                 p_b = new_p_b ;
144                 q_b = new_q_b;
145
146                 /* compute the number of steps to the right */
147                 x_num = alpha_num * q_b - denum * p_b;
148                 x_denum = - alpha_num * q_a + denum * p_a;
149                 x = (x_num + x_denum - 1) / x_denum; /* x = ceil(x_num / x_denum) */
150
151                 /* check whether we have a valid approximation */
152                 aa = matches(p_b + x * p_a, q_b + x * q_a, alpha_num, d_num, denum);
153                 bb = matches(p_b + (x - 1) * p_a, q_b + (x - 1) * q_a, alpha_num, d_num, denum);
154                 if (aa || bb) {
155                         find_exact_solution_right(p_a, q_a, p_b, q_b, alpha_num, d_num, denum, p, q);
156                         return 0;
157                  }
158                  
159                 /* update the interval */
160                 new_p_a = p_b + (x - 1) * p_a;
161                 new_q_a = q_b + (x - 1) * q_a;
162                 new_p_b = p_b + x * p_a;
163                 new_q_b = q_b + x * q_a;
164                 
165                 p_a = new_p_a;
166                 q_a = new_q_a;
167                 p_b = new_p_b;
168                 q_b = new_q_b;
169         }
170 }
171
172 int rte_approx(double alpha, double d, uint32_t *p, uint32_t *q)
173 {
174         uint32_t alpha_num, d_num, denum;
175         
176         /* Check input arguments */
177         if (!((0.0 < d) && (d < alpha) && (alpha < 1.0))) {
178                 return -1;
179         }
180         
181         if ((p == NULL) || (q == NULL)) {
182                 return -2;
183         }
184         
185         /* Compute alpha_num, d_num and denum */
186         denum = 1;
187         while (d < 1) {
188                 alpha *= 10;
189                 d *= 10;
190                 denum *= 10;
191         }
192         alpha_num = (uint32_t) alpha;
193         d_num = (uint32_t) d;
194         
195         /* Perform approximation */
196         return find_best_rational_approximation(alpha_num, d_num, denum, p, q); 
197 }