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