FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
libm.h
Go to the documentation of this file.
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
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_FMINF
86 #undef fminf
87 static av_always_inline av_const float fminf(float x, float y)
88 {
89  //Note, the NaN special case is needed for C spec compliance, it should be
90  //optimized away if the users compiler is configured to assume no NaN
91  return x > y ? y : (x == x ? x : y);
92 }
93 #endif
94 
95 #if !HAVE_ISINF
96 static av_always_inline av_const int isinf(float x)
97 {
98  uint32_t v = av_float2int(x);
99  if ((v & 0x7f800000) != 0x7f800000)
100  return 0;
101  return !(v & 0x007fffff);
102 }
103 #endif /* HAVE_ISINF */
104 
105 #if !HAVE_ISNAN
106 static av_always_inline av_const int isnan(float x)
107 {
108  uint32_t v = av_float2int(x);
109  if ((v & 0x7f800000) != 0x7f800000)
110  return 0;
111  return v & 0x007fffff;
112 }
113 #endif /* HAVE_ISNAN */
114 
115 #if !HAVE_LDEXPF
116 #undef ldexpf
117 #define ldexpf(x, exp) ((float)ldexp(x, exp))
118 #endif
119 
120 #if !HAVE_LLRINT
121 #undef llrint
122 #define llrint(x) ((long long)rint(x))
123 #endif /* HAVE_LLRINT */
124 
125 #if !HAVE_LLRINTF
126 #undef llrintf
127 #define llrintf(x) ((long long)rint(x))
128 #endif /* HAVE_LLRINT */
129 
130 #if !HAVE_LOG2
131 #undef log2
132 #define log2(x) (log(x) * 1.44269504088896340736)
133 #endif /* HAVE_LOG2 */
134 
135 #if !HAVE_LOG2F
136 #undef log2f
137 #define log2f(x) ((float)log2(x))
138 #endif /* HAVE_LOG2F */
139 
140 #if !HAVE_LOG10F
141 #undef log10f
142 #define log10f(x) ((float)log10(x))
143 #endif
144 
145 #if !HAVE_SINF
146 #undef sinf
147 #define sinf(x) ((float)sin(x))
148 #endif
149 
150 #if !HAVE_RINT
151 static inline double rint(double x)
152 {
153  return x >= 0 ? floor(x + 0.5) : ceil(x - 0.5);
154 }
155 #endif /* HAVE_RINT */
156 
157 #if !HAVE_LRINT
158 static av_always_inline av_const long int lrint(double x)
159 {
160  return rint(x);
161 }
162 #endif /* HAVE_LRINT */
163 
164 #if !HAVE_LRINTF
165 static av_always_inline av_const long int lrintf(float x)
166 {
167  return (int)(rint(x));
168 }
169 #endif /* HAVE_LRINTF */
170 
171 #if !HAVE_ROUND
172 static av_always_inline av_const double round(double x)
173 {
174  return (x > 0) ? floor(x + 0.5) : ceil(x - 0.5);
175 }
176 #endif /* HAVE_ROUND */
177 
178 #if !HAVE_ROUNDF
179 static av_always_inline av_const float roundf(float x)
180 {
181  return (x > 0) ? floor(x + 0.5) : ceil(x - 0.5);
182 }
183 #endif /* HAVE_ROUNDF */
184 
185 #if !HAVE_TRUNC
186 static av_always_inline av_const double trunc(double x)
187 {
188  return (x > 0) ? floor(x) : ceil(x);
189 }
190 #endif /* HAVE_TRUNC */
191 
192 #if !HAVE_TRUNCF
193 static av_always_inline av_const float truncf(float x)
194 {
195  return (x > 0) ? floor(x) : ceil(x);
196 }
197 #endif /* HAVE_TRUNCF */
198 
199 #endif /* AVUTIL_LIBM_H */