1 //===-- int_math.h - internal math inlines --------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file is not part of the interface of this library.
11 // This file defines substitutes for the libm functions used in some of the
12 // compiler-rt implementations, defined in such a way that there is not a direct
13 // dependency on libm or math.h. Instead, we use the compiler builtin versions
14 // where available. This reduces our dependencies on the system SDK by foisting
15 // the responsibility onto the compiler.
17 //===----------------------------------------------------------------------===//
23 #define __has_builtin(x) 0
26 #if defined(_MSC_VER) && !defined(__clang__)
31 #if defined(_MSC_VER) && !defined(__clang__)
32 #define CRT_INFINITY INFINITY
34 #define CRT_INFINITY __builtin_huge_valf()
37 #if defined(_MSC_VER) && !defined(__clang__)
38 #define crt_isfinite(x) _finite((x))
39 #define crt_isinf(x) !_finite((x))
40 #define crt_isnan(x) _isnan((x))
42 // Define crt_isfinite in terms of the builtin if available, otherwise provide
43 // an alternate version in terms of our other functions. This supports some
44 // versions of GCC which didn't have __builtin_isfinite.
45 #if __has_builtin(__builtin_isfinite)
46 #define crt_isfinite(x) __builtin_isfinite((x))
47 #elif defined(__GNUC__)
48 #define crt_isfinite(x) \
50 __typeof((x)) x_ = (x); \
51 !crt_isinf(x_) && !crt_isnan(x_); \
54 #error "Do not know how to check for infinity"
55 #endif // __has_builtin(__builtin_isfinite)
56 #define crt_isinf(x) __builtin_isinf((x))
57 #define crt_isnan(x) __builtin_isnan((x))
60 #if defined(_MSC_VER) && !defined(__clang__)
61 #define crt_copysign(x, y) copysign((x), (y))
62 #define crt_copysignf(x, y) copysignf((x), (y))
63 #define crt_copysignl(x, y) copysignl((x), (y))
65 #define crt_copysign(x, y) __builtin_copysign((x), (y))
66 #define crt_copysignf(x, y) __builtin_copysignf((x), (y))
67 #define crt_copysignl(x, y) __builtin_copysignl((x), (y))
70 #if defined(_MSC_VER) && !defined(__clang__)
71 #define crt_fabs(x) fabs((x))
72 #define crt_fabsf(x) fabsf((x))
73 #define crt_fabsl(x) fabs((x))
75 #define crt_fabs(x) __builtin_fabs((x))
76 #define crt_fabsf(x) __builtin_fabsf((x))
77 #define crt_fabsl(x) __builtin_fabsl((x))
80 #if defined(_MSC_VER) && !defined(__clang__)
81 #define crt_fmaxl(x, y) __max((x), (y))
83 #define crt_fmaxl(x, y) __builtin_fmaxl((x), (y))
86 #if defined(_MSC_VER) && !defined(__clang__)
87 #define crt_logbl(x) logbl((x))
89 #define crt_logbl(x) __builtin_logbl((x))
92 #if defined(_MSC_VER) && !defined(__clang__)
93 #define crt_scalbnl(x, y) scalbnl((x), (y))
95 #define crt_scalbnl(x, y) __builtin_scalbnl((x), (y))