2 * This file is part of FFmpeg.
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.
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.
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
21 * Replacements for frequently missing libm functions
29 #include "attributes.h"
32 #if HAVE_MIPSFPU && HAVE_INLINE_ASM
33 #include "libavutil/mips/libm_mips.h"
34 #endif /* HAVE_MIPSFPU && HAVE_INLINE_ASM*/
38 #define atanf(x) ((float)atan(x))
43 #define atan2f(y, x) ((float)atan2(y, x))
48 #define powf(x, y) ((float)pow(x, y))
52 static av_always_inline double cbrt(double x)
54 return x < 0 ? -pow(-x, 1.0 / 3.0) : pow(x, 1.0 / 3.0);
59 static av_always_inline float cbrtf(float x)
61 return x < 0 ? -powf(-x, 1.0 / 3.0) : powf(x, 1.0 / 3.0);
67 #define cosf(x) ((float)cos(x))
72 #define expf(x) ((float)exp(x))
77 #define exp2(x) exp((x) * 0.693147180559945)
78 #endif /* HAVE_EXP2 */
82 #define exp2f(x) ((float)exp2(x))
83 #endif /* HAVE_EXP2F */
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)
92 uint32_t v = av_float2int(x);
93 if ((v & 0x7f800000) != 0x7f800000)
95 return !(v & 0x007fffff);
98 static av_always_inline av_const int avpriv_isinf(double x)
100 uint64_t v = av_double2int(x);
101 if ((v & 0x7ff0000000000000) != 0x7ff0000000000000)
103 return !(v & 0x000fffffffffffff);
107 (sizeof(x) == sizeof(float) \
110 #endif /* HAVE_ISINF */
113 static av_always_inline av_const int avpriv_isnanf(float x)
115 uint32_t v = av_float2int(x);
116 if ((v & 0x7f800000) != 0x7f800000)
118 return v & 0x007fffff;
121 static av_always_inline av_const int avpriv_isnan(double x)
123 uint64_t v = av_double2int(x);
124 if ((v & 0x7ff0000000000000) != 0x7ff0000000000000)
126 return (v & 0x000fffffffffffff) && 1;
130 (sizeof(x) == sizeof(float) \
133 #endif /* HAVE_ISNAN */
137 static inline av_const double hypot(double x, double y)
143 if (isinf(x) || isinf(y))
144 return av_int2double(0x7ff0000000000000);
145 if (x == 0 || y == 0)
154 return x*sqrt(1 + y*y);
156 #endif /* HAVE_HYPOT */
160 #define ldexpf(x, exp) ((float)ldexp(x, exp))
165 #define llrint(x) ((long long)rint(x))
166 #endif /* HAVE_LLRINT */
170 #define llrintf(x) ((long long)rint(x))
171 #endif /* HAVE_LLRINT */
175 #define log2(x) (log(x) * 1.44269504088896340736)
176 #endif /* HAVE_LOG2 */
180 #define log2f(x) ((float)log2(x))
181 #endif /* HAVE_LOG2F */
185 #define log10f(x) ((float)log10(x))
190 #define sinf(x) ((float)sin(x))
194 static inline double rint(double x)
196 return x >= 0 ? floor(x + 0.5) : ceil(x - 0.5);
198 #endif /* HAVE_RINT */
201 static av_always_inline av_const long int lrint(double x)
205 #endif /* HAVE_LRINT */
208 static av_always_inline av_const long int lrintf(float x)
210 return (int)(rint(x));
212 #endif /* HAVE_LRINTF */
215 static av_always_inline av_const double round(double x)
217 return (x > 0) ? floor(x + 0.5) : ceil(x - 0.5);
219 #endif /* HAVE_ROUND */
222 static av_always_inline av_const float roundf(float x)
224 return (x > 0) ? floor(x + 0.5) : ceil(x - 0.5);
226 #endif /* HAVE_ROUNDF */
229 static av_always_inline av_const double trunc(double x)
231 return (x > 0) ? floor(x) : ceil(x);
233 #endif /* HAVE_TRUNC */
236 static av_always_inline av_const float truncf(float x)
238 return (x > 0) ? floor(x) : ceil(x);
240 #endif /* HAVE_TRUNCF */
242 #endif /* AVUTIL_LIBM_H */