Daniel Boulby | 318e7a5 | 2022-10-21 20:20:52 +0100 | [diff] [blame] | 1 | //===-- int_math.h - internal math inlines --------------------------------===// |
| 2 | // |
| 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 |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file is not part of the interface of this library. |
| 10 | // |
| 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. |
| 16 | // |
| 17 | //===----------------------------------------------------------------------===// |
dp-arm | 75b6057 | 2017-05-04 12:12:06 +0100 | [diff] [blame] | 18 | |
| 19 | #ifndef INT_MATH_H |
| 20 | #define INT_MATH_H |
| 21 | |
| 22 | #ifndef __has_builtin |
Daniel Boulby | 318e7a5 | 2022-10-21 20:20:52 +0100 | [diff] [blame] | 23 | #define __has_builtin(x) 0 |
dp-arm | 75b6057 | 2017-05-04 12:12:06 +0100 | [diff] [blame] | 24 | #endif |
| 25 | |
| 26 | #if defined(_MSC_VER) && !defined(__clang__) |
| 27 | #include <math.h> |
| 28 | #include <stdlib.h> |
dp-arm | 75b6057 | 2017-05-04 12:12:06 +0100 | [diff] [blame] | 29 | #endif |
| 30 | |
| 31 | #if defined(_MSC_VER) && !defined(__clang__) |
| 32 | #define CRT_INFINITY INFINITY |
| 33 | #else |
| 34 | #define CRT_INFINITY __builtin_huge_valf() |
| 35 | #endif |
| 36 | |
| 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)) |
| 41 | #else |
Daniel Boulby | 318e7a5 | 2022-10-21 20:20:52 +0100 | [diff] [blame] | 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. |
dp-arm | 75b6057 | 2017-05-04 12:12:06 +0100 | [diff] [blame] | 45 | #if __has_builtin(__builtin_isfinite) |
Daniel Boulby | 318e7a5 | 2022-10-21 20:20:52 +0100 | [diff] [blame] | 46 | #define crt_isfinite(x) __builtin_isfinite((x)) |
dp-arm | 75b6057 | 2017-05-04 12:12:06 +0100 | [diff] [blame] | 47 | #elif defined(__GNUC__) |
Daniel Boulby | 318e7a5 | 2022-10-21 20:20:52 +0100 | [diff] [blame] | 48 | #define crt_isfinite(x) \ |
| 49 | __extension__(({ \ |
| 50 | __typeof((x)) x_ = (x); \ |
| 51 | !crt_isinf(x_) && !crt_isnan(x_); \ |
| 52 | })) |
dp-arm | 75b6057 | 2017-05-04 12:12:06 +0100 | [diff] [blame] | 53 | #else |
Daniel Boulby | 318e7a5 | 2022-10-21 20:20:52 +0100 | [diff] [blame] | 54 | #error "Do not know how to check for infinity" |
| 55 | #endif // __has_builtin(__builtin_isfinite) |
dp-arm | 75b6057 | 2017-05-04 12:12:06 +0100 | [diff] [blame] | 56 | #define crt_isinf(x) __builtin_isinf((x)) |
| 57 | #define crt_isnan(x) __builtin_isnan((x)) |
Daniel Boulby | 318e7a5 | 2022-10-21 20:20:52 +0100 | [diff] [blame] | 58 | #endif // _MSC_VER |
dp-arm | 75b6057 | 2017-05-04 12:12:06 +0100 | [diff] [blame] | 59 | |
| 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)) |
| 64 | #else |
| 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)) |
Manish Pandey | 9320bd5 | 2023-11-06 16:45:14 +0000 | [diff] [blame^] | 68 | #if __has_builtin(__builtin_copysignf128) |
| 69 | #define crt_copysignf128(x, y) __builtin_copysignf128((x), (y)) |
| 70 | #elif __has_builtin(__builtin_copysignq) || (defined(__GNUC__) && __GNUC__ >= 7) |
| 71 | #define crt_copysignf128(x, y) __builtin_copysignq((x), (y)) |
| 72 | #endif |
dp-arm | 75b6057 | 2017-05-04 12:12:06 +0100 | [diff] [blame] | 73 | #endif |
| 74 | |
| 75 | #if defined(_MSC_VER) && !defined(__clang__) |
| 76 | #define crt_fabs(x) fabs((x)) |
| 77 | #define crt_fabsf(x) fabsf((x)) |
| 78 | #define crt_fabsl(x) fabs((x)) |
| 79 | #else |
| 80 | #define crt_fabs(x) __builtin_fabs((x)) |
| 81 | #define crt_fabsf(x) __builtin_fabsf((x)) |
| 82 | #define crt_fabsl(x) __builtin_fabsl((x)) |
Manish Pandey | 9320bd5 | 2023-11-06 16:45:14 +0000 | [diff] [blame^] | 83 | #if __has_builtin(__builtin_fabsf128) |
| 84 | #define crt_fabsf128(x) __builtin_fabsf128((x)) |
| 85 | #elif __has_builtin(__builtin_fabsq) || (defined(__GNUC__) && __GNUC__ >= 7) |
| 86 | #define crt_fabsf128(x) __builtin_fabsq((x)) |
| 87 | #endif |
dp-arm | 75b6057 | 2017-05-04 12:12:06 +0100 | [diff] [blame] | 88 | #endif |
| 89 | |
| 90 | #if defined(_MSC_VER) && !defined(__clang__) |
dp-arm | 75b6057 | 2017-05-04 12:12:06 +0100 | [diff] [blame] | 91 | #define crt_fmaxl(x, y) __max((x), (y)) |
| 92 | #else |
dp-arm | 75b6057 | 2017-05-04 12:12:06 +0100 | [diff] [blame] | 93 | #define crt_fmaxl(x, y) __builtin_fmaxl((x), (y)) |
| 94 | #endif |
| 95 | |
| 96 | #if defined(_MSC_VER) && !defined(__clang__) |
dp-arm | 75b6057 | 2017-05-04 12:12:06 +0100 | [diff] [blame] | 97 | #define crt_logbl(x) logbl((x)) |
| 98 | #else |
dp-arm | 75b6057 | 2017-05-04 12:12:06 +0100 | [diff] [blame] | 99 | #define crt_logbl(x) __builtin_logbl((x)) |
| 100 | #endif |
| 101 | |
| 102 | #if defined(_MSC_VER) && !defined(__clang__) |
dp-arm | 75b6057 | 2017-05-04 12:12:06 +0100 | [diff] [blame] | 103 | #define crt_scalbnl(x, y) scalbnl((x), (y)) |
| 104 | #else |
dp-arm | 75b6057 | 2017-05-04 12:12:06 +0100 | [diff] [blame] | 105 | #define crt_scalbnl(x, y) __builtin_scalbnl((x), (y)) |
| 106 | #endif |
| 107 | |
Daniel Boulby | 318e7a5 | 2022-10-21 20:20:52 +0100 | [diff] [blame] | 108 | #endif // INT_MATH_H |