1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
7 #include "rte_approx.h"
10 * Based on paper "Approximating Rational Numbers by Fractions" by Michal
11 * Forisek forisek@dcs.fmph.uniba.sk
13 * Given a rational number alpha with 0 < alpha < 1 and a precision d, the goal
14 * is to find positive integers p, q such that alpha - d < p/q < alpha + d, and
17 * http://people.ksp.sk/~misof/publications/2007approx.pdf
20 /* fraction comparison: compare (a/b) and (c/d) */
21 static inline uint32_t
22 less(uint32_t a, uint32_t b, uint32_t c, uint32_t d)
27 static inline uint32_t
28 less_or_equal(uint32_t a, uint32_t b, uint32_t c, uint32_t d)
33 /* check whether a/b is a valid approximation */
34 static inline uint32_t
35 matches(uint32_t a, uint32_t b,
36 uint32_t alpha_num, uint32_t d_num, uint32_t denum)
38 if (less_or_equal(a, b, alpha_num - d_num, denum))
41 if (less(a ,b, alpha_num + d_num, denum))
48 find_exact_solution_left(uint32_t p_a, uint32_t q_a, uint32_t p_b, uint32_t q_b,
49 uint32_t alpha_num, uint32_t d_num, uint32_t denum, uint32_t *p, uint32_t *q)
51 uint32_t k_num = denum * p_b - (alpha_num + d_num) * q_b;
52 uint32_t k_denum = (alpha_num + d_num) * q_a - denum * p_a;
53 uint32_t k = (k_num / k_denum) + 1;
60 find_exact_solution_right(uint32_t p_a, uint32_t q_a, uint32_t p_b, uint32_t q_b,
61 uint32_t alpha_num, uint32_t d_num, uint32_t denum, uint32_t *p, uint32_t *q)
63 uint32_t k_num = - denum * p_b + (alpha_num - d_num) * q_b;
64 uint32_t k_denum = - (alpha_num - d_num) * q_a + denum * p_a;
65 uint32_t k = (k_num / k_denum) + 1;
72 find_best_rational_approximation(uint32_t alpha_num, uint32_t d_num, uint32_t denum, uint32_t *p, uint32_t *q)
74 uint32_t p_a, q_a, p_b, q_b;
76 /* check assumptions on the inputs */
77 if (!((0 < d_num) && (d_num < alpha_num) && (alpha_num < denum) && (d_num + alpha_num < denum))) {
81 /* set initial bounds for the search */
88 uint32_t new_p_a, new_q_a, new_p_b, new_q_b;
89 uint32_t x_num, x_denum, x;
92 /* compute the number of steps to the left */
93 x_num = denum * p_b - alpha_num * q_b;
94 x_denum = - denum * p_a + alpha_num * q_a;
95 x = (x_num + x_denum - 1) / x_denum; /* x = ceil(x_num / x_denum) */
97 /* check whether we have a valid approximation */
98 aa = matches(p_b + x * p_a, q_b + x * q_a, alpha_num, d_num, denum);
99 bb = matches(p_b + (x-1) * p_a, q_b + (x - 1) * q_a, alpha_num, d_num, denum);
101 find_exact_solution_left(p_a, q_a, p_b, q_b, alpha_num, d_num, denum, p, q);
105 /* update the interval */
106 new_p_a = p_b + (x - 1) * p_a ;
107 new_q_a = q_b + (x - 1) * q_a;
108 new_p_b = p_b + x * p_a ;
109 new_q_b = q_b + x * q_a;
116 /* compute the number of steps to the right */
117 x_num = alpha_num * q_b - denum * p_b;
118 x_denum = - alpha_num * q_a + denum * p_a;
119 x = (x_num + x_denum - 1) / x_denum; /* x = ceil(x_num / x_denum) */
121 /* check whether we have a valid approximation */
122 aa = matches(p_b + x * p_a, q_b + x * q_a, alpha_num, d_num, denum);
123 bb = matches(p_b + (x - 1) * p_a, q_b + (x - 1) * q_a, alpha_num, d_num, denum);
125 find_exact_solution_right(p_a, q_a, p_b, q_b, alpha_num, d_num, denum, p, q);
129 /* update the interval */
130 new_p_a = p_b + (x - 1) * p_a;
131 new_q_a = q_b + (x - 1) * q_a;
132 new_p_b = p_b + x * p_a;
133 new_q_b = q_b + x * q_a;
142 int rte_approx(double alpha, double d, uint32_t *p, uint32_t *q)
144 uint32_t alpha_num, d_num, denum;
146 /* Check input arguments */
147 if (!((0.0 < d) && (d < alpha) && (alpha < 1.0))) {
151 if ((p == NULL) || (q == NULL)) {
155 /* Compute alpha_num, d_num and denum */
162 alpha_num = (uint32_t) alpha;
163 d_num = (uint32_t) d;
165 /* Perform approximation */
166 return find_best_rational_approximation(alpha_num, d_num, denum, p, q);
169 /* fraction comparison (64 bit version): compare (a/b) and (c/d) */
170 static inline uint64_t
171 less_64(uint64_t a, uint64_t b, uint64_t c, uint64_t d)
176 static inline uint64_t
177 less_or_equal_64(uint64_t a, uint64_t b, uint64_t c, uint64_t d)
182 /* check whether a/b is a valid approximation (64 bit version) */
183 static inline uint64_t
184 matches_64(uint64_t a, uint64_t b,
185 uint64_t alpha_num, uint64_t d_num, uint64_t denum)
187 if (less_or_equal_64(a, b, alpha_num - d_num, denum))
190 if (less_64(a, b, alpha_num + d_num, denum))
197 find_exact_solution_left_64(uint64_t p_a, uint64_t q_a, uint64_t p_b, uint64_t q_b,
198 uint64_t alpha_num, uint64_t d_num, uint64_t denum, uint64_t *p, uint64_t *q)
200 uint64_t k_num = denum * p_b - (alpha_num + d_num) * q_b;
201 uint64_t k_denum = (alpha_num + d_num) * q_a - denum * p_a;
202 uint64_t k = (k_num / k_denum) + 1;
209 find_exact_solution_right_64(uint64_t p_a, uint64_t q_a, uint64_t p_b, uint64_t q_b,
210 uint64_t alpha_num, uint64_t d_num, uint64_t denum, uint64_t *p, uint64_t *q)
212 uint64_t k_num = -denum * p_b + (alpha_num - d_num) * q_b;
213 uint64_t k_denum = -(alpha_num - d_num) * q_a + denum * p_a;
214 uint64_t k = (k_num / k_denum) + 1;
221 find_best_rational_approximation_64(uint64_t alpha_num, uint64_t d_num,
222 uint64_t denum, uint64_t *p, uint64_t *q)
224 uint64_t p_a, q_a, p_b, q_b;
226 /* check assumptions on the inputs */
227 if (!((d_num > 0) && (d_num < alpha_num) &&
228 (alpha_num < denum) && (d_num + alpha_num < denum))) {
232 /* set initial bounds for the search */
239 uint64_t new_p_a, new_q_a, new_p_b, new_q_b;
240 uint64_t x_num, x_denum, x;
243 /* compute the number of steps to the left */
244 x_num = denum * p_b - alpha_num * q_b;
245 x_denum = -denum * p_a + alpha_num * q_a;
246 x = (x_num + x_denum - 1) / x_denum; /* x = ceil(x_num / x_denum) */
248 /* check whether we have a valid approximation */
249 aa = matches_64(p_b + x * p_a, q_b + x * q_a, alpha_num, d_num, denum);
250 bb = matches_64(p_b + (x-1) * p_a, q_b + (x - 1) * q_a,
251 alpha_num, d_num, denum);
253 find_exact_solution_left_64(p_a, q_a, p_b, q_b,
254 alpha_num, d_num, denum, p, q);
258 /* update the interval */
259 new_p_a = p_b + (x - 1) * p_a;
260 new_q_a = q_b + (x - 1) * q_a;
261 new_p_b = p_b + x * p_a;
262 new_q_b = q_b + x * q_a;
269 /* compute the number of steps to the right */
270 x_num = alpha_num * q_b - denum * p_b;
271 x_denum = -alpha_num * q_a + denum * p_a;
272 x = (x_num + x_denum - 1) / x_denum; /* x = ceil(x_num / x_denum) */
274 /* check whether we have a valid approximation */
275 aa = matches_64(p_b + x * p_a, q_b + x * q_a, alpha_num, d_num, denum);
276 bb = matches_64(p_b + (x - 1) * p_a, q_b + (x - 1) * q_a,
277 alpha_num, d_num, denum);
279 find_exact_solution_right_64(p_a, q_a, p_b, q_b,
280 alpha_num, d_num, denum, p, q);
284 /* update the interval */
285 new_p_a = p_b + (x - 1) * p_a;
286 new_q_a = q_b + (x - 1) * q_a;
287 new_p_b = p_b + x * p_a;
288 new_q_b = q_b + x * q_a;
297 int rte_approx_64(double alpha, double d, uint64_t *p, uint64_t *q)
299 uint64_t alpha_num, d_num, denum;
301 /* Check input arguments */
302 if (!((0.0 < d) && (d < alpha) && (alpha < 1.0)))
305 if ((p == NULL) || (q == NULL))
308 /* Compute alpha_num, d_num and denum */
315 alpha_num = (uint64_t) alpha;
316 d_num = (uint64_t) d;
318 /* Perform approximation */
319 return find_best_rational_approximation_64(alpha_num, d_num, denum, p, q);