int_lib.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* ===-- int_lib.h - configuration header for compiler-rt -----------------===
  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 a configuration header for compiler-rt.
  11. * This file is not part of the interface of this library.
  12. *
  13. * ===----------------------------------------------------------------------===
  14. */
  15. #ifndef INT_LIB_H
  16. #define INT_LIB_H
  17. /* Assumption: Signed integral is 2's complement. */
  18. /* Assumption: Right shift of signed negative is arithmetic shift. */
  19. /* Assumption: Endianness is little or big (not mixed). */
  20. #if defined(__ELF__)
  21. #define FNALIAS(alias_name, original_name) \
  22. void alias_name() __attribute__((alias(#original_name)))
  23. #else
  24. #define FNALIAS(alias, name) _Pragma("GCC error(\"alias unsupported on this file format\")")
  25. #endif
  26. /* ABI macro definitions */
  27. #if __ARM_EABI__
  28. # define ARM_EABI_FNALIAS(aeabi_name, name) \
  29. void __aeabi_##aeabi_name() __attribute__((alias("__" #name)));
  30. # define COMPILER_RT_ABI __attribute__((pcs("aapcs")))
  31. #else
  32. # define ARM_EABI_FNALIAS(aeabi_name, name)
  33. # if defined(__arm__) && defined(_WIN32) && (!defined(_MSC_VER) || defined(__clang__))
  34. # define COMPILER_RT_ABI __attribute__((pcs("aapcs")))
  35. # else
  36. # define COMPILER_RT_ABI
  37. # endif
  38. #endif
  39. #ifdef _MSC_VER
  40. #define ALWAYS_INLINE __forceinline
  41. #define NOINLINE __declspec(noinline)
  42. #define NORETURN __declspec(noreturn)
  43. #define UNUSED
  44. #else
  45. #define ALWAYS_INLINE __attribute__((always_inline))
  46. #define NOINLINE __attribute__((noinline))
  47. #define NORETURN __attribute__((noreturn))
  48. #define UNUSED __attribute__((unused))
  49. #endif
  50. #if defined(__NetBSD__) && (defined(_KERNEL) || defined(_STANDALONE))
  51. /*
  52. * Kernel and boot environment can't use normal headers,
  53. * so use the equivalent system headers.
  54. */
  55. # include <machine/limits.h>
  56. # include <sys/stdint.h>
  57. # include <sys/types.h>
  58. #else
  59. /* Include the standard compiler builtin headers we use functionality from. */
  60. # include <limits.h>
  61. # include <stdint.h>
  62. # include <stdbool.h>
  63. # include <float.h>
  64. #endif
  65. /* Include the commonly used internal type definitions. */
  66. #include "int_types.h"
  67. /* Include internal utility function declarations. */
  68. #include "int_util.h"
  69. COMPILER_RT_ABI si_int __paritysi2(si_int a);
  70. COMPILER_RT_ABI si_int __paritydi2(di_int a);
  71. COMPILER_RT_ABI di_int __divdi3(di_int a, di_int b);
  72. COMPILER_RT_ABI si_int __divsi3(si_int a, si_int b);
  73. COMPILER_RT_ABI su_int __udivsi3(su_int n, su_int d);
  74. COMPILER_RT_ABI su_int __udivmodsi4(su_int a, su_int b, su_int* rem);
  75. COMPILER_RT_ABI du_int __udivmoddi4(du_int a, du_int b, du_int* rem);
  76. #ifdef CRT_HAS_128BIT
  77. COMPILER_RT_ABI si_int __clzti2(ti_int a);
  78. COMPILER_RT_ABI tu_int __udivmodti4(tu_int a, tu_int b, tu_int* rem);
  79. #endif
  80. /* Definitions for builtins unavailable on MSVC */
  81. #if defined(_MSC_VER) && !defined(__clang__)
  82. #include <intrin.h>
  83. uint32_t __inline __builtin_ctz(uint32_t value) {
  84. uint32_t trailing_zero = 0;
  85. if (_BitScanForward(&trailing_zero, value))
  86. return trailing_zero;
  87. return 32;
  88. }
  89. uint32_t __inline __builtin_clz(uint32_t value) {
  90. uint32_t leading_zero = 0;
  91. if (_BitScanReverse(&leading_zero, value))
  92. return 31 - leading_zero;
  93. return 32;
  94. }
  95. #if defined(_M_ARM) || defined(_M_X64)
  96. uint32_t __inline __builtin_clzll(uint64_t value) {
  97. uint32_t leading_zero = 0;
  98. if (_BitScanReverse64(&leading_zero, value))
  99. return 63 - leading_zero;
  100. return 64;
  101. }
  102. #else
  103. uint32_t __inline __builtin_clzll(uint64_t value) {
  104. if (value == 0)
  105. return 64;
  106. uint32_t msh = (uint32_t)(value >> 32);
  107. uint32_t lsh = (uint32_t)(value & 0xFFFFFFFF);
  108. if (msh != 0)
  109. return __builtin_clz(msh);
  110. return 32 + __builtin_clz(lsh);
  111. }
  112. #endif
  113. #define __builtin_clzl __builtin_clzll
  114. #endif // defined(_MSC_VER) && !defined(__clang__)
  115. #endif /* INT_LIB_H */