Skip to content

Commit 7d32b8e

Browse files
committed
Add numeric limits compatibility support to bson-compat.h
1 parent 21f8bd5 commit 7d32b8e

File tree

1 file changed

+151
-0
lines changed

1 file changed

+151
-0
lines changed

src/libbson/src/bson/bson-compat.h

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,157 @@ typedef SSIZE_T ssize_t;
130130
#endif
131131
#endif
132132

133+
/* Derive the maximum representable value of signed integer type T using the
134+
* formula 2^(N - 1) - 1 where N is the number of bits in type T. This assumes
135+
* T is represented using two's complement. */
136+
#define BSON_NUMERIC_LIMITS_MAX_SIGNED(T) \
137+
((T) ((((size_t) 0x01u) << (sizeof (T) * (size_t) CHAR_BIT - 1u)) - 1u))
138+
139+
/* Derive the minimum representable value of signed integer type T as one less
140+
* than the negation of its maximum representable value. This assumes T is
141+
* represented using two's complement. */
142+
#define BSON_NUMERIC_LIMITS_MIN_SIGNED(T, max) ((T) ((-(max)) - 1))
143+
144+
/* Derive the maximum representable value of unsigned integer type T by flipping
145+
* all its bits to 1. */
146+
#define BSON_NUMERIC_LIMITS_MAX_UNSIGNED(T) ((T) (~((T) 0)))
147+
148+
/* Define numeric limit constants if not already available for C90
149+
* compatibility. These can be removed once C99 is declared the minimum
150+
* supported C standard. */
151+
#if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L
152+
153+
#ifndef SCHAR_MAX
154+
#define SCHAR_MAX BSON_NUMERIC_LIMITS_MAX_SIGNED (signed char)
155+
#endif
156+
157+
#ifndef SHRT_MAX
158+
#define SHRT_MAX BSON_NUMERIC_LIMITS_MAX_SIGNED (short)
159+
#endif
160+
161+
#ifndef INT_MAX
162+
#define INT_MAX BSON_NUMERIC_LIMITS_MAX_SIGNED (int)
163+
#endif
164+
165+
#ifndef LONG_MAX
166+
#define LONG_MAX BSON_NUMERIC_LIMITS_MAX_SIGNED (long)
167+
#endif
168+
169+
#ifndef LLONG_MAX
170+
#define LLONG_MAX BSON_NUMERIC_LIMITS_MAX_SIGNED (long long)
171+
#endif
172+
173+
#ifndef UCHAR_MAX
174+
#define UCHAR_MAX BSON_NUMERIC_LIMITS_MAX_UNSIGNED (unsigned char)
175+
#endif
176+
177+
#ifndef USHRT_MAX
178+
#define USHRT_MAX BSON_NUMERIC_LIMITS_MAX_UNSIGNED (unsigned short)
179+
#endif
180+
181+
#ifndef UINT_MAX
182+
#define UINT_MAX BSON_NUMERIC_LIMITS_MAX_UNSIGNED (unsigned int)
183+
#endif
184+
185+
#ifndef ULONG_MAX
186+
#define ULONG_MAX BSON_NUMERIC_LIMITS_MAX_UNSIGNED (unsigned long)
187+
#endif
188+
189+
#ifndef ULLONG_MAX
190+
#define ULLONG_MAX BSON_NUMERIC_LIMITS_MAX_UNSIGNED (unsigned long long)
191+
#endif
192+
193+
#ifndef INT8_MAX
194+
#define INT8_MAX BSON_NUMERIC_LIMITS_MAX_SIGNED (int8_t)
195+
#endif
196+
197+
#ifndef INT16_MAX
198+
#define INT16_MAX BSON_NUMERIC_LIMITS_MAX_SIGNED (int16_t)
199+
#endif
200+
201+
#ifndef INT32_MAX
202+
#define INT32_MAX BSON_NUMERIC_LIMITS_MAX_SIGNED (int32_t)
203+
#endif
204+
205+
#ifndef INT64_MAX
206+
#define INT64_MAX BSON_NUMERIC_LIMITS_MAX_SIGNED (int64_t)
207+
#endif
208+
209+
#ifndef UINT8_MAX
210+
#define UINT8_MAX BSON_NUMERIC_LIMITS_MAX_UNSIGNED (uint8_t)
211+
#endif
212+
213+
#ifndef UINT16_MAX
214+
#define UINT16_MAX BSON_NUMERIC_LIMITS_MAX_UNSIGNED (uint16_t)
215+
#endif
216+
217+
#ifndef UINT32_MAX
218+
#define UINT32_MAX BSON_NUMERIC_LIMITS_MAX_UNSIGNED (uint32_t)
219+
#endif
220+
221+
#ifndef UINT64_MAX
222+
#define UINT64_MAX BSON_NUMERIC_LIMITS_MAX_UNSIGNED (uint64_t)
223+
#endif
224+
225+
#ifndef SIZE_MAX
226+
#define SIZE_MAX BSON_NUMERIC_LIMITS_MAX_UNSIGNED (size_t)
227+
#endif
228+
229+
#ifndef PTRDIFF_MAX
230+
#define PTRDIFF_MAX BSON_NUMERIC_LIMITS_MAX_SIGNED (ptrdiff_t)
231+
#endif
232+
233+
#ifndef SCHAR_MIN
234+
#define SCHAR_MIN BSON_NUMERIC_LIMITS_MIN_SIGNED (signed char, SCHAR_MAX)
235+
#endif
236+
237+
#ifndef SHRT_MIN
238+
#define SHRT_MIN BSON_NUMERIC_LIMITS_MIN_SIGNED (short, SHRT_MAX)
239+
#endif
240+
241+
#ifndef INT_MIN
242+
#define INT_MIN BSON_NUMERIC_LIMITS_MIN_SIGNED (int, INT_MAX)
243+
#endif
244+
245+
#ifndef LONG_MIN
246+
#define LONG_MIN BSON_NUMERIC_LIMITS_MIN_SIGNED (long, LONG_MAX)
247+
#endif
248+
249+
#ifndef LLONG_MIN
250+
#define LLONG_MIN BSON_NUMERIC_LIMITS_MIN_SIGNED (long long, LLONG_MAX)
251+
#endif
252+
253+
#ifndef INT8_MIN
254+
#define INT8_MIN BSON_NUMERIC_LIMITS_MIN_SIGNED (int8_t, INT8_MAX)
255+
#endif
256+
257+
#ifndef INT16_MIN
258+
#define INT16_MIN BSON_NUMERIC_LIMITS_MIN_SIGNED (int16_t, INT16_MAX)
259+
#endif
260+
261+
#ifndef INT32_MIN
262+
#define INT32_MIN BSON_NUMERIC_LIMITS_MIN_SIGNED (int32_t, INT32_MAX)
263+
#endif
264+
265+
#ifndef INT64_MIN
266+
#define INT64_MIN BSON_NUMERIC_LIMITS_MIN_SIGNED (int64_t, INT64_MAX)
267+
#endif
268+
269+
#ifndef PTRDIFF_MIN
270+
#define PTRDIFF_MIN BSON_NUMERIC_LIMITS_MIN_SIGNED (ptrdiff_t, PTRDIFF_MAX)
271+
#endif
272+
273+
#endif /* !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L */
274+
275+
276+
#ifndef SSIZE_MAX
277+
#define SSIZE_MAX BSON_NUMERIC_LIMITS_MAX_SIGNED (ssize_t)
278+
#endif
279+
280+
#ifndef SSIZE_MIN
281+
#define SSIZE_MIN BSON_NUMERIC_LIMITS_MIN_SIGNED (ssize_t, SSIZE_MAX)
282+
#endif
283+
133284
#if defined(__MINGW32__) && !defined(INIT_ONCE_STATIC_INIT)
134285
#define INIT_ONCE_STATIC_INIT RTL_RUN_ONCE_INIT
135286
typedef RTL_RUN_ONCE INIT_ONCE;

0 commit comments

Comments
 (0)