]> Dogcows Code - chaz/rasterize/blob - common.hh
add opengl support
[chaz/rasterize] / common.hh
1
2 /*
3 * CS5600 University of Utah
4 * Charles McGarvey
5 * mcgarvey@eng.utah.edu
6 */
7
8 #ifndef _COMMON_HH_
9 #define _COMMON_HH_
10
11 #include <cmath>
12 #include <cstdio>
13 #include <cstdlib>
14 #include <cstring>
15 #include <stdint.h>
16
17 #include "config.hh"
18
19
20 /*
21 * Define a keyword for use while defining small and fast functions.
22 */
23 #if EXTRA_INLINE
24 #define INLINE_MAYBE static inline
25 #else
26 #define INLINE_MAYBE static
27 #endif
28
29
30 /*
31 * Define a type for scalar values, either float or double.
32 */
33 #if DOUBLE_FLOAT
34 typedef double scal_t;
35 #define SCALAR_SIZE 8
36 #define S(K) K
37 #define scal_floor floor
38 #define scal_ceil ceil
39 #define scal_min fmin
40 #define scal_max fmax
41 #define scal_abs fabs
42 #define scal_pow pow
43 #define scal_sqrt sqrt
44 #define scal_sin sin
45 #define scal_cos cos
46 #define scal_tan tan
47 #define scal_asin asin
48 #define scal_acos acos
49 #define scal_atan atan
50 #else
51 typedef float scal_t;
52 #define SCALAR_SIZE 4
53 #define S(K) K##f
54 #define scal_floor floorf
55 #define scal_ceil ceilf
56 #define scal_min fminf
57 #define scal_max fmaxf
58 #define scal_abs fabsf
59 #define scal_pow powf
60 #define scal_sqrt sqrtf
61 #define scal_sin sinf
62 #define scal_cos cosf
63 #define scal_tan tanf
64 #define scal_asin asinf
65 #define scal_acos acosf
66 #define scal_atan atanf
67 #endif
68
69 #define scal_min2(A,B,C) scal_min(scal_min(A,B),C)
70 #define scal_max2(A,B,C) scal_max(scal_max(A,B),C)
71
72 #define SCAL_EPSILON (S(0.000001))
73
74 /*
75 * Check if two scalars are mostly equal, given a margin of error.
76 */
77 INLINE_MAYBE
78 bool scal_isequal2(scal_t a, scal_t b, scal_t e)
79 {
80 return scal_abs(a -b) < e;
81 }
82
83 /*
84 * Check if two scalars are mostly equal.
85 */
86 INLINE_MAYBE
87 bool scal_isequal(scal_t a, scal_t b)
88 {
89 return scal_isequal2(a, b, SCAL_EPSILON);
90 }
91
92 /*
93 * Clamp a scalar between two values.
94 */
95 INLINE_MAYBE
96 scal_t scal_clamp(scal_t s, scal_t min, scal_t max)
97 {
98 if (s < min) {
99 return min;
100 }
101 if (max < s) {
102 return max;
103 }
104 return s;
105 }
106
107
108 /*
109 * Define min and max functions for integers.
110 */
111 INLINE_MAYBE
112 int imin(int a, int b)
113 {
114 return a < b ? a : b;
115 }
116 INLINE_MAYBE
117 int imax(int a, int b)
118 {
119 return a < b ? b : a;
120 }
121
122 /*
123 * Define generic MIN and MAX macros.
124 */
125 #ifndef MIN
126 #define MIN(A,B) (((A) < (B)) ? (A) : (B))
127 #endif
128 #ifndef MAX
129 #define MAX(A,B) (((A) > (B)) ? (A) : (B))
130 #endif
131
132
133 /*
134 * Try to execute a statement and print a status message. If the statement
135 * resolves to 0, it is considered to have succeeded; any other evaluation is
136 * an error condition and an abort(3) occurs.
137 * return.
138 */
139 #if VERBOSITY >= 1
140 #define TRY_DO(L, K, ARGS...) \
141 printf("* " L "... ", ##ARGS); \
142 fflush(stdout); \
143 if ((K) == 0) printf("done!\n"); \
144 else abort()
145 #else
146 #define TRY_DO(L, K, ARGS...) \
147 if ((K) != 0) abort()
148 #endif
149
150
151 /*
152 * Declare a type of destructor functions.
153 */
154 typedef void (*dtor_t)(void*);
155 #define DTOR(A) (dtor_t)(A)
156
157
158 /*
159 * Allocate a block of memory of a certain size. This follows the semantics
160 * of malloc(3), except it will never return NULL and will abort(3) if the
161 * memory could not be allocated.
162 */
163 void* mem_alloc(size_t size);
164
165 /*
166 * Change the size of a block of memory. This follows the semantics of
167 * realloc(3), except it will never return NULL and will abort(3) if the
168 * memory could not be allocated.
169 */
170 void* mem_realloc(void* mem, size_t size);
171
172 /*
173 * Deallocate a block of memory previously allocated by mem_alloc or malloc(3)
174 * and friends. This is essentially just a call to free(3).
175 */
176 void mem_free(void* mem);
177
178 /*
179 * Set a function to call if either mem_alloc or mem_realloc fails, or NULL if
180 * no callback should be called. The callback takes the same arguments as
181 * realloc(3) and may try to fulfill the request. The return value of the
182 * callback function will be returned from the allocation function and must be
183 * a valid pointer to an allocated block of memory. The callback function
184 * should not call mem_alloc or mem_realloc and must not return if a block of
185 * memory could not be allocated.
186 */
187 void mem_set_fn(void* (*fn)(void*, size_t));
188
189 /*
190 * Get the number of blocks currently allocated with either mem_alloc or
191 * mem_realloc. This number should be zero at the end of a process running
192 * this program.
193 */
194 int mem_blocks();
195
196
197 /*
198 * Duplicate a string; like the non-standard strdup(3) but uses mem_alloc.
199 * The result needs to be freed with mem_free.
200 */
201 INLINE_MAYBE
202 char* mem_strdup(const char* str)
203 {
204 size_t size = strlen(str) + 1;
205 return (char*)memcpy(mem_alloc(size), str, size);
206 }
207
208 /*
209 * Get an allocated string made up of two strings concatenated together.
210 * The result needs to be freed with mem_free.
211 */
212 INLINE_MAYBE
213 char* mem_strcat(const char* str1, const char* str2)
214 {
215 size_t size1 = strlen(str1);
216 size_t size2 = strlen(str2);
217 char* str = (char*)mem_alloc(size1 + size2 + 1);
218 memcpy(str + size1, str2, size2 + 1);
219 return (char*)memcpy(str, str1, size1);
220 }
221
222
223 /*
224 * Cut a string short at the last matching character. The string will be
225 * modified if the character was matched. Either way, str is returned.
226 */
227 INLINE_MAYBE
228 char* strcut(char* str, int c)
229 {
230 char* ptr = strrchr(str, c);
231 if (ptr != NULL) {
232 *ptr = '\0';
233 }
234 return str;
235 }
236
237 /*
238 * Trim white space off of the right side of a string.
239 */
240 void rtrim(char *str);
241
242 /*
243 * Trim white space off of the left side of a string.
244 */
245 void ltrim(char *str);
246
247 /*
248 * Trim white space off of both sides of a string.
249 */
250 INLINE_MAYBE
251 char* trim(char *str)
252 {
253 rtrim(str);
254 ltrim(str);
255 return str;
256 }
257
258
259 /*
260 * Initialize the timer.
261 */
262 void timer_start();
263
264 /*
265 * Stop the timer and return how many microseconds passed after
266 * initialization.
267 */
268 long timer_stop();
269
270
271 #endif // _COMMON_HH_
272
This page took 0.051217 seconds and 4 git commands to generate.