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