int_math.h 3.7 KB

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