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