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