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