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