/* // Some miscellaneous C MACROS // Apr 05, 1998 // http://www.rdrs.net/document/ // // Note: // for some #define's it may be nessecary to // compile with flags; -lm, and include; math.h . */ /* compare last z bytes of string x, y */ /* Note: check string length before comparing */ #define strlncmp(x, y, z) (strcmp(x + strlen(x) - z, y + strlen(y) - z)) /* return digit width */ #define DIGLEN(x) (x ? (int)(log10((double)(abs(x)))) + 1 : 1) /* convert R[ed],G[reen],B[lue] values */ #define RGBINT(r,g,b) (b + 256 * g + 256 * 256 * r) /* or.. */ #define RGBINT(r,g,b) ((r << 16) | (g << 8) | b) /* return absolute value of the difference between its arguments */ #define DIFF(a, b) ((a) > (b) ? (a) - (b) : (b) - (a)) /* round a floating point value */ #define ROUND(x) (((x) < 0) ? (int)((x) - 0.5) : (int)((x) + 0.5)) /* octal conversion */ #define octtobin(c) ((c) - '0') /* hexadecimal conversion */ #define hextobin(c) ((c) >= 'A' && (c) <= 'F' ? c - 'A' + 10 : (c) >= 'a' && (c) <= 'f' ? c - 'a' + 10 : c - '0') /* clear the screen */ #define CLRSCRN "\e[0m\e[2J\e[1;1H" /* printf("%s", CLRSCRN); */ /* returns true if x is an even digit */ #define EVEN(x) (((x) & 1) == 0) /* test if bit number Y in X is set (rightmost bit is bit 0) */ #define BITSET(X, Y) ((X >> Y) & 0x1) /* set bit number Y in X to "1" (rightmost bit is bit 0) */ #define SETBIT(X, Y) X = X | (0x1 << Y) /* reset bit number Y in X to "0" (rightmost bit is bit 0) */ #define RESETBIT(X, Y) X = X & (~0 ^ (0x1 << Y)) /* macro to determine how many elements are within array */ /* note: does not matter what type of array ... */ #define ARRAY_SIZE(array) (sizeof(array) / sizeof(array[0])) /* define PI */ #define PI (4.0 * atan(1.0)) /* take square */ #define SQR(x) ((x == 0.0) ? 0.0 : x * x) /* #define SQUR(X) (X)*(X) */ /* take cube */ #define CUBE(A) (A)*(A)*(A) /* swap `binary` two values */ #define swap(a, b) a ^= b ^= a ^= b; /* test if value `x' is an exact power of two */ #define power_two(x) ((x &(x - 1)) == 0) /* define ctrl */ #define CTRL(c) ((c) & 037) /* compute the remainder */ #define MOD(x, y) ((x) - ((x) / (y)) * (y)) /* conversion */ #define CENTIMETER_INCH 0.3937 // multiply centimeters by 0.3937 #define CENTIMETER_FEET 0.0328 // multiply centimeters by 0.0328 #define METERS_INCH 39.37 // multiply meters by 39.37 #define METERS_FEET 3.2808 // multiply meters by 3.2808 #define INCH_CENTIMETER 2.54 // multiply inches by 2.54 #define INCH_METER 0.0254 // multiply inches by 0.0254 #define INCH_FEET 12 // divide inches by 12 #define FEET_CENTIMETER 30.48 // multiply feet by 30.48 #define FEET_METER 0.3048 // multiply feet by 0.3048 #define FEET_INCH 12 // multiply feet by 12