Blender V4.3
GHOST_utildefines.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2022-2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
11#pragma once
12
14
15/* -------------------------------------------------------------------- */
19/* hints for branch prediction, only use in code that runs a _lot_ where */
20#ifdef __GNUC__
21# define LIKELY(x) __builtin_expect(!!(x), 1)
22# define UNLIKELY(x) __builtin_expect(!!(x), 0)
23#else
24# define LIKELY(x) (x)
25# define UNLIKELY(x) (x)
26#endif
27
30/* -------------------------------------------------------------------- */
34/* unpack vector for args */
35#define UNPACK2(a) ((a)[0]), ((a)[1])
36#define UNPACK3(a) UNPACK2(a), ((a)[2])
37#define UNPACK4(a) UNPACK3(a), ((a)[3])
38/* pre may be '&', '*' or func, post may be '->member' */
39#define UNPACK2_EX(pre, a, post) (pre((a)[0]) post), (pre((a)[1]) post)
40#define UNPACK3_EX(pre, a, post) UNPACK2_EX(pre, a, post), (pre((a)[2]) post)
41#define UNPACK4_EX(pre, a, post) UNPACK3_EX(pre, a, post), (pre((a)[3]) post)
42
45/* -------------------------------------------------------------------- */
49/* Assuming a static array. */
50#if defined(__GNUC__) && !defined(__cplusplus) && !defined(__clang__) && !defined(__INTEL_COMPILER)
51# define ARRAY_SIZE(arr) \
52 ((sizeof(struct { int isnt_array : ((const void *)&(arr) == &(arr)[0]); }) * 0) + \
53 (sizeof(arr) / sizeof(*(arr))))
54#else
55# define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(*(arr)))
56#endif
57
60/* -------------------------------------------------------------------- */
64/* Manual line breaks for readability. */
65/* clang-format off */
66
67/* ELEM#(v, ...): is the first arg equal any others? */
68/* internal helpers. */
69#define _VA_ELEM2(v, a) ((v) == (a))
70#define _VA_ELEM3(v, a, b) \
71 (_VA_ELEM2(v, a) || _VA_ELEM2(v, b))
72#define _VA_ELEM4(v, a, b, c) \
73 (_VA_ELEM3(v, a, b) || _VA_ELEM2(v, c))
74#define _VA_ELEM5(v, a, b, c, d) \
75 (_VA_ELEM4(v, a, b, c) || _VA_ELEM2(v, d))
76#define _VA_ELEM6(v, a, b, c, d, e) \
77 (_VA_ELEM5(v, a, b, c, d) || _VA_ELEM2(v, e))
78#define _VA_ELEM7(v, a, b, c, d, e, f) \
79 (_VA_ELEM6(v, a, b, c, d, e) || _VA_ELEM2(v, f))
80#define _VA_ELEM8(v, a, b, c, d, e, f, g) \
81 (_VA_ELEM7(v, a, b, c, d, e, f) || _VA_ELEM2(v, g))
82#define _VA_ELEM9(v, a, b, c, d, e, f, g, h) \
83 (_VA_ELEM8(v, a, b, c, d, e, f, g) || _VA_ELEM2(v, h))
84#define _VA_ELEM10(v, a, b, c, d, e, f, g, h, i) \
85 (_VA_ELEM9(v, a, b, c, d, e, f, g, h) || _VA_ELEM2(v, i))
86#define _VA_ELEM11(v, a, b, c, d, e, f, g, h, i, j) \
87 (_VA_ELEM10(v, a, b, c, d, e, f, g, h, i) || _VA_ELEM2(v, j))
88#define _VA_ELEM12(v, a, b, c, d, e, f, g, h, i, j, k) \
89 (_VA_ELEM11(v, a, b, c, d, e, f, g, h, i, j) || _VA_ELEM2(v, k))
90#define _VA_ELEM13(v, a, b, c, d, e, f, g, h, i, j, k, l) \
91 (_VA_ELEM12(v, a, b, c, d, e, f, g, h, i, j, k) || _VA_ELEM2(v, l))
92#define _VA_ELEM14(v, a, b, c, d, e, f, g, h, i, j, k, l, m) \
93 (_VA_ELEM13(v, a, b, c, d, e, f, g, h, i, j, k, l) || _VA_ELEM2(v, m))
94#define _VA_ELEM15(v, a, b, c, d, e, f, g, h, i, j, k, l, m, n) \
95 (_VA_ELEM14(v, a, b, c, d, e, f, g, h, i, j, k, l, m) || _VA_ELEM2(v, n))
96#define _VA_ELEM16(v, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o) \
97 (_VA_ELEM15(v, a, b, c, d, e, f, g, h, i, j, k, l, m, n) || _VA_ELEM2(v, o))
98#define _VA_ELEM17(v, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) \
99 (_VA_ELEM16(v, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o) || _VA_ELEM2(v, p))
100/* clang-format on */
101
102/* reusable ELEM macro */
103#define ELEM(...) VA_NARGS_CALL_OVERLOAD(_VA_ELEM, __VA_ARGS__)
104
107/* -------------------------------------------------------------------- */
111/* Macro to convert a value to string in the preprocessor:
112 * - `STRINGIFY_ARG`: gives the argument as a string
113 * - `STRINGIFY_APPEND`: appends any argument 'b' onto the string argument 'a',
114 * used by `STRINGIFY` because some preprocessors warn about zero arguments.
115 * - `STRINGIFY`: gives the argument's value as a string. */
116
117#define STRINGIFY_ARG(x) "" #x
118#define STRINGIFY_APPEND(a, b) "" a #b
119#define STRINGIFY(x) STRINGIFY_APPEND("", x)
120
121/* generic strcmp macros */
122#if defined(_MSC_VER)
123# define strcasecmp _stricmp
124# define strncasecmp _strnicmp
125#endif
126
127#define STREQ(a, b) (strcmp(a, b) == 0)
128#define STRCASEEQ(a, b) (strcasecmp(a, b) == 0)
129#define STREQLEN(a, b, n) (strncmp(a, b, n) == 0)
130#define STRCASEEQLEN(a, b, n) (strncasecmp(a, b, n) == 0)
131
132#define STRPREFIX(a, b) (strncmp((a), (b), strlen(b)) == 0)
133