deflate-ruby 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (100) hide show
  1. checksums.yaml +7 -0
  2. data/CLAUDE.md +138 -0
  3. data/LICENSE.txt +21 -0
  4. data/README.md +117 -0
  5. data/ext/deflate_ruby/deflate_ruby.c +301 -0
  6. data/ext/deflate_ruby/extconf.rb +34 -0
  7. data/ext/deflate_ruby/libdeflate/CMakeLists.txt +270 -0
  8. data/ext/deflate_ruby/libdeflate/COPYING +22 -0
  9. data/ext/deflate_ruby/libdeflate/NEWS.md +494 -0
  10. data/ext/deflate_ruby/libdeflate/README.md +228 -0
  11. data/ext/deflate_ruby/libdeflate/common_defs.h +747 -0
  12. data/ext/deflate_ruby/libdeflate/lib/adler32.c +162 -0
  13. data/ext/deflate_ruby/libdeflate/lib/arm/adler32_impl.h +358 -0
  14. data/ext/deflate_ruby/libdeflate/lib/arm/cpu_features.c +230 -0
  15. data/ext/deflate_ruby/libdeflate/lib/arm/cpu_features.h +214 -0
  16. data/ext/deflate_ruby/libdeflate/lib/arm/crc32_impl.h +600 -0
  17. data/ext/deflate_ruby/libdeflate/lib/arm/crc32_pmull_helpers.h +156 -0
  18. data/ext/deflate_ruby/libdeflate/lib/arm/crc32_pmull_wide.h +226 -0
  19. data/ext/deflate_ruby/libdeflate/lib/arm/matchfinder_impl.h +78 -0
  20. data/ext/deflate_ruby/libdeflate/lib/bt_matchfinder.h +342 -0
  21. data/ext/deflate_ruby/libdeflate/lib/cpu_features_common.h +93 -0
  22. data/ext/deflate_ruby/libdeflate/lib/crc32.c +262 -0
  23. data/ext/deflate_ruby/libdeflate/lib/crc32_multipliers.h +377 -0
  24. data/ext/deflate_ruby/libdeflate/lib/crc32_tables.h +587 -0
  25. data/ext/deflate_ruby/libdeflate/lib/decompress_template.h +777 -0
  26. data/ext/deflate_ruby/libdeflate/lib/deflate_compress.c +4129 -0
  27. data/ext/deflate_ruby/libdeflate/lib/deflate_compress.h +15 -0
  28. data/ext/deflate_ruby/libdeflate/lib/deflate_constants.h +56 -0
  29. data/ext/deflate_ruby/libdeflate/lib/deflate_decompress.c +1208 -0
  30. data/ext/deflate_ruby/libdeflate/lib/gzip_compress.c +90 -0
  31. data/ext/deflate_ruby/libdeflate/lib/gzip_constants.h +45 -0
  32. data/ext/deflate_ruby/libdeflate/lib/gzip_decompress.c +144 -0
  33. data/ext/deflate_ruby/libdeflate/lib/hc_matchfinder.h +401 -0
  34. data/ext/deflate_ruby/libdeflate/lib/ht_matchfinder.h +234 -0
  35. data/ext/deflate_ruby/libdeflate/lib/lib_common.h +106 -0
  36. data/ext/deflate_ruby/libdeflate/lib/matchfinder_common.h +224 -0
  37. data/ext/deflate_ruby/libdeflate/lib/riscv/matchfinder_impl.h +97 -0
  38. data/ext/deflate_ruby/libdeflate/lib/utils.c +141 -0
  39. data/ext/deflate_ruby/libdeflate/lib/x86/adler32_impl.h +134 -0
  40. data/ext/deflate_ruby/libdeflate/lib/x86/adler32_template.h +518 -0
  41. data/ext/deflate_ruby/libdeflate/lib/x86/cpu_features.c +183 -0
  42. data/ext/deflate_ruby/libdeflate/lib/x86/cpu_features.h +169 -0
  43. data/ext/deflate_ruby/libdeflate/lib/x86/crc32_impl.h +160 -0
  44. data/ext/deflate_ruby/libdeflate/lib/x86/crc32_pclmul_template.h +495 -0
  45. data/ext/deflate_ruby/libdeflate/lib/x86/decompress_impl.h +57 -0
  46. data/ext/deflate_ruby/libdeflate/lib/x86/matchfinder_impl.h +122 -0
  47. data/ext/deflate_ruby/libdeflate/lib/zlib_compress.c +82 -0
  48. data/ext/deflate_ruby/libdeflate/lib/zlib_constants.h +21 -0
  49. data/ext/deflate_ruby/libdeflate/lib/zlib_decompress.c +104 -0
  50. data/ext/deflate_ruby/libdeflate/libdeflate-config.cmake.in +3 -0
  51. data/ext/deflate_ruby/libdeflate/libdeflate.h +411 -0
  52. data/ext/deflate_ruby/libdeflate/libdeflate.pc.in +18 -0
  53. data/ext/deflate_ruby/libdeflate/programs/CMakeLists.txt +105 -0
  54. data/ext/deflate_ruby/libdeflate/programs/benchmark.c +696 -0
  55. data/ext/deflate_ruby/libdeflate/programs/checksum.c +218 -0
  56. data/ext/deflate_ruby/libdeflate/programs/config.h.in +19 -0
  57. data/ext/deflate_ruby/libdeflate/programs/gzip.c +688 -0
  58. data/ext/deflate_ruby/libdeflate/programs/prog_util.c +521 -0
  59. data/ext/deflate_ruby/libdeflate/programs/prog_util.h +225 -0
  60. data/ext/deflate_ruby/libdeflate/programs/test_checksums.c +200 -0
  61. data/ext/deflate_ruby/libdeflate/programs/test_custom_malloc.c +155 -0
  62. data/ext/deflate_ruby/libdeflate/programs/test_incomplete_codes.c +385 -0
  63. data/ext/deflate_ruby/libdeflate/programs/test_invalid_streams.c +130 -0
  64. data/ext/deflate_ruby/libdeflate/programs/test_litrunlen_overflow.c +72 -0
  65. data/ext/deflate_ruby/libdeflate/programs/test_overread.c +95 -0
  66. data/ext/deflate_ruby/libdeflate/programs/test_slow_decompression.c +472 -0
  67. data/ext/deflate_ruby/libdeflate/programs/test_trailing_bytes.c +151 -0
  68. data/ext/deflate_ruby/libdeflate/programs/test_util.c +237 -0
  69. data/ext/deflate_ruby/libdeflate/programs/test_util.h +61 -0
  70. data/ext/deflate_ruby/libdeflate/programs/tgetopt.c +118 -0
  71. data/ext/deflate_ruby/libdeflate/scripts/android_build.sh +118 -0
  72. data/ext/deflate_ruby/libdeflate/scripts/android_tests.sh +69 -0
  73. data/ext/deflate_ruby/libdeflate/scripts/benchmark.sh +10 -0
  74. data/ext/deflate_ruby/libdeflate/scripts/checksum.sh +10 -0
  75. data/ext/deflate_ruby/libdeflate/scripts/checksum_benchmarks.sh +253 -0
  76. data/ext/deflate_ruby/libdeflate/scripts/cmake-helper.sh +17 -0
  77. data/ext/deflate_ruby/libdeflate/scripts/deflate_benchmarks.sh +119 -0
  78. data/ext/deflate_ruby/libdeflate/scripts/exec_tests.sh +38 -0
  79. data/ext/deflate_ruby/libdeflate/scripts/gen-release-archives.sh +37 -0
  80. data/ext/deflate_ruby/libdeflate/scripts/gen_bitreverse_tab.py +19 -0
  81. data/ext/deflate_ruby/libdeflate/scripts/gen_crc32_multipliers.c +199 -0
  82. data/ext/deflate_ruby/libdeflate/scripts/gen_crc32_tables.c +105 -0
  83. data/ext/deflate_ruby/libdeflate/scripts/gen_default_litlen_costs.py +44 -0
  84. data/ext/deflate_ruby/libdeflate/scripts/gen_offset_slot_map.py +29 -0
  85. data/ext/deflate_ruby/libdeflate/scripts/gzip_tests.sh +523 -0
  86. data/ext/deflate_ruby/libdeflate/scripts/libFuzzer/deflate_compress/corpus/0 +0 -0
  87. data/ext/deflate_ruby/libdeflate/scripts/libFuzzer/deflate_compress/fuzz.c +95 -0
  88. data/ext/deflate_ruby/libdeflate/scripts/libFuzzer/deflate_decompress/corpus/0 +3 -0
  89. data/ext/deflate_ruby/libdeflate/scripts/libFuzzer/deflate_decompress/fuzz.c +62 -0
  90. data/ext/deflate_ruby/libdeflate/scripts/libFuzzer/fuzz.sh +108 -0
  91. data/ext/deflate_ruby/libdeflate/scripts/libFuzzer/gzip_decompress/corpus/0 +0 -0
  92. data/ext/deflate_ruby/libdeflate/scripts/libFuzzer/gzip_decompress/fuzz.c +19 -0
  93. data/ext/deflate_ruby/libdeflate/scripts/libFuzzer/zlib_decompress/corpus/0 +3 -0
  94. data/ext/deflate_ruby/libdeflate/scripts/libFuzzer/zlib_decompress/fuzz.c +19 -0
  95. data/ext/deflate_ruby/libdeflate/scripts/run_tests.sh +416 -0
  96. data/ext/deflate_ruby/libdeflate/scripts/toolchain-i686-w64-mingw32.cmake +8 -0
  97. data/ext/deflate_ruby/libdeflate/scripts/toolchain-x86_64-w64-mingw32.cmake +8 -0
  98. data/lib/deflate_ruby/version.rb +5 -0
  99. data/lib/deflate_ruby.rb +71 -0
  100. metadata +191 -0
