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