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