simdjson  3.9.1
Ridiculously Fast JSON
common_defs.h
1 #ifndef SIMDJSON_COMMON_DEFS_H
2 #define SIMDJSON_COMMON_DEFS_H
3 
4 #include <cassert>
5 #include "simdjson/compiler_check.h"
6 #include "simdjson/portability.h"
7 
8 namespace simdjson {
9 namespace internal {
15 char *to_chars(char *first, const char *last, double value);
21 double from_chars(const char *first) noexcept;
22 double from_chars(const char *first, const char* end) noexcept;
23 }
24 
25 #ifndef SIMDJSON_EXCEPTIONS
26 #if __cpp_exceptions
27 #define SIMDJSON_EXCEPTIONS 1
28 #else
29 #define SIMDJSON_EXCEPTIONS 0
30 #endif
31 #endif
32 
33 } // namespace simdjson
34 
35 #if defined(__GNUC__)
36  // Marks a block with a name so that MCA analysis can see it.
37  #define SIMDJSON_BEGIN_DEBUG_BLOCK(name) __asm volatile("# LLVM-MCA-BEGIN " #name);
38  #define SIMDJSON_END_DEBUG_BLOCK(name) __asm volatile("# LLVM-MCA-END " #name);
39  #define SIMDJSON_DEBUG_BLOCK(name, block) BEGIN_DEBUG_BLOCK(name); block; END_DEBUG_BLOCK(name);
40 #else
41  #define SIMDJSON_BEGIN_DEBUG_BLOCK(name)
42  #define SIMDJSON_END_DEBUG_BLOCK(name)
43  #define SIMDJSON_DEBUG_BLOCK(name, block)
44 #endif
45 
46 // Align to N-byte boundary
47 #define SIMDJSON_ROUNDUP_N(a, n) (((a) + ((n)-1)) & ~((n)-1))
48 #define SIMDJSON_ROUNDDOWN_N(a, n) ((a) & ~((n)-1))
49 
50 #define SIMDJSON_ISALIGNED_N(ptr, n) (((uintptr_t)(ptr) & ((n)-1)) == 0)
51 
52 #if SIMDJSON_REGULAR_VISUAL_STUDIO
53 
54  #define simdjson_really_inline __forceinline
55  #define simdjson_never_inline __declspec(noinline)
56 
57  #define simdjson_unused
58  #define simdjson_warn_unused
59 
60  #ifndef simdjson_likely
61  #define simdjson_likely(x) x
62  #endif
63  #ifndef simdjson_unlikely
64  #define simdjson_unlikely(x) x
65  #endif
66 
67  #define SIMDJSON_PUSH_DISABLE_WARNINGS __pragma(warning( push ))
68  #define SIMDJSON_PUSH_DISABLE_ALL_WARNINGS __pragma(warning( push, 0 ))
69  #define SIMDJSON_DISABLE_VS_WARNING(WARNING_NUMBER) __pragma(warning( disable : WARNING_NUMBER ))
70  // Get rid of Intellisense-only warnings (Code Analysis)
71  // Though __has_include is C++17, it is supported in Visual Studio 2017 or better (_MSC_VER>=1910).
72  #ifdef __has_include
73  #if __has_include(<CppCoreCheck\Warnings.h>)
74  #include <CppCoreCheck\Warnings.h>
75  #define SIMDJSON_DISABLE_UNDESIRED_WARNINGS SIMDJSON_DISABLE_VS_WARNING(ALL_CPPCORECHECK_WARNINGS)
76  #endif
77  #endif
78 
79  #ifndef SIMDJSON_DISABLE_UNDESIRED_WARNINGS
80  #define SIMDJSON_DISABLE_UNDESIRED_WARNINGS
81  #endif
82 
83  #define SIMDJSON_DISABLE_DEPRECATED_WARNING SIMDJSON_DISABLE_VS_WARNING(4996)
84  #define SIMDJSON_DISABLE_STRICT_OVERFLOW_WARNING
85  #define SIMDJSON_POP_DISABLE_WARNINGS __pragma(warning( pop ))
86 
87  #define SIMDJSON_PUSH_DISABLE_UNUSED_WARNINGS
88  #define SIMDJSON_POP_DISABLE_UNUSED_WARNINGS
89 
90 #else // SIMDJSON_REGULAR_VISUAL_STUDIO
91 
92  #define simdjson_really_inline inline __attribute__((always_inline))
93  #define simdjson_never_inline inline __attribute__((noinline))
94 
95  #define simdjson_unused __attribute__((unused))
96  #define simdjson_warn_unused __attribute__((warn_unused_result))
97 
98  #ifndef simdjson_likely
99  #define simdjson_likely(x) __builtin_expect(!!(x), 1)
100  #endif
101  #ifndef simdjson_unlikely
102  #define simdjson_unlikely(x) __builtin_expect(!!(x), 0)
103  #endif
104 
105  #define SIMDJSON_PUSH_DISABLE_WARNINGS _Pragma("GCC diagnostic push")
106  // gcc doesn't seem to disable all warnings with all and extra, add warnings here as necessary
107  // We do it separately for clang since it has different warnings.
108  #ifdef __clang__
109  // clang is missing -Wmaybe-uninitialized.
110  #define SIMDJSON_PUSH_DISABLE_ALL_WARNINGS SIMDJSON_PUSH_DISABLE_WARNINGS \
111  SIMDJSON_DISABLE_GCC_WARNING(-Weffc++) \
112  SIMDJSON_DISABLE_GCC_WARNING(-Wall) \
113  SIMDJSON_DISABLE_GCC_WARNING(-Wconversion) \
114  SIMDJSON_DISABLE_GCC_WARNING(-Wextra) \
115  SIMDJSON_DISABLE_GCC_WARNING(-Wattributes) \
116  SIMDJSON_DISABLE_GCC_WARNING(-Wimplicit-fallthrough) \
117  SIMDJSON_DISABLE_GCC_WARNING(-Wnon-virtual-dtor) \
118  SIMDJSON_DISABLE_GCC_WARNING(-Wreturn-type) \
119  SIMDJSON_DISABLE_GCC_WARNING(-Wshadow) \
120  SIMDJSON_DISABLE_GCC_WARNING(-Wunused-parameter) \
121  SIMDJSON_DISABLE_GCC_WARNING(-Wunused-variable)
122  #else // __clang__
123  #define SIMDJSON_PUSH_DISABLE_ALL_WARNINGS SIMDJSON_PUSH_DISABLE_WARNINGS \
124  SIMDJSON_DISABLE_GCC_WARNING(-Weffc++) \
125  SIMDJSON_DISABLE_GCC_WARNING(-Wall) \
126  SIMDJSON_DISABLE_GCC_WARNING(-Wconversion) \
127  SIMDJSON_DISABLE_GCC_WARNING(-Wextra) \
128  SIMDJSON_DISABLE_GCC_WARNING(-Wattributes) \
129  SIMDJSON_DISABLE_GCC_WARNING(-Wimplicit-fallthrough) \
130  SIMDJSON_DISABLE_GCC_WARNING(-Wnon-virtual-dtor) \
131  SIMDJSON_DISABLE_GCC_WARNING(-Wreturn-type) \
132  SIMDJSON_DISABLE_GCC_WARNING(-Wshadow) \
133  SIMDJSON_DISABLE_GCC_WARNING(-Wunused-parameter) \
134  SIMDJSON_DISABLE_GCC_WARNING(-Wunused-variable) \
135  SIMDJSON_DISABLE_GCC_WARNING(-Wmaybe-uninitialized) \
136  SIMDJSON_DISABLE_GCC_WARNING(-Wformat-security)
137  #endif // __clang__
138 
139  #define SIMDJSON_PRAGMA(P) _Pragma(#P)
140  #define SIMDJSON_DISABLE_GCC_WARNING(WARNING) SIMDJSON_PRAGMA(GCC diagnostic ignored #WARNING)
141  #if SIMDJSON_CLANG_VISUAL_STUDIO
142  #define SIMDJSON_DISABLE_UNDESIRED_WARNINGS SIMDJSON_DISABLE_GCC_WARNING(-Wmicrosoft-include)
143  #else
144  #define SIMDJSON_DISABLE_UNDESIRED_WARNINGS
145  #endif
146  #define SIMDJSON_DISABLE_DEPRECATED_WARNING SIMDJSON_DISABLE_GCC_WARNING(-Wdeprecated-declarations)
147  #define SIMDJSON_DISABLE_STRICT_OVERFLOW_WARNING SIMDJSON_DISABLE_GCC_WARNING(-Wstrict-overflow)
148  #define SIMDJSON_POP_DISABLE_WARNINGS _Pragma("GCC diagnostic pop")
149 
150  #define SIMDJSON_PUSH_DISABLE_UNUSED_WARNINGS SIMDJSON_PUSH_DISABLE_WARNINGS \
151  SIMDJSON_DISABLE_GCC_WARNING(-Wunused)
152  #define SIMDJSON_POP_DISABLE_UNUSED_WARNINGS SIMDJSON_POP_DISABLE_WARNINGS
153 
154 
155 
156 #endif // MSC_VER
157 
158 #if defined(simdjson_inline)
159  // Prefer the user's definition of simdjson_inline; don't define it ourselves.
160 #elif defined(__GNUC__) && !defined(__OPTIMIZE__)
161  // If optimizations are disabled, forcing inlining can lead to significant
162  // code bloat and high compile times. Don't use simdjson_really_inline for
163  // unoptimized builds.
164  #define simdjson_inline inline
165 #else
166  // Force inlining for most simdjson functions.
167  #define simdjson_inline simdjson_really_inline
168 #endif
169 
170 #if SIMDJSON_VISUAL_STUDIO
186  #if SIMDJSON_BUILDING_WINDOWS_DYNAMIC_LIBRARY
187  // We set SIMDJSON_BUILDING_WINDOWS_DYNAMIC_LIBRARY when we build a DLL under Windows.
188  // It should never happen that both SIMDJSON_BUILDING_WINDOWS_DYNAMIC_LIBRARY and
189  // SIMDJSON_USING_WINDOWS_DYNAMIC_LIBRARY are set.
190  #define SIMDJSON_DLLIMPORTEXPORT __declspec(dllexport)
191  #elif SIMDJSON_USING_WINDOWS_DYNAMIC_LIBRARY
192  // Windows user who call a dynamic library should set SIMDJSON_USING_WINDOWS_DYNAMIC_LIBRARY to 1.
193  #define SIMDJSON_DLLIMPORTEXPORT __declspec(dllimport)
194  #else
195  // We assume by default static linkage
196  #define SIMDJSON_DLLIMPORTEXPORT
197  #endif
198 
203 #if SIMDJSON_USING_LIBRARY
204 #define SIMDJSON_DLLIMPORTEXPORT __declspec(dllimport)
205 #endif
209 #else
210  #define SIMDJSON_DLLIMPORTEXPORT
211 #endif
212 
213 // C++17 requires string_view.
214 #if SIMDJSON_CPLUSPLUS17
215 #define SIMDJSON_HAS_STRING_VIEW
216 #include <string_view> // by the standard, this has to be safe.
217 #endif
218 
219 // This macro (__cpp_lib_string_view) has to be defined
220 // for C++17 and better, but if it is otherwise defined,
221 // we are going to assume that string_view is available
222 // even if we do not have C++17 support.
223 #ifdef __cpp_lib_string_view
224 #define SIMDJSON_HAS_STRING_VIEW
225 #endif
226 
227 // Some systems have string_view even if we do not have C++17 support,
228 // and even if __cpp_lib_string_view is undefined, it is the case
229 // with Apple clang version 11.
230 // We must handle it. *This is important.*
231 #ifndef SIMDJSON_HAS_STRING_VIEW
232 #if defined __has_include
233 // do not combine the next #if with the previous one (unsafe)
234 #if __has_include (<string_view>)
235 // now it is safe to trigger the include
236 #include <string_view> // though the file is there, it does not follow that we got the implementation
237 #if defined(_LIBCPP_STRING_VIEW)
238 // Ah! So we under libc++ which under its Library Fundamentals Technical Specification, which preceded C++17,
239 // included string_view.
240 // This means that we have string_view *even though* we may not have C++17.
241 #define SIMDJSON_HAS_STRING_VIEW
242 #endif // _LIBCPP_STRING_VIEW
243 #endif // __has_include (<string_view>)
244 #endif // defined __has_include
245 #endif // def SIMDJSON_HAS_STRING_VIEW
246 // end of complicated but important routine to try to detect string_view.
247 
248 //
249 // Backfill std::string_view using nonstd::string_view on systems where
250 // we expect that string_view is missing. Important: if we get this wrong,
251 // we will end up with two string_view definitions and potential trouble.
252 // That is why we work so hard above to avoid it.
253 //
254 #ifndef SIMDJSON_HAS_STRING_VIEW
255 SIMDJSON_PUSH_DISABLE_ALL_WARNINGS
256 #include "simdjson/nonstd/string_view.hpp"
257 SIMDJSON_POP_DISABLE_WARNINGS
258 
259 namespace std {
260  using string_view = nonstd::string_view;
261 }
262 #endif // SIMDJSON_HAS_STRING_VIEW
263 #undef SIMDJSON_HAS_STRING_VIEW // We are not going to need this macro anymore.
264 
266 #define SIMDJSON_TRY(EXPR) { auto _err = (EXPR); if (_err) { return _err; } }
267 
268 // Unless the programmer has already set SIMDJSON_DEVELOPMENT_CHECKS,
269 // we want to set it under debug builds. We detect a debug build
270 // under Visual Studio when the _DEBUG macro is set. Under the other
271 // compilers, we use the fact that they define __OPTIMIZE__ whenever
272 // they allow optimizations.
273 // It is possible that this could miss some cases where SIMDJSON_DEVELOPMENT_CHECKS
274 // is helpful, but the programmer can set the macro SIMDJSON_DEVELOPMENT_CHECKS.
275 // It could also wrongly set SIMDJSON_DEVELOPMENT_CHECKS (e.g., if the programmer
276 // sets _DEBUG in a release build under Visual Studio, or if some compiler fails to
277 // set the __OPTIMIZE__ macro).
278 #ifndef SIMDJSON_DEVELOPMENT_CHECKS
279 #ifdef _MSC_VER
280 // Visual Studio seems to set _DEBUG for debug builds.
281 #ifdef _DEBUG
282 #define SIMDJSON_DEVELOPMENT_CHECKS 1
283 #endif // _DEBUG
284 #else // _MSC_VER
285 // All other compilers appear to set __OPTIMIZE__ to a positive integer
286 // when the compiler is optimizing.
287 #ifndef __OPTIMIZE__
288 #define SIMDJSON_DEVELOPMENT_CHECKS 1
289 #endif // __OPTIMIZE__
290 #endif // _MSC_VER
291 #endif // SIMDJSON_DEVELOPMENT_CHECKS
292 
293 // The SIMDJSON_CHECK_EOF macro is a feature flag for the "don't require padding"
294 // feature.
295 
296 #if SIMDJSON_CPLUSPLUS17
297 // if we have C++, then fallthrough is a default attribute
298 # define simdjson_fallthrough [[fallthrough]]
299 // check if we have __attribute__ support
300 #elif defined(__has_attribute)
301 // check if we have the __fallthrough__ attribute
302 #if __has_attribute(__fallthrough__)
303 // we are good to go:
304 # define simdjson_fallthrough __attribute__((__fallthrough__))
305 #endif // __has_attribute(__fallthrough__)
306 #endif // SIMDJSON_CPLUSPLUS17
307 // on some systems, we simply do not have support for fallthrough, so use a default:
308 #ifndef simdjson_fallthrough
309 # define simdjson_fallthrough do {} while (0) /* fallthrough */
310 #endif // simdjson_fallthrough
311 
312 #if SIMDJSON_DEVELOPMENT_CHECKS
313 #define SIMDJSON_DEVELOPMENT_ASSERT(expr) do { assert ((expr)); } while (0)
314 #else
315 #define SIMDJSON_DEVELOPMENT_ASSERT(expr) do { } while (0)
316 #endif
317 
318 #ifndef SIMDJSON_UTF8VALIDATION
319 #define SIMDJSON_UTF8VALIDATION 1
320 #endif
321 
322 #ifdef __has_include
323 // How do we detect that a compiler supports vbmi2?
324 // For sure if the following header is found, we are ok?
325 #if __has_include(<avx512vbmi2intrin.h>)
326 #define SIMDJSON_COMPILER_SUPPORTS_VBMI2 1
327 #endif
328 #endif
329 
330 #ifdef _MSC_VER
331 #if _MSC_VER >= 1920
332 // Visual Studio 2019 and up support VBMI2 under x64 even if the header
333 // avx512vbmi2intrin.h is not found.
334 #define SIMDJSON_COMPILER_SUPPORTS_VBMI2 1
335 #endif
336 #endif
337 
338 // By default, we allow AVX512.
339 #ifndef SIMDJSON_AVX512_ALLOWED
340 #define SIMDJSON_AVX512_ALLOWED 1
341 #endif
342 
343 #endif // SIMDJSON_COMMON_DEFS_H
The top level simdjson namespace, containing everything the library provides.
Definition: base.h:8