simdjson  3.11.0
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  // 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 
207 #if SIMDJSON_USING_LIBRARY
208 #define SIMDJSON_DLLIMPORTEXPORT __declspec(dllimport)
209 #endif
213 #else
214  #define SIMDJSON_DLLIMPORTEXPORT
215 #endif
216 
217 // C++17 requires string_view.
218 #if SIMDJSON_CPLUSPLUS17
219 #define SIMDJSON_HAS_STRING_VIEW
220 #include <string_view> // by the standard, this has to be safe.
221 #endif
222 
223 // This macro (__cpp_lib_string_view) has to be defined
224 // for C++17 and better, but if it is otherwise defined,
225 // we are going to assume that string_view is available
226 // even if we do not have C++17 support.
227 #ifdef __cpp_lib_string_view
228 #define SIMDJSON_HAS_STRING_VIEW
229 #endif
230 
231 // Some systems have string_view even if we do not have C++17 support,
232 // and even if __cpp_lib_string_view is undefined, it is the case
233 // with Apple clang version 11.
234 // We must handle it. *This is important.*
235 #ifndef SIMDJSON_HAS_STRING_VIEW
236 #if defined __has_include
237 // do not combine the next #if with the previous one (unsafe)
238 #if __has_include (<string_view>)
239 // now it is safe to trigger the include
240 #include <string_view> // though the file is there, it does not follow that we got the implementation
241 #if defined(_LIBCPP_STRING_VIEW)
242 // Ah! So we under libc++ which under its Library Fundamentals Technical Specification, which preceded C++17,
243 // included string_view.
244 // This means that we have string_view *even though* we may not have C++17.
245 #define SIMDJSON_HAS_STRING_VIEW
246 #endif // _LIBCPP_STRING_VIEW
247 #endif // __has_include (<string_view>)
248 #endif // defined __has_include
249 #endif // def SIMDJSON_HAS_STRING_VIEW
250 // end of complicated but important routine to try to detect string_view.
251 
252 //
253 // Backfill std::string_view using nonstd::string_view on systems where
254 // we expect that string_view is missing. Important: if we get this wrong,
255 // we will end up with two string_view definitions and potential trouble.
256 // That is why we work so hard above to avoid it.
257 //
258 #ifndef SIMDJSON_HAS_STRING_VIEW
259 SIMDJSON_PUSH_DISABLE_ALL_WARNINGS
260 #include "simdjson/nonstd/string_view.hpp"
261 SIMDJSON_POP_DISABLE_WARNINGS
262 
263 namespace std {
264  using string_view = nonstd::string_view;
265 }
266 #endif // SIMDJSON_HAS_STRING_VIEW
267 #undef SIMDJSON_HAS_STRING_VIEW // We are not going to need this macro anymore.
268 
270 #define SIMDJSON_TRY(EXPR) { auto _err = (EXPR); if (_err) { return _err; } }
271 
272 // Unless the programmer has already set SIMDJSON_DEVELOPMENT_CHECKS,
273 // we want to set it under debug builds. We detect a debug build
274 // under Visual Studio when the _DEBUG macro is set. Under the other
275 // compilers, we use the fact that they define __OPTIMIZE__ whenever
276 // they allow optimizations.
277 // It is possible that this could miss some cases where SIMDJSON_DEVELOPMENT_CHECKS
278 // is helpful, but the programmer can set the macro SIMDJSON_DEVELOPMENT_CHECKS.
279 // It could also wrongly set SIMDJSON_DEVELOPMENT_CHECKS (e.g., if the programmer
280 // sets _DEBUG in a release build under Visual Studio, or if some compiler fails to
281 // set the __OPTIMIZE__ macro).
282 #ifndef SIMDJSON_DEVELOPMENT_CHECKS
283 #ifdef _MSC_VER
284 // Visual Studio seems to set _DEBUG for debug builds.
285 #ifdef _DEBUG
286 #define SIMDJSON_DEVELOPMENT_CHECKS 1
287 #endif // _DEBUG
288 #else // _MSC_VER
289 // All other compilers appear to set __OPTIMIZE__ to a positive integer
290 // when the compiler is optimizing.
291 #ifndef __OPTIMIZE__
292 #define SIMDJSON_DEVELOPMENT_CHECKS 1
293 #endif // __OPTIMIZE__
294 #endif // _MSC_VER
295 #endif // SIMDJSON_DEVELOPMENT_CHECKS
296 
297 // The SIMDJSON_CHECK_EOF macro is a feature flag for the "don't require padding"
298 // feature.
299 
300 #if SIMDJSON_CPLUSPLUS17
301 // if we have C++, then fallthrough is a default attribute
302 # define simdjson_fallthrough [[fallthrough]]
303 // check if we have __attribute__ support
304 #elif defined(__has_attribute)
305 // check if we have the __fallthrough__ attribute
306 #if __has_attribute(__fallthrough__)
307 // we are good to go:
308 # define simdjson_fallthrough __attribute__((__fallthrough__))
309 #endif // __has_attribute(__fallthrough__)
310 #endif // SIMDJSON_CPLUSPLUS17
311 // on some systems, we simply do not have support for fallthrough, so use a default:
312 #ifndef simdjson_fallthrough
313 # define simdjson_fallthrough do {} while (0) /* fallthrough */
314 #endif // simdjson_fallthrough
315 
316 #if SIMDJSON_DEVELOPMENT_CHECKS
317 #define SIMDJSON_DEVELOPMENT_ASSERT(expr) do { assert ((expr)); } while (0)
318 #else
319 #define SIMDJSON_DEVELOPMENT_ASSERT(expr) do { } while (0)
320 #endif
321 
322 #ifndef SIMDJSON_UTF8VALIDATION
323 #define SIMDJSON_UTF8VALIDATION 1
324 #endif
325 
326 #ifdef __has_include
327 // How do we detect that a compiler supports vbmi2?
328 // For sure if the following header is found, we are ok?
329 #if __has_include(<avx512vbmi2intrin.h>)
330 #define SIMDJSON_COMPILER_SUPPORTS_VBMI2 1
331 #endif
332 #endif
333 
334 #ifdef _MSC_VER
335 #if _MSC_VER >= 1920
336 // Visual Studio 2019 and up support VBMI2 under x64 even if the header
337 // avx512vbmi2intrin.h is not found.
338 #define SIMDJSON_COMPILER_SUPPORTS_VBMI2 1
339 #endif
340 #endif
341 
342 // By default, we allow AVX512.
343 #ifndef SIMDJSON_AVX512_ALLOWED
344 #define SIMDJSON_AVX512_ALLOWED 1
345 #endif
346 
347 #endif // SIMDJSON_COMMON_DEFS_H
The top level simdjson namespace, containing everything the library provides.
Definition: base.h:8