6#ifndef __BLI_UTILDEFINES_H__
7#define __BLI_UTILDEFINES_H__
25# include <type_traits>
36# define MIN2(a, b) ((a) < (b) ? (a) : (b))
37# define MAX2(a, b) ((a) > (b) ? (a) : (b))
40#define INIT_MINMAX(min, max) \
42 (min)[0] = (min)[1] = (min)[2] = 1.0e30f; \
43 (max)[0] = (max)[1] = (max)[2] = -1.0e30f; \
46#define INIT_MINMAX2(min, max) \
48 (min)[0] = (min)[1] = 1.0e30f; \
49 (max)[0] = (max)[1] = -1.0e30f; \
59#define SWAP(type, a, b) \
62 CHECK_TYPE(a, type); \
63 CHECK_TYPE(b, type); \
71#define SHIFT3(type, a, b, c) \
74 CHECK_TYPE(a, type); \
75 CHECK_TYPE(b, type); \
76 CHECK_TYPE(c, type); \
84#define SHIFT4(type, a, b, c, d) \
87 CHECK_TYPE(a, type); \
88 CHECK_TYPE(b, type); \
89 CHECK_TYPE(c, type); \
90 CHECK_TYPE(d, type); \
110#define _VA_ELEM2(v, a) ((v) == (a))
111#define _VA_ELEM3(v, a, b) \
112 (_VA_ELEM2(v, a) || _VA_ELEM2(v, b))
113#define _VA_ELEM4(v, a, b, c) \
114 (_VA_ELEM3(v, a, b) || _VA_ELEM2(v, c))
115#define _VA_ELEM5(v, a, b, c, d) \
116 (_VA_ELEM4(v, a, b, c) || _VA_ELEM2(v, d))
117#define _VA_ELEM6(v, a, b, c, d, e) \
118 (_VA_ELEM5(v, a, b, c, d) || _VA_ELEM2(v, e))
119#define _VA_ELEM7(v, a, b, c, d, e, f) \
120 (_VA_ELEM6(v, a, b, c, d, e) || _VA_ELEM2(v, f))
121#define _VA_ELEM8(v, a, b, c, d, e, f, g) \
122 (_VA_ELEM7(v, a, b, c, d, e, f) || _VA_ELEM2(v, g))
123#define _VA_ELEM9(v, a, b, c, d, e, f, g, h) \
124 (_VA_ELEM8(v, a, b, c, d, e, f, g) || _VA_ELEM2(v, h))
125#define _VA_ELEM10(v, a, b, c, d, e, f, g, h, i) \
126 (_VA_ELEM9(v, a, b, c, d, e, f, g, h) || _VA_ELEM2(v, i))
127#define _VA_ELEM11(v, a, b, c, d, e, f, g, h, i, j) \
128 (_VA_ELEM10(v, a, b, c, d, e, f, g, h, i) || _VA_ELEM2(v, j))
129#define _VA_ELEM12(v, a, b, c, d, e, f, g, h, i, j, k) \
130 (_VA_ELEM11(v, a, b, c, d, e, f, g, h, i, j) || _VA_ELEM2(v, k))
131#define _VA_ELEM13(v, a, b, c, d, e, f, g, h, i, j, k, l) \
132 (_VA_ELEM12(v, a, b, c, d, e, f, g, h, i, j, k) || _VA_ELEM2(v, l))
133#define _VA_ELEM14(v, a, b, c, d, e, f, g, h, i, j, k, l, m) \
134 (_VA_ELEM13(v, a, b, c, d, e, f, g, h, i, j, k, l) || _VA_ELEM2(v, m))
135#define _VA_ELEM15(v, a, b, c, d, e, f, g, h, i, j, k, l, m, n) \
136 (_VA_ELEM14(v, a, b, c, d, e, f, g, h, i, j, k, l, m) || _VA_ELEM2(v, n))
137#define _VA_ELEM16(v, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o) \
138 (_VA_ELEM15(v, a, b, c, d, e, f, g, h, i, j, k, l, m, n) || _VA_ELEM2(v, o))
139#define _VA_ELEM17(v, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) \
140 (_VA_ELEM16(v, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o) || _VA_ELEM2(v, p))
144#define ELEM(...) VA_NARGS_CALL_OVERLOAD(_VA_ELEM, __VA_ARGS__)
155 (CHECK_TYPE_INLINE_NONCONST(a, double), \
156 CHECK_TYPE_INLINE_NONCONST(b, double), \
157 ((fabs((double)((a) - (b))) >= (double)FLT_EPSILON) ? false : true))
159#define IS_EQF(a, b) \
160 (CHECK_TYPE_INLINE_NONCONST(a, float), \
161 CHECK_TYPE_INLINE_NONCONST(b, float), \
162 ((fabsf((float)((a) - (b))) >= (float)FLT_EPSILON) ? false : true))
164#define IS_EQT(a, b, c) (((a) > (b)) ? ((((a) - (b)) <= (c))) : (((((b) - (a)) <= (c)))))
165#define IN_RANGE(a, b, c) (((b) < (c)) ? (((b) < (a) && (a) < (c))) : (((c) < (a) && (a) < (b))))
166#define IN_RANGE_INCL(a, b, c) \
167 (((b) < (c)) ? (((b) <= (a) && (a) <= (c))) : (((c) <= (a) && (a) <= (b))))
177#define DECIMAL_DIGITS_BOUND(t) (241 * sizeof(t) / 100 + 1)
183 return (x & (x - 1)) == 0;
189 return x <= 1 ? 0 : 1 + log2_floor(x >> 1);
195 return (is_power_of_2(
int(x))) ? log2_floor(x) : log2_floor(x) + 1;
201 return 1ll << log2_ceil(x);
211#define CLAMP(a, b, c) \
216 else if ((a) > (c)) { \
222#define CLAMP_MAX(a, c) \
230#define CLAMP_MIN(a, b) \
238#define CLAMP3(vec, b, c) \
240 CLAMP((vec)[0], b, c); \
241 CLAMP((vec)[1], b, c); \
242 CLAMP((vec)[2], b, c); \
246#define CLAMP3_MIN(vec, b) \
248 CLAMP_MIN((vec)[0], b); \
249 CLAMP_MIN((vec)[1], b); \
250 CLAMP_MIN((vec)[2], b); \
254#define CLAMP4_MIN(vec, b) \
256 CLAMP_MIN((vec)[0], b); \
257 CLAMP_MIN((vec)[1], b); \
258 CLAMP_MIN((vec)[2], b); \
259 CLAMP_MIN((vec)[3], b); \
270#define UNPACK2(a) ((a)[0]), ((a)[1])
271#define UNPACK3(a) UNPACK2(a), ((a)[2])
272#define UNPACK4(a) UNPACK3(a), ((a)[3])
274#define UNPACK2_EX(pre, a, post) (pre((a)[0]) post), (pre((a)[1]) post)
275#define UNPACK3_EX(pre, a, post) UNPACK2_EX(pre, a, post), (pre((a)[2]) post)
276#define UNPACK4_EX(pre, a, post) UNPACK3_EX(pre, a, post), (pre((a)[3]) post)
284#define ARRAY_HAS_ITEM(arr_item, arr_start, arr_len) \
285 (CHECK_TYPE_PAIR_INLINE(arr_start, arr_item), \
286 ((size_t)((arr_item) - (arr_start)) < (size_t)(arr_len)))
290# if defined(__GNUC__) && !defined(__cplusplus) && !defined(__clang__) && \
291 !defined(__INTEL_COMPILER)
292# define ARRAY_SIZE(arr) \
293 ((sizeof(struct { int isnt_array : ((const void *)&(arr) == &(arr)[0]); }) * 0) + \
294 (sizeof(arr) / sizeof(*(arr))))
296# define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(*(arr)))
302#define _VA_ARRAY_SET_ITEMS2(v, a) ((v)[0] = (a))
303#define _VA_ARRAY_SET_ITEMS3(v, a, b) \
304 _VA_ARRAY_SET_ITEMS2(v, a); \
306#define _VA_ARRAY_SET_ITEMS4(v, a, b, c) \
307 _VA_ARRAY_SET_ITEMS3(v, a, b); \
309#define _VA_ARRAY_SET_ITEMS5(v, a, b, c, d) \
310 _VA_ARRAY_SET_ITEMS4(v, a, b, c); \
312#define _VA_ARRAY_SET_ITEMS6(v, a, b, c, d, e) \
313 _VA_ARRAY_SET_ITEMS5(v, a, b, c, d); \
315#define _VA_ARRAY_SET_ITEMS7(v, a, b, c, d, e, f) \
316 _VA_ARRAY_SET_ITEMS6(v, a, b, c, d, e); \
318#define _VA_ARRAY_SET_ITEMS8(v, a, b, c, d, e, f, g) \
319 _VA_ARRAY_SET_ITEMS7(v, a, b, c, d, e, f); \
321#define _VA_ARRAY_SET_ITEMS9(v, a, b, c, d, e, f, g, h) \
322 _VA_ARRAY_SET_ITEMS8(v, a, b, c, d, e, f, g); \
324#define _VA_ARRAY_SET_ITEMS10(v, a, b, c, d, e, f, g, h, i) \
325 _VA_ARRAY_SET_ITEMS9(v, a, b, c, d, e, f, g, h); \
327#define _VA_ARRAY_SET_ITEMS11(v, a, b, c, d, e, f, g, h, i, j) \
328 _VA_ARRAY_SET_ITEMS10(v, a, b, c, d, e, f, g, h, i); \
330#define _VA_ARRAY_SET_ITEMS12(v, a, b, c, d, e, f, g, h, i, j, k) \
331 _VA_ARRAY_SET_ITEMS11(v, a, b, c, d, e, f, g, h, i, j); \
333#define _VA_ARRAY_SET_ITEMS13(v, a, b, c, d, e, f, g, h, i, j, k, l) \
334 _VA_ARRAY_SET_ITEMS12(v, a, b, c, d, e, f, g, h, i, j, k); \
336#define _VA_ARRAY_SET_ITEMS14(v, a, b, c, d, e, f, g, h, i, j, k, l, m) \
337 _VA_ARRAY_SET_ITEMS13(v, a, b, c, d, e, f, g, h, i, j, k, l); \
339#define _VA_ARRAY_SET_ITEMS15(v, a, b, c, d, e, f, g, h, i, j, k, l, m, n) \
340 _VA_ARRAY_SET_ITEMS14(v, a, b, c, d, e, f, g, h, i, j, k, l, m); \
342#define _VA_ARRAY_SET_ITEMS16(v, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o) \
343 _VA_ARRAY_SET_ITEMS15(v, a, b, c, d, e, f, g, h, i, j, k, l, m, n); \
345#define _VA_ARRAY_SET_ITEMS17(v, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) \
346 _VA_ARRAY_SET_ITEMS16(v, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o); \
350#define ARRAY_SET_ITEMS(...) \
352 VA_NARGS_CALL_OVERLOAD(_VA_ARRAY_SET_ITEMS, __VA_ARGS__); \
362#if defined(__GNUC__) || defined(__clang__)
363# define POINTER_OFFSET(v, ofs) ((typeof(v))((char *)(v) + (ofs)))
365# define POINTER_OFFSET(v, ofs) ((void *)((char *)(v) + (ofs)))
370#define POINTER_FROM_INT(i) ((void *)(intptr_t)(i))
371#define POINTER_AS_INT(i) ((void)0, ((int)(intptr_t)(i)))
373#define POINTER_FROM_UINT(i) ((void *)(uintptr_t)(i))
374#define POINTER_AS_UINT(i) ((void)0, ((unsigned int)(uintptr_t)(i)))
387#define OFFSETOF_STRUCT_AFTER(_struct, _member) \
388 ((size_t)(((const char *)&((_struct)->_member)) - ((const char *)(_struct))) + \
389 sizeof((_struct)->_member))
396#define MEMCPY_STRUCT_AFTER(struct_dst, struct_src, member) \
398 CHECK_TYPE_NONCONST(struct_dst); \
399 ((void)(struct_dst == struct_src), \
400 memcpy((char *)(struct_dst) + OFFSETOF_STRUCT_AFTER(struct_dst, member), \
401 (const char *)(struct_src) + OFFSETOF_STRUCT_AFTER(struct_dst, member), \
402 sizeof(*(struct_dst)) - OFFSETOF_STRUCT_AFTER(struct_dst, member))); \
406#define MEMSET_STRUCT_AFTER(struct_var, value, member) \
408 CHECK_TYPE_NONCONST(struct_var); \
409 memset((char *)(struct_var) + OFFSETOF_STRUCT_AFTER(struct_var, member), \
411 sizeof(*(struct_var)) - OFFSETOF_STRUCT_AFTER(struct_var, member)); \
417#ifndef __BLI_MEMORY_UTILS_H__
424#define MEMCMP_STRUCT_AFTER_IS_ZERO(struct_var, member) \
425 (BLI_memory_is_zero((const char *)(struct_var) + OFFSETOF_STRUCT_AFTER(struct_var, member), \
426 sizeof(*(struct_var)) - OFFSETOF_STRUCT_AFTER(struct_var, member)))
440#define STRINGIFY_ARG(x) "" #x
441#define STRINGIFY_APPEND(a, b) "" a #b
442#define STRINGIFY(x) STRINGIFY_APPEND("", x)
446# define strcasecmp _stricmp
447# define strncasecmp _strnicmp
450#define STREQ(a, b) (strcmp(a, b) == 0)
451#define STRCASEEQ(a, b) (strcasecmp(a, b) == 0)
452#define STREQLEN(a, b, n) (strncmp(a, b, n) == 0)
453#define STRCASEEQLEN(a, b, n) (strncasecmp(a, b, n) == 0)
455#define STRPREFIX(a, b) (strncmp((a), (b), strlen(b)) == 0)
465# if defined(__GNUC__) || defined(__clang__)
466# define UNUSED(x) UNUSED_##x __attribute__((__unused__))
467# elif defined(_MSC_VER)
469# define UNUSED(x) UNUSED_##x __pragma(warning(suppress : 4100))
471# define UNUSED(x) UNUSED_##x
479#if defined(__GNUC__) || defined(__clang__)
480# define UNUSED_FUNCTION(x) __attribute__((__unused__)) UNUSED_##x
482# define UNUSED_FUNCTION(x) UNUSED_##x
485#if defined(__GNUC__) || defined(__clang__)
486# define UNUSED_FUNCTION_WITH_RETURN_TYPE(rtype, x) __attribute__((__unused__)) rtype UNUSED_##x
488# define UNUSED_FUNCTION_WITH_RETURN_TYPE(rtype, x) rtype UNUSED_##x
504#define _VA_UNUSED_VARS_1(a0) ((void)(a0))
505#define _VA_UNUSED_VARS_2(a0, b0) ((void)(a0), _VA_UNUSED_VARS_1(b0))
506#define _VA_UNUSED_VARS_3(a0, b0, c0) ((void)(a0), _VA_UNUSED_VARS_2(b0, c0))
507#define _VA_UNUSED_VARS_4(a0, b0, c0, d0) ((void)(a0), _VA_UNUSED_VARS_3(b0, c0, d0))
508#define _VA_UNUSED_VARS_5(a0, b0, c0, d0, e0) ((void)(a0), _VA_UNUSED_VARS_4(b0, c0, d0, e0))
509#define _VA_UNUSED_VARS_6(a0, b0, c0, d0, e0, f0) \
510 ((void)(a0), _VA_UNUSED_VARS_5(b0, c0, d0, e0, f0))
511#define _VA_UNUSED_VARS_7(a0, b0, c0, d0, e0, f0, g0) \
512 ((void)(a0), _VA_UNUSED_VARS_6(b0, c0, d0, e0, f0, g0))
513#define _VA_UNUSED_VARS_8(a0, b0, c0, d0, e0, f0, g0, h0) \
514 ((void)(a0), _VA_UNUSED_VARS_7(b0, c0, d0, e0, f0, g0, h0))
515#define _VA_UNUSED_VARS_9(a0, b0, c0, d0, e0, f0, g0, h0, i0) \
516 ((void)(a0), _VA_UNUSED_VARS_8(b0, c0, d0, e0, f0, g0, h0, i0))
517#define _VA_UNUSED_VARS_10(a0, b0, c0, d0, e0, f0, g0, h0, i0, j0) \
518 ((void)(a0), _VA_UNUSED_VARS_9(b0, c0, d0, e0, f0, g0, h0, i0, j0))
519#define _VA_UNUSED_VARS_11(a0, b0, c0, d0, e0, f0, g0, h0, i0, j0, k0) \
520 ((void)(a0), _VA_UNUSED_VARS_10(b0, c0, d0, e0, f0, g0, h0, i0, j0, k0))
521#define _VA_UNUSED_VARS_12(a0, b0, c0, d0, e0, f0, g0, h0, i0, j0, k0, l0) \
522 ((void)(a0), _VA_UNUSED_VARS_11(b0, c0, d0, e0, f0, g0, h0, i0, j0, k0, l0))
523#define _VA_UNUSED_VARS_13(a0, b0, c0, d0, e0, f0, g0, h0, i0, j0, k0, l0, m0) \
524 ((void)(a0), _VA_UNUSED_VARS_12(b0, c0, d0, e0, f0, g0, h0, i0, j0, k0, l0, m0))
525#define _VA_UNUSED_VARS_14(a0, b0, c0, d0, e0, f0, g0, h0, i0, j0, k0, l0, m0, n0) \
526 ((void)(a0), _VA_UNUSED_VARS_13(b0, c0, d0, e0, f0, g0, h0, i0, j0, k0, l0, m0, n0))
527#define _VA_UNUSED_VARS_15(a0, b0, c0, d0, e0, f0, g0, h0, i0, j0, k0, l0, m0, n0, o0) \
528 ((void)(a0), _VA_UNUSED_VARS_14(b0, c0, d0, e0, f0, g0, h0, i0, j0, k0, l0, m0, n0, o0))
529#define _VA_UNUSED_VARS_16(a0, b0, c0, d0, e0, f0, g0, h0, i0, j0, k0, l0, m0, n0, o0, p0) \
530 ((void)(a0), _VA_UNUSED_VARS_15(b0, c0, d0, e0, f0, g0, h0, i0, j0, k0, l0, m0, n0, o0, p0))
533#define UNUSED_VARS(...) VA_NARGS_CALL_OVERLOAD(_VA_UNUSED_VARS_, __VA_ARGS__)
537# define UNUSED_VARS_NDEBUG(...)
539# define UNUSED_VARS_NDEBUG UNUSED_VARS
550# define LIKELY(x) __builtin_expect(!!(x), 1)
551# define UNLIKELY(x) __builtin_expect(!!(x), 0)
553# define LIKELY(x) (x)
554# define UNLIKELY(x) (x)
564#define SET_FLAG_FROM_TEST(value, test, flag) \
570 (value) &= ~(flag); \
586# define ENUM_OPERATORS(_enum_type, _max_enum_value) \
588 inline constexpr _enum_type operator|(_enum_type a, _enum_type b) \
590 return (_enum_type)(uint64_t(a) | uint64_t(b)); \
592 inline constexpr _enum_type operator&(_enum_type a, _enum_type b) \
594 return (_enum_type)(uint64_t(a) & uint64_t(b)); \
596 inline constexpr _enum_type operator~(_enum_type a) \
598 return (_enum_type)(~uint64_t(a) & (2 * uint64_t(_max_enum_value) - 1)); \
600 inline _enum_type &operator|=(_enum_type &a, _enum_type b) \
602 return a = (_enum_type)(uint64_t(a) | uint64_t(b)); \
604 inline _enum_type &operator&=(_enum_type &a, _enum_type b) \
606 return a = (_enum_type)(uint64_t(a) & uint64_t(b)); \
608 inline _enum_type &operator^=(_enum_type &a, _enum_type b) \
610 return a = (_enum_type)(uint64_t(a) ^ uint64_t(b)); \
616# define ENUM_OPERATORS(_type, _max)
626#define AT __FILE__ ":" STRINGIFY(__LINE__)
629#define EXPR_NOP(expr) (void)(0 ? ((void)(expr), 1) : 0)
638#define BLI_ENABLE_IF(condition) typename std::enable_if_t<(condition)> * = nullptr
640#if defined(_MSC_VER) && !defined(__clang__)
641# define BLI_NO_UNIQUE_ADDRESS [[msvc::no_unique_address]]
642#elif defined(__has_cpp_attribute)
643# if __has_cpp_attribute(no_unique_address)
644# define BLI_NO_UNIQUE_ADDRESS [[no_unique_address]]
646# define BLI_NO_UNIQUE_ADDRESS
649# define BLI_NO_UNIQUE_ADDRESS [[no_unique_address]]
657namespace blender::blenlib_internal {
660template<
class T>
struct IsBoundedArray : std::false_type {};
661template<
class T, std::
size_t N>
struct IsBoundedArray<T[
N]> : std::true_type {};
671template<
class T,
size_t N>
constexpr size_t ARRAY_SIZE(
const T (&arg)[
N])
noexcept
688constexpr std::enable_if_t<blender::blenlib_internal::IsBoundedArray<T>::value,
size_t>
689BOUNDED_ARRAY_TYPE_SIZE() noexcept
691 return sizeof(std::declval<T>()) /
sizeof(std::declval<T>()[0]);
bool BLI_memory_is_zero(const void *arr, size_t arr_size)