2 * This is where we define macros used across upb.
4 * All of these macros are undef'd in port_undef.inc to avoid leaking them to
7 * The correct usage is:
9 * #include "upb/foobar.h"
10 * #include "upb/baz.h"
12 * // MUST be last included header.
13 * #include "upb/port_def.inc"
15 * // Code for this file.
18 * // Can be omitted for .c files, required for .h.
19 * #include "upb/port_undef.inc"
21 * This file is private and must not be included by users!
24 #error must include stdint.h first
27 #if UINTPTR_MAX == 0xffffffff
28 #define UPB_SIZE(size32, size64) size32
30 #define UPB_SIZE(size32, size64) size64
33 #define UPB_FIELD_AT(msg, fieldtype, offset) \
34 *(fieldtype*)((const char*)(msg) + offset)
36 #define UPB_READ_ONEOF(msg, fieldtype, offset, case_offset, case_val, default) \
37 UPB_FIELD_AT(msg, int, case_offset) == case_val \
38 ? UPB_FIELD_AT(msg, fieldtype, offset) \
41 #define UPB_WRITE_ONEOF(msg, fieldtype, offset, value, case_offset, case_val) \
42 UPB_FIELD_AT(msg, int, case_offset) = case_val; \
43 UPB_FIELD_AT(msg, fieldtype, offset) = value;
45 /* UPB_INLINE: inline if possible, emit standalone code if required. */
47 #define UPB_INLINE inline
48 #elif defined (__GNUC__) || defined(__clang__)
49 #define UPB_INLINE static __inline__
51 #define UPB_INLINE static
54 /* Hints to the compiler about likely/unlikely branches. */
55 #if defined (__GNUC__) || defined(__clang__)
56 #define UPB_LIKELY(x) __builtin_expect((x),1)
57 #define UPB_UNLIKELY(x) __builtin_expect((x),0)
59 #define UPB_LIKELY(x) (x)
60 #define UPB_UNLIKELY(x) (x)
63 /* Define UPB_BIG_ENDIAN manually if you're on big endian and your compiler
64 * doesn't provide these preprocessor symbols. */
65 #if defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
66 #define UPB_BIG_ENDIAN
69 /* Macros for function attributes on compilers that support them. */
71 #define UPB_FORCEINLINE __inline__ __attribute__((always_inline))
72 #define UPB_NOINLINE __attribute__((noinline))
73 #define UPB_NORETURN __attribute__((__noreturn__))
74 #else /* !defined(__GNUC__) */
75 #define UPB_FORCEINLINE
80 #if __STDC_VERSION__ >= 199901L || __cplusplus >= 201103L
81 /* C99/C++11 versions. */
83 #define _upb_snprintf snprintf
84 #define _upb_vsnprintf vsnprintf
85 #define _upb_va_copy(a, b) va_copy(a, b)
86 #elif defined(_MSC_VER)
87 /* Microsoft C/C++ versions. */
91 int msvc_snprintf(char* s, size_t n, const char* format, ...);
92 int msvc_vsnprintf(char* s, size_t n, const char* format, va_list arg);
93 #define UPB_MSVC_VSNPRINTF
94 #define _upb_snprintf msvc_snprintf
95 #define _upb_vsnprintf msvc_vsnprintf
97 #define _upb_snprintf snprintf
98 #define _upb_vsnprintf vsnprintf
100 #define _upb_va_copy(a, b) va_copy(a, b)
101 #elif defined __GNUC__
102 /* A few hacky workarounds for functions not in C89.
103 * For internal use only!
104 * TODO(haberman): fix these by including our own implementations, or finding
105 * another workaround.
107 #define _upb_snprintf __builtin_snprintf
108 #define _upb_vsnprintf __builtin_vsnprintf
109 #define _upb_va_copy(a, b) __va_copy(a, b)
111 #error Need implementations of [v]snprintf and va_copy
115 #if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__) || \
116 (defined(_MSC_VER) && _MSC_VER >= 1900)
119 #error upb requires C++11 for C++ support
123 #define UPB_MAX(x, y) ((x) > (y) ? (x) : (y))
124 #define UPB_MIN(x, y) ((x) < (y) ? (x) : (y))
126 #define UPB_UNUSED(var) (void)var
128 /* UPB_ASSERT(): in release mode, we use the expression without letting it be
129 * evaluated. This prevents "unused variable" warnings. */
131 #define UPB_ASSERT(expr) do {} while (false && (expr))
133 #define UPB_ASSERT(expr) assert(expr)
136 /* UPB_ASSERT_DEBUGVAR(): assert that uses functions or variables that only
137 * exist in debug mode. This turns into regular assert. */
138 #define UPB_ASSERT_DEBUGVAR(expr) assert(expr)
140 #if defined(__GNUC__) || defined(__clang__)
141 #define UPB_UNREACHABLE() do { assert(0); __builtin_unreachable(); } while(0)
143 #define UPB_UNREACHABLE() do { assert(0); } while(0)
146 /* UPB_INFINITY representing floating-point positive infinity. */
149 #define UPB_INFINITY INFINITY
151 #define UPB_INFINITY (1.0 / 0.0)