simdutf 6.1.1
Unicode at GB/s.
Loading...
Searching...
No Matches
portability.h
1#ifndef SIMDUTF_PORTABILITY_H
2#define SIMDUTF_PORTABILITY_H
3
4#include "simdutf/compiler_check.h"
5
6#include <cfloat>
7#include <cstddef>
8#include <cstdint>
9#include <cstdlib>
10#ifndef _WIN32
11 // strcasecmp, strncasecmp
12 #include <strings.h>
13#endif
14
15#if defined(__apple_build_version__)
16 #if __apple_build_version__ < 14000000
17 #define SIMDUTF_SPAN_DISABLED \
18 1 // apple-clang/13 doesn't support std::convertible_to
19 #endif
20#endif
21
22#if SIMDUTF_CPLUSPLUS20
23 #include <version>
24 #if __cpp_concepts >= 201907L && __cpp_lib_span >= 202002L && \
25 !defined(SIMDUTF_SPAN_DISABLED)
26 #define SIMDUTF_SPAN 1
27 #endif
28#endif
29
35#if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__)
36 #define SIMDUTF_IS_BIG_ENDIAN (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
37#elif defined(_WIN32)
38 #define SIMDUTF_IS_BIG_ENDIAN 0
39#else
40 #if defined(__APPLE__) || \
41 defined(__FreeBSD__) // defined __BYTE_ORDER__ && defined
42 // __ORDER_BIG_ENDIAN__
43 #include <machine/endian.h>
44 #elif defined(sun) || \
45 defined(__sun) // defined(__APPLE__) || defined(__FreeBSD__)
46 #include <sys/byteorder.h>
47 #else // defined(__APPLE__) || defined(__FreeBSD__)
48
49 #ifdef __has_include
50 #if __has_include(<endian.h>)
51 #include <endian.h>
52 #endif //__has_include(<endian.h>)
53 #endif //__has_include
54
55 #endif // defined(__APPLE__) || defined(__FreeBSD__)
56
57 #ifndef !defined(__BYTE_ORDER__) || !defined(__ORDER_LITTLE_ENDIAN__)
58 #define SIMDUTF_IS_BIG_ENDIAN 0
59 #endif
60
61 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
62 #define SIMDUTF_IS_BIG_ENDIAN 0
63 #else // __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
64 #define SIMDUTF_IS_BIG_ENDIAN 1
65 #endif // __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
66
67#endif // defined __BYTE_ORDER__ && defined __ORDER_BIG_ENDIAN__
68
73#ifdef _MSC_VER
74 #define SIMDUTF_VISUAL_STUDIO 1
85 #ifdef __clang__
86 // clang under visual studio
87 #define SIMDUTF_CLANG_VISUAL_STUDIO 1
88 #else
89 // just regular visual studio (best guess)
90 #define SIMDUTF_REGULAR_VISUAL_STUDIO 1
91 #endif // __clang__
92#endif // _MSC_VER
93
94#ifdef SIMDUTF_REGULAR_VISUAL_STUDIO
95 // https://en.wikipedia.org/wiki/C_alternative_tokens
96 // This header should have no effect, except maybe
97 // under Visual Studio.
98 #include <iso646.h>
99#endif
100
101#if (defined(__x86_64__) || defined(_M_AMD64)) && !defined(_M_ARM64EC)
102 #define SIMDUTF_IS_X86_64 1
103#elif defined(__aarch64__) || defined(_M_ARM64) || defined(_M_ARM64EC)
104 #define SIMDUTF_IS_ARM64 1
105#elif defined(__PPC64__) || defined(_M_PPC64)
106// #define SIMDUTF_IS_PPC64 1
107// The simdutf library does yet support SIMD acceleration under
108// POWER processors. Please see https://github.com/lemire/simdutf/issues/51
109#elif defined(__s390__)
110// s390 IBM system. Big endian.
111#elif (defined(__riscv) || defined(__riscv__)) && __riscv_xlen == 64
112 // RISC-V 64-bit
113 #define SIMDUTF_IS_RISCV64 1
114
115 // #if __riscv_v_intrinsic >= 1000000
116 // #define SIMDUTF_HAS_RVV_INTRINSICS 1
117 // #define SIMDUTF_HAS_RVV_TARGET_REGION 1
118 // #elif ...
119 // Check for special compiler versions that implement pre v1.0 intrinsics
120 #if __riscv_v_intrinsic >= 11000
121 #define SIMDUTF_HAS_RVV_INTRINSICS 1
122 #endif
123
124 #define SIMDUTF_HAS_ZVBB_INTRINSICS \
125 0 // there is currently no way to detect this
126
127 #if SIMDUTF_HAS_RVV_INTRINSICS && __riscv_vector && \
128 __riscv_v_min_vlen >= 128 && __riscv_v_elen >= 64
129 // RISC-V V extension
130 #define SIMDUTF_IS_RVV 1
131 #if SIMDUTF_HAS_ZVBB_INTRINSICS && __riscv_zvbb >= 1000000
132 // RISC-V Vector Basic Bit-manipulation
133 #define SIMDUTF_IS_ZVBB 1
134 #endif
135 #endif
136
137#elif defined(__loongarch_lp64)
138 #if defined(__loongarch_sx) && defined(__loongarch_asx)
139 #define SIMDUTF_IS_LSX 1
140 #define SIMDUTF_IS_LASX 1
141 #elif defined(__loongarch_sx)
142 #define SIMDUTF_IS_LSX 1
143 #endif
144#else
145 // The simdutf library is designed
146 // for 64-bit processors and it seems that you are not
147 // compiling for a known 64-bit platform. Please
148 // use a 64-bit target such as x64 or 64-bit ARM for best performance.
149 #define SIMDUTF_IS_32BITS 1
150
151 // We do not support 32-bit platforms, but it can be
152 // handy to identify them.
153 #if defined(_M_IX86) || defined(__i386__)
154 #define SIMDUTF_IS_X86_32BITS 1
155 #elif defined(__arm__) || defined(_M_ARM)
156 #define SIMDUTF_IS_ARM_32BITS 1
157 #elif defined(__PPC__) || defined(_M_PPC)
158 #define SIMDUTF_IS_PPC_32BITS 1
159 #endif
160
161#endif // defined(__x86_64__) || defined(_M_AMD64)
162
163#ifdef SIMDUTF_IS_32BITS
164 #ifndef SIMDUTF_NO_PORTABILITY_WARNING
165 // In the future, we may want to warn users of 32-bit systems that
166 // the simdutf does not support accelerated kernels for such systems.
167 #endif // SIMDUTF_NO_PORTABILITY_WARNING
168#endif // SIMDUTF_IS_32BITS
169
170// this is almost standard?
171#define SIMDUTF_STRINGIFY_IMPLEMENTATION_(a) #a
172#define SIMDUTF_STRINGIFY(a) SIMDUTF_STRINGIFY_IMPLEMENTATION_(a)
173
174// Our fast kernels require 64-bit systems.
175//
176// On 32-bit x86, we lack 64-bit popcnt, lzcnt, blsr instructions.
177// Furthermore, the number of SIMD registers is reduced.
178//
179// On 32-bit ARM, we would have smaller registers.
180//
181// The simdutf users should still have the fallback kernel. It is
182// slower, but it should run everywhere.
183
184//
185// Enable valid runtime implementations, and select
186// SIMDUTF_BUILTIN_IMPLEMENTATION
187//
188
189// We are going to use runtime dispatch.
190#ifdef SIMDUTF_IS_X86_64
191 #ifdef __clang__
192 // clang does not have GCC push pop
193 // warning: clang attribute push can't be used within a namespace in clang
194 // up til 8.0 so SIMDUTF_TARGET_REGION and SIMDUTF_UNTARGET_REGION must be
195 // *outside* of a namespace.
196 #define SIMDUTF_TARGET_REGION(T) \
197 _Pragma(SIMDUTF_STRINGIFY(clang attribute push( \
198 __attribute__((target(T))), apply_to = function)))
199 #define SIMDUTF_UNTARGET_REGION _Pragma("clang attribute pop")
200 #elif defined(__GNUC__)
201 // GCC is easier
202 #define SIMDUTF_TARGET_REGION(T) \
203 _Pragma("GCC push_options") _Pragma(SIMDUTF_STRINGIFY(GCC target(T)))
204 #define SIMDUTF_UNTARGET_REGION _Pragma("GCC pop_options")
205 #endif // clang then gcc
206
207#endif // x86
208
209// Default target region macros don't do anything.
210#ifndef SIMDUTF_TARGET_REGION
211 #define SIMDUTF_TARGET_REGION(T)
212 #define SIMDUTF_UNTARGET_REGION
213#endif
214
215// Is threading enabled?
216#if defined(_REENTRANT) || defined(_MT)
217 #ifndef SIMDUTF_THREADS_ENABLED
218 #define SIMDUTF_THREADS_ENABLED
219 #endif
220#endif
221
222// workaround for large stack sizes under -O0.
223// https://github.com/simdutf/simdutf/issues/691
224#ifdef __APPLE__
225 #ifndef __OPTIMIZE__
226 // Apple systems have small stack sizes in secondary threads.
227 // Lack of compiler optimization may generate high stack usage.
228 // Users may want to disable threads for safety, but only when
229 // in debug mode which we detect by the fact that the __OPTIMIZE__
230 // macro is not defined.
231 #undef SIMDUTF_THREADS_ENABLED
232 #endif
233#endif
234
235#ifdef SIMDUTF_VISUAL_STUDIO
236 // This is one case where we do not distinguish between
237 // regular visual studio and clang under visual studio.
238 // clang under Windows has _stricmp (like visual studio) but not strcasecmp
239 // (as clang normally has)
240 #define simdutf_strcasecmp _stricmp
241 #define simdutf_strncasecmp _strnicmp
242#else
243 // The strcasecmp, strncasecmp, and strcasestr functions do not work with
244 // multibyte strings (e.g. UTF-8). So they are only useful for ASCII in our
245 // context.
246 // https://www.gnu.org/software/libunistring/manual/libunistring.html#char-_002a-strings
247 #define simdutf_strcasecmp strcasecmp
248 #define simdutf_strncasecmp strncasecmp
249#endif
250
251#if defined(__GNUC__) && !defined(__clang__)
252 #if __GNUC__ >= 11
253 #define SIMDUTF_GCC11ORMORE 1
254 #endif // __GNUC__ >= 11
255#endif // defined(__GNUC__) && !defined(__clang__)
256
257#endif // SIMDUTF_PORTABILITY_H