xref: /rk3399_ARM-atf/lib/compiler-rt/builtins/int_math.h (revision 8a6a9560b5dcccfb68064c0c8c9b4b47981c6ac7)
1*8a6a9560SDaniel Boulby //===-- int_math.h - internal math inlines --------------------------------===//
2*8a6a9560SDaniel Boulby //
3*8a6a9560SDaniel Boulby // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*8a6a9560SDaniel Boulby // See https://llvm.org/LICENSE.txt for license information.
5*8a6a9560SDaniel Boulby // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*8a6a9560SDaniel Boulby //
7*8a6a9560SDaniel Boulby //===----------------------------------------------------------------------===//
8*8a6a9560SDaniel Boulby //
9*8a6a9560SDaniel Boulby // This file is not part of the interface of this library.
10*8a6a9560SDaniel Boulby //
11*8a6a9560SDaniel Boulby // This file defines substitutes for the libm functions used in some of the
12*8a6a9560SDaniel Boulby // compiler-rt implementations, defined in such a way that there is not a direct
13*8a6a9560SDaniel Boulby // dependency on libm or math.h. Instead, we use the compiler builtin versions
14*8a6a9560SDaniel Boulby // where available. This reduces our dependencies on the system SDK by foisting
15*8a6a9560SDaniel Boulby // the responsibility onto the compiler.
16*8a6a9560SDaniel Boulby //
17*8a6a9560SDaniel Boulby //===----------------------------------------------------------------------===//
180e14a7fbSdp-arm 
190e14a7fbSdp-arm #ifndef INT_MATH_H
200e14a7fbSdp-arm #define INT_MATH_H
210e14a7fbSdp-arm 
220e14a7fbSdp-arm #ifndef __has_builtin
230e14a7fbSdp-arm #define __has_builtin(x) 0
240e14a7fbSdp-arm #endif
250e14a7fbSdp-arm 
260e14a7fbSdp-arm #if defined(_MSC_VER) && !defined(__clang__)
270e14a7fbSdp-arm #include <math.h>
280e14a7fbSdp-arm #include <stdlib.h>
290e14a7fbSdp-arm #endif
300e14a7fbSdp-arm 
310e14a7fbSdp-arm #if defined(_MSC_VER) && !defined(__clang__)
320e14a7fbSdp-arm #define CRT_INFINITY INFINITY
330e14a7fbSdp-arm #else
340e14a7fbSdp-arm #define CRT_INFINITY __builtin_huge_valf()
350e14a7fbSdp-arm #endif
360e14a7fbSdp-arm 
370e14a7fbSdp-arm #if defined(_MSC_VER) && !defined(__clang__)
380e14a7fbSdp-arm #define crt_isfinite(x) _finite((x))
390e14a7fbSdp-arm #define crt_isinf(x) !_finite((x))
400e14a7fbSdp-arm #define crt_isnan(x) _isnan((x))
410e14a7fbSdp-arm #else
42*8a6a9560SDaniel Boulby // Define crt_isfinite in terms of the builtin if available, otherwise provide
43*8a6a9560SDaniel Boulby // an alternate version in terms of our other functions. This supports some
44*8a6a9560SDaniel Boulby // versions of GCC which didn't have __builtin_isfinite.
450e14a7fbSdp-arm #if __has_builtin(__builtin_isfinite)
460e14a7fbSdp-arm #define crt_isfinite(x) __builtin_isfinite((x))
470e14a7fbSdp-arm #elif defined(__GNUC__)
480e14a7fbSdp-arm #define crt_isfinite(x)                                                        \
490e14a7fbSdp-arm   __extension__(({                                                             \
500e14a7fbSdp-arm     __typeof((x)) x_ = (x);                                                    \
510e14a7fbSdp-arm     !crt_isinf(x_) && !crt_isnan(x_);                                          \
520e14a7fbSdp-arm   }))
530e14a7fbSdp-arm #else
540e14a7fbSdp-arm #error "Do not know how to check for infinity"
55*8a6a9560SDaniel Boulby #endif // __has_builtin(__builtin_isfinite)
560e14a7fbSdp-arm #define crt_isinf(x) __builtin_isinf((x))
570e14a7fbSdp-arm #define crt_isnan(x) __builtin_isnan((x))
58*8a6a9560SDaniel Boulby #endif // _MSC_VER
590e14a7fbSdp-arm 
600e14a7fbSdp-arm #if defined(_MSC_VER) && !defined(__clang__)
610e14a7fbSdp-arm #define crt_copysign(x, y) copysign((x), (y))
620e14a7fbSdp-arm #define crt_copysignf(x, y) copysignf((x), (y))
630e14a7fbSdp-arm #define crt_copysignl(x, y) copysignl((x), (y))
640e14a7fbSdp-arm #else
650e14a7fbSdp-arm #define crt_copysign(x, y) __builtin_copysign((x), (y))
660e14a7fbSdp-arm #define crt_copysignf(x, y) __builtin_copysignf((x), (y))
670e14a7fbSdp-arm #define crt_copysignl(x, y) __builtin_copysignl((x), (y))
680e14a7fbSdp-arm #endif
690e14a7fbSdp-arm 
700e14a7fbSdp-arm #if defined(_MSC_VER) && !defined(__clang__)
710e14a7fbSdp-arm #define crt_fabs(x) fabs((x))
720e14a7fbSdp-arm #define crt_fabsf(x) fabsf((x))
730e14a7fbSdp-arm #define crt_fabsl(x) fabs((x))
740e14a7fbSdp-arm #else
750e14a7fbSdp-arm #define crt_fabs(x) __builtin_fabs((x))
760e14a7fbSdp-arm #define crt_fabsf(x) __builtin_fabsf((x))
770e14a7fbSdp-arm #define crt_fabsl(x) __builtin_fabsl((x))
780e14a7fbSdp-arm #endif
790e14a7fbSdp-arm 
800e14a7fbSdp-arm #if defined(_MSC_VER) && !defined(__clang__)
810e14a7fbSdp-arm #define crt_fmaxl(x, y) __max((x), (y))
820e14a7fbSdp-arm #else
830e14a7fbSdp-arm #define crt_fmaxl(x, y) __builtin_fmaxl((x), (y))
840e14a7fbSdp-arm #endif
850e14a7fbSdp-arm 
860e14a7fbSdp-arm #if defined(_MSC_VER) && !defined(__clang__)
870e14a7fbSdp-arm #define crt_logbl(x) logbl((x))
880e14a7fbSdp-arm #else
890e14a7fbSdp-arm #define crt_logbl(x) __builtin_logbl((x))
900e14a7fbSdp-arm #endif
910e14a7fbSdp-arm 
920e14a7fbSdp-arm #if defined(_MSC_VER) && !defined(__clang__)
930e14a7fbSdp-arm #define crt_scalbnl(x, y) scalbnl((x), (y))
940e14a7fbSdp-arm #else
950e14a7fbSdp-arm #define crt_scalbnl(x, y) __builtin_scalbnl((x), (y))
960e14a7fbSdp-arm #endif
970e14a7fbSdp-arm 
98*8a6a9560SDaniel Boulby #endif // INT_MATH_H
99