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