]> git.sesse.net Git - ffmpeg/blob - libavutil/libm.h
Merge commit '11c9bd633f635f07a762be1ecd672de55daf4edc'
[ffmpeg] / libavutil / libm.h
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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 #include "intfloat.h"
31
32 #if HAVE_MIPSFPU && HAVE_INLINE_ASM
33 #include "libavutil/mips/libm_mips.h"
34 #endif /* HAVE_MIPSFPU && HAVE_INLINE_ASM*/
35
36 #if !HAVE_ATANF
37 #undef atanf
38 #define atanf(x) ((float)atan(x))
39 #endif
40
41 #if !HAVE_ATAN2F
42 #undef atan2f
43 #define atan2f(y, x) ((float)atan2(y, x))
44 #endif
45
46 #if !HAVE_POWF
47 #undef powf
48 #define powf(x, y) ((float)pow(x, y))
49 #endif
50
51 #if !HAVE_CBRT
52 static av_always_inline double cbrt(double x)
53 {
54     return x < 0 ? -pow(-x, 1.0 / 3.0) : pow(x, 1.0 / 3.0);
55 }
56 #endif
57
58 #if !HAVE_CBRTF
59 static av_always_inline float cbrtf(float x)
60 {
61     return x < 0 ? -powf(-x, 1.0 / 3.0) : powf(x, 1.0 / 3.0);
62 }
63 #endif
64
65 #if !HAVE_COSF
66 #undef cosf
67 #define cosf(x) ((float)cos(x))
68 #endif
69
70 #if !HAVE_EXPF
71 #undef expf
72 #define expf(x) ((float)exp(x))
73 #endif
74
75 #if !HAVE_EXP2
76 #undef exp2
77 #define exp2(x) exp((x) * 0.693147180559945)
78 #endif /* HAVE_EXP2 */
79
80 #if !HAVE_EXP2F
81 #undef exp2f
82 #define exp2f(x) ((float)exp2(x))
83 #endif /* HAVE_EXP2F */
84
85 #if !HAVE_ISINF
86 #undef isinf
87 /* Note: these do not follow the BSD/Apple/GNU convention of returning -1 for
88 -Inf, +1 for Inf, 0 otherwise, but merely follow the POSIX/ISO mandated spec of
89 returning a non-zero value for +/-Inf, 0 otherwise. */
90 static av_always_inline av_const int avpriv_isinff(float x)
91 {
92     uint32_t v = av_float2int(x);
93     if ((v & 0x7f800000) != 0x7f800000)
94         return 0;
95     return !(v & 0x007fffff);
96 }
97
98 static av_always_inline av_const int avpriv_isinf(double x)
99 {
100     uint64_t v = av_double2int(x);
101     if ((v & 0x7ff0000000000000) != 0x7ff0000000000000)
102         return 0;
103     return !(v & 0x000fffffffffffff);
104 }
105
106 #define isinf(x)                  \
107     (sizeof(x) == sizeof(float)   \
108         ? avpriv_isinff(x)        \
109         : avpriv_isinf(x))
110 #endif /* HAVE_ISINF */
111
112 #if !HAVE_ISNAN
113 static av_always_inline av_const int avpriv_isnanf(float x)
114 {
115     uint32_t v = av_float2int(x);
116     if ((v & 0x7f800000) != 0x7f800000)
117         return 0;
118     return v & 0x007fffff;
119 }
120
121 static av_always_inline av_const int avpriv_isnan(double x)
122 {
123     uint64_t v = av_double2int(x);
124     if ((v & 0x7ff0000000000000) != 0x7ff0000000000000)
125         return 0;
126     return (v & 0x000fffffffffffff) && 1;
127 }
128
129 #define isnan(x)                  \
130     (sizeof(x) == sizeof(float)   \
131         ? avpriv_isnanf(x)        \
132         : avpriv_isnan(x))
133 #endif /* HAVE_ISNAN */
134
135 #if !HAVE_HYPOT
136 #undef hypot
137 static inline av_const double hypot(double x, double y)
138 {
139     double ret, temp;
140     x = fabs(x);
141     y = fabs(y);
142
143     if (isinf(x) || isinf(y))
144         return av_int2double(0x7ff0000000000000);
145     if (x == 0 || y == 0)
146         return x + y;
147     if (x < y) {
148         temp = x;
149         x = y;
150         y = temp;
151     }
152
153     y = y/x;
154     return x*sqrt(1 + y*y);
155 }
156 #endif /* HAVE_HYPOT */
157
158 #if !HAVE_LDEXPF
159 #undef ldexpf
160 #define ldexpf(x, exp) ((float)ldexp(x, exp))
161 #endif
162
163 #if !HAVE_LLRINT
164 #undef llrint
165 #define llrint(x) ((long long)rint(x))
166 #endif /* HAVE_LLRINT */
167
168 #if !HAVE_LLRINTF
169 #undef llrintf
170 #define llrintf(x) ((long long)rint(x))
171 #endif /* HAVE_LLRINT */
172
173 #if !HAVE_LOG2
174 #undef log2
175 #define log2(x) (log(x) * 1.44269504088896340736)
176 #endif /* HAVE_LOG2 */
177
178 #if !HAVE_LOG2F
179 #undef log2f
180 #define log2f(x) ((float)log2(x))
181 #endif /* HAVE_LOG2F */
182
183 #if !HAVE_LOG10F
184 #undef log10f
185 #define log10f(x) ((float)log10(x))
186 #endif
187
188 #if !HAVE_SINF
189 #undef sinf
190 #define sinf(x) ((float)sin(x))
191 #endif
192
193 #if !HAVE_RINT
194 static inline double rint(double x)
195 {
196     return x >= 0 ? floor(x + 0.5) : ceil(x - 0.5);
197 }
198 #endif /* HAVE_RINT */
199
200 #if !HAVE_LRINT
201 static av_always_inline av_const long int lrint(double x)
202 {
203     return rint(x);
204 }
205 #endif /* HAVE_LRINT */
206
207 #if !HAVE_LRINTF
208 static av_always_inline av_const long int lrintf(float x)
209 {
210     return (int)(rint(x));
211 }
212 #endif /* HAVE_LRINTF */
213
214 #if !HAVE_ROUND
215 static av_always_inline av_const double round(double x)
216 {
217     return (x > 0) ? floor(x + 0.5) : ceil(x - 0.5);
218 }
219 #endif /* HAVE_ROUND */
220
221 #if !HAVE_ROUNDF
222 static av_always_inline av_const float roundf(float x)
223 {
224     return (x > 0) ? floor(x + 0.5) : ceil(x - 0.5);
225 }
226 #endif /* HAVE_ROUNDF */
227
228 #if !HAVE_TRUNC
229 static av_always_inline av_const double trunc(double x)
230 {
231     return (x > 0) ? floor(x) : ceil(x);
232 }
233 #endif /* HAVE_TRUNC */
234
235 #if !HAVE_TRUNCF
236 static av_always_inline av_const float truncf(float x)
237 {
238     return (x > 0) ? floor(x) : ceil(x);
239 }
240 #endif /* HAVE_TRUNCF */
241
242 #endif /* AVUTIL_LIBM_H */