]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/builtins/int_math.h
Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / builtins / int_math.h
1 /* ===-- int_math.h - internal math inlines ---------------------------------===
2  *
3  *                     The LLVM Compiler Infrastructure
4  *
5  * This file is dual licensed under the MIT and the University of Illinois Open
6  * Source Licenses. See LICENSE.TXT for details.
7  *
8  * ===-----------------------------------------------------------------------===
9  *
10  * This file is not part of the interface of this library.
11  *
12  * This file defines substitutes for the libm functions used in some of the
13  * compiler-rt implementations, defined in such a way that there is not a direct
14  * dependency on libm or math.h. Instead, we use the compiler builtin versions
15  * where available. This reduces our dependencies on the system SDK by foisting
16  * the responsibility onto the compiler.
17  *
18  * ===-----------------------------------------------------------------------===
19  */
20
21 #ifndef INT_MATH_H
22 #define INT_MATH_H
23
24 #ifndef __has_builtin
25 #  define  __has_builtin(x) 0
26 #endif
27
28 #if defined(_MSC_VER) && !defined(__clang__)
29 #include <math.h>
30 #include <stdlib.h>
31 #include <ymath.h>
32 #endif
33
34 #if defined(_MSC_VER) && !defined(__clang__)
35 #define CRT_INFINITY INFINITY
36 #else
37 #define CRT_INFINITY __builtin_huge_valf()
38 #endif
39
40 #if defined(_MSC_VER) && !defined(__clang__)
41 #define crt_isfinite(x) _finite((x))
42 #define crt_isinf(x) !_finite((x))
43 #define crt_isnan(x) _isnan((x))
44 #else
45 /* Define crt_isfinite in terms of the builtin if available, otherwise provide
46  * an alternate version in terms of our other functions. This supports some
47  * versions of GCC which didn't have __builtin_isfinite.
48  */
49 #if __has_builtin(__builtin_isfinite)
50 #  define crt_isfinite(x) __builtin_isfinite((x))
51 #elif defined(__GNUC__)
52 #  define crt_isfinite(x) \
53   __extension__(({ \
54       __typeof((x)) x_ = (x); \
55       !crt_isinf(x_) && !crt_isnan(x_); \
56     }))
57 #else
58 #  error "Do not know how to check for infinity"
59 #endif /* __has_builtin(__builtin_isfinite) */
60 #define crt_isinf(x) __builtin_isinf((x))
61 #define crt_isnan(x) __builtin_isnan((x))
62 #endif /* _MSC_VER */
63
64 #if defined(_MSC_VER) && !defined(__clang__)
65 #define crt_copysign(x, y) copysign((x), (y))
66 #define crt_copysignf(x, y) copysignf((x), (y))
67 #define crt_copysignl(x, y) copysignl((x), (y))
68 #else
69 #define crt_copysign(x, y) __builtin_copysign((x), (y))
70 #define crt_copysignf(x, y) __builtin_copysignf((x), (y))
71 #define crt_copysignl(x, y) __builtin_copysignl((x), (y))
72 #endif
73
74 #if defined(_MSC_VER) && !defined(__clang__)
75 #define crt_fabs(x) fabs((x))
76 #define crt_fabsf(x) fabsf((x))
77 #define crt_fabsl(x) fabs((x))
78 #else
79 #define crt_fabs(x) __builtin_fabs((x))
80 #define crt_fabsf(x) __builtin_fabsf((x))
81 #define crt_fabsl(x) __builtin_fabsl((x))
82 #endif
83
84 #if defined(_MSC_VER) && !defined(__clang__)
85 #define crt_fmax(x, y) __max((x), (y))
86 #define crt_fmaxf(x, y) __max((x), (y))
87 #define crt_fmaxl(x, y) __max((x), (y))
88 #else
89 #define crt_fmax(x, y) __builtin_fmax((x), (y))
90 #define crt_fmaxf(x, y) __builtin_fmaxf((x), (y))
91 #define crt_fmaxl(x, y) __builtin_fmaxl((x), (y))
92 #endif
93
94 #if defined(_MSC_VER) && !defined(__clang__)
95 #define crt_logbl(x) logbl((x))
96 #else
97 #define crt_logbl(x) __builtin_logbl((x))
98 #endif
99
100 #if defined(_MSC_VER) && !defined(__clang__)
101 #define crt_scalbn(x, y) scalbn((x), (y))
102 #define crt_scalbnf(x, y) scalbnf((x), (y))
103 #define crt_scalbnl(x, y) scalbnl((x), (y))
104 #else
105 #define crt_scalbn(x, y) __builtin_scalbn((x), (y))
106 #define crt_scalbnf(x, y) __builtin_scalbnf((x), (y))
107 #define crt_scalbnl(x, y) __builtin_scalbnl((x), (y))
108 #endif
109
110 #endif /* INT_MATH_H */