debase-ruby_core_source 0.10.6 → 0.10.11

Sign up to get free protection for your applications and to get access to all the features.
Files changed (172) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +17 -0
  3. data/README.md +1 -1
  4. data/Rakefile +6 -3
  5. data/debase-ruby_core_source.gemspec +2 -1
  6. data/lib/debase/ruby_core_source.rb +1 -1
  7. data/lib/debase/ruby_core_source/ruby-2.7.0-p0/addr2line.h +21 -0
  8. data/lib/debase/ruby_core_source/ruby-2.7.0-p0/builtin.h +78 -0
  9. data/lib/debase/ruby_core_source/ruby-2.7.0-p0/ccan/build_assert/build_assert.h +40 -0
  10. data/lib/debase/ruby_core_source/ruby-2.7.0-p0/ccan/check_type/check_type.h +63 -0
  11. data/lib/debase/ruby_core_source/ruby-2.7.0-p0/ccan/container_of/container_of.h +142 -0
  12. data/lib/debase/ruby_core_source/ruby-2.7.0-p0/ccan/list/list.h +788 -0
  13. data/lib/debase/ruby_core_source/ruby-2.7.0-p0/ccan/str/str.h +16 -0
  14. data/lib/debase/ruby_core_source/ruby-2.7.0-p0/constant.h +50 -0
  15. data/lib/debase/ruby_core_source/ruby-2.7.0-p0/debug_counter.h +390 -0
  16. data/lib/debase/ruby_core_source/ruby-2.7.0-p0/dln.h +51 -0
  17. data/lib/debase/ruby_core_source/ruby-2.7.0-p0/encindex.h +69 -0
  18. data/lib/debase/ruby_core_source/ruby-2.7.0-p0/eval_intern.h +312 -0
  19. data/lib/debase/ruby_core_source/ruby-2.7.0-p0/gc.h +131 -0
  20. data/lib/debase/ruby_core_source/ruby-2.7.0-p0/hrtime.h +168 -0
  21. data/lib/debase/ruby_core_source/ruby-2.7.0-p0/id.h +290 -0
  22. data/lib/debase/ruby_core_source/ruby-2.7.0-p0/id_table.h +34 -0
  23. data/lib/debase/ruby_core_source/ruby-2.7.0-p0/insns.inc +253 -0
  24. data/lib/debase/ruby_core_source/ruby-2.7.0-p0/insns_info.inc +9197 -0
  25. data/lib/debase/ruby_core_source/ruby-2.7.0-p0/internal.h +2697 -0
  26. data/lib/debase/ruby_core_source/ruby-2.7.0-p0/iseq.h +312 -0
  27. data/lib/debase/ruby_core_source/ruby-2.7.0-p0/known_errors.inc +791 -0
  28. data/lib/debase/ruby_core_source/ruby-2.7.0-p0/method.h +229 -0
  29. data/lib/debase/ruby_core_source/ruby-2.7.0-p0/mjit.h +177 -0
  30. data/lib/debase/ruby_core_source/ruby-2.7.0-p0/mjit_compile.inc +7779 -0
  31. data/lib/debase/ruby_core_source/ruby-2.7.0-p0/node.h +476 -0
  32. data/lib/debase/ruby_core_source/ruby-2.7.0-p0/node_name.inc +208 -0
  33. data/lib/debase/ruby_core_source/ruby-2.7.0-p0/opt_sc.inc +109 -0
  34. data/lib/debase/ruby_core_source/ruby-2.7.0-p0/optinsn.inc +128 -0
  35. data/lib/debase/ruby_core_source/ruby-2.7.0-p0/optunifs.inc +43 -0
  36. data/lib/debase/ruby_core_source/ruby-2.7.0-p0/parse.h +210 -0
  37. data/lib/debase/ruby_core_source/ruby-2.7.0-p0/probes_helper.h +42 -0
  38. data/lib/debase/ruby_core_source/ruby-2.7.0-p0/regenc.h +254 -0
  39. data/lib/debase/ruby_core_source/ruby-2.7.0-p0/regint.h +938 -0
  40. data/lib/debase/ruby_core_source/ruby-2.7.0-p0/regparse.h +370 -0
  41. data/lib/debase/ruby_core_source/ruby-2.7.0-p0/revision.h +2 -0
  42. data/lib/debase/ruby_core_source/ruby-2.7.0-p0/ruby_assert.h +15 -0
  43. data/lib/debase/ruby_core_source/ruby-2.7.0-p0/ruby_atomic.h +244 -0
  44. data/lib/debase/ruby_core_source/ruby-2.7.0-p0/siphash.h +48 -0
  45. data/lib/debase/ruby_core_source/ruby-2.7.0-p0/symbol.h +119 -0
  46. data/lib/debase/ruby_core_source/ruby-2.7.0-p0/thread_pthread.h +75 -0
  47. data/lib/debase/ruby_core_source/ruby-2.7.0-p0/thread_win32.h +36 -0
  48. data/lib/debase/ruby_core_source/ruby-2.7.0-p0/timev.h +56 -0
  49. data/lib/debase/ruby_core_source/ruby-2.7.0-p0/transcode_data.h +139 -0
  50. data/lib/debase/ruby_core_source/ruby-2.7.0-p0/transient_heap.h +62 -0
  51. data/lib/debase/ruby_core_source/ruby-2.7.0-p0/variable.h +9 -0
  52. data/lib/debase/ruby_core_source/ruby-2.7.0-p0/version.h +77 -0
  53. data/lib/debase/ruby_core_source/ruby-2.7.0-p0/vm.inc +5466 -0
  54. data/lib/debase/ruby_core_source/ruby-2.7.0-p0/vm_call_iseq_optimized.inc +237 -0
  55. data/lib/debase/ruby_core_source/ruby-2.7.0-p0/vm_core.h +1968 -0
  56. data/lib/debase/ruby_core_source/ruby-2.7.0-p0/vm_debug.h +34 -0
  57. data/lib/debase/ruby_core_source/ruby-2.7.0-p0/vm_exec.h +193 -0
  58. data/lib/debase/ruby_core_source/ruby-2.7.0-p0/vm_insnhelper.h +267 -0
  59. data/lib/debase/ruby_core_source/ruby-2.7.0-p0/vm_opts.h +72 -0
  60. data/lib/debase/ruby_core_source/ruby-2.7.0-p0/vmtc.inc +247 -0
  61. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/addr2line.h +20 -0
  62. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/builtin.h +83 -0
  63. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/ccan/build_assert/build_assert.h +40 -0
  64. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/ccan/check_type/check_type.h +63 -0
  65. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/ccan/container_of/container_of.h +142 -0
  66. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/ccan/list/list.h +788 -0
  67. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/ccan/str/str.h +16 -0
  68. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/constant.h +55 -0
  69. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/debug_counter.h +423 -0
  70. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/dln.h +36 -0
  71. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/encindex.h +70 -0
  72. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/eval_intern.h +312 -0
  73. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/gc.h +140 -0
  74. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/hrtime.h +168 -0
  75. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/id.h +290 -0
  76. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/id_table.h +36 -0
  77. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/insns.inc +249 -0
  78. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/insns_info.inc +8979 -0
  79. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/internal.h +107 -0
  80. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/internal/array.h +114 -0
  81. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/internal/bignum.h +246 -0
  82. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/internal/bits.h +566 -0
  83. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/internal/class.h +168 -0
  84. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/internal/compar.h +50 -0
  85. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/internal/compile.h +32 -0
  86. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/internal/compilers.h +108 -0
  87. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/internal/complex.h +29 -0
  88. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/internal/cont.h +25 -0
  89. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/internal/dir.h +17 -0
  90. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/internal/enc.h +20 -0
  91. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/internal/encoding.h +28 -0
  92. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/internal/enum.h +19 -0
  93. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/internal/enumerator.h +22 -0
  94. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/internal/error.h +131 -0
  95. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/internal/eval.h +33 -0
  96. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/internal/file.h +39 -0
  97. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/internal/fixnum.h +185 -0
  98. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/internal/gc.h +154 -0
  99. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/internal/hash.h +240 -0
  100. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/internal/imemo.h +243 -0
  101. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/internal/inits.h +51 -0
  102. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/internal/io.h +35 -0
  103. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/internal/load.h +19 -0
  104. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/internal/loadpath.h +17 -0
  105. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/internal/math.h +24 -0
  106. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/internal/missing.h +19 -0
  107. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/internal/mjit.h +29 -0
  108. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/internal/numeric.h +252 -0
  109. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/internal/object.h +83 -0
  110. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/internal/parse.h +23 -0
  111. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/internal/proc.h +32 -0
  112. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/internal/process.h +136 -0
  113. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/internal/random.h +17 -0
  114. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/internal/range.h +37 -0
  115. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/internal/rational.h +69 -0
  116. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/internal/re.h +29 -0
  117. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/internal/sanitizers.h +191 -0
  118. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/internal/scheduler.h +35 -0
  119. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/internal/serial.h +24 -0
  120. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/internal/signal.h +22 -0
  121. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/internal/static_assert.h +17 -0
  122. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/internal/string.h +135 -0
  123. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/internal/struct.h +154 -0
  124. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/internal/symbol.h +41 -0
  125. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/internal/thread.h +60 -0
  126. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/internal/time.h +35 -0
  127. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/internal/transcode.h +21 -0
  128. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/internal/util.h +31 -0
  129. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/internal/variable.h +83 -0
  130. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/internal/vm.h +135 -0
  131. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/internal/warnings.h +17 -0
  132. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/iseq.h +304 -0
  133. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/known_errors.inc +791 -0
  134. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/method.h +245 -0
  135. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/mjit.h +197 -0
  136. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/mjit_compile.inc +8082 -0
  137. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/node.h +484 -0
  138. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/node_name.inc +210 -0
  139. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/opt_sc.inc +109 -0
  140. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/optinsn.inc +128 -0
  141. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/optunifs.inc +43 -0
  142. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/parse.h +211 -0
  143. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/probes_helper.h +44 -0
  144. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/ractor.h +271 -0
  145. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/ractor_pub.h +50 -0
  146. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/regenc.h +254 -0
  147. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/regint.h +938 -0
  148. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/regparse.h +370 -0
  149. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/revision.h +2 -0
  150. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/ruby_assert.h +15 -0
  151. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/ruby_atomic.h +236 -0
  152. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/siphash.h +48 -0
  153. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/symbol.h +119 -0
  154. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/thread_pthread.h +106 -0
  155. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/thread_win32.h +66 -0
  156. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/timev.h +57 -0
  157. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/transcode_data.h +138 -0
  158. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/transient_heap.h +61 -0
  159. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/variable.h +21 -0
  160. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/version.h +89 -0
  161. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/vm.inc +5360 -0
  162. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/vm_call_iseq_optimized.inc +244 -0
  163. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/vm_callinfo.h +457 -0
  164. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/vm_core.h +1952 -0
  165. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/vm_debug.h +116 -0
  166. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/vm_exec.h +196 -0
  167. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/vm_insnhelper.h +260 -0
  168. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/vm_opts.h +73 -0
  169. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/vm_sync.h +97 -0
  170. data/lib/debase/ruby_core_source/ruby-3.0.0-preview1/vmtc.inc +243 -0
  171. data/lib/debase/ruby_core_source/version.rb +1 -1
  172. metadata +183 -5
