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