]> git.sesse.net Git - ffmpeg/blob - libavutil/libm.h
libm: provide fallback definition for cbrtf() using powf()
[ffmpeg] / libavutil / libm.h
1 /*
2  * This file is part of Libav.
3  *
4  * Libav is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * Libav is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with Libav; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18
19 /**
20  * @file
21  * Replacements for frequently missing libm functions
22  */
23
24 #ifndef AVUTIL_LIBM_H
25 #define AVUTIL_LIBM_H
26
27 #include <math.h>
28 #include "config.h"
29 #include "attributes.h"
30
31 #if !HAVE_CBRTF
32 static av_always_inline float cbrtf(float x)
33 {
34     return x < 0 ? -powf(-x, 1.0 / 3.0) : powf(x, 1.0 / 3.0);
35 }
36 #endif
37
38 #if !HAVE_EXP2
39 #undef exp2
40 #define exp2(x) exp((x) * 0.693147180559945)
41 #endif /* HAVE_EXP2 */
42
43 #if !HAVE_EXP2F
44 #undef exp2f
45 #define exp2f(x) ((float)exp2(x))
46 #endif /* HAVE_EXP2F */
47
48 #if !HAVE_LLRINT
49 #undef llrint
50 #define llrint(x) ((long long)rint(x))
51 #endif /* HAVE_LLRINT */
52
53 #if !HAVE_LLRINTF
54 #undef llrintf
55 #define llrintf(x) ((long long)rint(x))
56 #endif /* HAVE_LLRINT */
57
58 #if !HAVE_LOG2
59 #undef log2
60 #define log2(x) (log(x) * 1.44269504088896340736)
61 #endif /* HAVE_LOG2 */
62
63 #if !HAVE_LOG2F
64 #undef log2f
65 #define log2f(x) ((float)log2(x))
66 #endif /* HAVE_LOG2F */
67
68 #if !HAVE_LRINT
69 static av_always_inline av_const long int lrint(double x)
70 {
71     return rint(x);
72 }
73 #endif /* HAVE_LRINT */
74
75 #if !HAVE_LRINTF
76 static av_always_inline av_const long int lrintf(float x)
77 {
78     return (int)(rint(x));
79 }
80 #endif /* HAVE_LRINTF */
81
82 #if !HAVE_ROUND
83 static av_always_inline av_const double round(double x)
84 {
85     return (x > 0) ? floor(x + 0.5) : ceil(x - 0.5);
86 }
87 #endif /* HAVE_ROUND */
88
89 #if !HAVE_ROUNDF
90 static av_always_inline av_const float roundf(float x)
91 {
92     return (x > 0) ? floor(x + 0.5) : ceil(x - 0.5);
93 }
94 #endif /* HAVE_ROUNDF */
95
96 #if !HAVE_TRUNC
97 static av_always_inline av_const double trunc(double x)
98 {
99     return (x > 0) ? floor(x) : ceil(x);
100 }
101 #endif /* HAVE_TRUNC */
102
103 #if !HAVE_TRUNCF
104 static av_always_inline av_const float truncf(float x)
105 {
106     return (x > 0) ? floor(x) : ceil(x);
107 }
108 #endif /* HAVE_TRUNCF */
109
110 #endif /* AVUTIL_LIBM_H */