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