int_lib.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. //===-- int_lib.h - configuration header for compiler-rt -----------------===//
  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 a configuration header for compiler-rt.
  10. // This file is not part of the interface of this library.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef INT_LIB_H
  14. #define INT_LIB_H
  15. // Assumption: Signed integral is 2's complement.
  16. // Assumption: Right shift of signed negative is arithmetic shift.
  17. // Assumption: Endianness is little or big (not mixed).
  18. // ABI macro definitions
  19. #if __ARM_EABI__
  20. #ifdef COMPILER_RT_ARMHF_TARGET
  21. #define COMPILER_RT_ABI
  22. #else
  23. #define COMPILER_RT_ABI __attribute__((__pcs__("aapcs")))
  24. #endif
  25. #else
  26. #define COMPILER_RT_ABI
  27. #endif
  28. #define AEABI_RTABI __attribute__((__pcs__("aapcs")))
  29. #if defined(_MSC_VER) && !defined(__clang__)
  30. #define ALWAYS_INLINE __forceinline
  31. #define NOINLINE __declspec(noinline)
  32. #define NORETURN __declspec(noreturn)
  33. #define UNUSED
  34. #else
  35. #define ALWAYS_INLINE __attribute__((always_inline))
  36. #define NOINLINE __attribute__((noinline))
  37. #define NORETURN __attribute__((noreturn))
  38. #define UNUSED __attribute__((unused))
  39. #endif
  40. #define STR(a) #a
  41. #define XSTR(a) STR(a)
  42. #define SYMBOL_NAME(name) XSTR(__USER_LABEL_PREFIX__) #name
  43. #if defined(__ELF__) || defined(__MINGW32__) || defined(__wasm__) || \
  44. defined(_AIX) || defined(__CYGWIN__)
  45. #define COMPILER_RT_ALIAS(name, aliasname) \
  46. COMPILER_RT_ABI __typeof(name) aliasname __attribute__((__alias__(#name)));
  47. #elif defined(__APPLE__)
  48. #if defined(VISIBILITY_HIDDEN)
  49. #define COMPILER_RT_ALIAS_VISIBILITY(name) \
  50. __asm__(".private_extern " SYMBOL_NAME(name));
  51. #else
  52. #define COMPILER_RT_ALIAS_VISIBILITY(name)
  53. #endif
  54. #define COMPILER_RT_ALIAS(name, aliasname) \
  55. __asm__(".globl " SYMBOL_NAME(aliasname)); \
  56. COMPILER_RT_ALIAS_VISIBILITY(aliasname) \
  57. __asm__(SYMBOL_NAME(aliasname) " = " SYMBOL_NAME(name)); \
  58. COMPILER_RT_ABI __typeof(name) aliasname;
  59. #elif defined(_WIN32)
  60. #define COMPILER_RT_ALIAS(name, aliasname)
  61. #else
  62. #error Unsupported target
  63. #endif
  64. #if (defined(__FreeBSD__) || defined(__NetBSD__)) && \
  65. (defined(_KERNEL) || defined(_STANDALONE))
  66. //
  67. // Kernel and boot environment can't use normal headers,
  68. // so use the equivalent system headers.
  69. // NB: FreeBSD (and OpenBSD) deprecate machine/limits.h in
  70. // favour of sys/limits.h, so prefer the former, but fall
  71. // back on the latter if not available since NetBSD only has
  72. // the latter.
  73. //
  74. #if defined(__has_include) && __has_include(<sys/limits.h>)
  75. #include <sys/limits.h>
  76. #else
  77. #include <machine/limits.h>
  78. #endif
  79. #include <sys/stdint.h>
  80. #include <sys/types.h>
  81. #else
  82. // Include the standard compiler builtin headers we use functionality from.
  83. #include <float.h>
  84. #include <limits.h>
  85. #include <stdbool.h>
  86. #include <stdint.h>
  87. #endif
  88. // Include the commonly used internal type definitions.
  89. #include "int_types.h"
  90. // Include internal utility function declarations.
  91. #include "int_util.h"
  92. COMPILER_RT_ABI int __paritysi2(si_int a);
  93. COMPILER_RT_ABI int __paritydi2(di_int a);
  94. COMPILER_RT_ABI di_int __divdi3(di_int a, di_int b);
  95. COMPILER_RT_ABI si_int __divsi3(si_int a, si_int b);
  96. COMPILER_RT_ABI su_int __udivsi3(su_int n, su_int d);
  97. COMPILER_RT_ABI su_int __udivmodsi4(su_int a, su_int b, su_int *rem);
  98. COMPILER_RT_ABI du_int __udivmoddi4(du_int a, du_int b, du_int *rem);
  99. #ifdef CRT_HAS_128BIT
  100. COMPILER_RT_ABI int __clzti2(ti_int a);
  101. COMPILER_RT_ABI tu_int __udivmodti4(tu_int a, tu_int b, tu_int *rem);
  102. #endif
  103. // Definitions for builtins unavailable on MSVC
  104. #if defined(_MSC_VER) && !defined(__clang__)
  105. #include <intrin.h>
  106. static int __inline __builtin_ctz(uint32_t value) {
  107. unsigned long trailing_zero = 0;
  108. if (_BitScanForward(&trailing_zero, value))
  109. return trailing_zero;
  110. return 32;
  111. }
  112. static int __inline __builtin_clz(uint32_t value) {
  113. unsigned long leading_zero = 0;
  114. if (_BitScanReverse(&leading_zero, value))
  115. return 31 - leading_zero;
  116. return 32;
  117. }
  118. #if defined(_M_ARM) || defined(_M_X64)
  119. static int __inline __builtin_clzll(uint64_t value) {
  120. unsigned long leading_zero = 0;
  121. if (_BitScanReverse64(&leading_zero, value))
  122. return 63 - leading_zero;
  123. return 64;
  124. }
  125. #else
  126. static int __inline __builtin_clzll(uint64_t value) {
  127. if (value == 0)
  128. return 64;
  129. uint32_t msh = (uint32_t)(value >> 32);
  130. uint32_t lsh = (uint32_t)(value & 0xFFFFFFFF);
  131. if (msh != 0)
  132. return __builtin_clz(msh);
  133. return 32 + __builtin_clz(lsh);
  134. }
  135. #endif
  136. #define __builtin_clzl __builtin_clzll
  137. static bool __inline __builtin_sadd_overflow(int x, int y, int *result) {
  138. if ((x < 0) != (y < 0)) {
  139. *result = x + y;
  140. return false;
  141. }
  142. int tmp = (unsigned int)x + (unsigned int)y;
  143. if ((tmp < 0) != (x < 0))
  144. return true;
  145. *result = tmp;
  146. return false;
  147. }
  148. #endif // defined(_MSC_VER) && !defined(__clang__)
  149. #endif // INT_LIB_H