/* This is a simple benchmark that calculates a 32x22 pixel * mandelbrot set. The results are displayed by using ANSI * codes to change the text background color and then * printing two spaces. * * Compile options: * * -DFLOAT uses floating point math * otherwise some quick and dirty 8.8 * bit fixed point math is used * * -DPOSIX reports the run time in seconds * * compile with: * * gcc -O2 br.c -o br * cc -O2 br.c -o br * gcc -O2 br.c -o br -DFLOAT -DPOSIX * * etc. * * On an Amiga you can either compile with -DPOSIX * and then ixemul.library will be required: * * gcc -O2 br.c -o br -DPOSIX * * Or without -DPOSIX (so run time won't be reported) * and then ixemul.library isn't necessary: * * gcc -O2 br.c -o br -noixemul * * code by Iljitsch van Beijnum, inspired * by Matt Heffernan's "Battle Royale" */ #include #ifdef POSIX #include #endif char *colorstring[] = { "\033[41m ", "\033[42m ", "\033[43m ", "\033[44m ", "\033[45m ", "\033[46m ", "\033[47m ", "\033[100m ", "\033[101m ", "\033[102m ", "\033[103m ", "\033[104m ", "\033[105m ", "\033[106m ", "\033[107m ", "\033[40m ", }; /* BASIC version for reference: for py = 0 to 21 for px = 0 to 31 xz = px * 3.5 / 32 - 2.5 yz = py * 2 / 22 - 1 x = 0 y = 0 for i = 0 to 14 if x * x + y * y > 4 break xt = x * x - y * y + xz y = 2 * x * y + yz x = xt next i vpoke bla, i next px print "" next py */ int main() { int i; #ifdef FLOAT float py, px; float x, y, xz, yz, xt; #else short py, px, t; short x, y, xz, yz, xt; #endif #ifdef POSIX struct timeval ts1, ts2; float diff; #endif // clear the screen printf("\033c"); #ifdef POSIX gettimeofday(&ts1, NULL); #endif #ifdef FLOAT // floating point constants and calculations for (py = 0; py <= 21; py++) { for (px = 0; px <= 31; px++) { xz = px * 3.5 / 32 - 2.5; yz = py * 2 / 22 - 1; #else // fixed point constants and calculations // constants need to be multiplied by 256 (or << 8) // multiplications require one operand to be // divided by 256 (or >> 8), or both operands to be // divided by 16 (or >> 4) for (py = 0; py <= 21 << 8; py += 256) { for (px = 0; px <= 31 << 8; px += 256) { xz = (px * 7 / 32 - 1280) >> 1; yz = (py * 2 / 22 - 256); #endif // common part x = 0; y = 0; for (i = 0; i <= 14; i++) { #ifdef FLOAT // floating point if (x * x + y * y > 4) break; xt = x * x - y * y + xz; y = 2 * x * y + yz; #else // fixed point if ((x >> 4) * (x >> 4) + (y >> 4) * (y >> 4) > 1024) break; xt = (x >> 4) * (x >> 4) - (y >> 4) * (y >> 4) + xz; y = (x >> 4) * (y >> 3) + yz; #endif // common part x = xt; } printf("%s", colorstring[i]); } printf("\033[0m\n"); } #ifdef POSIX // calculate and print how long everything took // but only if compiled with -DPOSIX, these // functions may not be available on some systems gettimeofday(&ts2, NULL); diff = ts2.tv_sec - ts1.tv_sec + ((ts2.tv_usec - ts1.tv_usec) / 1000000.0); printf("%f sec\n", diff); #endif }