]> git.sesse.net Git - ffmpeg/blob - libavutil/libm.h
Merge commit 'f154ef1ae5b03f288dd8c025dab1884b4cb20c1a'
[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_CBRTF
37 static av_always_inline float cbrtf(float x)
38 {
39     return x < 0 ? -powf(-x, 1.0 / 3.0) : powf(x, 1.0 / 3.0);
40 }
41 #endif
42
43 #if !HAVE_EXP2
44 #undef exp2
45 #define exp2(x) exp((x) * 0.693147180559945)
46 #endif /* HAVE_EXP2 */
47
48 #if !HAVE_EXP2F
49 #undef exp2f
50 #define exp2f(x) ((float)exp2(x))
51 #endif /* HAVE_EXP2F */
52
53 #if !HAVE_ISINF
54 static av_always_inline av_const int isinf(float x)
55 {
56     uint32_t v = av_float2int(x);
57     if ((v & 0x7f800000) != 0x7f800000)
58         return 0;
59     return !(v & 0x007fffff);
60 }
61 #endif /* HAVE_ISINF */
62
63 #if !HAVE_ISNAN
64 static av_always_inline av_const int isnan(float x)
65 {
66     uint32_t v = av_float2int(x);
67     if ((v & 0x7f800000) != 0x7f800000)
68         return 0;
69     return v & 0x007fffff;
70 }
71 #endif /* HAVE_ISNAN */
72
73 #if !HAVE_LLRINT
74 #undef llrint
75 #define llrint(x) ((long long)rint(x))
76 #endif /* HAVE_LLRINT */
77
78 #if !HAVE_LLRINTF
79 #undef llrintf
80 #define llrintf(x) ((long long)rint(x))
81 #endif /* HAVE_LLRINT */
82
83 #if !HAVE_LOG2
84 #undef log2
85 #define log2(x) (log(x) * 1.44269504088896340736)
86 #endif /* HAVE_LOG2 */
87
88 #if !HAVE_LOG2F
89 #undef log2f
90 #define log2f(x) ((float)log2(x))
91 #endif /* HAVE_LOG2F */
92
93 #if !HAVE_RINT
94 static inline double rint(double x)
95 {
96     return x >= 0 ? floor(x + 0.5) : ceil(x - 0.5);
97 }
98 #endif /* HAVE_RINT */
99
100 #if !HAVE_LRINT
101 static av_always_inline av_const long int lrint(double x)
102 {
103     return rint(x);
104 }
105 #endif /* HAVE_LRINT */
106
107 #if !HAVE_LRINTF
108 static av_always_inline av_const long int lrintf(float x)
109 {
110     return (int)(rint(x));
111 }
112 #endif /* HAVE_LRINTF */
113
114 #if !HAVE_ROUND
115 static av_always_inline av_const double round(double x)
116 {
117     return (x > 0) ? floor(x + 0.5) : ceil(x - 0.5);
118 }
119 #endif /* HAVE_ROUND */
120
121 #if !HAVE_ROUNDF
122 static av_always_inline av_const float roundf(float x)
123 {
124     return (x > 0) ? floor(x + 0.5) : ceil(x - 0.5);
125 }
126 #endif /* HAVE_ROUNDF */
127
128 #if !HAVE_TRUNC
129 static av_always_inline av_const double trunc(double x)
130 {
131     return (x > 0) ? floor(x) : ceil(x);
132 }
133 #endif /* HAVE_TRUNC */
134
135 #if !HAVE_TRUNCF
136 static av_always_inline av_const float truncf(float x)
137 {
138     return (x > 0) ? floor(x) : ceil(x);
139 }
140 #endif /* HAVE_TRUNCF */
141
142 #endif /* AVUTIL_LIBM_H */