Blender  V3.3
BLI_utildefines.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2001-2002 NaN Holding BV. All rights reserved. */
3 
4 /* Use a define instead of `#pragma once` because of `BLI_memory_utils.h` */
5 #ifndef __BLI_UTILDEFINES_H__
6 #define __BLI_UTILDEFINES_H__
7 
12 /* avoid many includes for now */
13 #include "BLI_compiler_compat.h"
14 #include "BLI_sys_types.h"
16 
17 /* We could remove in future. */
18 #include "BLI_assert.h"
19 
20 /* include after _VA_NARGS macro */
21 #include "BLI_compiler_typecheck.h"
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 /* -------------------------------------------------------------------- */
31 /* useful for finding bad use of min/max */
32 #if 0
33 /* gcc only */
34 # define _TYPECHECK(a, b) ((void)(((typeof(a) *)0) == ((typeof(b) *)0)))
35 # define MIN2(x, y) (_TYPECHECK(x, y), (((x) < (y) ? (x) : (y))))
36 # define MAX2(x, y) (_TYPECHECK(x, y), (((x) > (y) ? (x) : (y))))
37 #endif
38 
39 /* min/max */
40 #if defined(__GNUC__) || defined(__clang__)
41 
42 # define MIN2(a, b) \
43  __extension__({ \
44  typeof(a) a_ = (a); \
45  typeof(b) b_ = (b); \
46  ((a_) < (b_) ? (a_) : (b_)); \
47  })
48 
49 # define MAX2(a, b) \
50  __extension__({ \
51  typeof(a) a_ = (a); \
52  typeof(b) b_ = (b); \
53  ((a_) > (b_) ? (a_) : (b_)); \
54  })
55 
56 # define MIN3(a, b, c) \
57  __extension__({ \
58  typeof(a) a_ = (a); \
59  typeof(b) b_ = (b); \
60  typeof(c) c_ = (c); \
61  ((a_ < b_) ? ((a_ < c_) ? a_ : c_) : ((b_ < c_) ? b_ : c_)); \
62  })
63 
64 # define MAX3(a, b, c) \
65  __extension__({ \
66  typeof(a) a_ = (a); \
67  typeof(b) b_ = (b); \
68  typeof(c) c_ = (c); \
69  ((a_ > b_) ? ((a_ > c_) ? a_ : c_) : ((b_ > c_) ? b_ : c_)); \
70  })
71 
72 # define MIN4(a, b, c, d) \
73  __extension__({ \
74  typeof(a) a_ = (a); \
75  typeof(b) b_ = (b); \
76  typeof(c) c_ = (c); \
77  typeof(d) d_ = (d); \
78  ((a_ < b_) ? ((a_ < c_) ? ((a_ < d_) ? a_ : d_) : ((c_ < d_) ? c_ : d_)) : \
79  ((b_ < c_) ? ((b_ < d_) ? b_ : d_) : ((c_ < d_) ? c_ : d_))); \
80  })
81 
82 # define MAX4(a, b, c, d) \
83  __extension__({ \
84  typeof(a) a_ = (a); \
85  typeof(b) b_ = (b); \
86  typeof(c) c_ = (c); \
87  typeof(d) d_ = (d); \
88  ((a_ > b_) ? ((a_ > c_) ? ((a_ > d_) ? a_ : d_) : ((c_ > d_) ? c_ : d_)) : \
89  ((b_ > c_) ? ((b_ > d_) ? b_ : d_) : ((c_ > d_) ? c_ : d_))); \
90  })
91 
92 #else
93 # define MIN2(a, b) ((a) < (b) ? (a) : (b))
94 # define MAX2(a, b) ((a) > (b) ? (a) : (b))
95 
96 # define MIN3(a, b, c) (MIN2(MIN2((a), (b)), (c)))
97 # define MIN4(a, b, c, d) (MIN2(MIN2((a), (b)), MIN2((c), (d))))
98 
99 # define MAX3(a, b, c) (MAX2(MAX2((a), (b)), (c)))
100 # define MAX4(a, b, c, d) (MAX2(MAX2((a), (b)), MAX2((c), (d))))
101 #endif
102 
103 /* min/max that return a value of our choice */
104 #define MAX3_PAIR(cmp_a, cmp_b, cmp_c, ret_a, ret_b, ret_c) \
105  ((cmp_a > cmp_b) ? ((cmp_a > cmp_c) ? ret_a : ret_c) : ((cmp_b > cmp_c) ? ret_b : ret_c))
106 
107 #define MIN3_PAIR(cmp_a, cmp_b, cmp_c, ret_a, ret_b, ret_c) \
108  ((cmp_a < cmp_b) ? ((cmp_a < cmp_c) ? ret_a : ret_c) : ((cmp_b < cmp_c) ? ret_b : ret_c))
109 
110 #define INIT_MINMAX(min, max) \
111  { \
112  (min)[0] = (min)[1] = (min)[2] = 1.0e30f; \
113  (max)[0] = (max)[1] = (max)[2] = -1.0e30f; \
114  } \
115  (void)0
116 #define INIT_MINMAX2(min, max) \
117  { \
118  (min)[0] = (min)[1] = 1.0e30f; \
119  (max)[0] = (max)[1] = -1.0e30f; \
120  } \
121  (void)0
122 #define DO_MIN(vec, min) \
123  { \
124  if ((min)[0] > (vec)[0]) { \
125  (min)[0] = (vec)[0]; \
126  } \
127  if ((min)[1] > (vec)[1]) { \
128  (min)[1] = (vec)[1]; \
129  } \
130  if ((min)[2] > (vec)[2]) { \
131  (min)[2] = (vec)[2]; \
132  } \
133  } \
134  (void)0
135 #define DO_MAX(vec, max) \
136  { \
137  if ((max)[0] < (vec)[0]) { \
138  (max)[0] = (vec)[0]; \
139  } \
140  if ((max)[1] < (vec)[1]) { \
141  (max)[1] = (vec)[1]; \
142  } \
143  if ((max)[2] < (vec)[2]) { \
144  (max)[2] = (vec)[2]; \
145  } \
146  } \
147  (void)0
148 #define DO_MINMAX(vec, min, max) \
149  { \
150  if ((min)[0] > (vec)[0]) { \
151  (min)[0] = (vec)[0]; \
152  } \
153  if ((min)[1] > (vec)[1]) { \
154  (min)[1] = (vec)[1]; \
155  } \
156  if ((min)[2] > (vec)[2]) { \
157  (min)[2] = (vec)[2]; \
158  } \
159  if ((max)[0] < (vec)[0]) { \
160  (max)[0] = (vec)[0]; \
161  } \
162  if ((max)[1] < (vec)[1]) { \
163  (max)[1] = (vec)[1]; \
164  } \
165  if ((max)[2] < (vec)[2]) { \
166  (max)[2] = (vec)[2]; \
167  } \
168  } \
169  (void)0
170 #define DO_MINMAX2(vec, min, max) \
171  { \
172  if ((min)[0] > (vec)[0]) { \
173  (min)[0] = (vec)[0]; \
174  } \
175  if ((min)[1] > (vec)[1]) { \
176  (min)[1] = (vec)[1]; \
177  } \
178  if ((max)[0] < (vec)[0]) { \
179  (max)[0] = (vec)[0]; \
180  } \
181  if ((max)[1] < (vec)[1]) { \
182  (max)[1] = (vec)[1]; \
183  } \
184  } \
185  (void)0
186 
189 /* -------------------------------------------------------------------- */
193 #define SWAP(type, a, b) \
194  { \
195  type sw_ap; \
196  CHECK_TYPE(a, type); \
197  CHECK_TYPE(b, type); \
198  sw_ap = (a); \
199  (a) = (b); \
200  (b) = sw_ap; \
201  } \
202  (void)0
203 
204 /* swap with a temp value */
205 #define SWAP_TVAL(tval, a, b) \
206  { \
207  CHECK_TYPE_PAIR(tval, a); \
208  CHECK_TYPE_PAIR(tval, b); \
209  (tval) = (a); \
210  (a) = (b); \
211  (b) = (tval); \
212  } \
213  (void)0
214 
215 /* shift around elements */
216 #define SHIFT3(type, a, b, c) \
217  { \
218  type tmp; \
219  CHECK_TYPE(a, type); \
220  CHECK_TYPE(b, type); \
221  CHECK_TYPE(c, type); \
222  tmp = a; \
223  a = c; \
224  c = b; \
225  b = tmp; \
226  } \
227  (void)0
228 
229 #define SHIFT4(type, a, b, c, d) \
230  { \
231  type tmp; \
232  CHECK_TYPE(a, type); \
233  CHECK_TYPE(b, type); \
234  CHECK_TYPE(c, type); \
235  CHECK_TYPE(d, type); \
236  tmp = a; \
237  a = d; \
238  d = c; \
239  c = b; \
240  b = tmp; \
241  } \
242  (void)0
243 
246 /* -------------------------------------------------------------------- */
250 /* Manual line breaks for readability. */
251 /* clang-format off */
252 
253 /* ELEM#(v, ...): is the first arg equal any others? */
254 /* internal helpers. */
255 #define _VA_ELEM2(v, a) ((v) == (a))
256 #define _VA_ELEM3(v, a, b) \
257  (_VA_ELEM2(v, a) || _VA_ELEM2(v, b))
258 #define _VA_ELEM4(v, a, b, c) \
259  (_VA_ELEM3(v, a, b) || _VA_ELEM2(v, c))
260 #define _VA_ELEM5(v, a, b, c, d) \
261  (_VA_ELEM4(v, a, b, c) || _VA_ELEM2(v, d))
262 #define _VA_ELEM6(v, a, b, c, d, e) \
263  (_VA_ELEM5(v, a, b, c, d) || _VA_ELEM2(v, e))
264 #define _VA_ELEM7(v, a, b, c, d, e, f) \
265  (_VA_ELEM6(v, a, b, c, d, e) || _VA_ELEM2(v, f))
266 #define _VA_ELEM8(v, a, b, c, d, e, f, g) \
267  (_VA_ELEM7(v, a, b, c, d, e, f) || _VA_ELEM2(v, g))
268 #define _VA_ELEM9(v, a, b, c, d, e, f, g, h) \
269  (_VA_ELEM8(v, a, b, c, d, e, f, g) || _VA_ELEM2(v, h))
270 #define _VA_ELEM10(v, a, b, c, d, e, f, g, h, i) \
271  (_VA_ELEM9(v, a, b, c, d, e, f, g, h) || _VA_ELEM2(v, i))
272 #define _VA_ELEM11(v, a, b, c, d, e, f, g, h, i, j) \
273  (_VA_ELEM10(v, a, b, c, d, e, f, g, h, i) || _VA_ELEM2(v, j))
274 #define _VA_ELEM12(v, a, b, c, d, e, f, g, h, i, j, k) \
275  (_VA_ELEM11(v, a, b, c, d, e, f, g, h, i, j) || _VA_ELEM2(v, k))
276 #define _VA_ELEM13(v, a, b, c, d, e, f, g, h, i, j, k, l) \
277  (_VA_ELEM12(v, a, b, c, d, e, f, g, h, i, j, k) || _VA_ELEM2(v, l))
278 #define _VA_ELEM14(v, a, b, c, d, e, f, g, h, i, j, k, l, m) \
279  (_VA_ELEM13(v, a, b, c, d, e, f, g, h, i, j, k, l) || _VA_ELEM2(v, m))
280 #define _VA_ELEM15(v, a, b, c, d, e, f, g, h, i, j, k, l, m, n) \
281  (_VA_ELEM14(v, a, b, c, d, e, f, g, h, i, j, k, l, m) || _VA_ELEM2(v, n))
282 #define _VA_ELEM16(v, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o) \
283  (_VA_ELEM15(v, a, b, c, d, e, f, g, h, i, j, k, l, m, n) || _VA_ELEM2(v, o))
284 #define _VA_ELEM17(v, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) \
285  (_VA_ELEM16(v, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o) || _VA_ELEM2(v, p))
286 /* clang-format on */
287 
288 /* reusable ELEM macro */
289 #define ELEM(...) VA_NARGS_CALL_OVERLOAD(_VA_ELEM, __VA_ARGS__)
290 
293 /* -------------------------------------------------------------------- */
297 /* Float equality checks. */
298 
299 #define IS_EQ(a, b) \
300  (CHECK_TYPE_INLINE(a, double), \
301  CHECK_TYPE_INLINE(b, double), \
302  ((fabs((double)((a) - (b))) >= (double)FLT_EPSILON) ? false : true))
303 
304 #define IS_EQF(a, b) \
305  (CHECK_TYPE_INLINE(a, float), \
306  CHECK_TYPE_INLINE(b, float), \
307  ((fabsf((float)((a) - (b))) >= (float)FLT_EPSILON) ? false : true))
308 
309 #define IS_EQT(a, b, c) (((a) > (b)) ? ((((a) - (b)) <= (c))) : (((((b) - (a)) <= (c)))))
310 #define IN_RANGE(a, b, c) (((b) < (c)) ? (((b) < (a) && (a) < (c))) : (((c) < (a) && (a) < (b))))
311 #define IN_RANGE_INCL(a, b, c) \
312  (((b) < (c)) ? (((b) <= (a) && (a) <= (c))) : (((c) <= (a) && (a) <= (b))))
313 
322 #define DECIMAL_DIGITS_BOUND(t) (241 * sizeof(t) / 100 + 1)
323 
326 /* -------------------------------------------------------------------- */
330 #define CLAMPIS(a, b, c) ((a) < (b) ? (b) : (a) > (c) ? (c) : (a))
331 
332 #define CLAMP(a, b, c) \
333  { \
334  if ((a) < (b)) { \
335  (a) = (b); \
336  } \
337  else if ((a) > (c)) { \
338  (a) = (c); \
339  } \
340  } \
341  (void)0
342 
343 #define CLAMP_MAX(a, c) \
344  { \
345  if ((a) > (c)) { \
346  (a) = (c); \
347  } \
348  } \
349  (void)0
350 
351 #define CLAMP_MIN(a, b) \
352  { \
353  if ((a) < (b)) { \
354  (a) = (b); \
355  } \
356  } \
357  (void)0
358 
359 #define CLAMP2(vec, b, c) \
360  { \
361  CLAMP((vec)[0], b, c); \
362  CLAMP((vec)[1], b, c); \
363  } \
364  (void)0
365 
366 #define CLAMP2_MIN(vec, b) \
367  { \
368  CLAMP_MIN((vec)[0], b); \
369  CLAMP_MIN((vec)[1], b); \
370  } \
371  (void)0
372 
373 #define CLAMP2_MAX(vec, b) \
374  { \
375  CLAMP_MAX((vec)[0], b); \
376  CLAMP_MAX((vec)[1], b); \
377  } \
378  (void)0
379 
380 #define CLAMP3(vec, b, c) \
381  { \
382  CLAMP((vec)[0], b, c); \
383  CLAMP((vec)[1], b, c); \
384  CLAMP((vec)[2], b, c); \
385  } \
386  (void)0
387 
388 #define CLAMP3_MIN(vec, b) \
389  { \
390  CLAMP_MIN((vec)[0], b); \
391  CLAMP_MIN((vec)[1], b); \
392  CLAMP_MIN((vec)[2], b); \
393  } \
394  (void)0
395 
396 #define CLAMP3_MAX(vec, b) \
397  { \
398  CLAMP_MAX((vec)[0], b); \
399  CLAMP_MAX((vec)[1], b); \
400  CLAMP_MAX((vec)[2], b); \
401  } \
402  (void)0
403 
404 #define CLAMP4(vec, b, c) \
405  { \
406  CLAMP((vec)[0], b, c); \
407  CLAMP((vec)[1], b, c); \
408  CLAMP((vec)[2], b, c); \
409  CLAMP((vec)[3], b, c); \
410  } \
411  (void)0
412 
413 #define CLAMP4_MIN(vec, b) \
414  { \
415  CLAMP_MIN((vec)[0], b); \
416  CLAMP_MIN((vec)[1], b); \
417  CLAMP_MIN((vec)[2], b); \
418  CLAMP_MIN((vec)[3], b); \
419  } \
420  (void)0
421 
422 #define CLAMP4_MAX(vec, b) \
423  { \
424  CLAMP_MAX((vec)[0], b); \
425  CLAMP_MAX((vec)[1], b); \
426  CLAMP_MAX((vec)[2], b); \
427  CLAMP_MAX((vec)[3], b); \
428  } \
429  (void)0
430 
433 /* -------------------------------------------------------------------- */
437 /* unpack vector for args */
438 #define UNPACK2(a) ((a)[0]), ((a)[1])
439 #define UNPACK3(a) UNPACK2(a), ((a)[2])
440 #define UNPACK4(a) UNPACK3(a), ((a)[3])
441 /* pre may be '&', '*' or func, post may be '->member' */
442 #define UNPACK2_EX(pre, a, post) (pre((a)[0]) post), (pre((a)[1]) post)
443 #define UNPACK3_EX(pre, a, post) UNPACK2_EX(pre, a, post), (pre((a)[2]) post)
444 #define UNPACK4_EX(pre, a, post) UNPACK3_EX(pre, a, post), (pre((a)[3]) post)
445 
448 /* -------------------------------------------------------------------- */
452 /* array helpers */
453 #define ARRAY_LAST_ITEM(arr_start, arr_dtype, arr_len) \
454  (arr_dtype *)((char *)(arr_start) + (sizeof(*((arr_dtype *)NULL)) * (size_t)(arr_len - 1)))
455 
456 #define ARRAY_HAS_ITEM(arr_item, arr_start, arr_len) \
457  (CHECK_TYPE_PAIR_INLINE(arr_start, arr_item), \
458  ((unsigned int)((arr_item) - (arr_start)) < (unsigned int)(arr_len)))
459 
463 #define ARRAY_DELETE(arr, index, delete_len, arr_len) \
464  { \
465  BLI_assert((&arr[index] >= arr) && ((index) + delete_len <= arr_len)); \
466  memmove(&(arr)[index], \
467  &(arr)[(index) + (delete_len)], \
468  (((arr_len) - (index)) - (delete_len)) * sizeof(*(arr))); \
469  } \
470  ((void)0)
471 
479 #define ARRAY_DELETE_REORDER_LAST(arr, index, delete_len, arr_len) \
480  { \
481  BLI_assert((&arr[index] >= arr) && ((index) + delete_len <= arr_len)); \
482  if ((index) + (delete_len) != (arr_len)) { \
483  if (((delete_len) == 1) || ((delete_len) <= ((arr_len) - ((index) + (delete_len))))) { \
484  memcpy(&(arr)[index], &(arr)[(arr_len) - (delete_len)], (delete_len) * sizeof(*(arr))); \
485  } \
486  else { \
487  memcpy(&(arr)[index], \
488  &(arr)[(arr_len) - ((arr_len) - ((index) + (delete_len)))], \
489  ((arr_len) - ((index) + (delete_len))) * sizeof(*(arr))); \
490  } \
491  } \
492  } \
493  ((void)0)
494 
495 /* assuming a static array */
496 #if defined(__GNUC__) && !defined(__cplusplus) && !defined(__clang__) && !defined(__INTEL_COMPILER)
497 # define ARRAY_SIZE(arr) \
498  ((sizeof(struct { int isnt_array : ((const void *)&(arr) == &(arr)[0]); }) * 0) + \
499  (sizeof(arr) / sizeof(*(arr))))
500 #else
501 # define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(*(arr)))
502 #endif
503 
504 /* ARRAY_SET_ITEMS#(v, ...): set indices of array 'v' */
505 /* internal helpers */
506 #define _VA_ARRAY_SET_ITEMS2(v, a) ((v)[0] = (a))
507 #define _VA_ARRAY_SET_ITEMS3(v, a, b) \
508  _VA_ARRAY_SET_ITEMS2(v, a); \
509  ((v)[1] = (b))
510 #define _VA_ARRAY_SET_ITEMS4(v, a, b, c) \
511  _VA_ARRAY_SET_ITEMS3(v, a, b); \
512  ((v)[2] = (c))
513 #define _VA_ARRAY_SET_ITEMS5(v, a, b, c, d) \
514  _VA_ARRAY_SET_ITEMS4(v, a, b, c); \
515  ((v)[3] = (d))
516 #define _VA_ARRAY_SET_ITEMS6(v, a, b, c, d, e) \
517  _VA_ARRAY_SET_ITEMS5(v, a, b, c, d); \
518  ((v)[4] = (e))
519 #define _VA_ARRAY_SET_ITEMS7(v, a, b, c, d, e, f) \
520  _VA_ARRAY_SET_ITEMS6(v, a, b, c, d, e); \
521  ((v)[5] = (f))
522 #define _VA_ARRAY_SET_ITEMS8(v, a, b, c, d, e, f, g) \
523  _VA_ARRAY_SET_ITEMS7(v, a, b, c, d, e, f); \
524  ((v)[6] = (g))
525 #define _VA_ARRAY_SET_ITEMS9(v, a, b, c, d, e, f, g, h) \
526  _VA_ARRAY_SET_ITEMS8(v, a, b, c, d, e, f, g); \
527  ((v)[7] = (h))
528 #define _VA_ARRAY_SET_ITEMS10(v, a, b, c, d, e, f, g, h, i) \
529  _VA_ARRAY_SET_ITEMS9(v, a, b, c, d, e, f, g, h); \
530  ((v)[8] = (i))
531 #define _VA_ARRAY_SET_ITEMS11(v, a, b, c, d, e, f, g, h, i, j) \
532  _VA_ARRAY_SET_ITEMS10(v, a, b, c, d, e, f, g, h, i); \
533  ((v)[9] = (j))
534 #define _VA_ARRAY_SET_ITEMS12(v, a, b, c, d, e, f, g, h, i, j, k) \
535  _VA_ARRAY_SET_ITEMS11(v, a, b, c, d, e, f, g, h, i, j); \
536  ((v)[10] = (k))
537 #define _VA_ARRAY_SET_ITEMS13(v, a, b, c, d, e, f, g, h, i, j, k, l) \
538  _VA_ARRAY_SET_ITEMS12(v, a, b, c, d, e, f, g, h, i, j, k); \
539  ((v)[11] = (l))
540 #define _VA_ARRAY_SET_ITEMS14(v, a, b, c, d, e, f, g, h, i, j, k, l, m) \
541  _VA_ARRAY_SET_ITEMS13(v, a, b, c, d, e, f, g, h, i, j, k, l); \
542  ((v)[12] = (m))
543 #define _VA_ARRAY_SET_ITEMS15(v, a, b, c, d, e, f, g, h, i, j, k, l, m, n) \
544  _VA_ARRAY_SET_ITEMS14(v, a, b, c, d, e, f, g, h, i, j, k, l, m); \
545  ((v)[13] = (n))
546 #define _VA_ARRAY_SET_ITEMS16(v, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o) \
547  _VA_ARRAY_SET_ITEMS15(v, a, b, c, d, e, f, g, h, i, j, k, l, m, n); \
548  ((v)[14] = (o))
549 #define _VA_ARRAY_SET_ITEMS17(v, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) \
550  _VA_ARRAY_SET_ITEMS16(v, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o); \
551  ((v)[15] = (p))
552 
553 /* reusable ARRAY_SET_ITEMS macro */
554 #define ARRAY_SET_ITEMS(...) \
555  { \
556  VA_NARGS_CALL_OVERLOAD(_VA_ARRAY_SET_ITEMS, __VA_ARGS__); \
557  } \
558  (void)0
559 
562 /* -------------------------------------------------------------------- */
566 #if defined(__GNUC__) || defined(__clang__)
567 # define POINTER_OFFSET(v, ofs) ((typeof(v))((char *)(v) + (ofs)))
568 #else
569 # define POINTER_OFFSET(v, ofs) ((void *)((char *)(v) + (ofs)))
570 #endif
571 
572 /* Warning-free macros for storing ints in pointers. Use these _only_
573  * for storing an int in a pointer, not a pointer in an int (64bit)! */
574 #define POINTER_FROM_INT(i) ((void *)(intptr_t)(i))
575 #define POINTER_AS_INT(i) ((void)0, ((int)(intptr_t)(i)))
576 
577 #define POINTER_FROM_UINT(i) ((void *)(uintptr_t)(i))
578 #define POINTER_AS_UINT(i) ((void)0, ((unsigned int)(uintptr_t)(i)))
579 
582 /* -------------------------------------------------------------------- */
591 #define OFFSETOF_STRUCT_AFTER(_struct, _member) \
592  ((((const char *)&((_struct)->_member)) - ((const char *)(_struct))) + \
593  sizeof((_struct)->_member))
594 
600 #define MEMCPY_STRUCT_AFTER(struct_dst, struct_src, member) \
601  { \
602  CHECK_TYPE_NONCONST(struct_dst); \
603  ((void)(struct_dst == struct_src), \
604  memcpy((char *)(struct_dst) + OFFSETOF_STRUCT_AFTER(struct_dst, member), \
605  (const char *)(struct_src) + OFFSETOF_STRUCT_AFTER(struct_dst, member), \
606  sizeof(*(struct_dst)) - OFFSETOF_STRUCT_AFTER(struct_dst, member))); \
607  } \
608  ((void)0)
609 
610 #define MEMSET_STRUCT_AFTER(struct_var, value, member) \
611  { \
612  CHECK_TYPE_NONCONST(struct_var); \
613  memset((char *)(struct_var) + OFFSETOF_STRUCT_AFTER(struct_var, member), \
614  value, \
615  sizeof(*(struct_var)) - OFFSETOF_STRUCT_AFTER(struct_var, member)); \
616  } \
617  ((void)0)
618 
619 /* defined
620  * in memory_utils.c for now. I do not know where we should put it actually... */
621 #ifndef __BLI_MEMORY_UTILS_H__
625 extern bool BLI_memory_is_zero(const void *arr, size_t arr_size);
626 #endif
627 
628 #define MEMCMP_STRUCT_AFTER_IS_ZERO(struct_var, member) \
629  (BLI_memory_is_zero((const char *)(struct_var) + OFFSETOF_STRUCT_AFTER(struct_var, member), \
630  sizeof(*(struct_var)) - OFFSETOF_STRUCT_AFTER(struct_var, member)))
631 
634 /* -------------------------------------------------------------------- */
638 /* Macro to convert a value to string in the preprocessor:
639  * - `STRINGIFY_ARG`: gives the argument as a string
640  * - `STRINGIFY_APPEND`: appends any argument 'b' onto the string argument 'a',
641  * used by `STRINGIFY` because some preprocessors warn about zero arguments.
642  * - `STRINGIFY`: gives the argument's value as a string. */
643 
644 #define STRINGIFY_ARG(x) "" #x
645 #define STRINGIFY_APPEND(a, b) "" a #b
646 #define STRINGIFY(x) STRINGIFY_APPEND("", x)
647 
648 /* generic strcmp macros */
649 #if defined(_MSC_VER)
650 # define strcasecmp _stricmp
651 # define strncasecmp _strnicmp
652 #endif
653 
654 #define STREQ(a, b) (strcmp(a, b) == 0)
655 #define STRCASEEQ(a, b) (strcasecmp(a, b) == 0)
656 #define STREQLEN(a, b, n) (strncmp(a, b, n) == 0)
657 #define STRCASEEQLEN(a, b, n) (strncasecmp(a, b, n) == 0)
658 
659 #define STRPREFIX(a, b) (strncmp((a), (b), strlen(b)) == 0)
660 
663 /* -------------------------------------------------------------------- */
667 /* UNUSED macro, for function argument */
668 #if defined(__GNUC__) || defined(__clang__)
669 # define UNUSED(x) UNUSED_##x __attribute__((__unused__))
670 #else
671 # define UNUSED(x) UNUSED_##x
672 #endif
673 
678 #if defined(__GNUC__) || defined(__clang__)
679 # define UNUSED_FUNCTION(x) __attribute__((__unused__)) UNUSED_##x
680 #else
681 # define UNUSED_FUNCTION(x) UNUSED_##x
682 #endif
683 
684 #if defined(__GNUC__) || defined(__clang__)
685 # define UNUSED_FUNCTION_WITH_RETURN_TYPE(rtype, x) __attribute__((__unused__)) rtype UNUSED_##x
686 #else
687 # define UNUSED_FUNCTION_WITH_RETURN_TYPE(rtype, x) rtype UNUSED_##x
688 #endif
689 
703 #define _VA_UNUSED_VARS_1(a0) ((void)(a0))
704 #define _VA_UNUSED_VARS_2(a0, b0) ((void)(a0), _VA_UNUSED_VARS_1(b0))
705 #define _VA_UNUSED_VARS_3(a0, b0, c0) ((void)(a0), _VA_UNUSED_VARS_2(b0, c0))
706 #define _VA_UNUSED_VARS_4(a0, b0, c0, d0) ((void)(a0), _VA_UNUSED_VARS_3(b0, c0, d0))
707 #define _VA_UNUSED_VARS_5(a0, b0, c0, d0, e0) ((void)(a0), _VA_UNUSED_VARS_4(b0, c0, d0, e0))
708 #define _VA_UNUSED_VARS_6(a0, b0, c0, d0, e0, f0) \
709  ((void)(a0), _VA_UNUSED_VARS_5(b0, c0, d0, e0, f0))
710 #define _VA_UNUSED_VARS_7(a0, b0, c0, d0, e0, f0, g0) \
711  ((void)(a0), _VA_UNUSED_VARS_6(b0, c0, d0, e0, f0, g0))
712 #define _VA_UNUSED_VARS_8(a0, b0, c0, d0, e0, f0, g0, h0) \
713  ((void)(a0), _VA_UNUSED_VARS_7(b0, c0, d0, e0, f0, g0, h0))
714 #define _VA_UNUSED_VARS_9(a0, b0, c0, d0, e0, f0, g0, h0, i0) \
715  ((void)(a0), _VA_UNUSED_VARS_8(b0, c0, d0, e0, f0, g0, h0, i0))
716 #define _VA_UNUSED_VARS_10(a0, b0, c0, d0, e0, f0, g0, h0, i0, j0) \
717  ((void)(a0), _VA_UNUSED_VARS_9(b0, c0, d0, e0, f0, g0, h0, i0, j0))
718 #define _VA_UNUSED_VARS_11(a0, b0, c0, d0, e0, f0, g0, h0, i0, j0, k0) \
719  ((void)(a0), _VA_UNUSED_VARS_10(b0, c0, d0, e0, f0, g0, h0, i0, j0, k0))
720 #define _VA_UNUSED_VARS_12(a0, b0, c0, d0, e0, f0, g0, h0, i0, j0, k0, l0) \
721  ((void)(a0), _VA_UNUSED_VARS_11(b0, c0, d0, e0, f0, g0, h0, i0, j0, k0, l0))
722 #define _VA_UNUSED_VARS_13(a0, b0, c0, d0, e0, f0, g0, h0, i0, j0, k0, l0, m0) \
723  ((void)(a0), _VA_UNUSED_VARS_12(b0, c0, d0, e0, f0, g0, h0, i0, j0, k0, l0, m0))
724 #define _VA_UNUSED_VARS_14(a0, b0, c0, d0, e0, f0, g0, h0, i0, j0, k0, l0, m0, n0) \
725  ((void)(a0), _VA_UNUSED_VARS_13(b0, c0, d0, e0, f0, g0, h0, i0, j0, k0, l0, m0, n0))
726 #define _VA_UNUSED_VARS_15(a0, b0, c0, d0, e0, f0, g0, h0, i0, j0, k0, l0, m0, n0, o0) \
727  ((void)(a0), _VA_UNUSED_VARS_14(b0, c0, d0, e0, f0, g0, h0, i0, j0, k0, l0, m0, n0, o0))
728 #define _VA_UNUSED_VARS_16(a0, b0, c0, d0, e0, f0, g0, h0, i0, j0, k0, l0, m0, n0, o0, p0) \
729  ((void)(a0), _VA_UNUSED_VARS_15(b0, c0, d0, e0, f0, g0, h0, i0, j0, k0, l0, m0, n0, o0, p0))
730 
731 /* reusable ELEM macro */
732 #define UNUSED_VARS(...) VA_NARGS_CALL_OVERLOAD(_VA_UNUSED_VARS_, __VA_ARGS__)
733 
734 /* for debug-only variables */
735 #ifndef NDEBUG
736 # define UNUSED_VARS_NDEBUG(...)
737 #else
738 # define UNUSED_VARS_NDEBUG UNUSED_VARS
739 #endif
740 
743 /* -------------------------------------------------------------------- */
747 /* hints for branch prediction, only use in code that runs a _lot_ where */
748 #ifdef __GNUC__
749 # define LIKELY(x) __builtin_expect(!!(x), 1)
750 # define UNLIKELY(x) __builtin_expect(!!(x), 0)
751 #else
752 # define LIKELY(x) (x)
753 # define UNLIKELY(x) (x)
754 #endif
755 
758 /* -------------------------------------------------------------------- */
762 /* Set flag from a single test */
763 #define SET_FLAG_FROM_TEST(value, test, flag) \
764  { \
765  if (test) { \
766  (value) |= (flag); \
767  } \
768  else { \
769  (value) &= ~(flag); \
770  } \
771  } \
772  ((void)0)
773 
776 /* -------------------------------------------------------------------- */
780 #ifdef __cplusplus
781 
782 /* Useful to port C code using enums to C++ where enums are strongly typed.
783  * To use after the enum declaration. */
784 /* If any enumerator `C` is set to say `A|B`, then `C` would be the max enum value. */
785 # define ENUM_OPERATORS(_enum_type, _max_enum_value) \
786  extern "C++" { \
787  inline constexpr _enum_type operator|(_enum_type a, _enum_type b) \
788  { \
789  return static_cast<_enum_type>(static_cast<uint64_t>(a) | static_cast<uint64_t>(b)); \
790  } \
791  inline constexpr _enum_type operator&(_enum_type a, _enum_type b) \
792  { \
793  return static_cast<_enum_type>(static_cast<uint64_t>(a) & static_cast<uint64_t>(b)); \
794  } \
795  inline constexpr _enum_type operator~(_enum_type a) \
796  { \
797  return static_cast<_enum_type>(~static_cast<uint64_t>(a) & \
798  (2 * static_cast<uint64_t>(_max_enum_value) - 1)); \
799  } \
800  inline _enum_type &operator|=(_enum_type &a, _enum_type b) \
801  { \
802  return a = static_cast<_enum_type>(static_cast<uint64_t>(a) | static_cast<uint64_t>(b)); \
803  } \
804  inline _enum_type &operator&=(_enum_type &a, _enum_type b) \
805  { \
806  return a = static_cast<_enum_type>(static_cast<uint64_t>(a) & static_cast<uint64_t>(b)); \
807  } \
808  } /* extern "C++" */
809 
810 #else
811 /* Output nothing. */
812 # define ENUM_OPERATORS(_type, _max)
813 #endif
814 
819 #ifdef __cplusplus
820 # define CPP_ARG_DEFAULT(default_value) = default_value
821 #else
822 # define CPP_ARG_DEFAULT(default_value)
823 #endif
824 
827 /* -------------------------------------------------------------------- */
832 #define AT __FILE__ ":" STRINGIFY(__LINE__)
833 
835 #define EXPR_NOP(expr) (void)(0 ? ((void)(expr), 1) : 0)
836 
844 #define BLI_ENABLE_IF(condition) typename std::enable_if_t<(condition)> * = nullptr
845 
846 #if defined(_MSC_VER)
847 # define BLI_NO_UNIQUE_ADDRESS [[msvc::no_unique_address]]
848 #elif defined(__has_cpp_attribute)
849 # if __has_cpp_attribute(no_unique_address)
850 # define BLI_NO_UNIQUE_ADDRESS [[no_unique_address]]
851 # else
852 # define BLI_NO_UNIQUE_ADDRESS
853 # endif
854 #else
855 # define BLI_NO_UNIQUE_ADDRESS [[no_unique_address]]
856 #endif
857 
860 #ifdef __cplusplus
861 }
862 #endif
863 
864 #endif /* __BLI_UTILDEFINES_H__ */
bool BLI_memory_is_zero(const void *arr, size_t arr_size)
Definition: memory_utils.c:19