@@ -0,0 +1,566 @@
1
+ #ifndef INTERNAL_BITS_H /*-*-C-*-vi:se ft=c:*/
2
+ #define INTERNAL_BITS_H
3
+ /**
4
+ * @file
5
+ * @author Ruby developers <ruby-core@ruby-lang.org>
6
+ * @copyright This file is a part of the programming language Ruby.
7
+ * Permission is hereby granted, to either redistribute and/or
8
+ * modify this file, provided that the conditions mentioned in the
9
+ * file COPYING are met. Consult the file for details.
10
+ * @brief Internal header for bitwise integer algorithms.
11
+ * @see Henry S. Warren Jr., "Hacker's Delight" (2nd ed.), 2013.
12
+ * @see SEI CERT C Coding Standard INT32-C. "Ensure that operations on
13
+ * signed integers do not result in overflow"
14
+ * @see https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html
15
+ * @see https://clang.llvm.org/docs/LanguageExtensions.html#builtin-rotateleft
16
+ * @see https://clang.llvm.org/docs/LanguageExtensions.html#builtin-rotateright
17
+ * @see https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/byteswap-uint64-byteswap-ulong-byteswap-ushort
18
+ * @see https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/rotl-rotl64-rotr-rotr64
19
+ * @see https://docs.microsoft.com/en-us/cpp/intrinsics/bitscanforward-bitscanforward64
20
+ * @see https://docs.microsoft.com/en-us/cpp/intrinsics/bitscanreverse-bitscanreverse64
21
+ * @see https://docs.microsoft.com/en-us/cpp/intrinsics/lzcnt16-lzcnt-lzcnt64
22
+ * @see https://docs.microsoft.com/en-us/cpp/intrinsics/popcnt16-popcnt-popcnt64
23
+ * @see https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_lzcnt_u32
24
+ * @see https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_tzcnt_u32
25
+ * @see https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_rotl64
26
+ * @see https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_rotr64
27
+ * @see https://stackoverflow.com/a/776523
28
+ */
29
+ #include "ruby/internal/config.h"
30
+ #include <limits.h> /* for CHAR_BITS */
31
+ #include <stdint.h> /* for uintptr_t */
32
+ #include "internal/compilers.h" /* for MSC_VERSION_SINCE */
33
+
34
+ #if MSC_VERSION_SINCE(1310)
35
+ # include <stdlib.h> /* for _byteswap_uint64 */
36
+ #endif
37
+
38
+ #if defined(HAVE_X86INTRIN_H) && ! defined(MJIT_HEADER)
39
+ # /* Rule out MJIT_HEADER, which does not interface well with <immintrin.h> */
40
+ # include <x86intrin.h> /* for _lzcnt_u64 */
41
+ #elif MSC_VERSION_SINCE(1310)
42
+ # include <intrin.h> /* for the following intrinsics */
43
+ #endif
44
+
45
+ #if defined(_MSC_VER) && defined(__AVX__)
46
+ # pragma intrinsic(__popcnt)
47
+ # pragma intrinsic(__popcnt64)
48
+ #endif
49
+
50
+ #if defined(_MSC_VER) && defined(__AVX2__)
51
+ # pragma intrinsic(__lzcnt)
52
+ # pragma intrinsic(__lzcnt64)
53
+ #endif
54
+
55
+ #if MSC_VERSION_SINCE(1310)
56
+ # pragma intrinsic(_rotl)
57
+ # pragma intrinsic(_rotr)
58
+ # ifdef _WIN64
59
+ # pragma intrinsic(_rotl64)
60
+ # pragma intrinsic(_rotr64)
61
+ # endif
62
+ #endif
63
+
64
+ #if MSC_VERSION_SINCE(1400)
65
+ # pragma intrinsic(_BitScanForward)
66
+ # pragma intrinsic(_BitScanReverse)
67
+ # ifdef _WIN64
68
+ # pragma intrinsic(_BitScanForward64)
69
+ # pragma intrinsic(_BitScanReverse64)
70
+ # endif
71
+ #endif
72
+
73
+ #include "ruby/ruby.h" /* for VALUE */
74
+ #include "internal/static_assert.h" /* for STATIC_ASSERT */
75
+
76
+ /* The most significant bit of the lower part of half-long integer.
77
+ * If sizeof(long) == 4, this is 0x8000.
78
+ * If sizeof(long) == 8, this is 0x80000000.
79
+ */
80
+ #define HALF_LONG_MSB ((SIGNED_VALUE)1<<((SIZEOF_LONG*CHAR_BIT-1)/2))
81
+
82
+ #define SIGNED_INTEGER_TYPE_P(T) (0 > ((T)0)-1)
83
+
84
+ #define SIGNED_INTEGER_MIN(T) \
85
+ ((sizeof(T) == sizeof(int8_t)) ? ((T)INT8_MIN) : \
86
+ ((sizeof(T) == sizeof(int16_t)) ? ((T)INT16_MIN) : \
87
+ ((sizeof(T) == sizeof(int32_t)) ? ((T)INT32_MIN) : \
88
+ ((sizeof(T) == sizeof(int64_t)) ? ((T)INT64_MIN) : \
89
+ 0))))
90
+
91
+ #define SIGNED_INTEGER_MAX(T) ((T)(SIGNED_INTEGER_MIN(T) ^ ((T)~(T)0)))
92
+
93
+ #define UNSIGNED_INTEGER_MAX(T) ((T)~(T)0)
94
+
95
+ #if __has_builtin(__builtin_mul_overflow_p)
96
+ # define MUL_OVERFLOW_P(a, b) \
97
+ __builtin_mul_overflow_p((a), (b), (__typeof__(a * b))0)
98
+ #elif __has_builtin(__builtin_mul_overflow)
99
+ # define MUL_OVERFLOW_P(a, b) \
100
+ __extension__ ({ __typeof__(a) c; __builtin_mul_overflow((a), (b), &c); })
101
+ #endif
102
+
103
+ #define MUL_OVERFLOW_SIGNED_INTEGER_P(a, b, min, max) ( \
104
+ (a) == 0 ? 0 : \
105
+ (a) == -1 ? (b) < -(max) : \
106
+ (a) > 0 ? \
107
+ ((b) > 0 ? (max) / (a) < (b) : (min) / (a) > (b)) : \
108
+ ((b) > 0 ? (min) / (a) < (b) : (max) / (a) > (b)))
109
+
110
+ #if __has_builtin(__builtin_mul_overflow_p)
111
+ /* __builtin_mul_overflow_p can take bitfield */
112
+ /* and GCC permits bitfields for integers other than int */
113
+ # define MUL_OVERFLOW_FIXNUM_P(a, b) \
114
+ __extension__ ({ \
115
+ struct { long fixnum : sizeof(long) * CHAR_BIT - 1; } c = { 0 }; \
116
+ __builtin_mul_overflow_p((a), (b), c.fixnum); \
117
+ })
118
+ #else
119
+ # define MUL_OVERFLOW_FIXNUM_P(a, b) \
120
+ MUL_OVERFLOW_SIGNED_INTEGER_P(a, b, FIXNUM_MIN, FIXNUM_MAX)
121
+ #endif
122
+
123
+ #ifdef MUL_OVERFLOW_P
124
+ # define MUL_OVERFLOW_LONG_LONG_P(a, b) MUL_OVERFLOW_P(a, b)
125
+ # define MUL_OVERFLOW_LONG_P(a, b) MUL_OVERFLOW_P(a, b)
126
+ # define MUL_OVERFLOW_INT_P(a, b) MUL_OVERFLOW_P(a, b)
127
+ #else
128
+ # define MUL_OVERFLOW_LONG_LONG_P(a, b) MUL_OVERFLOW_SIGNED_INTEGER_P(a, b, LLONG_MIN, LLONG_MAX)
129
+ # define MUL_OVERFLOW_LONG_P(a, b) MUL_OVERFLOW_SIGNED_INTEGER_P(a, b, LONG_MIN, LONG_MAX)
130
+ # define MUL_OVERFLOW_INT_P(a, b) MUL_OVERFLOW_SIGNED_INTEGER_P(a, b, INT_MIN, INT_MAX)
131
+ #endif
132
+
133
+ #ifdef HAVE_UINT128_T
134
+ # define bit_length(x) \
135
+ (unsigned int) \
136
+ (sizeof(x) <= sizeof(int32_t) ? 32 - nlz_int32((uint32_t)(x)) : \
137
+ sizeof(x) <= sizeof(int64_t) ? 64 - nlz_int64((uint64_t)(x)) : \
138
+ 128 - nlz_int128((uint128_t)(x)))
139
+ #else
140
+ # define bit_length(x) \
141
+ (unsigned int) \
142
+ (sizeof(x) <= sizeof(int32_t) ? 32 - nlz_int32((uint32_t)(x)) : \
143
+ 64 - nlz_int64((uint64_t)(x)))
144
+ #endif
145
+
146
+ #ifndef swap16
147
+ # define swap16 ruby_swap16
148
+ #endif
149
+
150
+ #ifndef swap32
151
+ # define swap32 ruby_swap32
152
+ #endif
153
+
154
+ #ifndef swap64
155
+ # define swap64 ruby_swap64
156
+ #endif
157
+
158
+ static inline uint16_t ruby_swap16(uint16_t);
159
+ static inline uint32_t ruby_swap32(uint32_t);
160
+ static inline uint64_t ruby_swap64(uint64_t);
161
+ static inline unsigned nlz_int(unsigned x);
162
+ static inline unsigned nlz_long(unsigned long x);
163
+ static inline unsigned nlz_long_long(unsigned long long x);
164
+ static inline unsigned nlz_intptr(uintptr_t x);
165
+ static inline unsigned nlz_int32(uint32_t x);
166
+ static inline unsigned nlz_int64(uint64_t x);
167
+ #ifdef HAVE_UINT128_T
168
+ static inline unsigned nlz_int128(uint128_t x);
169
+ #endif
170
+ static inline unsigned rb_popcount32(uint32_t x);
171
+ static inline unsigned rb_popcount64(uint64_t x);
172
+ static inline unsigned rb_popcount_intptr(uintptr_t x);
173
+ static inline int ntz_int32(uint32_t x);
174
+ static inline int ntz_int64(uint64_t x);
175
+ static inline int ntz_intptr(uintptr_t x);
176
+ static inline VALUE RUBY_BIT_ROTL(VALUE, int);
177
+ static inline VALUE RUBY_BIT_ROTR(VALUE, int);
178
+
179
+ static inline uint16_t
180
+ ruby_swap16(uint16_t x)
181
+ {
182
+ #if __has_builtin(__builtin_bswap16)
183
+ return __builtin_bswap16(x);
184
+
185
+ #elif MSC_VERSION_SINCE(1310)
186
+ return _byteswap_ushort(x);
187
+
188
+ #else
189
+ return (x << 8) | (x >> 8);
190
+
191
+ #endif
192
+ }
193
+
194
+ static inline uint32_t
195
+ ruby_swap32(uint32_t x)
196
+ {
197
+ #if __has_builtin(__builtin_bswap32)
198
+ return __builtin_bswap32(x);
199
+
200
+ #elif MSC_VERSION_SINCE(1310)
201
+ return _byteswap_ulong(x);
202
+
203
+ #else
204
+ x = ((x & 0x0000FFFF) << 16) | ((x & 0xFFFF0000) >> 16);
205
+ x = ((x & 0x00FF00FF) << 8) | ((x & 0xFF00FF00) >> 8);
206
+ return x;
207
+
208
+ #endif
209
+ }
210
+
211
+ static inline uint64_t
212
+ ruby_swap64(uint64_t x)
213
+ {
214
+ #if __has_builtin(__builtin_bswap64)
215
+ return __builtin_bswap64(x);
216
+
217
+ #elif MSC_VERSION_SINCE(1310)
218
+ return _byteswap_uint64(x);
219
+
220
+ #else
221
+ x = ((x & 0x00000000FFFFFFFFULL) << 32) | ((x & 0xFFFFFFFF00000000ULL) >> 32);
222
+ x = ((x & 0x0000FFFF0000FFFFULL) << 16) | ((x & 0xFFFF0000FFFF0000ULL) >> 16);
223
+ x = ((x & 0x00FF00FF00FF00FFULL) << 8) | ((x & 0xFF00FF00FF00FF00ULL) >> 8);
224
+ return x;
225
+
226
+ #endif
227
+ }
228
+
229
+ static inline unsigned int
230
+ nlz_int32(uint32_t x)
231
+ {
232
+ #if defined(_MSC_VER) && defined(__AVX2__)
233
+ /* Note: It seems there is no such thing like __LZCNT__ predefined in MSVC.
234
+ * AMD CPUs have had this instruction for decades (since K10) but for
235
+ * Intel, Haswell is the oldest one. We need to use __AVX2__ for maximum
236
+ * safety. */
237
+ return (unsigned int)__lzcnt(x);
238
+
239
+ #elif defined(__x86_64__) && defined(__LZCNT__) && ! defined(MJIT_HEADER)
240
+ return (unsigned int)_lzcnt_u32(x);
241
+
242
+ #elif MSC_VERSION_SINCE(1400) /* &&! defined(__AVX2__) */
243
+ unsigned long r;
244
+ return _BitScanReverse(&r, x) ? (31 - (int)r) : 32;
245
+
246
+ #elif __has_builtin(__builtin_clz)
247
+ STATIC_ASSERT(sizeof_int, sizeof(int) * CHAR_BIT == 32);
248
+ return x ? (unsigned int)__builtin_clz(x) : 32;
249
+
250
+ #else
251
+ uint32_t y;
252
+ unsigned n = 32;
253
+ y = x >> 16; if (y) {n -= 16; x = y;}
254
+ y = x >> 8; if (y) {n -= 8; x = y;}
255
+ y = x >> 4; if (y) {n -= 4; x = y;}
256
+ y = x >> 2; if (y) {n -= 2; x = y;}
257
+ y = x >> 1; if (y) {return n - 2;}
258
+ return (unsigned int)(n - x);
259
+ #endif
260
+ }
261
+
262
+ static inline unsigned int
263
+ nlz_int64(uint64_t x)
264
+ {
265
+ #if defined(_MSC_VER) && defined(__AVX2__)
266
+ return (unsigned int)__lzcnt64(x);
267
+
268
+ #elif defined(__x86_64__) && defined(__LZCNT__) && ! defined(MJIT_HEADER)
269
+ return (unsigned int)_lzcnt_u64(x);
270
+
271
+ #elif defined(_WIN64) && MSC_VERSION_SINCE(1400) /* &&! defined(__AVX2__) */
272
+ unsigned long r;
273
+ return _BitScanReverse64(&r, x) ? (63u - (unsigned int)r) : 64;
274
+
275
+ #elif __has_builtin(__builtin_clzl)
276
+ if (x == 0) {
277
+ return 64;
278
+ }
279
+ else if (sizeof(long) * CHAR_BIT == 64) {
280
+ return (unsigned int)__builtin_clzl((unsigned long)x);
281
+ }
282
+ else if (sizeof(long long) * CHAR_BIT == 64) {
283
+ return (unsigned int)__builtin_clzll((unsigned long long)x);
284
+ }
285
+ else {
286
+ /* :FIXME: Is there a way to make this branch a compile-time error? */
287
+ __builtin_unreachable();
288
+ }
289
+
290
+ #else
291
+ uint64_t y;
292
+ unsigned int n = 64;
293
+ y = x >> 32; if (y) {n -= 32; x = y;}
294
+ y = x >> 16; if (y) {n -= 16; x = y;}
295
+ y = x >> 8; if (y) {n -= 8; x = y;}
296
+ y = x >> 4; if (y) {n -= 4; x = y;}
297
+ y = x >> 2; if (y) {n -= 2; x = y;}
298
+ y = x >> 1; if (y) {return n - 2;}
299
+ return (unsigned int)(n - x);
300
+
301
+ #endif
302
+ }
303
+
304
+ #ifdef HAVE_UINT128_T
305
+ static inline unsigned int
306
+ nlz_int128(uint128_t x)
307
+ {
308
+ uint64_t y = (uint64_t)(x >> 64);
309
+
310
+ if (x == 0) {
311
+ return 128;
312
+ }
313
+ else if (y == 0) {
314
+ return (unsigned int)nlz_int64(x) + 64;
315
+ }
316
+ else {
317
+ return (unsigned int)nlz_int64(y);
318
+ }
319
+ }
320
+ #endif
321
+
322
+ static inline unsigned int
323
+ nlz_int(unsigned int x)
324
+ {
325
+ if (sizeof(unsigned int) * CHAR_BIT == 32) {
326
+ return nlz_int32((uint32_t)x);
327
+ }
328
+ else if (sizeof(unsigned int) * CHAR_BIT == 64) {
329
+ return nlz_int64((uint64_t)x);
330
+ }
331
+ else {
332
+ UNREACHABLE_RETURN(~0);
333
+ }
334
+ }
335
+
336
+ static inline unsigned int
337
+ nlz_long(unsigned long x)
338
+ {
339
+ if (sizeof(unsigned long) * CHAR_BIT == 32) {
340
+ return nlz_int32((uint32_t)x);
341
+ }
342
+ else if (sizeof(unsigned long) * CHAR_BIT == 64) {
343
+ return nlz_int64((uint64_t)x);
344
+ }
345
+ else {
346
+ UNREACHABLE_RETURN(~0);
347
+ }
348
+ }
349
+
350
+ static inline unsigned int
351
+ nlz_long_long(unsigned long long x)
352
+ {
353
+ if (sizeof(unsigned long long) * CHAR_BIT == 64) {
354
+ return nlz_int64((uint64_t)x);
355
+ }
356
+ #ifdef HAVE_UINT128_T
357
+ else if (sizeof(unsigned long long) * CHAR_BIT == 128) {
358
+ return nlz_int128((uint128_t)x);
359
+ }
360
+ #endif
361
+ else {
362
+ UNREACHABLE_RETURN(~0);
363
+ }
364
+ }
365
+
366
+ static inline unsigned int
367
+ nlz_intptr(uintptr_t x)
368
+ {
369
+ if (sizeof(uintptr_t) == sizeof(unsigned int)) {
370
+ return nlz_int((unsigned int)x);
371
+ }
372
+ if (sizeof(uintptr_t) == sizeof(unsigned long)) {
373
+ return nlz_long((unsigned long)x);
374
+ }
375
+ if (sizeof(uintptr_t) == sizeof(unsigned long long)) {
376
+ return nlz_long_long((unsigned long long)x);
377
+ }
378
+ else {
379
+ UNREACHABLE_RETURN(~0);
380
+ }
381
+ }
382
+
383
+ static inline unsigned int
384
+ rb_popcount32(uint32_t x)
385
+ {
386
+ #if defined(_MSC_VER) && defined(__AVX__)
387
+ /* Note: CPUs since Nehalem and Barcelona have had this instruction so SSE
388
+ * 4.2 should suffice, but it seems there is no such thing like __SSE_4_2__
389
+ * predefined macro in MSVC. They do have __AVX__ so use it instead. */
390
+ return (unsigned int)__popcnt(x);
391
+
392
+ #elif __has_builtin(__builtin_popcount)
393
+ STATIC_ASSERT(sizeof_int, sizeof(int) * CHAR_BIT >= 32);
394
+ return (unsigned int)__builtin_popcount(x);
395
+
396
+ #else
397
+ x = (x & 0x55555555) + (x >> 1 & 0x55555555);
398
+ x = (x & 0x33333333) + (x >> 2 & 0x33333333);
399
+ x = (x & 0x0f0f0f0f) + (x >> 4 & 0x0f0f0f0f);
400
+ x = (x & 0x001f001f) + (x >> 8 & 0x001f001f);
401
+ x = (x & 0x0000003f) + (x >>16 & 0x0000003f);
402
+ return (unsigned int)x;
403
+
404
+ #endif
405
+ }
406
+
407
+ static inline unsigned int
408
+ rb_popcount64(uint64_t x)
409
+ {
410
+ #if defined(_MSC_VER) && defined(__AVX__)
411
+ return (unsigned int)__popcnt64(x);
412
+
413
+ #elif __has_builtin(__builtin_popcount)
414
+ if (sizeof(long) * CHAR_BIT == 64) {
415
+ return (unsigned int)__builtin_popcountl((unsigned long)x);
416
+ }
417
+ else if (sizeof(long long) * CHAR_BIT == 64) {
418
+ return (unsigned int)__builtin_popcountll((unsigned long long)x);
419
+ }
420
+ else {
421
+ /* :FIXME: Is there a way to make this branch a compile-time error? */
422
+ __builtin_unreachable();
423
+ }
424
+
425
+ #else
426
+ x = (x & 0x5555555555555555) + (x >> 1 & 0x5555555555555555);
427
+ x = (x & 0x3333333333333333) + (x >> 2 & 0x3333333333333333);
428
+ x = (x & 0x0707070707070707) + (x >> 4 & 0x0707070707070707);
429
+ x = (x & 0x001f001f001f001f) + (x >> 8 & 0x001f001f001f001f);
430
+ x = (x & 0x0000003f0000003f) + (x >>16 & 0x0000003f0000003f);
431
+ x = (x & 0x000000000000007f) + (x >>32 & 0x000000000000007f);
432
+ return (unsigned int)x;
433
+
434
+ #endif
435
+ }
436
+
437
+ static inline unsigned int
438
+ rb_popcount_intptr(uintptr_t x)
439
+ {
440
+ if (sizeof(uintptr_t) * CHAR_BIT == 64) {
441
+ return rb_popcount64((uint64_t)x);
442
+ }
443
+ else if (sizeof(uintptr_t) * CHAR_BIT == 32) {
444
+ return rb_popcount32((uint32_t)x);
445
+ }
446
+ else {
447
+ UNREACHABLE_RETURN(~0);
448
+ }
449
+ }
450
+
451
+ static inline int
452
+ ntz_int32(uint32_t x)
453
+ {
454
+ #if defined(__x86_64__) && defined(__BMI__) && ! defined(MJIT_HEADER)
455
+ return (unsigned)_tzcnt_u32(x);
456
+
457
+ #elif MSC_VERSION_SINCE(1400)
458
+ /* :FIXME: Is there any way to issue TZCNT instead of BSF, apart from using
459
+ * assembly? Because issuing LZCNT seems possible (see nlz.h). */
460
+ unsigned long r;
461
+ return _BitScanForward(&r, x) ? (int)r : 32;
462
+
463
+ #elif __has_builtin(__builtin_ctz)
464
+ STATIC_ASSERT(sizeof_int, sizeof(int) * CHAR_BIT == 32);
465
+ return x ? (unsigned)__builtin_ctz(x) : 32;
466
+
467
+ #else
468
+ return rb_popcount32((~x) & (x-1));
469
+
470
+ #endif
471
+ }
472
+
473
+ static inline int
474
+ ntz_int64(uint64_t x)
475
+ {
476
+ #if defined(__x86_64__) && defined(__BMI__) && ! defined(MJIT_HEADER)
477
+ return (unsigned)_tzcnt_u64(x);
478
+
479
+ #elif defined(_WIN64) && MSC_VERSION_SINCE(1400)
480
+ unsigned long r;
481
+ return _BitScanForward64(&r, x) ? (int)r : 64;
482
+
483
+ #elif __has_builtin(__builtin_ctzl)
484
+ if (x == 0) {
485
+ return 64;
486
+ }
487
+ else if (sizeof(long) * CHAR_BIT == 64) {
488
+ return (unsigned)__builtin_ctzl((unsigned long)x);
489
+ }
490
+ else if (sizeof(long long) * CHAR_BIT == 64) {
491
+ return (unsigned)__builtin_ctzll((unsigned long long)x);
492
+ }
493
+ else {
494
+ /* :FIXME: Is there a way to make this branch a compile-time error? */
495
+ __builtin_unreachable();
496
+ }
497
+
498
+ #else
499
+ return rb_popcount64((~x) & (x-1));
500
+
501
+ #endif
502
+ }
503
+
504
+ static inline int
505
+ ntz_intptr(uintptr_t x)
506
+ {
507
+ if (sizeof(uintptr_t) * CHAR_BIT == 64) {
508
+ return ntz_int64((uint64_t)x);
509
+ }
510
+ else if (sizeof(uintptr_t) * CHAR_BIT == 32) {
511
+ return ntz_int32((uint32_t)x);
512
+ }
513
+ else {
514
+ UNREACHABLE_RETURN(~0);
515
+ }
516
+ }
517
+
518
+ static inline VALUE
519
+ RUBY_BIT_ROTL(VALUE v, int n)
520
+ {
521
+ #if __has_builtin(__builtin_rotateleft32) && (SIZEOF_VALUE * CHAR_BIT == 32)
522
+ return __builtin_rotateleft32(v, n);
523
+
524
+ #elif __has_builtin(__builtin_rotateleft64) && (SIZEOF_VALUE * CHAR_BIT == 64)
525
+ return __builtin_rotateleft64(v, n);
526
+
527
+ #elif MSC_VERSION_SINCE(1310) && (SIZEOF_VALUE * CHAR_BIT == 32)
528
+ return _rotl(v, n);
529
+
530
+ #elif MSC_VERSION_SINCE(1310) && (SIZEOF_VALUE * CHAR_BIT == 64)
531
+ return _rotl64(v, n);
532
+
533
+ #elif defined(_lrotl) && (SIZEOF_VALUE == SIZEOF_LONG)
534
+ return _lrotl(v, n);
535
+
536
+ #else
537
+ const int m = (sizeof(VALUE) * CHAR_BIT) - 1;
538
+ return (v << (n & m)) | (v >> (-n & m));
539
+ #endif
540
+ }
541
+
542
+ static inline VALUE
543
+ RUBY_BIT_ROTR(VALUE v, int n)
544
+ {
545
+ #if __has_builtin(__builtin_rotateright32) && (SIZEOF_VALUE * CHAR_BIT == 32)
546
+ return __builtin_rotateright32(v, n);
547
+
548
+ #elif __has_builtin(__builtin_rotateright64) && (SIZEOF_VALUE * CHAR_BIT == 64)
549
+ return __builtin_rotateright64(v, n);
550
+
551
+ #elif MSC_VERSION_SINCE(1310) && (SIZEOF_VALUE * CHAR_BIT == 32)
552
+ return _rotr(v, n);
553
+
554
+ #elif MSC_VERSION_SINCE(1310) && (SIZEOF_VALUE * CHAR_BIT == 64)
555
+ return _rotr64(v, n);
556
+
557
+ #elif defined(_lrotr) && (SIZEOF_VALUE == SIZEOF_LONG)
558
+ return _lrotr(v, n);
559
+
560
+ #else
561
+ const int m = (sizeof(VALUE) * CHAR_BIT) - 1;
562
+ return (v << (-n & m)) | (v >> (n & m));
563
+ #endif
564
+ }
565
+
566
+ #endif /* INTERNAL_BITS_H */