Home | History | Annotate | Line # | 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 #elif defined(__lint__)
     58 #  define crt_isfinite(x) 0
     59 #else
     60 #  error "Do not know how to check for infinity"
     61 #endif /* __has_builtin(__builtin_isfinite) */
     62 #define crt_isinf(x) __builtin_isinf((x))
     63 #define crt_isnan(x) __builtin_isnan((x))
     64 #endif /* _MSC_VER */
     65 
     66 #if defined(_MSC_VER) && !defined(__clang__)
     67 #define crt_copysign(x, y) copysign((x), (y))
     68 #define crt_copysignf(x, y) copysignf((x), (y))
     69 #define crt_copysignl(x, y) copysignl((x), (y))
     70 #else
     71 #define crt_copysign(x, y) __builtin_copysign((x), (y))
     72 #define crt_copysignf(x, y) __builtin_copysignf((x), (y))
     73 #define crt_copysignl(x, y) __builtin_copysignl((x), (y))
     74 #endif
     75 
     76 #if defined(_MSC_VER) && !defined(__clang__)
     77 #define crt_fabs(x) fabs((x))
     78 #define crt_fabsf(x) fabsf((x))
     79 #define crt_fabsl(x) fabs((x))
     80 #else
     81 #define crt_fabs(x) __builtin_fabs((x))
     82 #define crt_fabsf(x) __builtin_fabsf((x))
     83 #define crt_fabsl(x) __builtin_fabsl((x))
     84 #endif
     85 
     86 #if defined(_MSC_VER) && !defined(__clang__)
     87 #define crt_fmax(x, y) __max((x), (y))
     88 #define crt_fmaxf(x, y) __max((x), (y))
     89 #define crt_fmaxl(x, y) __max((x), (y))
     90 #else
     91 #define crt_fmax(x, y) __builtin_fmax((x), (y))
     92 #define crt_fmaxf(x, y) __builtin_fmaxf((x), (y))
     93 #define crt_fmaxl(x, y) __builtin_fmaxl((x), (y))
     94 #endif
     95 
     96 #if defined(_MSC_VER) && !defined(__clang__)
     97 #define crt_logb(x) logb((x))
     98 #define crt_logbf(x) logbf((x))
     99 #define crt_logbl(x) logbl((x))
    100 #else
    101 #define crt_logb(x) __builtin_logb((x))
    102 #define crt_logbf(x) __builtin_logbf((x))
    103 #define crt_logbl(x) __builtin_logbl((x))
    104 #endif
    105 
    106 #if defined(_MSC_VER) && !defined(__clang__)
    107 #define crt_scalbn(x, y) scalbn((x), (y))
    108 #define crt_scalbnf(x, y) scalbnf((x), (y))
    109 #define crt_scalbnl(x, y) scalbnl((x), (y))
    110 #else
    111 #define crt_scalbn(x, y) __builtin_scalbn((x), (y))
    112 #define crt_scalbnf(x, y) __builtin_scalbnf((x), (y))
    113 #define crt_scalbnl(x, y) __builtin_scalbnl((x), (y))
    114 #endif
    115 
    116 #endif /* INT_MATH_H */
    117