]> git.sesse.net Git - ffmpeg/blob - libavutil/libm.h
Merge commit '5c7bf2dddee5bdfa247ff0d57cb8a37d19077f66'
[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_CBRTF
52 static av_always_inline float cbrtf(float x)
53 {
54     return x < 0 ? -powf(-x, 1.0 / 3.0) : powf(x, 1.0 / 3.0);
55 }
56 #endif
57
58 #if !HAVE_COSF
59 #undef cosf
60 #define cosf(x) ((float)cos(x))
61 #endif
62
63 #if !HAVE_EXPF
64 #undef expf
65 #define expf(x) ((float)exp(x))
66 #endif
67
68 #if !HAVE_EXP2
69 #undef exp2
70 #define exp2(x) exp((x) * 0.693147180559945)
71 #endif /* HAVE_EXP2 */
72
73 #if !HAVE_EXP2F
74 #undef exp2f
75 #define exp2f(x) ((float)exp2(x))
76 #endif /* HAVE_EXP2F */
77
78 #if !HAVE_ISINF
79 static av_always_inline av_const int isinf(float x)
80 {
81     uint32_t v = av_float2int(x);
82     if ((v & 0x7f800000) != 0x7f800000)
83         return 0;
84     return !(v & 0x007fffff);
85 }
86 #endif /* HAVE_ISINF */
87
88 #if !HAVE_ISNAN
89 static av_always_inline av_const int isnan(float x)
90 {
91     uint32_t v = av_float2int(x);
92     if ((v & 0x7f800000) != 0x7f800000)
93         return 0;
94     return v & 0x007fffff;
95 }
96 #endif /* HAVE_ISNAN */
97
98 #if !HAVE_LDEXPF
99 #undef ldexpf
100 #define ldexpf(x, exp) ((float)ldexp(x, exp))
101 #endif
102
103 #if !HAVE_LLRINT
104 #undef llrint
105 #define llrint(x) ((long long)rint(x))
106 #endif /* HAVE_LLRINT */
107
108 #if !HAVE_LLRINTF
109 #undef llrintf
110 #define llrintf(x) ((long long)rint(x))
111 #endif /* HAVE_LLRINT */
112
113 #if !HAVE_LOG2
114 #undef log2
115 #define log2(x) (log(x) * 1.44269504088896340736)
116 #endif /* HAVE_LOG2 */
117
118 #if !HAVE_LOG2F
119 #undef log2f
120 #define log2f(x) ((float)log2(x))
121 #endif /* HAVE_LOG2F */
122
123 #if !HAVE_LOG10F
124 #undef log10f
125 #define log10f(x) ((float)log10(x))
126 #endif
127
128 #if !HAVE_SINF
129 #undef sinf
130 #define sinf(x) ((float)sin(x))
131 #endif
132
133 #if !HAVE_RINT
134 static inline double rint(double x)
135 {
136     return x >= 0 ? floor(x + 0.5) : ceil(x - 0.5);
137 }
138 #endif /* HAVE_RINT */
139
140 #if !HAVE_LRINT
141 static av_always_inline av_const long int lrint(double x)
142 {
143     return rint(x);
144 }
145 #endif /* HAVE_LRINT */
146
147 #if !HAVE_LRINTF
148 static av_always_inline av_const long int lrintf(float x)
149 {
150     return (int)(rint(x));
151 }
152 #endif /* HAVE_LRINTF */
153
154 #if !HAVE_ROUND
155 static av_always_inline av_const double round(double x)
156 {
157     return (x > 0) ? floor(x + 0.5) : ceil(x - 0.5);
158 }
159 #endif /* HAVE_ROUND */
160
161 #if !HAVE_ROUNDF
162 static av_always_inline av_const float roundf(float x)
163 {
164     return (x > 0) ? floor(x + 0.5) : ceil(x - 0.5);
165 }
166 #endif /* HAVE_ROUNDF */
167
168 #if !HAVE_TRUNC
169 static av_always_inline av_const double trunc(double x)
170 {
171     return (x > 0) ? floor(x) : ceil(x);
172 }
173 #endif /* HAVE_TRUNC */
174
175 #if !HAVE_TRUNCF
176 static av_always_inline av_const float truncf(float x)
177 {
178     return (x > 0) ? floor(x) : ceil(x);
179 }
180 #endif /* HAVE_TRUNCF */
181
182 #endif /* AVUTIL_LIBM_H */