libm: Provide fallback definitions for isnan() and isinf()
[libav.git] / libavutil / libm.h
CommitLineData
335ee1aa 1/*
2912e87a 2 * This file is part of Libav.
335ee1aa 3 *
2912e87a 4 * Libav is free software; you can redistribute it and/or
335ee1aa
MR
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 *
2912e87a 9 * Libav is distributed in the hope that it will be useful,
335ee1aa
MR
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
2912e87a 15 * License along with Libav; if not, write to the Free Software
335ee1aa
MR
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19/**
ba87f080 20 * @file
335ee1aa
MR
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"
297bfb2f 29#include "attributes.h"
46df708b 30#include "intfloat.h"
335ee1aa 31
15333562
MR
32#if !HAVE_CBRTF
33static av_always_inline float cbrtf(float x)
34{
35 return x < 0 ? -powf(-x, 1.0 / 3.0) : powf(x, 1.0 / 3.0);
36}
37#endif
38
335ee1aa
MR
39#if !HAVE_EXP2
40#undef exp2
41#define exp2(x) exp((x) * 0.693147180559945)
42#endif /* HAVE_EXP2 */
43
44#if !HAVE_EXP2F
45#undef exp2f
46#define exp2f(x) ((float)exp2(x))
47#endif /* HAVE_EXP2F */
48
46df708b
MS
49#if !HAVE_ISINF
50static av_always_inline av_const int isinf(float x)
51{
52 uint32_t v = av_float2int(x);
53 if ((v & 0x7f800000) != 0x7f800000)
54 return 0;
55 return !(v & 0x007fffff);
56}
57#endif /* HAVE_ISINF */
58
59#if !HAVE_ISNAN
60static av_always_inline av_const int isnan(float x)
61{
62 uint32_t v = av_float2int(x);
63 if ((v & 0x7f800000) != 0x7f800000)
64 return 0;
65 return v & 0x007fffff;
66}
67#endif /* HAVE_ISNAN */
68
335ee1aa
MR
69#if !HAVE_LLRINT
70#undef llrint
71#define llrint(x) ((long long)rint(x))
72#endif /* HAVE_LLRINT */
73
07876e25
MR
74#if !HAVE_LLRINTF
75#undef llrintf
76#define llrintf(x) ((long long)rint(x))
77#endif /* HAVE_LLRINT */
78
335ee1aa
MR
79#if !HAVE_LOG2
80#undef log2
81#define log2(x) (log(x) * 1.44269504088896340736)
82#endif /* HAVE_LOG2 */
83
84#if !HAVE_LOG2F
85#undef log2f
86#define log2f(x) ((float)log2(x))
87#endif /* HAVE_LOG2F */
88
89#if !HAVE_LRINT
90static av_always_inline av_const long int lrint(double x)
91{
92 return rint(x);
93}
94#endif /* HAVE_LRINT */
95
96#if !HAVE_LRINTF
97static av_always_inline av_const long int lrintf(float x)
98{
99 return (int)(rint(x));
100}
101#endif /* HAVE_LRINTF */
102
103#if !HAVE_ROUND
104static av_always_inline av_const double round(double x)
105{
106 return (x > 0) ? floor(x + 0.5) : ceil(x - 0.5);
107}
108#endif /* HAVE_ROUND */
109
110#if !HAVE_ROUNDF
111static av_always_inline av_const float roundf(float x)
112{
113 return (x > 0) ? floor(x + 0.5) : ceil(x - 0.5);
114}
115#endif /* HAVE_ROUNDF */
116
23d3931a
MS
117#if !HAVE_TRUNC
118static av_always_inline av_const double trunc(double x)
119{
120 return (x > 0) ? floor(x) : ceil(x);
121}
122#endif /* HAVE_TRUNC */
123
335ee1aa
MR
124#if !HAVE_TRUNCF
125static av_always_inline av_const float truncf(float x)
126{
127 return (x > 0) ? floor(x) : ceil(x);
128}
129#endif /* HAVE_TRUNCF */
130
131#endif /* AVUTIL_LIBM_H */