| 1 | /* bmpbit.c |
| 2 | * Greg Cook, 9/Apr/2015 |
| 3 | */ |
| 4 | |
| 5 | /* CRC RevEng, an arbitrary-precision CRC calculator and algorithm finder |
| 6 | * Copyright (C) 2010, 2011, 2012, 2013, 2014, 2015 Gregory Cook |
| 7 | * |
| 8 | * This file is part of CRC RevEng. |
| 9 | * |
| 10 | * CRC RevEng is free software: you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation, either version 3 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * CRC RevEng is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with CRC RevEng. If not, see <http://www.gnu.org/licenses/>. |
| 22 | */ |
| 23 | |
| 24 | #ifdef BMPTST |
| 25 | # include <stdio.h> |
| 26 | # include <stdlib.h> |
| 27 | #else |
| 28 | # define FILE void |
| 29 | #endif |
| 30 | #include "reveng.h" |
| 31 | |
| 32 | #if (defined BMPTST) || (BMP_BIT < 32) |
| 33 | /* Size in bits of a bmp_t. Not necessarily a power of two. */ |
| 34 | int bmpbit; |
| 35 | |
| 36 | /* The highest power of two that is strictly less than BMP_BIT. |
| 37 | * Initialises the index of a binary search for set bits in a bmp_t. |
| 38 | * (Computed correctly for BMP_BIT >= 2) |
| 39 | */ |
| 40 | int bmpsub; |
| 41 | |
| 42 | void |
| 43 | setbmp(void) { |
| 44 | /* Initialise BMP_BIT and BMP_SUB for the local architecture. */ |
| 45 | bmp_t bmpmax = ~(bmp_t) 0; |
| 46 | |
| 47 | bmpbit = 0; bmpsub = 1; |
| 48 | |
| 49 | while(bmpmax) { |
| 50 | bmpmax <<= 1; |
| 51 | ++bmpbit; |
| 52 | } |
| 53 | |
| 54 | while((bmpsub | (bmpsub - 1)) < bmpbit - 1) |
| 55 | bmpsub <<= 1; |
| 56 | } |
| 57 | #endif |
| 58 | |
| 59 | #ifdef BMPTST |
| 60 | int |
| 61 | main(int argc, char *argv[]) { |
| 62 | /* check the compile-time bitmap width is correct, otherwise |
| 63 | * searches run forever. */ |
| 64 | # if BMP_BIT > 0 |
| 65 | setbmp(); |
| 66 | if(BMP_BIT != bmpbit || BMP_SUB != bmpsub) { |
| 67 | fprintf(stderr,"reveng: configuration fault. Update " |
| 68 | "config.h with these definitions and " |
| 69 | "recompile:\n" |
| 70 | "\t#define BMP_BIT %d\n" |
| 71 | "\t#define BMP_SUB %d\n", |
| 72 | bmpbit, bmpsub); |
| 73 | exit(EXIT_FAILURE); |
| 74 | } |
| 75 | # endif /* BMP_BIT > 0 */ |
| 76 | /* check the bitmap constant macro */ |
| 77 | if(~(bmp_t) 0 != ~BMP_C(0)) { |
| 78 | fprintf(stderr, "reveng: configuration fault. Edit " |
| 79 | "the definition of BMP_C() in config.h to " |
| 80 | "match BMP_T and recompile.\n"); |
| 81 | exit(EXIT_FAILURE); |
| 82 | } |
| 83 | exit(EXIT_SUCCESS); |
| 84 | } |
| 85 | |
| 86 | #endif /* BMPTST */ |