Built motion from commit 6a09e18b.|2.6.11
[motion2.git] / legacy-libs / grpc / deps / grpc / third_party / upb / upb / port_def.inc
1 /*
2 * This is where we define macros used across upb.
3 *
4 * All of these macros are undef'd in port_undef.inc to avoid leaking them to
5 * users.
6 *
7 * The correct usage is:
8 *
9 *   #include "upb/foobar.h"
10 *   #include "upb/baz.h"
11 *
12 *   // MUST be last included header.
13 *   #include "upb/port_def.inc"
14 *
15 *   // Code for this file.
16 *   // <...>
17 *
18 *   // Can be omitted for .c files, required for .h.
19 *   #include "upb/port_undef.inc"
20 *
21 * This file is private and must not be included by users!
22 */
23 #ifndef UINTPTR_MAX
24 #error must include stdint.h first
25 #endif
26
27 #if UINTPTR_MAX == 0xffffffff
28 #define UPB_SIZE(size32, size64) size32
29 #else
30 #define UPB_SIZE(size32, size64) size64
31 #endif
32
33 #define UPB_FIELD_AT(msg, fieldtype, offset) \
34   *(fieldtype*)((const char*)(msg) + offset)
35
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)                                   \
39       : default
40
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;
44
45 /* UPB_INLINE: inline if possible, emit standalone code if required. */
46 #ifdef __cplusplus
47 #define UPB_INLINE inline
48 #elif defined (__GNUC__) || defined(__clang__)
49 #define UPB_INLINE static __inline__
50 #else
51 #define UPB_INLINE static
52 #endif
53
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)
58 #else
59 #define UPB_LIKELY(x) (x)
60 #define UPB_UNLIKELY(x) (x)
61 #endif
62
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
67 #endif
68
69 /* Macros for function attributes on compilers that support them. */
70 #ifdef __GNUC__
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
76 #define UPB_NOINLINE
77 #define UPB_NORETURN
78 #endif
79
80 #if __STDC_VERSION__ >= 199901L || __cplusplus >= 201103L
81 /* C99/C++11 versions. */
82 #include <stdio.h>
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. */
88 #include <stdarg.h>
89 #include <stdio.h>
90 #if _MSC_VER < 1900
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
96 #else
97 #define _upb_snprintf snprintf
98 #define _upb_vsnprintf vsnprintf
99 #endif
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.
106  */
107 #define _upb_snprintf __builtin_snprintf
108 #define _upb_vsnprintf __builtin_vsnprintf
109 #define _upb_va_copy(a, b) __va_copy(a, b)
110 #else
111 #error Need implementations of [v]snprintf and va_copy
112 #endif
113
114 #ifdef __cplusplus
115 #if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__) || \
116     (defined(_MSC_VER) && _MSC_VER >= 1900)
117 // C++11 is present
118 #else
119 #error upb requires C++11 for C++ support
120 #endif
121 #endif
122
123 #define UPB_MAX(x, y) ((x) > (y) ? (x) : (y))
124 #define UPB_MIN(x, y) ((x) < (y) ? (x) : (y))
125
126 #define UPB_UNUSED(var) (void)var
127
128 /* UPB_ASSERT(): in release mode, we use the expression without letting it be
129  * evaluated.  This prevents "unused variable" warnings. */
130 #ifdef NDEBUG
131 #define UPB_ASSERT(expr) do {} while (false && (expr))
132 #else
133 #define UPB_ASSERT(expr) assert(expr)
134 #endif
135
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)
139
140 #if defined(__GNUC__) || defined(__clang__)
141 #define UPB_UNREACHABLE() do { assert(0); __builtin_unreachable(); } while(0)
142 #else
143 #define UPB_UNREACHABLE() do { assert(0); } while(0)
144 #endif
145
146 /* UPB_INFINITY representing floating-point positive infinity. */
147 #include <math.h>
148 #ifdef INFINITY
149 #define UPB_INFINITY INFINITY
150 #else
151 #define UPB_INFINITY (1.0 / 0.0)
152 #endif