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