1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2010-2014 Intel Corporation 3 */ 4 5 #ifndef __INCLUDE_RTE_APPROX_H__ 6 #define __INCLUDE_RTE_APPROX_H__ 7 8 /** 9 * @file 10 * RTE Rational Approximation 11 * 12 * Given a rational number alpha with 0 < alpha < 1 and a precision d, the goal 13 * is to find positive integers p, q such that alpha - d < p/q < alpha + d, and 14 * q is minimal. 15 */ 16 17 #include <stdint.h> 18 19 #ifdef __cplusplus 20 extern "C" { 21 #endif 22 23 /** 24 * Find best rational approximation 25 * 26 * @param alpha 27 * Rational number to approximate 28 * @param d 29 * Precision for the rational approximation 30 * @param p 31 * Pointer to pre-allocated space where the numerator of the rational 32 * approximation will be stored when operation is successful 33 * @param q 34 * Pointer to pre-allocated space where the denominator of the rational 35 * approximation will be stored when operation is successful 36 * @return 37 * 0 upon success, error code otherwise 38 */ 39 int rte_approx(double alpha, double d, uint32_t *p, uint32_t *q); 40 41 /** 42 * Find best rational approximation (64 bit version) 43 * 44 * @param alpha 45 * Rational number to approximate 46 * @param d 47 * Precision for the rational approximation 48 * @param p 49 * Pointer to pre-allocated space where the numerator of the rational 50 * approximation will be stored when operation is successful 51 * @param q 52 * Pointer to pre-allocated space where the denominator of the rational 53 * approximation will be stored when operation is successful 54 * @return 55 * 0 upon success, error code otherwise 56 */ 57 int rte_approx_64(double alpha, double d, uint64_t *p, uint64_t *q); 58 59 #ifdef __cplusplus 60 } 61 #endif 62 63 #endif /* __INCLUDE_RTE_APPROX_H__ */ 64