1 // -*- Mode: C++; tab-width: 2; -*-
2 // vi: set ts=2:
3 //
4 
5 #ifndef BALL_COMMON_MACROS_H
6 #define BALL_COMMON_MACROS_H
7 
8 #ifndef BALL_CONFIG_CONFIG_H
9 #	include <BALL/CONFIG/config.h>
10 #endif
11 
12 #ifndef BALL_COMMON_CONSTANTS_H
13 #	include <BALL/COMMON/constants.h>
14 #endif
15 
16 #ifndef BALL_COMMON_RTTI_H
17 #	include <BALL/COMMON/rtti.h>
18 #endif
19 
20 #include <math.h>     // needed for fabs
21 #include <typeinfo>		// needed for typeid
22 
23 #define BALL_MAX(a, b)                           (((a) < (b)) ? (b) : (a))
24 #define BALL_MAX3(x, y, z)                       ((x) > (y) ? BALL_MAX(x, z) : BALL_MAX(y, z))
25 #define BALL_MIN(a, b)                           (((a) > (b)) ? (b) : (a))
26 #define BALL_MIN3(x, y, z)                       ((x) < (y) ? BALL_MIN(x, z) : BALL_MIN(y, z))
27 #define BALL_ABS(x)                              (((x) >= 0) ? (x) : -(x))
28 #define BALL_SGN(x)                              (((x) < 0) ? -1 : ((x) == 0) ? 0 : 1)
29 #define BALL_ODD(x)                              (((x) % 2)!=0)
30 
31 #define BALL_INT_ODD(x)                          (((x) & 0x1) == 1)
32 #define BALL_INT_EVEN(x)                         (((x) & 0x1) == 0)
33 
34 #define BALL_REAL_ROUND                          BALL::Maths::round
35 #define BALL_REAL_EQUAL(x, y, e)                 (fabs((x) - (y)) <= e)
36 #define BALL_REAL_NOT_EQUAL(x, y, e)             (fabs((x) - (y)) > e)
37 #define BALL_REAL_LESS(x, y, e)                  (((x) - (y)) < -e)
38 #define BALL_REAL_LESS_OR_EQUAL(x, y, e)         (((x) - (y)) <= e)
39 #define BALL_REAL_GREATER(x, y, e)               (((x) - (y)) > e)
40 #define BALL_REAL_GREATER_OR_EQUAL(x, y, e)      (((x) - (y)) >= -e)
41 #define BALL_REAL_ABS(x)                         fabs(x)
42 #define BALL_REAL_SGN(x)                         BALL_SGN(x)
43 #define BALL_REAL_ODD(x)                         (((x) % 2) != 0)
44 #define BALL_REAL_EVEN(x)                        (((x) % 2) == 0)
45 #define BALL_REAL_FLOOR(x)                       (long)((x) > 0 ? (x): ((x) == (long)(x) ? (x) : (x) - 1))
46 #define BALL_REAL_CEILING(x)                     (long)((x) < 0 ? (x): ((x) == (long)(x) ? (x) : (x) + 1))
47 #define BALL_REAL_ROUND_INT(x)                   ((x) > 0 ? (int)(x + 0.5) : -(int)(0.5 - x))
48 
49 
50 // The following macros assume BALL_CHAR_BITS is one of either 8, 16, or 32
51 #define BALL_CHAR_BITS                           BALL_CHAR_SIZE * 8
52 #define BALL_CHAR_MASK                           BALL_CHAR_BITS - 1
53 #define BALL_CHAR_SHIFT                          ((BALL_CHAR_BITS == 8) ? 3 : (BALL_CHAR_BITS == 16) ? 4 : 5)
54 #define BALL_CHAR_ALL_BITS_SET                   ((BALL_CHAR_BITS == 8) ? 0xFF : (BALL_CHAR_BITS == 16) ? 0xFFFF : 0xFFFFFFFF)
55 #define BALL_CHAR_ALL_BITS_CLEARED               ((BALL_CHAR_BITS == 8) ? 0x00 : (BALL_CHAR_BITS == 16) ? 0x0000 : 0x00000000)
56 #define BALL_NUMBER_OF_BYTES(bits)               (((bits) + BALL_CHAR_MASK) >> BALL_CHAR_SHIFT)
57 
58 #define BALL_SIZEOF_ARRAY(a)                     (sizeof(a) / sizeof(*(a)))
59 
60 #define BALL_BITARRAY_SIZE(number_of_bits)       (((number_of_bits - 1) >> BALL_CHAR_SHIFT) + 1)
61 #define BALL_BITARRAY_CLEAR_BIT(array, x)        ((array)[(x) >> BALL_CHAR_SHIFT] &= ~(1 << ((x) & BALL_CHAR_MASK)))
62 #define BALL_BITARRAY_SET_BIT(array, x)          ((array)[(x) >> BALL_CHAR_SHIFT] |=  (1 << ((x) & BALL_CHAR_MASK)))
63 #define BALL_BITARRAY_TOGGLE_BIT(array, x)       ((array)[(x) >> BALL_CHAR_SHIFT] ^=  (1 << ((x) & BALL_CHAR_MASK)))
64 #define BALL_BITARRAY_IS_BIT_SET(array, x)       (((array)[(x) >> BALL_CHAR_SHIFT] &   (1 << ((x) & BALL_CHAR_MASK))) != 0)
65 
66 #define BALL_BIT(bit)                            (1 << (bit))
67 #define BALL_BIT_SET(bitset, bit)                ((bitset) |= (1 << (bit)))
68 #define BALL_BIT_SET_ALL(bitset)                 ((bitset) = -1)
69 #define BALL_BIT_SET_ALL_TO(bitset, bit)         ((bitset) |= ~(-1 << (bit + 1)))
70 #define BALL_BIT_SET_ALL_FROM(bitset, bit)       ((bitset) |= (-1 << (bit)))
71 #define BALL_BIT_CLEAR(bitset, bit)              ((bitset) &= ~(1 << (bit)))
72 #define BALL_BIT_CLEAR_ALL(bitset)               ((bitset) = 0)
73 #define BALL_BIT_CLEAR_ALL_TO(bitset, bit)       ((bitset) &= (-1 << (bit + 1)))
74 #define BALL_BIT_CLEAR_ALL_FROM(bitset, bit)     ((bitset) &= ~(-1 << (bit)))
75 #define BALL_BIT_IS_SET(bitset, bit)             ((bitset) & (1 << (bit)))
76 #define BALL_BIT_IS_CLEARED(bitset, bit)         !((bitset) & (1 << (bit)))
77 
78 #define BALL_ANGLE_RADIAN_TO_DEGREE(rad_angle)   (180.0 / ::BALL::Constants::PI * (rad_angle))
79 #define BALL_ANGLE_DEGREE_TO_RADIAN(deg_angle)   (::BALL::Constants::PI / 180.0 * (deg_angle))
80 
81 #define BALL_OFFSET_OF(struct_name, struct_var_name)   ((long)&(((struct_name*)0)->struct_var_name))
82 
83 #define BALL_DUMP_DEPTH(os, depth)               for (dump_indent_depth_ = 0; dump_indent_depth_ < depth; ++dump_indent_depth_) { os << "    "; }
84 #define BALL_DUMP_STREAM_PREFIX(os)              Size dump_indent_depth_ = 0;
85 
86 #define BALL_DUMP_HEADER(os,cl,ob)               os << "Object: " << (void *)ob << " is instance of class: " << streamClassName(typeid(*ob)) << std::endl;
87 #define BALL_DUMP_CLASS_HEADER(os,cl,ob)         os << "Object: " << (void *)ob << " is instance of class: " << #cl << ::std::endl;
88 #define BALL_DUMP_STREAM_SUFFIX(os)
89 
90 #endif // BALL_COMMON_MACROS_H
91