FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ffmath.h
Go to the documentation of this file.
1 /*
2  * copyright (c) 2016 Ganesh Ajjanagadde <gajjanag@gmail.com>
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 /**
22  * @file
23  * internal math functions header
24  */
25 
26 #ifndef AVUTIL_FFMATH_H
27 #define AVUTIL_FFMATH_H
28 
29 #include "attributes.h"
30 #include "libm.h"
31 
32 /**
33  * Compute 10^x for floating point values. Note: this function is by no means
34  * "correctly rounded", and is meant as a fast, reasonably accurate approximation.
35  * For instance, maximum relative error for the double precision variant is
36  * ~ 1e-13 for very small and very large values.
37  * This is ~2x faster than GNU libm's approach, which is still off by 2ulp on
38  * some inputs.
39  * @param x exponent
40  * @return 10^x
41  */
42 static av_always_inline double ff_exp10(double x)
43 {
44  return exp2(M_LOG2_10 * x);
45 }
46 
47 static av_always_inline float ff_exp10f(float x)
48 {
49  return exp2f(M_LOG2_10 * x);
50 }
51 
52 /**
53  * Compute x^y for floating point x, y. Note: this function is faster than the
54  * libm variant due to mainly 2 reasons:
55  * 1. It does not handle any edge cases. In particular, this is only guaranteed
56  * to work correctly for x > 0.
57  * 2. It is not as accurate as a standard nearly "correctly rounded" libm variant.
58  * @param x base
59  * @param y exponent
60  * @return x^y
61  */
62 static av_always_inline float ff_fast_powf(float x, float y)
63 {
64  return expf(logf(x) * y);
65 }
66 
67 #endif /* AVUTIL_FFMATH_H */
static av_always_inline float ff_exp10f(float x)
Definition: ffmath.h:47
Macro definitions for various function/variable attributes.
#define expf(x)
Definition: libm.h:283
static av_always_inline double ff_exp10(double x)
Compute 10^x for floating point values.
Definition: ffmath.h:42
static av_always_inline float ff_fast_powf(float x, float y)
Compute x^y for floating point x, y.
Definition: ffmath.h:62
#define M_LOG2_10
Definition: mathematics.h:40
#define exp2f(x)
Definition: libm.h:293
Replacements for frequently missing libm functions.
#define exp2(x)
Definition: libm.h:288
#define av_always_inline
Definition: attributes.h:39