X-Git-Url: https://git.dogcows.com/gitweb?a=blobdiff_plain;f=common.h;h=ec7913ea4b13853d1880611bf02e877348f476d8;hb=bc662e293c854e1bdc9d46e9a410fe220247e6d4;hp=6224c80cc13eab493ed6cbae0b8d99b3ea0e572f;hpb=c875478cdd823c7df8fdc859941bd9e5948c9315;p=chaz%2Frasterize diff --git a/common.h b/common.h index 6224c80..ec7913e 100644 --- a/common.h +++ b/common.h @@ -15,6 +15,8 @@ #include #include +#include "config.h" + /* * Define a keyword for use while defining small and fast functions. @@ -37,6 +39,7 @@ typedef double scal_t; #define scal_ceil ceil #define scal_min fmin #define scal_max fmax +#define scal_abs fabs #define scal_pow pow #define scal_sqrt sqrt #define scal_sin sin @@ -53,6 +56,7 @@ typedef float scal_t; #define scal_ceil ceilf #define scal_min fminf #define scal_max fmaxf +#define scal_abs fabsf #define scal_pow powf #define scal_sqrt sqrtf #define scal_sin sinf @@ -66,6 +70,26 @@ typedef float scal_t; #define scal_min2(A,B,C) scal_min(scal_min(A,B),C) #define scal_max2(A,B,C) scal_max(scal_max(A,B),C) +#define SCAL_EPSILON (S(0.000001)) + +/* + * Check if two scalars are mostly equal, given a margin of error. + */ +INLINE_MAYBE +bool scal_isequal2(scal_t a, scal_t b, scal_t e) +{ + return scal_abs(a -b) < e; +} + +/* + * Check if two scalars are mostly equal. + */ +INLINE_MAYBE +bool scal_isequal(scal_t a, scal_t b) +{ + return scal_isequal2(a, b, SCAL_EPSILON); +} + /* * Clamp a scalar between two values. */ @@ -96,12 +120,15 @@ int imax(int a, int b) return a < b ? b : a; } - /* - * Define some macros for packing and unpacking bytes to and from larger ints. + * Define generic MIN and MAX macros. */ -#define PACK(W,N,B) (((B) << (8 * (N))) | ((W) & ~(0xff << (8 * (N))))) -#define UNPACK(W,N) ((uint8_t)((W) >> (8 * (N))) & 0xff) +#ifndef MIN +#define MIN(A,B) (((A) < (B)) ? (A) : (B)) +#endif +#ifndef MAX +#define MAX(A,B) (((A) > (B)) ? (A) : (B)) +#endif /* @@ -110,11 +137,16 @@ int imax(int a, int b) * an error condition and an abort(3) occurs. * return. */ +#if VERBOSITY >= 1 #define TRY_DO(L, K, ARGS...) \ printf("* " L "... ", ##ARGS); \ fflush(stdout); \ if ((K) == 0) printf("done!\n"); \ else abort() +#else +#define TRY_DO(L, K, ARGS...) \ +if ((K) != 0) abort() +#endif /* @@ -217,10 +249,11 @@ void ltrim(char *str); * Trim white space off of both sides of a string. */ INLINE_MAYBE -void trim(char *str) +char* trim(char *str) { rtrim(str); ltrim(str); + return str; }