1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
17  * All rights reserved.
18  */
19 
20 #ifndef __BLI_UTILDEFINES_H__
21 #define __BLI_UTILDEFINES_H__
22 
23 /** \file
24  * \ingroup bli
25  */
26 
27 /* avoid many includes for now */
28 #include "BLI_compiler_compat.h"
29 #include "BLI_sys_types.h"
30 #include "BLI_utildefines_variadic.h"
31 
32 /* We could remove in future. */
33 #include "BLI_assert.h"
34 
35 /* include after _VA_NARGS macro */
36 #include "BLI_compiler_typecheck.h"
37 
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41 
42 /* -------------------------------------------------------------------- */
43 /** \name Min/Max Macros
44  * \{ */
45 
46 /* useful for finding bad use of min/max */
47 #if 0
48 /* gcc only */
49 #  define _TYPECHECK(a, b) ((void)(((typeof(a) *)0) == ((typeof(b) *)0)))
50 #  define MIN2(x, y) (_TYPECHECK(x, y), (((x) < (y) ? (x) : (y))))
51 #  define MAX2(x, y) (_TYPECHECK(x, y), (((x) > (y) ? (x) : (y))))
52 #endif
53 
54 /* min/max */
55 #if defined(__GNUC__) || defined(__clang__)
56 
57 #  define MIN2(a, b) \
58     __extension__({ \
59       typeof(a) a_ = (a); \
60       typeof(b) b_ = (b); \
61       ((a_) < (b_) ? (a_) : (b_)); \
62     })
63 
64 #  define MAX2(a, b) \
65     __extension__({ \
66       typeof(a) a_ = (a); \
67       typeof(b) b_ = (b); \
68       ((a_) > (b_) ? (a_) : (b_)); \
69     })
70 
71 #  define MIN3(a, b, c) \
72     __extension__({ \
73       typeof(a) a_ = (a); \
74       typeof(b) b_ = (b); \
75       typeof(c) c_ = (c); \
76       ((a_ < b_) ? ((a_ < c_) ? a_ : c_) : ((b_ < c_) ? b_ : c_)); \
77     })
78 
79 #  define MAX3(a, b, c) \
80     __extension__({ \
81       typeof(a) a_ = (a); \
82       typeof(b) b_ = (b); \
83       typeof(c) c_ = (c); \
84       ((a_ > b_) ? ((a_ > c_) ? a_ : c_) : ((b_ > c_) ? b_ : c_)); \
85     })
86 
87 #  define MIN4(a, b, c, d) \
88     __extension__({ \
89       typeof(a) a_ = (a); \
90       typeof(b) b_ = (b); \
91       typeof(c) c_ = (c); \
92       typeof(d) d_ = (d); \
93       ((a_ < b_) ? ((a_ < c_) ? ((a_ < d_) ? a_ : d_) : ((c_ < d_) ? c_ : d_)) : \
94                    ((b_ < c_) ? ((b_ < d_) ? b_ : d_) : ((c_ < d_) ? c_ : d_))); \
95     })
96 
97 #  define MAX4(a, b, c, d) \
98     __extension__({ \
99       typeof(a) a_ = (a); \
100       typeof(b) b_ = (b); \
101       typeof(c) c_ = (c); \
102       typeof(d) d_ = (d); \
103       ((a_ > b_) ? ((a_ > c_) ? ((a_ > d_) ? a_ : d_) : ((c_ > d_) ? c_ : d_)) : \
104                    ((b_ > c_) ? ((b_ > d_) ? b_ : d_) : ((c_ > d_) ? c_ : d_))); \
105     })
106 
107 #else
108 #  define MIN2(a, b) ((a) < (b) ? (a) : (b))
109 #  define MAX2(a, b) ((a) > (b) ? (a) : (b))
110 
111 #  define MIN3(a, b, c) (MIN2(MIN2((a), (b)), (c)))
112 #  define MIN4(a, b, c, d) (MIN2(MIN2((a), (b)), MIN2((c), (d))))
113 
114 #  define MAX3(a, b, c) (MAX2(MAX2((a), (b)), (c)))
115 #  define MAX4(a, b, c, d) (MAX2(MAX2((a), (b)), MAX2((c), (d))))
116 #endif
117 
118 /* min/max that return a value of our choice */
119 #define MAX3_PAIR(cmp_a, cmp_b, cmp_c, ret_a, ret_b, ret_c) \
120   ((cmp_a > cmp_b) ? ((cmp_a > cmp_c) ? ret_a : ret_c) : ((cmp_b > cmp_c) ? ret_b : ret_c))
121 
122 #define MIN3_PAIR(cmp_a, cmp_b, cmp_c, ret_a, ret_b, ret_c) \
123   ((cmp_a < cmp_b) ? ((cmp_a < cmp_c) ? ret_a : ret_c) : ((cmp_b < cmp_c) ? ret_b : ret_c))
124 
125 #define INIT_MINMAX(min, max) \
126   { \
127     (min)[0] = (min)[1] = (min)[2] = 1.0e30f; \
128     (max)[0] = (max)[1] = (max)[2] = -1.0e30f; \
129   } \
130   (void)0
131 #define INIT_MINMAX2(min, max) \
132   { \
133     (min)[0] = (min)[1] = 1.0e30f; \
134     (max)[0] = (max)[1] = -1.0e30f; \
135   } \
136   (void)0
137 #define DO_MIN(vec, min) \
138   { \
139     if ((min)[0] > (vec)[0]) { \
140       (min)[0] = (vec)[0]; \
141     } \
142     if ((min)[1] > (vec)[1]) { \
143       (min)[1] = (vec)[1]; \
144     } \
145     if ((min)[2] > (vec)[2]) { \
146       (min)[2] = (vec)[2]; \
147     } \
148   } \
149   (void)0
150 #define DO_MAX(vec, max) \
151   { \
152     if ((max)[0] < (vec)[0]) { \
153       (max)[0] = (vec)[0]; \
154     } \
155     if ((max)[1] < (vec)[1]) { \
156       (max)[1] = (vec)[1]; \
157     } \
158     if ((max)[2] < (vec)[2]) { \
159       (max)[2] = (vec)[2]; \
160     } \
161   } \
162   (void)0
163 #define DO_MINMAX(vec, min, max) \
164   { \
165     if ((min)[0] > (vec)[0]) { \
166       (min)[0] = (vec)[0]; \
167     } \
168     if ((min)[1] > (vec)[1]) { \
169       (min)[1] = (vec)[1]; \
170     } \
171     if ((min)[2] > (vec)[2]) { \
172       (min)[2] = (vec)[2]; \
173     } \
174     if ((max)[0] < (vec)[0]) { \
175       (max)[0] = (vec)[0]; \
176     } \
177     if ((max)[1] < (vec)[1]) { \
178       (max)[1] = (vec)[1]; \
179     } \
180     if ((max)[2] < (vec)[2]) { \
181       (max)[2] = (vec)[2]; \
182     } \
183   } \
184   (void)0
185 #define DO_MINMAX2(vec, min, max) \
186   { \
187     if ((min)[0] > (vec)[0]) { \
188       (min)[0] = (vec)[0]; \
189     } \
190     if ((min)[1] > (vec)[1]) { \
191       (min)[1] = (vec)[1]; \
192     } \
193     if ((max)[0] < (vec)[0]) { \
194       (max)[0] = (vec)[0]; \
195     } \
196     if ((max)[1] < (vec)[1]) { \
197       (max)[1] = (vec)[1]; \
198     } \
199   } \
200   (void)0
201 
202 /** \} */
203 
204 /* -------------------------------------------------------------------- */
205 /** \name Swap/Shift Macros
206  * \{ */
207 
208 #define SWAP(type, a, b) \
209   { \
210     type sw_ap; \
211     CHECK_TYPE(a, type); \
212     CHECK_TYPE(b, type); \
213     sw_ap = (a); \
214     (a) = (b); \
215     (b) = sw_ap; \
216   } \
217   (void)0
218 
219 /* swap with a temp value */
220 #define SWAP_TVAL(tval, a, b) \
221   { \
222     CHECK_TYPE_PAIR(tval, a); \
223     CHECK_TYPE_PAIR(tval, b); \
224     (tval) = (a); \
225     (a) = (b); \
226     (b) = (tval); \
227   } \
228   (void)0
229 
230 /* shift around elements */
231 #define SHIFT3(type, a, b, c) \
232   { \
233     type tmp; \
234     CHECK_TYPE(a, type); \
235     CHECK_TYPE(b, type); \
236     CHECK_TYPE(c, type); \
237     tmp = a; \
238     a = c; \
239     c = b; \
240     b = tmp; \
241   } \
242   (void)0
243 
244 #define SHIFT4(type, a, b, c, d) \
245   { \
246     type tmp; \
247     CHECK_TYPE(a, type); \
248     CHECK_TYPE(b, type); \
249     CHECK_TYPE(c, type); \
250     CHECK_TYPE(d, type); \
251     tmp = a; \
252     a = d; \
253     d = c; \
254     c = b; \
255     b = tmp; \
256   } \
257   (void)0
258 
259 /** \} */
260 
261 /* -------------------------------------------------------------------- */
262 /** \name Equal to Any Element (ELEM) Macro
263  * \{ */
264 
265 /* Manual line breaks for readability. */
266 /* clang-format off */
267 
268 /* ELEM#(v, ...): is the first arg equal any others? */
269 /* internal helpers. */
270 #define _VA_ELEM2(v, a) ((v) == (a))
271 #define _VA_ELEM3(v, a, b) \
272   (_VA_ELEM2(v, a) || _VA_ELEM2(v, b))
273 #define _VA_ELEM4(v, a, b, c) \
274   (_VA_ELEM3(v, a, b) || _VA_ELEM2(v, c))
275 #define _VA_ELEM5(v, a, b, c, d) \
276   (_VA_ELEM4(v, a, b, c) || _VA_ELEM2(v, d))
277 #define _VA_ELEM6(v, a, b, c, d, e) \
278   (_VA_ELEM5(v, a, b, c, d) || _VA_ELEM2(v, e))
279 #define _VA_ELEM7(v, a, b, c, d, e, f) \
280   (_VA_ELEM6(v, a, b, c, d, e) || _VA_ELEM2(v, f))
281 #define _VA_ELEM8(v, a, b, c, d, e, f, g) \
282   (_VA_ELEM7(v, a, b, c, d, e, f) || _VA_ELEM2(v, g))
283 #define _VA_ELEM9(v, a, b, c, d, e, f, g, h) \
284   (_VA_ELEM8(v, a, b, c, d, e, f, g) || _VA_ELEM2(v, h))
285 #define _VA_ELEM10(v, a, b, c, d, e, f, g, h, i) \
286   (_VA_ELEM9(v, a, b, c, d, e, f, g, h) || _VA_ELEM2(v, i))
287 #define _VA_ELEM11(v, a, b, c, d, e, f, g, h, i, j) \
288   (_VA_ELEM10(v, a, b, c, d, e, f, g, h, i) || _VA_ELEM2(v, j))
289 #define _VA_ELEM12(v, a, b, c, d, e, f, g, h, i, j, k) \
290   (_VA_ELEM11(v, a, b, c, d, e, f, g, h, i, j) || _VA_ELEM2(v, k))
291 #define _VA_ELEM13(v, a, b, c, d, e, f, g, h, i, j, k, l) \
292   (_VA_ELEM12(v, a, b, c, d, e, f, g, h, i, j, k) || _VA_ELEM2(v, l))
293 #define _VA_ELEM14(v, a, b, c, d, e, f, g, h, i, j, k, l, m) \
294   (_VA_ELEM13(v, a, b, c, d, e, f, g, h, i, j, k, l) || _VA_ELEM2(v, m))
295 #define _VA_ELEM15(v, a, b, c, d, e, f, g, h, i, j, k, l, m, n) \
296   (_VA_ELEM14(v, a, b, c, d, e, f, g, h, i, j, k, l, m) || _VA_ELEM2(v, n))
297 #define _VA_ELEM16(v, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o) \
298   (_VA_ELEM15(v, a, b, c, d, e, f, g, h, i, j, k, l, m, n) || _VA_ELEM2(v, o))
299 #define _VA_ELEM17(v, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) \
300   (_VA_ELEM16(v, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o) || _VA_ELEM2(v, p))
301 /* clang-format on */
302 
303 /* reusable ELEM macro */
304 #define ELEM(...) VA_NARGS_CALL_OVERLOAD(_VA_ELEM, __VA_ARGS__)
305 
306 /** \} */
307 
308 /* -------------------------------------------------------------------- */
309 /** \name Simple Math Macros
310  * \{ */
311 
312 /* Float equality checks. */
313 
314 #define IS_EQ(a, b) \
315   (CHECK_TYPE_INLINE(a, double), \
316    CHECK_TYPE_INLINE(b, double), \
317    ((fabs((double)((a) - (b))) >= (double)FLT_EPSILON) ? false : true))
318 
319 #define IS_EQF(a, b) \
320   (CHECK_TYPE_INLINE(a, float), \
321    CHECK_TYPE_INLINE(b, float), \
322    ((fabsf((float)((a) - (b))) >= (float)FLT_EPSILON) ? false : true))
323 
324 #define IS_EQT(a, b, c) (((a) > (b)) ? ((((a) - (b)) <= (c))) : (((((b) - (a)) <= (c)))))
325 #define IN_RANGE(a, b, c) (((b) < (c)) ? (((b) < (a) && (a) < (c))) : (((c) < (a) && (a) < (b))))
326 #define IN_RANGE_INCL(a, b, c) \
327   (((b) < (c)) ? (((b) <= (a) && (a) <= (c))) : (((c) <= (a) && (a) <= (b))))
328 
329 /**
330  * Expands to an integer constant expression evaluating to a close upper bound
331  * on the number the number of decimal digits in a value expressible in the
332  * integer type given by the argument (if it is a type name) or the integer
333  * type of the argument (if it is an expression). The meaning of the resulting
334  * expression is unspecified for other arguments.
335  * i.e: `DECIMAL_DIGITS_BOUND(uchar)` is equal to 3.
336  */
337 #define DECIMAL_DIGITS_BOUND(t) (241 * sizeof(t) / 100 + 1)
338 
339 /** \} */
340 
341 /* -------------------------------------------------------------------- */
342 /** \name Clamp Macros
343  * \{ */
344 
345 #define CLAMPIS(a, b, c) ((a) < (b) ? (b) : (a) > (c) ? (c) : (a))
346 
347 #define CLAMP(a, b, c) \
348   { \
349     if ((a) < (b)) { \
350       (a) = (b); \
351     } \
352     else if ((a) > (c)) { \
353       (a) = (c); \
354     } \
355   } \
356   (void)0
357 
358 #define CLAMP_MAX(a, c) \
359   { \
360     if ((a) > (c)) { \
361       (a) = (c); \
362     } \
363   } \
364   (void)0
365 
366 #define CLAMP_MIN(a, b) \
367   { \
368     if ((a) < (b)) { \
369       (a) = (b); \
370     } \
371   } \
372   (void)0
373 
374 #define CLAMP2(vec, b, c) \
375   { \
376     CLAMP((vec)[0], b, c); \
377     CLAMP((vec)[1], b, c); \
378   } \
379   (void)0
380 
381 #define CLAMP2_MIN(vec, b) \
382   { \
383     CLAMP_MIN((vec)[0], b); \
384     CLAMP_MIN((vec)[1], b); \
385   } \
386   (void)0
387 
388 #define CLAMP2_MAX(vec, b) \
389   { \
390     CLAMP_MAX((vec)[0], b); \
391     CLAMP_MAX((vec)[1], b); \
392   } \
393   (void)0
394 
395 #define CLAMP3(vec, b, c) \
396   { \
397     CLAMP((vec)[0], b, c); \
398     CLAMP((vec)[1], b, c); \
399     CLAMP((vec)[2], b, c); \
400   } \
401   (void)0
402 
403 #define CLAMP3_MIN(vec, b) \
404   { \
405     CLAMP_MIN((vec)[0], b); \
406     CLAMP_MIN((vec)[1], b); \
407     CLAMP_MIN((vec)[2], b); \
408   } \
409   (void)0
410 
411 #define CLAMP3_MAX(vec, b) \
412   { \
413     CLAMP_MAX((vec)[0], b); \
414     CLAMP_MAX((vec)[1], b); \
415     CLAMP_MAX((vec)[2], b); \
416   } \
417   (void)0
418 
419 #define CLAMP4(vec, b, c) \
420   { \
421     CLAMP((vec)[0], b, c); \
422     CLAMP((vec)[1], b, c); \
423     CLAMP((vec)[2], b, c); \
424     CLAMP((vec)[3], b, c); \
425   } \
426   (void)0
427 
428 #define CLAMP4_MIN(vec, b) \
429   { \
430     CLAMP_MIN((vec)[0], b); \
431     CLAMP_MIN((vec)[1], b); \
432     CLAMP_MIN((vec)[2], b); \
433     CLAMP_MIN((vec)[3], b); \
434   } \
435   (void)0
436 
437 #define CLAMP4_MAX(vec, b) \
438   { \
439     CLAMP_MAX((vec)[0], b); \
440     CLAMP_MAX((vec)[1], b); \
441     CLAMP_MAX((vec)[2], b); \
442     CLAMP_MAX((vec)[3], b); \
443   } \
444   (void)0
445 
446 /** \} */
447 
448 /* -------------------------------------------------------------------- */
449 /** \name Array Unpacking Macros
450  * \{ */
451 
452 /* unpack vector for args */
453 #define UNPACK2(a) ((a)[0]), ((a)[1])
454 #define UNPACK3(a) UNPACK2(a), ((a)[2])
455 #define UNPACK4(a) UNPACK3(a), ((a)[3])
456 /* pre may be '&', '*' or func, post may be '->member' */
457 #define UNPACK2_EX(pre, a, post) (pre((a)[0]) post), (pre((a)[1]) post)
458 #define UNPACK3_EX(pre, a, post) UNPACK2_EX(pre, a, post), (pre((a)[2]) post)
459 #define UNPACK4_EX(pre, a, post) UNPACK3_EX(pre, a, post), (pre((a)[3]) post)
460 
461 /** \} */
462 
463 /* -------------------------------------------------------------------- */
464 /** \name Array Macros
465  * \{ */
466 
467 /* array helpers */
468 #define ARRAY_LAST_ITEM(arr_start, arr_dtype, arr_len) \
469   (arr_dtype *)((char *)(arr_start) + (sizeof(*((arr_dtype *)NULL)) * (size_t)(arr_len - 1)))
470 
471 #define ARRAY_HAS_ITEM(arr_item, arr_start, arr_len) \
472   (CHECK_TYPE_PAIR_INLINE(arr_start, arr_item), \
473    ((unsigned int)((arr_item) - (arr_start)) < (unsigned int)(arr_len)))
474 
475 /**
476  * \note use faster #ARRAY_DELETE_REORDER_LAST when we can re-order.
477  */
478 #define ARRAY_DELETE(arr, index, delete_len, arr_len) \
479   { \
480     BLI_assert((&arr[index] >= arr) && ((index) + delete_len <= arr_len)); \
481     memmove(&(arr)[index], \
482             &(arr)[(index) + (delete_len)], \
483             (((arr_len) - (index)) - (delete_len)) * sizeof(*(arr))); \
484   } \
485   ((void)0)
486 
487 /**
488  * Re-ordering array removal.
489  *
490  * When removing single items this compiles down to:
491  * `if (index + 1 != arr_len) { arr[index] = arr[arr_len - 1]; }` (typical reordering removal),
492  * with removing multiple items, overlap is detected to avoid memcpy errors.
493  */
494 #define ARRAY_DELETE_REORDER_LAST(arr, index, delete_len, arr_len) \
495   { \
496     BLI_assert((&arr[index] >= arr) && ((index) + delete_len <= arr_len)); \
497     if ((index) + (delete_len) != (arr_len)) { \
498       if (((delete_len) == 1) || ((delete_len) <= ((arr_len) - ((index) + (delete_len))))) { \
499         memcpy(&(arr)[index], &(arr)[(arr_len) - (delete_len)], (delete_len) * sizeof(*(arr))); \
500       } \
501       else { \
502         memcpy(&(arr)[index], \
503                &(arr)[(arr_len) - ((arr_len) - ((index) + (delete_len)))], \
504                ((arr_len) - ((index) + (delete_len))) * sizeof(*(arr))); \
505       } \
506     } \
507   } \
508   ((void)0)
509 
510 /* assuming a static array */
511 #if defined(__GNUC__) && !defined(__cplusplus) && !defined(__clang__) && !defined(__INTEL_COMPILER)
512 #  define ARRAY_SIZE(arr) \
513     ((sizeof(struct { int isnt_array : ((const void *)&(arr) == &(arr)[0]); }) * 0) + \
514      (sizeof(arr) / sizeof(*(arr))))
515 #else
516 #  define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(*(arr)))
517 #endif
518 
519 /* ARRAY_SET_ITEMS#(v, ...): set indices of array 'v'  */
520 /* internal helpers */
521 #define _VA_ARRAY_SET_ITEMS2(v, a) ((v)[0] = (a))
522 #define _VA_ARRAY_SET_ITEMS3(v, a, b) \
523   _VA_ARRAY_SET_ITEMS2(v, a); \
524   ((v)[1] = (b))
525 #define _VA_ARRAY_SET_ITEMS4(v, a, b, c) \
526   _VA_ARRAY_SET_ITEMS3(v, a, b); \
527   ((v)[2] = (c))
528 #define _VA_ARRAY_SET_ITEMS5(v, a, b, c, d) \
529   _VA_ARRAY_SET_ITEMS4(v, a, b, c); \
530   ((v)[3] = (d))
531 #define _VA_ARRAY_SET_ITEMS6(v, a, b, c, d, e) \
532   _VA_ARRAY_SET_ITEMS5(v, a, b, c, d); \
533   ((v)[4] = (e))
534 #define _VA_ARRAY_SET_ITEMS7(v, a, b, c, d, e, f) \
535   _VA_ARRAY_SET_ITEMS6(v, a, b, c, d, e); \
536   ((v)[5] = (f))
537 #define _VA_ARRAY_SET_ITEMS8(v, a, b, c, d, e, f, g) \
538   _VA_ARRAY_SET_ITEMS7(v, a, b, c, d, e, f); \
539   ((v)[6] = (g))
540 #define _VA_ARRAY_SET_ITEMS9(v, a, b, c, d, e, f, g, h) \
541   _VA_ARRAY_SET_ITEMS8(v, a, b, c, d, e, f, g); \
542   ((v)[7] = (h))
543 #define _VA_ARRAY_SET_ITEMS10(v, a, b, c, d, e, f, g, h, i) \
544   _VA_ARRAY_SET_ITEMS9(v, a, b, c, d, e, f, g, h); \
545   ((v)[8] = (i))
546 #define _VA_ARRAY_SET_ITEMS11(v, a, b, c, d, e, f, g, h, i, j) \
547   _VA_ARRAY_SET_ITEMS10(v, a, b, c, d, e, f, g, h, i); \
548   ((v)[9] = (j))
549 #define _VA_ARRAY_SET_ITEMS12(v, a, b, c, d, e, f, g, h, i, j, k) \
550   _VA_ARRAY_SET_ITEMS11(v, a, b, c, d, e, f, g, h, i, j); \
551   ((v)[10] = (k))
552 #define _VA_ARRAY_SET_ITEMS13(v, a, b, c, d, e, f, g, h, i, j, k, l) \
553   _VA_ARRAY_SET_ITEMS12(v, a, b, c, d, e, f, g, h, i, j, k); \
554   ((v)[11] = (l))
555 #define _VA_ARRAY_SET_ITEMS14(v, a, b, c, d, e, f, g, h, i, j, k, l, m) \
556   _VA_ARRAY_SET_ITEMS13(v, a, b, c, d, e, f, g, h, i, j, k, l); \
557   ((v)[12] = (m))
558 #define _VA_ARRAY_SET_ITEMS15(v, a, b, c, d, e, f, g, h, i, j, k, l, m, n) \
559   _VA_ARRAY_SET_ITEMS14(v, a, b, c, d, e, f, g, h, i, j, k, l, m); \
560   ((v)[13] = (n))
561 #define _VA_ARRAY_SET_ITEMS16(v, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o) \
562   _VA_ARRAY_SET_ITEMS15(v, a, b, c, d, e, f, g, h, i, j, k, l, m, n); \
563   ((v)[14] = (o))
564 #define _VA_ARRAY_SET_ITEMS17(v, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) \
565   _VA_ARRAY_SET_ITEMS16(v, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o); \
566   ((v)[15] = (p))
567 
568 /* reusable ARRAY_SET_ITEMS macro */
569 #define ARRAY_SET_ITEMS(...) \
570   { \
571     VA_NARGS_CALL_OVERLOAD(_VA_ARRAY_SET_ITEMS, __VA_ARGS__); \
572   } \
573   (void)0
574 
575 /** \} */
576 
577 /* -------------------------------------------------------------------- */
578 /** \name Pointer Macros
579  * \{ */
580 
581 #if defined(__GNUC__) || defined(__clang__)
582 #  define POINTER_OFFSET(v, ofs) ((typeof(v))((char *)(v) + (ofs)))
583 #else
584 #  define POINTER_OFFSET(v, ofs) ((void *)((char *)(v) + (ofs)))
585 #endif
586 
587 /* Warning-free macros for storing ints in pointers. Use these _only_
588  * for storing an int in a pointer, not a pointer in an int (64bit)! */
589 #define POINTER_FROM_INT(i) ((void *)(intptr_t)(i))
590 #define POINTER_AS_INT(i) ((void)0, ((int)(intptr_t)(i)))
591 
592 #define POINTER_FROM_UINT(i) ((void *)(uintptr_t)(i))
593 #define POINTER_AS_UINT(i) ((void)0, ((unsigned int)(uintptr_t)(i)))
594 
595 /** \} */
596 
597 /* -------------------------------------------------------------------- */
598 /** \name Struct After Macros
599  *
600  * Typically used to copy/clear polymorphic structs which have a generic
601  * member at the start which needs to be left as-is.
602  *
603  * \{ */
604 
605 /** Performs `offsetof(typeof(data), member) + sizeof((data)->member)` for non-gcc compilers. */
606 #define OFFSETOF_STRUCT_AFTER(_struct, _member) \
607   ((((const char *)&((_struct)->_member)) - ((const char *)(_struct))) + \
608    sizeof((_struct)->_member))
609 
610 /**
611  * memcpy helper, skipping the first part of a struct,
612  * ensures 'struct_dst' isn't const and the offset can be computed at compile time.
613  * This isn't inclusive, the value of \a member isn't copied.
614  */
615 #define MEMCPY_STRUCT_AFTER(struct_dst, struct_src, member) \
616   { \
617     CHECK_TYPE_NONCONST(struct_dst); \
618     ((void)(struct_dst == struct_src), \
619      memcpy((char *)(struct_dst) + OFFSETOF_STRUCT_AFTER(struct_dst, member), \
620             (const char *)(struct_src) + OFFSETOF_STRUCT_AFTER(struct_dst, member), \
621             sizeof(*(struct_dst)) - OFFSETOF_STRUCT_AFTER(struct_dst, member))); \
622   } \
623   ((void)0)
624 
625 #define MEMSET_STRUCT_AFTER(struct_var, value, member) \
626   { \
627     CHECK_TYPE_NONCONST(struct_var); \
628     memset((char *)(struct_var) + OFFSETOF_STRUCT_AFTER(struct_var, member), \
629            value, \
630            sizeof(*(struct_var)) - OFFSETOF_STRUCT_AFTER(struct_var, member)); \
631   } \
632   ((void)0)
633 
634 /* defined
635  * in memory_utils.c for now. I do not know where we should put it actually... */
636 #ifndef __BLI_MEMORY_UTILS_H__
637 extern bool BLI_memory_is_zero(const void *arr, const size_t arr_size);
638 #endif
639 
640 #define MEMCMP_STRUCT_AFTER_IS_ZERO(struct_var, member) \
641   (BLI_memory_is_zero((const char *)(struct_var) + OFFSETOF_STRUCT_AFTER(struct_var, member), \
642                       sizeof(*(struct_var)) - OFFSETOF_STRUCT_AFTER(struct_var, member)))
643 
644 /** \} */
645 
646 /* -------------------------------------------------------------------- */
647 /** \name String Macros
648  * \{ */
649 
650 /* Macro to convert a value to string in the pre-processor:
651  * - `STRINGIFY_ARG`: gives the argument as a string
652  * - `STRINGIFY_APPEND`: appends any argument 'b' onto the string argument 'a',
653  *   used by `STRINGIFY` because some preprocessors warn about zero arguments
654  * - `STRINGIFY`: gives the argument's value as a string. */
655 #define STRINGIFY_ARG(x) "" #x
656 #define STRINGIFY_APPEND(a, b) "" a #b
657 #define STRINGIFY(x) STRINGIFY_APPEND("", x)
658 
659 /* generic strcmp macros */
660 #if defined(_MSC_VER)
661 #  define strcasecmp _stricmp
662 #  define strncasecmp _strnicmp
663 #endif
664 
665 #define STREQ(a, b) (strcmp(a, b) == 0)
666 #define STRCASEEQ(a, b) (strcasecmp(a, b) == 0)
667 #define STREQLEN(a, b, n) (strncmp(a, b, n) == 0)
668 #define STRCASEEQLEN(a, b, n) (strncasecmp(a, b, n) == 0)
669 
670 #define STRPREFIX(a, b) (strncmp((a), (b), strlen(b)) == 0)
671 
672 /** \} */
673 
674 /* -------------------------------------------------------------------- */
675 /** \name Unused Function/Argument Macros
676  * \{ */
677 
678 /* UNUSED macro, for function argument */
679 #if defined(__GNUC__) || defined(__clang__)
680 #  define UNUSED(x) UNUSED_##x __attribute__((__unused__))
681 #else
682 #  define UNUSED(x) UNUSED_##x
683 #endif
684 
685 #if defined(__GNUC__) || defined(__clang__)
686 #  define UNUSED_FUNCTION(x) __attribute__((__unused__)) UNUSED_##x
687 #else
688 #  define UNUSED_FUNCTION(x) UNUSED_##x
689 #endif
690 
691 /**
692  * UNUSED_VARS#(a, ...): quiet unused warnings
693  *
694  * \code{.py}
695  * for i in range(16):
696  *     args = [(chr(ord('a') + (c % 26)) + (chr(ord('0') + (c // 26)))) for c in range(i + 1)]
697  *     print("#define _VA_UNUSED_VARS_%d(%s) \\" % (i + 1, ", ".join(args)))
698  *     print("\t((void)(%s)%s)" %
699  *             (args[0], ((", _VA_UNUSED_VARS_" + str(i) + "(%s)") if i else "%s") %
700  *              ", ".join((args[1:]))))
701  * \endcode
702  */
703 
704 #define _VA_UNUSED_VARS_1(a0) ((void)(a0))
705 #define _VA_UNUSED_VARS_2(a0, b0) ((void)(a0), _VA_UNUSED_VARS_1(b0))
706 #define _VA_UNUSED_VARS_3(a0, b0, c0) ((void)(a0), _VA_UNUSED_VARS_2(b0, c0))
707 #define _VA_UNUSED_VARS_4(a0, b0, c0, d0) ((void)(a0), _VA_UNUSED_VARS_3(b0, c0, d0))
708 #define _VA_UNUSED_VARS_5(a0, b0, c0, d0, e0) ((void)(a0), _VA_UNUSED_VARS_4(b0, c0, d0, e0))
709 #define _VA_UNUSED_VARS_6(a0, b0, c0, d0, e0, f0) \
710   ((void)(a0), _VA_UNUSED_VARS_5(b0, c0, d0, e0, f0))
711 #define _VA_UNUSED_VARS_7(a0, b0, c0, d0, e0, f0, g0) \
712   ((void)(a0), _VA_UNUSED_VARS_6(b0, c0, d0, e0, f0, g0))
713 #define _VA_UNUSED_VARS_8(a0, b0, c0, d0, e0, f0, g0, h0) \
714   ((void)(a0), _VA_UNUSED_VARS_7(b0, c0, d0, e0, f0, g0, h0))
715 #define _VA_UNUSED_VARS_9(a0, b0, c0, d0, e0, f0, g0, h0, i0) \
716   ((void)(a0), _VA_UNUSED_VARS_8(b0, c0, d0, e0, f0, g0, h0, i0))
717 #define _VA_UNUSED_VARS_10(a0, b0, c0, d0, e0, f0, g0, h0, i0, j0) \
718   ((void)(a0), _VA_UNUSED_VARS_9(b0, c0, d0, e0, f0, g0, h0, i0, j0))
719 #define _VA_UNUSED_VARS_11(a0, b0, c0, d0, e0, f0, g0, h0, i0, j0, k0) \
720   ((void)(a0), _VA_UNUSED_VARS_10(b0, c0, d0, e0, f0, g0, h0, i0, j0, k0))
721 #define _VA_UNUSED_VARS_12(a0, b0, c0, d0, e0, f0, g0, h0, i0, j0, k0, l0) \
722   ((void)(a0), _VA_UNUSED_VARS_11(b0, c0, d0, e0, f0, g0, h0, i0, j0, k0, l0))
723 #define _VA_UNUSED_VARS_13(a0, b0, c0, d0, e0, f0, g0, h0, i0, j0, k0, l0, m0) \
724   ((void)(a0), _VA_UNUSED_VARS_12(b0, c0, d0, e0, f0, g0, h0, i0, j0, k0, l0, m0))
725 #define _VA_UNUSED_VARS_14(a0, b0, c0, d0, e0, f0, g0, h0, i0, j0, k0, l0, m0, n0) \
726   ((void)(a0), _VA_UNUSED_VARS_13(b0, c0, d0, e0, f0, g0, h0, i0, j0, k0, l0, m0, n0))
727 #define _VA_UNUSED_VARS_15(a0, b0, c0, d0, e0, f0, g0, h0, i0, j0, k0, l0, m0, n0, o0) \
728   ((void)(a0), _VA_UNUSED_VARS_14(b0, c0, d0, e0, f0, g0, h0, i0, j0, k0, l0, m0, n0, o0))
729 #define _VA_UNUSED_VARS_16(a0, b0, c0, d0, e0, f0, g0, h0, i0, j0, k0, l0, m0, n0, o0, p0) \
730   ((void)(a0), _VA_UNUSED_VARS_15(b0, c0, d0, e0, f0, g0, h0, i0, j0, k0, l0, m0, n0, o0, p0))
731 
732 /* reusable ELEM macro */
733 #define UNUSED_VARS(...) VA_NARGS_CALL_OVERLOAD(_VA_UNUSED_VARS_, __VA_ARGS__)
734 
735 /* for debug-only variables */
736 #ifndef NDEBUG
737 #  define UNUSED_VARS_NDEBUG(...)
738 #else
739 #  define UNUSED_VARS_NDEBUG UNUSED_VARS
740 #endif
741 
742 /** \} */
743 
744 /* -------------------------------------------------------------------- */
745 /** \name Branch Prediction Macros
746  * \{ */
747 
748 /* hints for branch prediction, only use in code that runs a _lot_ where */
749 #ifdef __GNUC__
750 #  define LIKELY(x) __builtin_expect(!!(x), 1)
751 #  define UNLIKELY(x) __builtin_expect(!!(x), 0)
752 #else
753 #  define LIKELY(x) (x)
754 #  define UNLIKELY(x) (x)
755 #endif
756 
757 /** \} */
758 
759 /* -------------------------------------------------------------------- */
760 /** \name Flag Macros
761  * \{ */
762 
763 /* Set flag from a single test */
764 #define SET_FLAG_FROM_TEST(value, test, flag) \
765   { \
766     if (test) { \
767       (value) |= (flag); \
768     } \
769     else { \
770       (value) &= ~(flag); \
771     } \
772   } \
773   ((void)0)
774 
775 /** \} */
776 
777 /* -------------------------------------------------------------------- */
778 /** \name C++ Macros
779  * \{ */
780 
781 #ifdef __cplusplus
782 
783 /* Useful to port C code using enums to C++ where enums are strongly typed.
784  * To use after the enum declaration. */
785 /* If any enumerator `C` is set to say `A|B`, then `C` would be the max enum value. */
786 #  define ENUM_OPERATORS(_enum_type, _max_enum_value) \
787     inline constexpr _enum_type operator|(_enum_type a, _enum_type b) \
788     { \
789       return static_cast<_enum_type>(static_cast<int>(a) | b); \
790     } \
791     inline constexpr _enum_type operator&(_enum_type a, _enum_type b) \
792     { \
793       return static_cast<_enum_type>(static_cast<int>(a) & b); \
794     } \
795     inline constexpr _enum_type operator~(_enum_type a) \
796     { \
797       return static_cast<_enum_type>(~static_cast<int>(a) & (2 * _max_enum_value - 1)); \
798     } \
799     inline _enum_type &operator|=(_enum_type &a, _enum_type b) \
800     { \
801       return a = static_cast<_enum_type>(static_cast<int>(a) | b); \
802     } \
803     inline _enum_type &operator&=(_enum_type &a, _enum_type b) \
804     { \
805       return a = static_cast<_enum_type>(static_cast<int>(a) & b); \
806     }
807 
808 #else
809 /* Output nothing. */
810 #  define ENUM_OPERATORS(_type, _max)
811 #endif
812 
813 /** \} */
814 
815 /* -------------------------------------------------------------------- */
816 /** \name Misc Macros
817  * \{ */
818 
819 /** Useful for debugging. */
820 #define AT __FILE__ ":" STRINGIFY(__LINE__)
821 
822 /** No-op for expressions we don't want to instantiate, but must remain valid. */
823 #define EXPR_NOP(expr) (void)(0 ? ((void)(expr), 1) : 0)
824 
825 /** \} */
826 
827 #ifdef __cplusplus
828 }
829 #endif
830 
831 #endif /* __BLI_UTILDEFINES_H__ */
832