@@ -0,0 +1,747 @@
1
+ /*
2
+ * common_defs.h
3
+ *
4
+ * Copyright 2016 Eric Biggers
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person
7
+ * obtaining a copy of this software and associated documentation
8
+ * files (the "Software"), to deal in the Software without
9
+ * restriction, including without limitation the rights to use,
10
+ * copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ * copies of the Software, and to permit persons to whom the
12
+ * Software is furnished to do so, subject to the following
13
+ * conditions:
14
+ *
15
+ * The above copyright notice and this permission notice shall be
16
+ * included in all copies or substantial portions of the Software.
17
+ *
18
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20
+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25
+ * OTHER DEALINGS IN THE SOFTWARE.
26
+ */
27
+
28
+ #ifndef COMMON_DEFS_H
29
+ #define COMMON_DEFS_H
30
+
31
+ #include "libdeflate.h"
32
+
33
+ #include <stdbool.h>
34
+ #include <stddef.h> /* for size_t */
35
+ #include <stdint.h>
36
+ #ifdef _MSC_VER
37
+ # include <intrin.h> /* for _BitScan*() and other intrinsics */
38
+ # include <stdlib.h> /* for _byteswap_*() */
39
+ /* Disable MSVC warnings that are expected. */
40
+ /* /W2 */
41
+ # pragma warning(disable : 4146) /* unary minus on unsigned type */
42
+ /* /W3 */
43
+ # pragma warning(disable : 4018) /* signed/unsigned mismatch */
44
+ # pragma warning(disable : 4244) /* possible loss of data */
45
+ # pragma warning(disable : 4267) /* possible loss of precision */
46
+ # pragma warning(disable : 4310) /* cast truncates constant value */
47
+ /* /W4 */
48
+ # pragma warning(disable : 4100) /* unreferenced formal parameter */
49
+ # pragma warning(disable : 4127) /* conditional expression is constant */
50
+ # pragma warning(disable : 4189) /* local variable initialized but not referenced */
51
+ # pragma warning(disable : 4232) /* nonstandard extension used */
52
+ # pragma warning(disable : 4245) /* conversion from 'int' to 'unsigned int' */
53
+ # pragma warning(disable : 4295) /* array too small to include terminating null */
54
+ #endif
55
+ #ifndef FREESTANDING
56
+ # include <string.h> /* for memcpy() */
57
+ #endif
58
+
59
+ /* ========================================================================== */
60
+ /* Target architecture */
61
+ /* ========================================================================== */
62
+
63
+ /* If possible, define a compiler-independent ARCH_* macro. */
64
+ #undef ARCH_X86_64
65
+ #undef ARCH_X86_32
66
+ #undef ARCH_ARM64
67
+ #undef ARCH_ARM32
68
+ #undef ARCH_RISCV
69
+ #ifdef _MSC_VER
70
+ # if defined(_M_X64)
71
+ # define ARCH_X86_64
72
+ # elif defined(_M_IX86)
73
+ # define ARCH_X86_32
74
+ # elif defined(_M_ARM64)
75
+ # define ARCH_ARM64
76
+ # elif defined(_M_ARM)
77
+ # define ARCH_ARM32
78
+ # endif
79
+ #else
80
+ # if defined(__x86_64__)
81
+ # define ARCH_X86_64
82
+ # elif defined(__i386__)
83
+ # define ARCH_X86_32
84
+ # elif defined(__aarch64__)
85
+ # define ARCH_ARM64
86
+ # elif defined(__arm__)
87
+ # define ARCH_ARM32
88
+ # elif defined(__riscv)
89
+ # define ARCH_RISCV
90
+ # endif
91
+ #endif
92
+
93
+ /* ========================================================================== */
94
+ /* Type definitions */
95
+ /* ========================================================================== */
96
+
97
+ /* Fixed-width integer types */
98
+ typedef uint8_t u8;
99
+ typedef uint16_t u16;
100
+ typedef uint32_t u32;
101
+ typedef uint64_t u64;
102
+ typedef int8_t s8;
103
+ typedef int16_t s16;
104
+ typedef int32_t s32;
105
+ typedef int64_t s64;
106
+
107
+ /* ssize_t, if not available in <sys/types.h> */
108
+ #ifdef _MSC_VER
109
+ # ifdef _WIN64
110
+ typedef long long ssize_t;
111
+ # else
112
+ typedef long ssize_t;
113
+ # endif
114
+ #endif
115
+
116
+ /*
117
+ * Word type of the target architecture. Use 'size_t' instead of
118
+ * 'unsigned long' to account for platforms such as Windows that use 32-bit
119
+ * 'unsigned long' on 64-bit architectures.
120
+ */
121
+ typedef size_t machine_word_t;
122
+
123
+ /* Number of bytes in a word */
124
+ #define WORDBYTES ((int)sizeof(machine_word_t))
125
+
126
+ /* Number of bits in a word */
127
+ #define WORDBITS (8 * WORDBYTES)
128
+
129
+ /* ========================================================================== */
130
+ /* Optional compiler features */
131
+ /* ========================================================================== */
132
+
133
+ /* Compiler version checks. Only use when absolutely necessary. */
134
+ #if defined(__GNUC__) && !defined(__clang__) && !defined(__INTEL_COMPILER)
135
+ # define GCC_PREREQ(major, minor) \
136
+ (__GNUC__ > (major) || \
137
+ (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor)))
138
+ # if !GCC_PREREQ(4, 9)
139
+ # error "gcc versions older than 4.9 are no longer supported"
140
+ # endif
141
+ #else
142
+ # define GCC_PREREQ(major, minor) 0
143
+ #endif
144
+ #ifdef __clang__
145
+ # ifdef __apple_build_version__
146
+ # define CLANG_PREREQ(major, minor, apple_version) \
147
+ (__apple_build_version__ >= (apple_version))
148
+ # else
149
+ # define CLANG_PREREQ(major, minor, apple_version) \
150
+ (__clang_major__ > (major) || \
151
+ (__clang_major__ == (major) && __clang_minor__ >= (minor)))
152
+ # endif
153
+ # if !CLANG_PREREQ(3, 9, 8000000)
154
+ # error "clang versions older than 3.9 are no longer supported"
155
+ # endif
156
+ #else
157
+ # define CLANG_PREREQ(major, minor, apple_version) 0
158
+ #endif
159
+ #ifdef _MSC_VER
160
+ # define MSVC_PREREQ(version) (_MSC_VER >= (version))
161
+ # if !MSVC_PREREQ(1900)
162
+ # error "MSVC versions older than Visual Studio 2015 are no longer supported"
163
+ # endif
164
+ #else
165
+ # define MSVC_PREREQ(version) 0
166
+ #endif
167
+
168
+ /*
169
+ * __has_attribute(attribute) - check whether the compiler supports the given
170
+ * attribute (and also supports doing the check in the first place). Mostly
171
+ * useful just for clang, since gcc didn't add this macro until gcc 5.
172
+ */
173
+ #ifndef __has_attribute
174
+ # define __has_attribute(attribute) 0
175
+ #endif
176
+
177
+ /*
178
+ * __has_builtin(builtin) - check whether the compiler supports the given
179
+ * builtin (and also supports doing the check in the first place). Mostly
180
+ * useful just for clang, since gcc didn't add this macro until gcc 10.
181
+ */
182
+ #ifndef __has_builtin
183
+ # define __has_builtin(builtin) 0
184
+ #endif
185
+
186
+ /* inline - suggest that a function be inlined */
187
+ #ifdef _MSC_VER
188
+ # define inline __inline
189
+ #endif /* else assume 'inline' is usable as-is */
190
+
191
+ /* forceinline - force a function to be inlined, if possible */
192
+ #if defined(__GNUC__) || __has_attribute(always_inline)
193
+ # define forceinline inline __attribute__((always_inline))
194
+ #elif defined(_MSC_VER)
195
+ # define forceinline __forceinline
196
+ #else
197
+ # define forceinline inline
198
+ #endif
199
+
200
+ /* MAYBE_UNUSED - mark a function or variable as maybe unused */
201
+ #if defined(__GNUC__) || __has_attribute(unused)
202
+ # define MAYBE_UNUSED __attribute__((unused))
203
+ #else
204
+ # define MAYBE_UNUSED
205
+ #endif
206
+
207
+ /* NORETURN - mark a function as never returning, e.g. due to calling abort() */
208
+ #if defined(__GNUC__) || __has_attribute(noreturn)
209
+ # define NORETURN __attribute__((noreturn))
210
+ #else
211
+ # define NORETURN
212
+ #endif
213
+
214
+ /*
215
+ * restrict - hint that writes only occur through the given pointer.
216
+ *
217
+ * Don't use MSVC's __restrict, since it has nonstandard behavior.
218
+ * Standard restrict is okay, if it is supported.
219
+ */
220
+ #if !defined(__STDC_VERSION__) || (__STDC_VERSION__ < 201112L)
221
+ # if defined(__GNUC__) || defined(__clang__)
222
+ # define restrict __restrict__
223
+ # else
224
+ # define restrict
225
+ # endif
226
+ #endif /* else assume 'restrict' is usable as-is */
227
+
228
+ /* likely(expr) - hint that an expression is usually true */
229
+ #if defined(__GNUC__) || __has_builtin(__builtin_expect)
230
+ # define likely(expr) __builtin_expect(!!(expr), 1)
231
+ #else
232
+ # define likely(expr) (expr)
233
+ #endif
234
+
235
+ /* unlikely(expr) - hint that an expression is usually false */
236
+ #if defined(__GNUC__) || __has_builtin(__builtin_expect)
237
+ # define unlikely(expr) __builtin_expect(!!(expr), 0)
238
+ #else
239
+ # define unlikely(expr) (expr)
240
+ #endif
241
+
242
+ /* prefetchr(addr) - prefetch into L1 cache for read */
243
+ #undef prefetchr
244
+ #if defined(__GNUC__) || __has_builtin(__builtin_prefetch)
245
+ # define prefetchr(addr) __builtin_prefetch((addr), 0)
246
+ #elif defined(_MSC_VER)
247
+ # if defined(ARCH_X86_32) || defined(ARCH_X86_64)
248
+ # define prefetchr(addr) _mm_prefetch((addr), _MM_HINT_T0)
249
+ # elif defined(ARCH_ARM64)
250
+ # define prefetchr(addr) __prefetch2((addr), 0x00 /* prfop=PLDL1KEEP */)
251
+ # elif defined(ARCH_ARM32)
252
+ # define prefetchr(addr) __prefetch(addr)
253
+ # endif
254
+ #endif
255
+ #ifndef prefetchr
256
+ # define prefetchr(addr)
257
+ #endif
258
+
259
+ /* prefetchw(addr) - prefetch into L1 cache for write */
260
+ #undef prefetchw
261
+ #if defined(__GNUC__) || __has_builtin(__builtin_prefetch)
262
+ # define prefetchw(addr) __builtin_prefetch((addr), 1)
263
+ #elif defined(_MSC_VER)
264
+ # if defined(ARCH_X86_32) || defined(ARCH_X86_64)
265
+ # define prefetchw(addr) _m_prefetchw(addr)
266
+ # elif defined(ARCH_ARM64)
267
+ # define prefetchw(addr) __prefetch2((addr), 0x10 /* prfop=PSTL1KEEP */)
268
+ # elif defined(ARCH_ARM32)
269
+ # define prefetchw(addr) __prefetchw(addr)
270
+ # endif
271
+ #endif
272
+ #ifndef prefetchw
273
+ # define prefetchw(addr)
274
+ #endif
275
+
276
+ /*
277
+ * _aligned_attribute(n) - declare that the annotated variable, or variables of
278
+ * the annotated type, must be aligned on n-byte boundaries.
279
+ */
280
+ #undef _aligned_attribute
281
+ #if defined(__GNUC__) || __has_attribute(aligned)
282
+ # define _aligned_attribute(n) __attribute__((aligned(n)))
283
+ #elif defined(_MSC_VER)
284
+ # define _aligned_attribute(n) __declspec(align(n))
285
+ #endif
286
+
287
+ /*
288
+ * _target_attribute(attrs) - override the compilation target for a function.
289
+ *
290
+ * This accepts one or more comma-separated suffixes to the -m prefix jointly
291
+ * forming the name of a machine-dependent option. On gcc-like compilers, this
292
+ * enables codegen for the given targets, including arbitrary compiler-generated
293
+ * code as well as the corresponding intrinsics. On other compilers this macro
294
+ * expands to nothing, though MSVC allows intrinsics to be used anywhere anyway.
295
+ */
296
+ #if defined(__GNUC__) || __has_attribute(target)
297
+ # define _target_attribute(attrs) __attribute__((target(attrs)))
298
+ #else
299
+ # define _target_attribute(attrs)
300
+ #endif
301
+
302
+ /* ========================================================================== */
303
+ /* Miscellaneous macros */
304
+ /* ========================================================================== */
305
+
306
+ #define ARRAY_LEN(A) (sizeof(A) / sizeof((A)[0]))
307
+ #define MIN(a, b) ((a) <= (b) ? (a) : (b))
308
+ #define MAX(a, b) ((a) >= (b) ? (a) : (b))
309
+ #define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
310
+ #define STATIC_ASSERT(expr) ((void)sizeof(char[1 - 2 * !(expr)]))
311
+ #define ALIGN(n, a) (((n) + (a) - 1) & ~((a) - 1))
312
+ #define ROUND_UP(n, d) ((d) * DIV_ROUND_UP((n), (d)))
313
+
314
+ /* ========================================================================== */
315
+ /* Endianness handling */
316
+ /* ========================================================================== */
317
+
318
+ /*
319
+ * CPU_IS_LITTLE_ENDIAN() - 1 if the CPU is little endian, or 0 if it is big
320
+ * endian. When possible this is a compile-time macro that can be used in
321
+ * preprocessor conditionals. As a fallback, a generic method is used that
322
+ * can't be used in preprocessor conditionals but should still be optimized out.
323
+ */
324
+ #if defined(__BYTE_ORDER__) /* gcc v4.6+ and clang */
325
+ # define CPU_IS_LITTLE_ENDIAN() (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
326
+ #elif defined(_MSC_VER)
327
+ # define CPU_IS_LITTLE_ENDIAN() true
328
+ #else
329
+ static forceinline bool CPU_IS_LITTLE_ENDIAN(void)
330
+ {
331
+ union {
332
+ u32 w;
333
+ u8 b;
334
+ } u;
335
+
336
+ u.w = 1;
337
+ return u.b;
338
+ }
339
+ #endif
340
+
341
+ /* bswap16(v) - swap the bytes of a 16-bit integer */
342
+ static forceinline u16 bswap16(u16 v)
343
+ {
344
+ #if defined(__GNUC__) || __has_builtin(__builtin_bswap16)
345
+ return __builtin_bswap16(v);
346
+ #elif defined(_MSC_VER)
347
+ return _byteswap_ushort(v);
348
+ #else
349
+ return (v << 8) | (v >> 8);
350
+ #endif
351
+ }
352
+
353
+ /* bswap32(v) - swap the bytes of a 32-bit integer */
354
+ static forceinline u32 bswap32(u32 v)
355
+ {
356
+ #if defined(__GNUC__) || __has_builtin(__builtin_bswap32)
357
+ return __builtin_bswap32(v);
358
+ #elif defined(_MSC_VER)
359
+ return _byteswap_ulong(v);
360
+ #else
361
+ return ((v & 0x000000FF) << 24) |
362
+ ((v & 0x0000FF00) << 8) |
363
+ ((v & 0x00FF0000) >> 8) |
364
+ ((v & 0xFF000000) >> 24);
365
+ #endif
366
+ }
367
+
368
+ /* bswap64(v) - swap the bytes of a 64-bit integer */
369
+ static forceinline u64 bswap64(u64 v)
370
+ {
371
+ #if defined(__GNUC__) || __has_builtin(__builtin_bswap64)
372
+ return __builtin_bswap64(v);
373
+ #elif defined(_MSC_VER)
374
+ return _byteswap_uint64(v);
375
+ #else
376
+ return ((v & 0x00000000000000FF) << 56) |
377
+ ((v & 0x000000000000FF00) << 40) |
378
+ ((v & 0x0000000000FF0000) << 24) |
379
+ ((v & 0x00000000FF000000) << 8) |
380
+ ((v & 0x000000FF00000000) >> 8) |
381
+ ((v & 0x0000FF0000000000) >> 24) |
382
+ ((v & 0x00FF000000000000) >> 40) |
383
+ ((v & 0xFF00000000000000) >> 56);
384
+ #endif
385
+ }
386
+
387
+ #define le16_bswap(v) (CPU_IS_LITTLE_ENDIAN() ? (v) : bswap16(v))
388
+ #define le32_bswap(v) (CPU_IS_LITTLE_ENDIAN() ? (v) : bswap32(v))
389
+ #define le64_bswap(v) (CPU_IS_LITTLE_ENDIAN() ? (v) : bswap64(v))
390
+ #define be16_bswap(v) (CPU_IS_LITTLE_ENDIAN() ? bswap16(v) : (v))
391
+ #define be32_bswap(v) (CPU_IS_LITTLE_ENDIAN() ? bswap32(v) : (v))
392
+ #define be64_bswap(v) (CPU_IS_LITTLE_ENDIAN() ? bswap64(v) : (v))
393
+
394
+ /* ========================================================================== */
395
+ /* Unaligned memory accesses */
396
+ /* ========================================================================== */
397
+
398
+ /*
399
+ * UNALIGNED_ACCESS_IS_FAST() - 1 if unaligned memory accesses can be performed
400
+ * efficiently on the target platform, otherwise 0.
401
+ */
402
+ #if (defined(__GNUC__) || defined(__clang__)) && \
403
+ (defined(ARCH_X86_64) || defined(ARCH_X86_32) || \
404
+ defined(__ARM_FEATURE_UNALIGNED) || defined(__powerpc64__) || \
405
+ defined(__riscv_misaligned_fast) || \
406
+ /*
407
+ * For all compilation purposes, WebAssembly behaves like any other CPU
408
+ * instruction set. Even though WebAssembly engine might be running on
409
+ * top of different actual CPU architectures, the WebAssembly spec
410
+ * itself permits unaligned access and it will be fast on most of those
411
+ * platforms, and simulated at the engine level on others, so it's
412
+ * worth treating it as a CPU architecture with fast unaligned access.
413
+ */ defined(__wasm__))
414
+ # define UNALIGNED_ACCESS_IS_FAST 1
415
+ #elif defined(_MSC_VER)
416
+ # define UNALIGNED_ACCESS_IS_FAST 1
417
+ #else
418
+ # define UNALIGNED_ACCESS_IS_FAST 0
419
+ #endif
420
+
421
+ /*
422
+ * Implementing unaligned memory accesses using memcpy() is portable, and it
423
+ * usually gets optimized appropriately by modern compilers. I.e., each
424
+ * memcpy() of 1, 2, 4, or WORDBYTES bytes gets compiled to a load or store
425
+ * instruction, not to an actual function call.
426
+ *
427
+ * We no longer use the "packed struct" approach to unaligned accesses, as that
428
+ * is nonstandard, has unclear semantics, and doesn't receive enough testing
429
+ * (see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94994).
430
+ *
431
+ * arm32 with __ARM_FEATURE_UNALIGNED in gcc 5 and earlier is a known exception
432
+ * where memcpy() generates inefficient code
433
+ * (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67366). However, we no longer
434
+ * consider that one case important enough to maintain different code for.
435
+ * If you run into it, please just use a newer version of gcc (or use clang).
436
+ */
437
+
438
+ #ifdef FREESTANDING
439
+ # define MEMCOPY __builtin_memcpy
440
+ #else
441
+ # define MEMCOPY memcpy
442
+ #endif
443
+
444
+ /* Unaligned loads and stores without endianness conversion */
445
+
446
+ #define DEFINE_UNALIGNED_TYPE(type) \
447
+ static forceinline type \
448
+ load_##type##_unaligned(const void *p) \
449
+ { \
450
+ type v; \
451
+ \
452
+ MEMCOPY(&v, p, sizeof(v)); \
453
+ return v; \
454
+ } \
455
+ \
456
+ static forceinline void \
457
+ store_##type##_unaligned(type v, void *p) \
458
+ { \
459
+ MEMCOPY(p, &v, sizeof(v)); \
460
+ }
461
+
462
+ DEFINE_UNALIGNED_TYPE(u16)
463
+ DEFINE_UNALIGNED_TYPE(u32)
464
+ DEFINE_UNALIGNED_TYPE(u64)
465
+ DEFINE_UNALIGNED_TYPE(machine_word_t)
466
+
467
+ #undef MEMCOPY
468
+
469
+ #define load_word_unaligned load_machine_word_t_unaligned
470
+ #define store_word_unaligned store_machine_word_t_unaligned
471
+
472
+ /* Unaligned loads with endianness conversion */
473
+
474
+ static forceinline u16
475
+ get_unaligned_le16(const u8 *p)
476
+ {
477
+ if (UNALIGNED_ACCESS_IS_FAST)
478
+ return le16_bswap(load_u16_unaligned(p));
479
+ else
480
+ return ((u16)p[1] << 8) | p[0];
481
+ }
482
+
483
+ static forceinline u16
484
+ get_unaligned_be16(const u8 *p)
485
+ {
486
+ if (UNALIGNED_ACCESS_IS_FAST)
487
+ return be16_bswap(load_u16_unaligned(p));
488
+ else
489
+ return ((u16)p[0] << 8) | p[1];
490
+ }
491
+
492
+ static forceinline u32
493
+ get_unaligned_le32(const u8 *p)
494
+ {
495
+ if (UNALIGNED_ACCESS_IS_FAST)
496
+ return le32_bswap(load_u32_unaligned(p));
497
+ else
498
+ return ((u32)p[3] << 24) | ((u32)p[2] << 16) |
499
+ ((u32)p[1] << 8) | p[0];
500
+ }
501
+
502
+ static forceinline u32
503
+ get_unaligned_be32(const u8 *p)
504
+ {
505
+ if (UNALIGNED_ACCESS_IS_FAST)
506
+ return be32_bswap(load_u32_unaligned(p));
507
+ else
508
+ return ((u32)p[0] << 24) | ((u32)p[1] << 16) |
509
+ ((u32)p[2] << 8) | p[3];
510
+ }
511
+
512
+ static forceinline u64
513
+ get_unaligned_le64(const u8 *p)
514
+ {
515
+ if (UNALIGNED_ACCESS_IS_FAST)
516
+ return le64_bswap(load_u64_unaligned(p));
517
+ else
518
+ return ((u64)p[7] << 56) | ((u64)p[6] << 48) |
519
+ ((u64)p[5] << 40) | ((u64)p[4] << 32) |
520
+ ((u64)p[3] << 24) | ((u64)p[2] << 16) |
521
+ ((u64)p[1] << 8) | p[0];
522
+ }
523
+
524
+ static forceinline machine_word_t
525
+ get_unaligned_leword(const u8 *p)
526
+ {
527
+ STATIC_ASSERT(WORDBITS == 32 || WORDBITS == 64);
528
+ if (WORDBITS == 32)
529
+ return get_unaligned_le32(p);
530
+ else
531
+ return get_unaligned_le64(p);
532
+ }
533
+
534
+ /* Unaligned stores with endianness conversion */
535
+
536
+ static forceinline void
537
+ put_unaligned_le16(u16 v, u8 *p)
538
+ {
539
+ if (UNALIGNED_ACCESS_IS_FAST) {
540
+ store_u16_unaligned(le16_bswap(v), p);
541
+ } else {
542
+ p[0] = (u8)(v >> 0);
543
+ p[1] = (u8)(v >> 8);
544
+ }
545
+ }
546
+
547
+ static forceinline void
548
+ put_unaligned_be16(u16 v, u8 *p)
549
+ {
550
+ if (UNALIGNED_ACCESS_IS_FAST) {
551
+ store_u16_unaligned(be16_bswap(v), p);
552
+ } else {
553
+ p[0] = (u8)(v >> 8);
554
+ p[1] = (u8)(v >> 0);
555
+ }
556
+ }
557
+
558
+ static forceinline void
559
+ put_unaligned_le32(u32 v, u8 *p)
560
+ {
561
+ if (UNALIGNED_ACCESS_IS_FAST) {
562
+ store_u32_unaligned(le32_bswap(v), p);
563
+ } else {
564
+ p[0] = (u8)(v >> 0);
565
+ p[1] = (u8)(v >> 8);
566
+ p[2] = (u8)(v >> 16);
567
+ p[3] = (u8)(v >> 24);
568
+ }
569
+ }
570
+
571
+ static forceinline void
572
+ put_unaligned_be32(u32 v, u8 *p)
573
+ {
574
+ if (UNALIGNED_ACCESS_IS_FAST) {
575
+ store_u32_unaligned(be32_bswap(v), p);
576
+ } else {
577
+ p[0] = (u8)(v >> 24);
578
+ p[1] = (u8)(v >> 16);
579
+ p[2] = (u8)(v >> 8);
580
+ p[3] = (u8)(v >> 0);
581
+ }
582
+ }
583
+
584
+ static forceinline void
585
+ put_unaligned_le64(u64 v, u8 *p)
586
+ {
587
+ if (UNALIGNED_ACCESS_IS_FAST) {
588
+ store_u64_unaligned(le64_bswap(v), p);
589
+ } else {
590
+ p[0] = (u8)(v >> 0);
591
+ p[1] = (u8)(v >> 8);
592
+ p[2] = (u8)(v >> 16);
593
+ p[3] = (u8)(v >> 24);
594
+ p[4] = (u8)(v >> 32);
595
+ p[5] = (u8)(v >> 40);
596
+ p[6] = (u8)(v >> 48);
597
+ p[7] = (u8)(v >> 56);
598
+ }
599
+ }
600
+
601
+ static forceinline void
602
+ put_unaligned_leword(machine_word_t v, u8 *p)
603
+ {
604
+ STATIC_ASSERT(WORDBITS == 32 || WORDBITS == 64);
605
+ if (WORDBITS == 32)
606
+ put_unaligned_le32(v, p);
607
+ else
608
+ put_unaligned_le64(v, p);
609
+ }
610
+
611
+ /* ========================================================================== */
612
+ /* Bit manipulation functions */
613
+ /* ========================================================================== */
614
+
615
+ /*
616
+ * Bit Scan Reverse (BSR) - find the 0-based index (relative to the least
617
+ * significant end) of the *most* significant 1 bit in the input value. The
618
+ * input value must be nonzero!
619
+ */
620
+
621
+ static forceinline unsigned
622
+ bsr32(u32 v)
623
+ {
624
+ #if defined(__GNUC__) || __has_builtin(__builtin_clz)
625
+ return 31 - __builtin_clz(v);
626
+ #elif defined(_MSC_VER)
627
+ unsigned long i;
628
+
629
+ _BitScanReverse(&i, v);
630
+ return i;
631
+ #else
632
+ unsigned i = 0;
633
+
634
+ while ((v >>= 1) != 0)
635
+ i++;
636
+ return i;
637
+ #endif
638
+ }
639
+
640
+ static forceinline unsigned
641
+ bsr64(u64 v)
642
+ {
643
+ #if defined(__GNUC__) || __has_builtin(__builtin_clzll)
644
+ return 63 - __builtin_clzll(v);
645
+ #elif defined(_MSC_VER) && defined(_WIN64)
646
+ unsigned long i;
647
+
648
+ _BitScanReverse64(&i, v);
649
+ return i;
650
+ #else
651
+ unsigned i = 0;
652
+
653
+ while ((v >>= 1) != 0)
654
+ i++;
655
+ return i;
656
+ #endif
657
+ }
658
+
659
+ static forceinline unsigned
660
+ bsrw(machine_word_t v)
661
+ {
662
+ STATIC_ASSERT(WORDBITS == 32 || WORDBITS == 64);
663
+ if (WORDBITS == 32)
664
+ return bsr32(v);
665
+ else
666
+ return bsr64(v);
667
+ }
668
+
669
+ /*
670
+ * Bit Scan Forward (BSF) - find the 0-based index (relative to the least
671
+ * significant end) of the *least* significant 1 bit in the input value. The
672
+ * input value must be nonzero!
673
+ */
674
+
675
+ static forceinline unsigned
676
+ bsf32(u32 v)
677
+ {
678
+ #if defined(__GNUC__) || __has_builtin(__builtin_ctz)
679
+ return __builtin_ctz(v);
680
+ #elif defined(_MSC_VER)
681
+ unsigned long i;
682
+
683
+ _BitScanForward(&i, v);
684
+ return i;
685
+ #else
686
+ unsigned i = 0;
687
+
688
+ for (; (v & 1) == 0; v >>= 1)
689
+ i++;
690
+ return i;
691
+ #endif
692
+ }
693
+
694
+ static forceinline unsigned
695
+ bsf64(u64 v)
696
+ {
697
+ #if defined(__GNUC__) || __has_builtin(__builtin_ctzll)
698
+ return __builtin_ctzll(v);
699
+ #elif defined(_MSC_VER) && defined(_WIN64)
700
+ unsigned long i;
701
+
702
+ _BitScanForward64(&i, v);
703
+ return i;
704
+ #else
705
+ unsigned i = 0;
706
+
707
+ for (; (v & 1) == 0; v >>= 1)
708
+ i++;
709
+ return i;
710
+ #endif
711
+ }
712
+
713
+ static forceinline unsigned
714
+ bsfw(machine_word_t v)
715
+ {
716
+ STATIC_ASSERT(WORDBITS == 32 || WORDBITS == 64);
717
+ if (WORDBITS == 32)
718
+ return bsf32(v);
719
+ else
720
+ return bsf64(v);
721
+ }
722
+
723
+ /*
724
+ * rbit32(v): reverse the bits in a 32-bit integer. This doesn't have a
725
+ * fallback implementation; use '#ifdef rbit32' to check if this is available.
726
+ */
727
+ #undef rbit32
728
+ #if (defined(__GNUC__) || defined(__clang__)) && defined(ARCH_ARM32) && \
729
+ (__ARM_ARCH >= 7 || (__ARM_ARCH == 6 && defined(__ARM_ARCH_6T2__)))
730
+ static forceinline u32
731
+ rbit32(u32 v)
732
+ {
733
+ __asm__("rbit %0, %1" : "=r" (v) : "r" (v));
734
+ return v;
735
+ }
736
+ #define rbit32 rbit32
737
+ #elif (defined(__GNUC__) || defined(__clang__)) && defined(ARCH_ARM64)
738
+ static forceinline u32
739
+ rbit32(u32 v)
740
+ {
741
+ __asm__("rbit %w0, %w1" : "=r" (v) : "r" (v));
742
+ return v;
743
+ }
744
+ #define rbit32 rbit32
745
+ #endif
746
+
747
+ #endif /* COMMON_DEFS_H */