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