1 /*
2
3 Copyright (C) 1991-2001 and beyond by Bo Lindbergh
4 and the "Aleph One" developers.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 This license is contained in the file "COPYING",
17 which is included with this source code; it is available online at
18 http://www.gnu.org/licenses/gpl.html
19
20 */
21
22 /*
23 Jul 1, 2000 (Loren Petrich):
24 Added accessor template function
25
26 Aug 27, 2000 (Loren Petrich):
27 Added object wrappers for memcpy() and memset(); these copy, set, and clear objects
28 */
29
30 #ifndef _CSERIES_MACROS_
31 #define _CSERIES_MACROS_
32
33 #include <string.h>
34 #include "FilmProfile.h" // TERRIBLE
35
36 #undef MAX
37 #define MAX(a,b) ((a)>=(b) ? (a) : (b))
38 #undef MIN
39 #define MIN(a,b) ((a)<=(b) ? (a) : (b))
40
41 #define FLOOR(value,floor) MAX(value,floor)
42 #define CEILING(value,ceiling) MIN(value,ceiling)
43
44 #define M2_PIN(value,floor,ceiling) \
45 ((value)<(floor) ? (floor) : (value)>(ceiling) ? (ceiling) : (value))
46 #define A1_PIN(value,floor,ceiling) (CEILING(FLOOR((value),(floor)),(ceiling)))
47
48 #define PIN(value,floor,ceiling) \
49 ((film_profile.inexplicable_pin_change) ? (A1_PIN(value,floor,ceiling)) : (M2_PIN(value,floor,ceiling)))
50
51 #define ABS(x) ((x)<0 ? -(x) : (x))
52 #define SGN(x) ((x)<0 ? -1 : (x)>0 ? 1 : 0)
53
54 template <typename T> void
SWAP(T & a,T & b)55 SWAP(T& a, T& b)
56 {
57 T t = a;
58 a = b;
59 b = t;
60 }
61
62 #define FLAG(bit) (1L<<(bit))
63 #define TEST_FLAG32(flags,bit) (((flags)&FLAG(bit))!=0)
64 #define SET_FLAG32(flags,bit,value) ((value) ? ((flags)|=FLAG(bit)) : ((flags)&=~FLAG(bit)))
65
66 #define FLAG16(bit) (1<<(bit))
67 #define TEST_FLAG16(flags,bit) (((flags)&FLAG16(bit))!=0)
68 #define SET_FLAG16(flags,bit,value) ((void)((value) ? ((flags)|=FLAG16(bit)) : ((flags)&=~FLAG16(bit))))
69
70 // LP addition (Mar 2, 2000): some more generic routines for flags
71 #define TEST_FLAG(obj,flag) ((obj)&(flag))
72 #define SET_FLAG(obj,flag,value) ((void)((value)?((obj)|=(flag)):((obj)&=~(flag))))
73
74 #define RECTANGLE_WIDTH(rectptr) ((rectptr)->right-(rectptr)->left)
75 #define RECTANGLE_HEIGHT(rectptr) ((rectptr)->bottom-(rectptr)->top)
76
NextPowerOfTwo(int n)77 static inline int NextPowerOfTwo(int n)
78 {
79 int p = 1;
80 while(p < n) {p <<= 1;}
81 return p;
82 }
83
84 #ifdef __cplusplus
85 /*
86 LP addition: template class for doing bounds checking when accessing an array;
87 it uses an array, an index value, and an intended number of members for that array.
88 It will return a pointer to the array member, if that member is in range, or else
89 the null pointer. Its caller must check whether a null pointer had been returned,
90 and then perform the appropriate action.
91 */
92
GetMemberWithBounds(T * Array,const size_t Index,const size_t Number)93 template<class T> T* GetMemberWithBounds(T* Array, const size_t Index, const size_t Number)
94 {
95 // Bounds checking
96 if (!(Index>=0 && Index<Number)) return NULL;
97
98 // The appropriate pointer
99 return (Array + Index);
100 }
101
102 /*
103 LP addition: convenient type-safe wrappers for memcpy and memset,
104 that get rid of annoying sizeof's. obj_ means a single object
105 and objlist_ means a list of num_objects of them.
106 The _copy set copies "source" to "destination"
107 The _set set sets all the bytes to "value"
108 The _clear set sets all the bytes to zero (a common operation)
109 */
110
obj_copy(T & destination,const T & source)111 template<class T> void obj_copy(T& destination, const T& source)
112 {memcpy(&destination, &source, sizeof(T));}
113
objlist_copy(T * destination,const T * source,size_t num_objects)114 template<class T> void objlist_copy(T* destination, const T* source, size_t num_objects)
115 {if (num_objects > 0) memcpy(destination, source, num_objects*sizeof(T));}
116
obj_set(T & object,int value)117 template<class T> void obj_set(T& object, int value)
118 {memset(&object, value, sizeof(T));}
119
objlist_set(T * object_list,int value,size_t num_objects)120 template<class T> void objlist_set(T* object_list, int value, size_t num_objects)
121 {if (num_objects > 0) memset(object_list, value, num_objects*sizeof(T));}
122
obj_clear(T & object)123 template<class T> void obj_clear(T& object)
124 {obj_set(object, 0);}
125
objlist_clear(T * object_list,size_t num_objects)126 template<class T> void objlist_clear(T* object_list, size_t num_objects)
127 {objlist_set(object_list, 0, num_objects);}
128 #endif
129 #endif
130