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