int_math.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. #ifndef INT_MATH_H
  21. #define INT_MATH_H
  22. #ifndef __has_builtin
  23. # define __has_builtin(x) 0
  24. #endif
  25. #if defined(_MSC_VER) && !defined(__clang__)
  26. #include <math.h>
  27. #include <stdlib.h>
  28. #endif
  29. #if defined(_MSC_VER) && !defined(__clang__)
  30. #define CRT_INFINITY INFINITY
  31. #else
  32. #define CRT_INFINITY __builtin_huge_valf()
  33. #endif
  34. #if defined(_MSC_VER) && !defined(__clang__)
  35. #define crt_isfinite(x) _finite((x))
  36. #define crt_isinf(x) !_finite((x))
  37. #define crt_isnan(x) _isnan((x))
  38. #else
  39. /* Define crt_isfinite in terms of the builtin if available, otherwise provide
  40. * an alternate version in terms of our other functions. This supports some
  41. * versions of GCC which didn't have __builtin_isfinite.
  42. */
  43. #if __has_builtin(__builtin_isfinite)
  44. # define crt_isfinite(x) __builtin_isfinite((x))
  45. #elif defined(__GNUC__)
  46. # define crt_isfinite(x) \
  47. __extension__(({ \
  48. __typeof((x)) x_ = (x); \
  49. !crt_isinf(x_) && !crt_isnan(x_); \
  50. }))
  51. #else
  52. # error "Do not know how to check for infinity"
  53. #endif /* __has_builtin(__builtin_isfinite) */
  54. #define crt_isinf(x) __builtin_isinf((x))
  55. #define crt_isnan(x) __builtin_isnan((x))
  56. #endif /* _MSC_VER */
  57. #if defined(_MSC_VER) && !defined(__clang__)
  58. #define crt_copysign(x, y) copysign((x), (y))
  59. #define crt_copysignf(x, y) copysignf((x), (y))
  60. #define crt_copysignl(x, y) copysignl((x), (y))
  61. #else
  62. #define crt_copysign(x, y) __builtin_copysign((x), (y))
  63. #define crt_copysignf(x, y) __builtin_copysignf((x), (y))
  64. #define crt_copysignl(x, y) __builtin_copysignl((x), (y))
  65. #endif
  66. #if defined(_MSC_VER) && !defined(__clang__)
  67. #define crt_fabs(x) fabs((x))
  68. #define crt_fabsf(x) fabsf((x))
  69. #define crt_fabsl(x) fabs((x))
  70. #else
  71. #define crt_fabs(x) __builtin_fabs((x))
  72. #define crt_fabsf(x) __builtin_fabsf((x))
  73. #define crt_fabsl(x) __builtin_fabsl((x))
  74. #endif
  75. #if defined(_MSC_VER) && !defined(__clang__)
  76. #define crt_fmax(x, y) __max((x), (y))
  77. #define crt_fmaxf(x, y) __max((x), (y))
  78. #define crt_fmaxl(x, y) __max((x), (y))
  79. #else
  80. #define crt_fmax(x, y) __builtin_fmax((x), (y))
  81. #define crt_fmaxf(x, y) __builtin_fmaxf((x), (y))
  82. #define crt_fmaxl(x, y) __builtin_fmaxl((x), (y))
  83. #endif
  84. #if defined(_MSC_VER) && !defined(__clang__)
  85. #define crt_logb(x) logb((x))
  86. #define crt_logbf(x) logbf((x))
  87. #define crt_logbl(x) logbl((x))
  88. #else
  89. #define crt_logb(x) __builtin_logb((x))
  90. #define crt_logbf(x) __builtin_logbf((x))
  91. #define crt_logbl(x) __builtin_logbl((x))
  92. #endif
  93. #if defined(_MSC_VER) && !defined(__clang__)
  94. #define crt_scalbn(x, y) scalbn((x), (y))
  95. #define crt_scalbnf(x, y) scalbnf((x), (y))
  96. #define crt_scalbnl(x, y) scalbnl((x), (y))
  97. #else
  98. #define crt_scalbn(x, y) __builtin_scalbn((x), (y))
  99. #define crt_scalbnf(x, y) __builtin_scalbnf((x), (y))
  100. #define crt_scalbnl(x, y) __builtin_scalbnl((x), (y))
  101. #endif
  102. #endif /* INT_MATH_H */