/* ex: set ts=2 et: */ #include #include #include /* * here is an example of how to select from a relatively small pool of choices * using a relatively large pool of possible, consecutive inputs. the output is * strictly customized for a single system but can easily be adjusted */ static unsigned CR = 0; /* our Cols/Rows index */ static struct { /* our Cols/Rows metrics for display */ unsigned cols, rows; } ColsRows[] = { { 1, 2 }, /* default */ { 1, 2 }, /* 1-2 */ { 2, 2 }, /* 3-4 */ { 2, 4 }, /* 5-8 */ { 4, 4 } /* 9-16 */ }; int main(void) { unsigned i, n; for (i = 0; i <= 16; i++) { n = i - 1; /* find next-highest power of 2 for i */ n |= n >> 1; /* fill in low bits below...*/ n |= n >> 2; n++; CR = (unsigned)((log(n) / log(2)) + DBL_EPSILON); /* take log2 of n, round and integer-ize */ printf("#%2u: n=%2u, CR=%u, rows=%u, cols=%u...\n", /* use l as index into Rows and Cols */ i, n, CR, ColsRows[CR].cols, ColsRows[CR].rows); } return 0; } #if 0 pizza@yeti:/vh/pe/www/~pizza/c $ gcc -lm -o refactor_1-16_switch refactor_1-16_switch.c pizza@yeti:/vh/pe/www/~pizza/c $ ./refactor_1-16_switch | less # 0: n= 0, CR=0, rows=1, cols=2... # 1: n= 1, CR=0, rows=1, cols=2... # 2: n= 2, CR=1, rows=1, cols=2... # 3: n= 4, CR=2, rows=2, cols=2... # 4: n= 4, CR=2, rows=2, cols=2... # 5: n= 8, CR=3, rows=2, cols=4... # 6: n= 8, CR=3, rows=2, cols=4... # 7: n= 8, CR=3, rows=2, cols=4... # 8: n= 8, CR=3, rows=2, cols=4... # 9: n=16, CR=4, rows=4, cols=4... #10: n=16, CR=4, rows=4, cols=4... #11: n=16, CR=4, rows=4, cols=4... #12: n=16, CR=4, rows=4, cols=4... #13: n=16, CR=4, rows=4, cols=4... #14: n=16, CR=4, rows=4, cols=4... #15: n=16, CR=4, rows=4, cols=4... #16: n=16, CR=4, rows=4, cols=4... pizza@yeti:/vh/pe/www/~pizza/c $ #endif