2011-01-07 18:36:47 +00:00
|
|
|
/*
|
|
|
|
* ***** BEGIN GPL LICENSE BLOCK *****
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* The Original Code is: all of this file.
|
|
|
|
*
|
|
|
|
* Contributor(s): none yet.
|
|
|
|
*
|
|
|
|
* ***** END GPL LICENSE BLOCK *****
|
2011-10-23 17:52:20 +00:00
|
|
|
*/
|
2011-01-07 18:36:47 +00:00
|
|
|
|
2012-02-17 18:59:41 +00:00
|
|
|
#ifndef __BLI_UTILDEFINES_H__
|
|
|
|
#define __BLI_UTILDEFINES_H__
|
2011-01-07 18:36:47 +00:00
|
|
|
|
2011-02-18 13:58:08 +00:00
|
|
|
/** \file BLI_utildefines.h
|
|
|
|
* \ingroup bli
|
|
|
|
*/
|
|
|
|
|
2013-01-06 09:24:45 +00:00
|
|
|
#ifndef NDEBUG /* for BLI_assert */
|
|
|
|
#include <stdio.h>
|
|
|
|
#endif
|
|
|
|
|
2013-01-01 12:47:58 +00:00
|
|
|
/* note: use of (int, TRUE / FALSE) is deprecated,
|
|
|
|
* use (bool, true / false) instead */
|
|
|
|
#ifdef HAVE_STDBOOL_H
|
|
|
|
# include <stdbool.h>
|
|
|
|
#else
|
|
|
|
# ifndef HAVE__BOOL
|
|
|
|
# ifdef __cplusplus
|
2013-01-01 18:18:45 +00:00
|
|
|
typedef bool _BLI_Bool;
|
2013-01-01 12:47:58 +00:00
|
|
|
# else
|
2013-01-31 14:25:07 +00:00
|
|
|
/* using char here may cause nasty tricky bugs, e.g.
|
2013-02-08 15:16:57 +00:00
|
|
|
* bool is_bit_flag = RNA_property_flag(prop) & PROP_ENUM_FLAG;
|
|
|
|
* as PROP_ENUM_FLAG is farther than 8th bit, do_translate would be always false!
|
2013-01-31 14:25:07 +00:00
|
|
|
*/
|
|
|
|
# define _BLI_Bool unsigned int
|
2013-01-01 12:47:58 +00:00
|
|
|
# endif
|
2013-01-01 18:18:45 +00:00
|
|
|
# else
|
|
|
|
# define _BLI_Bool _Bool
|
2013-01-01 12:47:58 +00:00
|
|
|
# endif
|
2013-01-01 18:18:45 +00:00
|
|
|
# define bool _BLI_Bool
|
2013-01-01 12:47:58 +00:00
|
|
|
# define false 0
|
|
|
|
# define true 1
|
|
|
|
# define __bool_true_false_are_defined 1
|
2011-01-07 18:36:47 +00:00
|
|
|
#endif
|
|
|
|
|
2013-01-01 12:47:58 +00:00
|
|
|
/* remove this when we're ready to remove TRUE/FALSE completely */
|
|
|
|
#ifdef WITH_BOOL_COMPAT
|
|
|
|
/* interim until all occurrences of these can be updated to stdbool */
|
2013-01-01 18:18:45 +00:00
|
|
|
/* XXX Why not use the true/false velues here? */
|
2013-01-01 12:47:58 +00:00
|
|
|
# ifndef FALSE
|
|
|
|
# define FALSE 0
|
|
|
|
# endif
|
|
|
|
|
|
|
|
# ifndef TRUE
|
|
|
|
# define TRUE 1
|
|
|
|
# endif
|
2011-01-07 18:36:47 +00:00
|
|
|
#endif
|
|
|
|
|
2012-10-27 11:18:54 +00:00
|
|
|
/* useful for finding bad use of min/max */
|
|
|
|
#if 0
|
|
|
|
/* gcc only */
|
|
|
|
# define _TYPECHECK(a, b) ((void)(((typeof(a) *)0) == ((typeof(b) *)0)))
|
2012-10-27 15:08:40 +00:00
|
|
|
# define MIN2(x, y) (_TYPECHECK(x, y), (((x) < (y) ? (x) : (y))))
|
|
|
|
# define MAX2(x, y) (_TYPECHECK(x, y), (((x) > (y) ? (x) : (y))))
|
2012-10-27 11:18:54 +00:00
|
|
|
#endif
|
2011-01-07 18:36:47 +00:00
|
|
|
|
|
|
|
/* min/max */
|
2012-10-27 15:08:40 +00:00
|
|
|
#define MIN2(x, y) ((x) < (y) ? (x) : (y))
|
2012-10-27 11:18:54 +00:00
|
|
|
#define MIN3(x, y, z) (MIN2(MIN2((x), (y)), (z)))
|
|
|
|
#define MIN4(x, y, z, a) (MIN2(MIN2((x), (y)), MIN2((z), (a))))
|
2011-01-07 18:36:47 +00:00
|
|
|
|
2012-10-27 15:08:40 +00:00
|
|
|
#define MAX2(x, y) ((x) > (y) ? (x) : (y))
|
2012-10-27 11:18:54 +00:00
|
|
|
#define MAX3(x, y, z) (MAX2(MAX2((x), (y)), (z)))
|
|
|
|
#define MAX4(x, y, z, a) (MAX2(MAX2((x), (y)), MAX2((z), (a))))
|
2011-01-07 18:36:47 +00:00
|
|
|
|
2011-10-22 03:39:13 +00:00
|
|
|
#define INIT_MINMAX(min, max) { \
|
2012-04-29 17:11:40 +00:00
|
|
|
(min)[0] = (min)[1] = (min)[2] = 1.0e30f; \
|
|
|
|
(max)[0] = (max)[1] = (max)[2] = -1.0e30f; \
|
2012-08-08 21:20:10 +00:00
|
|
|
} (void)0
|
2011-10-22 03:39:13 +00:00
|
|
|
#define INIT_MINMAX2(min, max) { \
|
2012-04-29 17:11:40 +00:00
|
|
|
(min)[0] = (min)[1] = 1.0e30f; \
|
|
|
|
(max)[0] = (max)[1] = -1.0e30f; \
|
2012-03-22 01:35:13 +00:00
|
|
|
} (void)0
|
2011-10-22 03:39:13 +00:00
|
|
|
#define DO_MIN(vec, min) { \
|
2012-04-29 17:11:40 +00:00
|
|
|
if ((min)[0] > (vec)[0]) (min)[0] = (vec)[0]; \
|
|
|
|
if ((min)[1] > (vec)[1]) (min)[1] = (vec)[1]; \
|
|
|
|
if ((min)[2] > (vec)[2]) (min)[2] = (vec)[2]; \
|
2012-03-22 01:35:13 +00:00
|
|
|
} (void)0
|
2011-10-22 03:39:13 +00:00
|
|
|
#define DO_MAX(vec, max) { \
|
2012-04-29 17:11:40 +00:00
|
|
|
if ((max)[0] < (vec)[0]) (max)[0] = (vec)[0]; \
|
|
|
|
if ((max)[1] < (vec)[1]) (max)[1] = (vec)[1]; \
|
|
|
|
if ((max)[2] < (vec)[2]) (max)[2] = (vec)[2]; \
|
2012-03-22 01:35:13 +00:00
|
|
|
} (void)0
|
2011-10-22 03:39:13 +00:00
|
|
|
#define DO_MINMAX(vec, min, max) { \
|
2012-04-29 17:11:40 +00:00
|
|
|
if ((min)[0] > (vec)[0] ) (min)[0] = (vec)[0]; \
|
|
|
|
if ((min)[1] > (vec)[1] ) (min)[1] = (vec)[1]; \
|
|
|
|
if ((min)[2] > (vec)[2] ) (min)[2] = (vec)[2]; \
|
|
|
|
if ((max)[0] < (vec)[0] ) (max)[0] = (vec)[0]; \
|
|
|
|
if ((max)[1] < (vec)[1] ) (max)[1] = (vec)[1]; \
|
|
|
|
if ((max)[2] < (vec)[2] ) (max)[2] = (vec)[2]; \
|
2012-03-22 01:35:13 +00:00
|
|
|
} (void)0
|
2011-10-22 03:39:13 +00:00
|
|
|
#define DO_MINMAX2(vec, min, max) { \
|
2012-04-29 17:11:40 +00:00
|
|
|
if ((min)[0] > (vec)[0] ) (min)[0] = (vec)[0]; \
|
|
|
|
if ((min)[1] > (vec)[1] ) (min)[1] = (vec)[1]; \
|
|
|
|
if ((max)[0] < (vec)[0] ) (max)[0] = (vec)[0]; \
|
|
|
|
if ((max)[1] < (vec)[1] ) (max)[1] = (vec)[1]; \
|
2012-03-22 01:35:13 +00:00
|
|
|
} (void)0
|
2011-01-07 18:36:47 +00:00
|
|
|
|
|
|
|
/* some math and copy defines */
|
|
|
|
|
2012-08-25 20:16:08 +00:00
|
|
|
/* Causes warning:
|
|
|
|
* incompatible types when assigning to type 'Foo' from type 'Bar'
|
|
|
|
* ... the compiler optimizes away the temp var */
|
|
|
|
#ifdef __GNUC__
|
|
|
|
#define CHECK_TYPE(var, type) { \
|
|
|
|
__typeof(var) *__tmp; \
|
|
|
|
__tmp = (type *)NULL; \
|
|
|
|
(void)__tmp; \
|
|
|
|
} (void)0
|
2012-10-22 03:25:53 +00:00
|
|
|
|
|
|
|
#define CHECK_TYPE_PAIR(var_a, var_b) { \
|
|
|
|
__typeof(var_a) *__tmp; \
|
|
|
|
__tmp = (__typeof(var_b) *)NULL; \
|
|
|
|
(void)__tmp; \
|
|
|
|
} (void)0
|
2012-08-25 20:16:08 +00:00
|
|
|
#else
|
2012-10-22 03:25:53 +00:00
|
|
|
# define CHECK_TYPE(var, type)
|
|
|
|
# define CHECK_TYPE_PAIR(var_a, var_b)
|
2012-08-25 20:16:08 +00:00
|
|
|
#endif
|
|
|
|
|
2012-10-07 07:27:09 +00:00
|
|
|
/* can be used in simple macros */
|
|
|
|
#define CHECK_TYPE_INLINE(val, type) \
|
2012-10-07 23:58:57 +00:00
|
|
|
((void)(((type *)0) != (val)))
|
2012-10-07 07:27:09 +00:00
|
|
|
|
2012-10-27 11:18:54 +00:00
|
|
|
#define SWAP(type, a, b) { \
|
2012-08-25 20:16:08 +00:00
|
|
|
type sw_ap; \
|
|
|
|
CHECK_TYPE(a, type); \
|
|
|
|
CHECK_TYPE(b, type); \
|
|
|
|
sw_ap = (a); \
|
|
|
|
(a) = (b); \
|
|
|
|
(b) = sw_ap; \
|
|
|
|
} (void)0
|
2011-01-07 18:36:47 +00:00
|
|
|
|
2012-10-22 03:25:53 +00:00
|
|
|
/* swap with a temp value */
|
|
|
|
#define SWAP_TVAL(tval, a, b) { \
|
|
|
|
CHECK_TYPE_PAIR(tval, a); \
|
|
|
|
CHECK_TYPE_PAIR(tval, b); \
|
|
|
|
(tval) = (a); \
|
|
|
|
(a) = (b); \
|
|
|
|
(b) = (tval); \
|
|
|
|
} (void)0
|
|
|
|
|
2013-03-05 03:17:46 +00:00
|
|
|
/* ELEM#(a, ...): is the first arg equal any of the others */
|
2012-10-22 03:25:53 +00:00
|
|
|
#define ELEM(a, b, c) ((a) == (b) || (a) == (c))
|
|
|
|
#define ELEM3(a, b, c, d) (ELEM(a, b, c) || (a) == (d) )
|
|
|
|
#define ELEM4(a, b, c, d, e) (ELEM(a, b, c) || ELEM(a, d, e) )
|
|
|
|
#define ELEM5(a, b, c, d, e, f) (ELEM(a, b, c) || ELEM3(a, d, e, f) )
|
|
|
|
#define ELEM6(a, b, c, d, e, f, g) (ELEM(a, b, c) || ELEM4(a, d, e, f, g) )
|
|
|
|
#define ELEM7(a, b, c, d, e, f, g, h) (ELEM3(a, b, c, d) || ELEM4(a, e, f, g, h) )
|
|
|
|
#define ELEM8(a, b, c, d, e, f, g, h, i) (ELEM4(a, b, c, d, e) || ELEM4(a, f, g, h, i) )
|
|
|
|
#define ELEM9(a, b, c, d, e, f, g, h, i, j) (ELEM4(a, b, c, d, e) || ELEM5(a, f, g, h, i, j) )
|
|
|
|
#define ELEM10(a, b, c, d, e, f, g, h, i, j, k) (ELEM4(a, b, c, d, e) || ELEM6(a, f, g, h, i, j, k) )
|
|
|
|
#define ELEM11(a, b, c, d, e, f, g, h, i, j, k, l) (ELEM4(a, b, c, d, e) || ELEM7(a, f, g, h, i, j, k, l) )
|
|
|
|
|
|
|
|
/* shift around elements */
|
|
|
|
#define SHIFT3(type, a, b, c) { \
|
|
|
|
type tmp; \
|
|
|
|
CHECK_TYPE(a, type); \
|
|
|
|
CHECK_TYPE(b, type); \
|
|
|
|
CHECK_TYPE(c, type); \
|
|
|
|
tmp = a; \
|
|
|
|
a = c; \
|
|
|
|
c = b; \
|
|
|
|
b = tmp; \
|
|
|
|
} (void)0
|
|
|
|
|
|
|
|
#define SHIFT4(type, a, b, c, d) { \
|
|
|
|
type tmp; \
|
|
|
|
CHECK_TYPE(a, type); \
|
|
|
|
CHECK_TYPE(b, type); \
|
|
|
|
CHECK_TYPE(c, type); \
|
|
|
|
CHECK_TYPE(d, type); \
|
|
|
|
tmp = a; \
|
|
|
|
a = d; \
|
|
|
|
d = c; \
|
|
|
|
c = b; \
|
|
|
|
b = tmp; \
|
|
|
|
} (void)0
|
|
|
|
|
|
|
|
|
2012-05-12 20:39:39 +00:00
|
|
|
#define ABS(a) ( (a) < 0 ? (-(a)) : (a) )
|
2011-01-07 18:36:47 +00:00
|
|
|
|
2012-05-12 20:39:39 +00:00
|
|
|
#define FTOCHAR(val) ((val) <= 0.0f) ? 0 : (((val) > (1.0f - 0.5f / 255.0f)) ? 255 : (char)((255.0f * (val)) + 0.5f))
|
|
|
|
#define FTOUSHORT(val) ((val >= 1.0f - 0.5f / 65535) ? 65535 : (val <= 0.0f) ? 0 : (unsigned short)(val * 65535.0f + 0.5f))
|
|
|
|
#define USHORTTOUCHAR(val) ((unsigned char)(((val) >= 65535 - 128) ? 255 : ((val) + 128) >> 8))
|
2011-10-22 03:39:13 +00:00
|
|
|
#define F3TOCHAR3(v2, v1) { \
|
2012-05-12 20:39:39 +00:00
|
|
|
(v1)[0] = FTOCHAR((v2[0])); \
|
|
|
|
(v1)[1] = FTOCHAR((v2[1])); \
|
|
|
|
(v1)[2] = FTOCHAR((v2[2])); \
|
|
|
|
} (void)0
|
2011-10-22 03:39:13 +00:00
|
|
|
#define F3TOCHAR4(v2, v1) { \
|
2012-05-12 20:39:39 +00:00
|
|
|
(v1)[0] = FTOCHAR((v2[0])); \
|
|
|
|
(v1)[1] = FTOCHAR((v2[1])); \
|
|
|
|
(v1)[2] = FTOCHAR((v2[2])); \
|
|
|
|
(v1)[3] = 255; \
|
|
|
|
} (void)0
|
2011-10-22 03:39:13 +00:00
|
|
|
#define F4TOCHAR4(v2, v1) { \
|
2012-05-12 20:39:39 +00:00
|
|
|
(v1)[0] = FTOCHAR((v2[0])); \
|
|
|
|
(v1)[1] = FTOCHAR((v2[1])); \
|
|
|
|
(v1)[2] = FTOCHAR((v2[2])); \
|
|
|
|
(v1)[3] = FTOCHAR((v2[3])); \
|
|
|
|
} (void)0
|
2011-10-22 03:39:13 +00:00
|
|
|
#define VECCOPY(v1, v2) { \
|
2012-05-12 20:39:39 +00:00
|
|
|
*(v1) = *(v2); \
|
|
|
|
*(v1 + 1) = *(v2 + 1); \
|
|
|
|
*(v1 + 2) = *(v2 + 2); \
|
|
|
|
} (void)0
|
2011-10-22 03:39:13 +00:00
|
|
|
#define VECCOPY2D(v1, v2) { \
|
2012-05-12 20:39:39 +00:00
|
|
|
*(v1) = *(v2); \
|
|
|
|
*(v1 + 1) = *(v2 + 1); \
|
|
|
|
} (void)0
|
2012-05-28 18:12:08 +00:00
|
|
|
#define VECADD(v1, v2, v3) { \
|
|
|
|
*(v1) = *(v2) + *(v3); \
|
|
|
|
*(v1 + 1) = *(v2 + 1) + *(v3 + 1); \
|
|
|
|
*(v1 + 2) = *(v2 + 2) + *(v3 + 2); \
|
2012-05-12 20:39:39 +00:00
|
|
|
} (void)0
|
2012-05-28 18:12:08 +00:00
|
|
|
#define VECSUB(v1, v2, v3) { \
|
|
|
|
*(v1) = *(v2) - *(v3); \
|
|
|
|
*(v1 + 1) = *(v2 + 1) - *(v3 + 1); \
|
|
|
|
*(v1 + 2) = *(v2 + 2) - *(v3 + 2); \
|
2012-05-12 20:39:39 +00:00
|
|
|
} (void)0
|
2012-05-28 18:12:08 +00:00
|
|
|
#define VECSUB2D(v1, v2, v3) { \
|
|
|
|
*(v1) = *(v2) - *(v3); \
|
|
|
|
*(v1 + 1) = *(v2 + 1) - *(v3 + 1); \
|
2012-05-12 20:39:39 +00:00
|
|
|
} (void)0
|
2012-05-28 18:12:08 +00:00
|
|
|
#define VECADDFAC(v1, v2, v3, fac) { \
|
|
|
|
*(v1) = *(v2) + *(v3) * (fac); \
|
|
|
|
*(v1 + 1) = *(v2 + 1) + *(v3 + 1) * (fac); \
|
|
|
|
*(v1 + 2) = *(v2 + 2) + *(v3 + 2) * (fac); \
|
2012-05-12 20:39:39 +00:00
|
|
|
} (void)0
|
2012-10-10 13:18:07 +00:00
|
|
|
#define VECMADD(v1, v2, v3, v4) { \
|
|
|
|
*(v1) = *(v2) + *(v3) * (*(v4)); \
|
|
|
|
*(v1 + 1) = *(v2 + 1) + *(v3 + 1) * (*(v4 + 1)); \
|
|
|
|
*(v1 + 2) = *(v2 + 2) + *(v3 + 2) * (*(v4 + 2)); \
|
|
|
|
} (void)0
|
2012-05-28 18:12:08 +00:00
|
|
|
#define VECSUBFAC(v1, v2, v3, fac) { \
|
|
|
|
*(v1) = *(v2) - *(v3) * (fac); \
|
|
|
|
*(v1 + 1) = *(v2 + 1) - *(v3 + 1) * (fac); \
|
|
|
|
*(v1 + 2) = *(v2 + 2) - *(v3 + 2) * (fac); \
|
2012-05-12 20:39:39 +00:00
|
|
|
} (void)0
|
|
|
|
|
|
|
|
#define INPR(v1, v2) ( (v1)[0] * (v2)[0] + (v1)[1] * (v2)[1] + (v1)[2] * (v2)[2])
|
2011-01-07 18:36:47 +00:00
|
|
|
|
|
|
|
/* some misc stuff.... */
|
2012-09-08 06:40:03 +00:00
|
|
|
#define CLAMP(a, b, c) { \
|
|
|
|
if ((a) < (b)) (a) = (b); \
|
|
|
|
else if ((a) > (c)) (a) = (c); \
|
|
|
|
} (void)0
|
2012-05-12 20:39:39 +00:00
|
|
|
|
|
|
|
#define CLAMPIS(a, b, c) ((a) < (b) ? (b) : (a) > (c) ? (c) : (a))
|
2012-03-22 01:35:13 +00:00
|
|
|
#define CLAMPTEST(a, b, c) \
|
|
|
|
if ((b) < (c)) { \
|
|
|
|
CLAMP(a, b, c); \
|
|
|
|
} \
|
|
|
|
else { \
|
|
|
|
CLAMP(a, c, b); \
|
2012-08-08 21:20:10 +00:00
|
|
|
} (void)0
|
2011-01-07 18:36:47 +00:00
|
|
|
|
2012-05-12 20:39:39 +00:00
|
|
|
#define IS_EQ(a, b) ((fabs((double)(a) - (b)) >= (double) FLT_EPSILON) ? 0 : 1)
|
|
|
|
#define IS_EQF(a, b) ((fabsf((float)(a) - (b)) >= (float) FLT_EPSILON) ? 0 : 1)
|
2011-01-07 18:36:47 +00:00
|
|
|
|
2012-05-12 20:39:39 +00:00
|
|
|
#define IS_EQT(a, b, c) ((a > b) ? (((a - b) <= c) ? 1 : 0) : ((((b - a) <= c) ? 1 : 0)))
|
|
|
|
#define IN_RANGE(a, b, c) ((b < c) ? ((b < a && a < c) ? 1 : 0) : ((c < a && a < b) ? 1 : 0))
|
|
|
|
#define IN_RANGE_INCL(a, b, c) ((b < c) ? ((b <= a && a <= c) ? 1 : 0) : ((c <= a && a <= b) ? 1 : 0))
|
2011-01-07 18:36:47 +00:00
|
|
|
|
2012-11-15 22:20:18 +00:00
|
|
|
/* unpack vector for args */
|
|
|
|
#define UNPACK2(a) ((a)[0]), ((a)[1])
|
|
|
|
#define UNPACK3(a) ((a)[0]), ((a)[1]), ((a)[2])
|
|
|
|
#define UNPACK4(a) ((a)[0]), ((a)[1]), ((a)[2]), ((a)[3])
|
|
|
|
/* op may be '&' or '*' */
|
2013-03-07 16:12:36 +00:00
|
|
|
#define UNPACK2OP(op, a) op((a)[0]), op((a)[1])
|
|
|
|
#define UNPACK3OP(op, a) op((a)[0]), op((a)[1]), op((a)[2])
|
|
|
|
#define UNPACK4OP(op, a) op((a)[0]), op((a)[1]), op((a)[2]), op((a)[3])
|
2012-11-15 22:20:18 +00:00
|
|
|
|
2011-03-17 10:02:37 +00:00
|
|
|
/* array helpers */
|
2011-10-22 03:39:13 +00:00
|
|
|
#define ARRAY_LAST_ITEM(arr_start, arr_dtype, elem_size, tot) \
|
2012-05-12 20:39:39 +00:00
|
|
|
(arr_dtype *)((char *)arr_start + (elem_size * (tot - 1)))
|
2011-10-22 03:39:13 +00:00
|
|
|
|
|
|
|
#define ARRAY_HAS_ITEM(item, arr_start, arr_dtype, elem_size, tot) ( \
|
|
|
|
(item >= arr_start) && \
|
|
|
|
(item <= ARRAY_LAST_ITEM(arr_start, arr_dtype, elem_size, tot)) \
|
|
|
|
)
|
2011-01-07 18:36:47 +00:00
|
|
|
|
|
|
|
/* Warning-free macros for storing ints in pointers. Use these _only_
|
|
|
|
* for storing an int in a pointer, not a pointer in an int (64bit)! */
|
2012-03-22 01:35:13 +00:00
|
|
|
#define SET_INT_IN_POINTER(i) ((void *)(intptr_t)(i))
|
|
|
|
#define GET_INT_FROM_POINTER(i) ((int)(intptr_t)(i))
|
2011-01-07 18:36:47 +00:00
|
|
|
|
2012-07-14 00:33:58 +00:00
|
|
|
#define SET_UINT_IN_POINTER(i) ((void *)(uintptr_t)(i))
|
|
|
|
#define GET_UINT_FROM_POINTER(i) ((unsigned int)(uintptr_t)(i))
|
|
|
|
|
|
|
|
|
2011-01-07 18:36:47 +00:00
|
|
|
/* Macro to convert a value to string in the preprocessor
|
2012-02-17 19:21:47 +00:00
|
|
|
* STRINGIFY_ARG: gives the argument as a string
|
|
|
|
* STRINGIFY_APPEND: appends any argument 'b' onto the string argument 'a',
|
|
|
|
* used by STRINGIFY because some preprocessors warn about zero arguments
|
|
|
|
* STRINGIFY: gives the argument's value as a string */
|
|
|
|
#define STRINGIFY_ARG(x) "" #x
|
|
|
|
#define STRINGIFY_APPEND(a, b) "" a #b
|
|
|
|
#define STRINGIFY(x) STRINGIFY_APPEND("", x)
|
2011-01-07 18:36:47 +00:00
|
|
|
|
2013-03-10 06:18:03 +00:00
|
|
|
/* generic strcmp macros */
|
|
|
|
#define STREQ(a, b) (strcmp(a, b) == 0)
|
|
|
|
#define STRNEQ(a, b) (!STREQ(a, b))
|
|
|
|
|
|
|
|
#define STRCASEEQ(a, b) (strcasecmp(a, b) == 0)
|
|
|
|
#define STRCASENEQ(a, b) (!STRCASEEQ(a, b))
|
|
|
|
|
|
|
|
#define STREQLEN(a, b, n) (strncmp(a, b, n) == 0)
|
|
|
|
#define STRNEQLEN(a, b, n) (!STREQLEN(a, b, n))
|
|
|
|
|
|
|
|
#define STRCASEEQLEN(a, b, n) (strncasecmp(a, b, n) == 0)
|
|
|
|
#define STRCASENEQLEN(a, b, n) (!STRCASEEQLEN(a, b, n))
|
|
|
|
|
|
|
|
#define STRPREFIX(a, b) (strncmp((a), (b), strlen(b)) == 0)
|
|
|
|
|
|
|
|
|
2011-01-07 18:36:47 +00:00
|
|
|
/* useful for debugging */
|
|
|
|
#define AT __FILE__ ":" STRINGIFY(__LINE__)
|
|
|
|
|
2011-09-09 01:29:53 +00:00
|
|
|
/* so we can use __func__ everywhere */
|
|
|
|
#if defined(_MSC_VER)
|
|
|
|
# define __func__ __FUNCTION__
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2011-01-07 18:36:47 +00:00
|
|
|
/* UNUSED macro, for function argument */
|
|
|
|
#ifdef __GNUC__
|
|
|
|
# define UNUSED(x) UNUSED_ ## x __attribute__((__unused__))
|
|
|
|
#else
|
|
|
|
# define UNUSED(x) UNUSED_ ## x
|
|
|
|
#endif
|
|
|
|
|
2011-08-15 16:18:04 +00:00
|
|
|
#ifdef __GNUC__
|
|
|
|
# define UNUSED_FUNCTION(x) __attribute__((__unused__)) UNUSED_ ## x
|
|
|
|
#else
|
|
|
|
# define UNUSED_FUNCTION(x) UNUSED_ ## x
|
|
|
|
#endif
|
|
|
|
|
2011-02-12 10:37:37 +00:00
|
|
|
#ifdef __GNUC__
|
|
|
|
# define WARN_UNUSED __attribute__((warn_unused_result))
|
|
|
|
#else
|
|
|
|
# define WARN_UNUSED
|
|
|
|
#endif
|
2011-01-07 18:36:47 +00:00
|
|
|
|
|
|
|
/*little macro so inline keyword works*/
|
|
|
|
#if defined(_MSC_VER)
|
2012-03-20 08:42:26 +00:00
|
|
|
# define BLI_INLINE static __forceinline
|
2011-01-07 18:36:47 +00:00
|
|
|
#else
|
2012-11-26 20:37:04 +00:00
|
|
|
# if (defined(__APPLE__) && defined(__ppc__))
|
|
|
|
/* static inline __attribute__ here breaks osx ppc gcc42 build */
|
|
|
|
# define BLI_INLINE static __attribute__((always_inline))
|
|
|
|
# else
|
|
|
|
# define BLI_INLINE static inline __attribute__((always_inline))
|
|
|
|
# endif
|
2011-01-07 18:36:47 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2011-01-09 15:12:08 +00:00
|
|
|
/* BLI_assert(), default only to print
|
2011-01-07 18:36:47 +00:00
|
|
|
* for aborting need to define WITH_ASSERT_ABORT
|
|
|
|
*/
|
2012-03-09 18:28:30 +00:00
|
|
|
#ifndef NDEBUG
|
2011-01-07 18:36:47 +00:00
|
|
|
# ifdef WITH_ASSERT_ABORT
|
2012-02-26 17:24:04 +00:00
|
|
|
# define _BLI_DUMMY_ABORT abort
|
2011-01-07 18:36:47 +00:00
|
|
|
# else
|
2012-02-26 17:24:04 +00:00
|
|
|
# define _BLI_DUMMY_ABORT() (void)0
|
2011-01-07 18:36:47 +00:00
|
|
|
# endif
|
2011-10-22 03:39:13 +00:00
|
|
|
# if defined(__GNUC__) || defined(_MSC_VER) /* check __func__ is available */
|
|
|
|
# define BLI_assert(a) \
|
2011-12-19 03:06:44 +00:00
|
|
|
(void)((!(a)) ? ( \
|
|
|
|
( \
|
2011-10-22 03:39:13 +00:00
|
|
|
fprintf(stderr, \
|
2012-03-28 05:09:50 +00:00
|
|
|
"BLI_assert failed: %s:%d, %s(), at \'%s\'\n", \
|
|
|
|
__FILE__, __LINE__, __func__, STRINGIFY(a)), \
|
2012-02-26 17:24:04 +00:00
|
|
|
_BLI_DUMMY_ABORT(), \
|
2011-12-19 03:06:44 +00:00
|
|
|
NULL)) : NULL)
|
2011-01-07 18:36:47 +00:00
|
|
|
# else
|
2011-12-19 03:06:44 +00:00
|
|
|
# define BLI_assert(a) \
|
|
|
|
(void)((!(a)) ? ( \
|
|
|
|
( \
|
2011-10-22 03:39:13 +00:00
|
|
|
fprintf(stderr, \
|
2012-03-28 05:09:50 +00:00
|
|
|
"BLI_assert failed: %s:%d, at \'%s\'\n", \
|
2011-12-19 03:06:44 +00:00
|
|
|
__FILE__, __LINE__, STRINGIFY(a)), \
|
2012-02-26 17:24:04 +00:00
|
|
|
_BLI_DUMMY_ABORT(), \
|
2011-12-19 03:06:44 +00:00
|
|
|
NULL)) : NULL)
|
2011-01-07 18:36:47 +00:00
|
|
|
# endif
|
|
|
|
#else
|
2011-01-09 15:12:08 +00:00
|
|
|
# define BLI_assert(a) (void)0
|
2011-01-07 18:36:47 +00:00
|
|
|
#endif
|
|
|
|
|
2013-03-03 01:24:09 +00:00
|
|
|
/* C++ can't use _Static_assert, expects static_assert() but c++0x only */
|
|
|
|
#if (!defined(__cplusplus)) && (defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 406)) /* gcc4.6+ only */
|
2012-10-25 04:44:46 +00:00
|
|
|
# define BLI_STATIC_ASSERT(a, msg) _Static_assert(a, msg);
|
|
|
|
#else
|
|
|
|
/* TODO msvc, clang */
|
2012-10-25 07:53:11 +00:00
|
|
|
# define BLI_STATIC_ASSERT(a, msg)
|
2012-10-25 04:44:46 +00:00
|
|
|
#endif
|
|
|
|
|
2011-12-29 01:46:58 +00:00
|
|
|
/* hints for branch pradiction, only use in code that runs a _lot_ where */
|
|
|
|
#ifdef __GNUC__
|
|
|
|
# define LIKELY(x) __builtin_expect(!!(x), 1)
|
|
|
|
# define UNLIKELY(x) __builtin_expect(!!(x), 0)
|
|
|
|
#else
|
|
|
|
# define LIKELY(x) (x)
|
|
|
|
# define UNLIKELY(x) (x)
|
|
|
|
#endif
|
|
|
|
|
2012-10-09 13:36:42 +00:00
|
|
|
#endif /* __BLI_UTILDEFINES_H__ */
|