Compile-time constant CRCs
I didn't tag this because I'm not sure if it should belong to [preprocessor], [mpl] or [crc]. Given the black-box (macro, function, class, whatever) I'll call ConstCRC32 that is assignable to a unsigned int is there a way to write: unsigned int hash = ConstCRC32( "HELLO" ); and have an optimized build yield the equivalent of: unsigned int hash = 0x58a10fc0; I had a solution (although obviously non-portable) in msvc7.1 but after moving to msvc8 and stepping through the code I noticed that msvc8's "improved" optimizing compiler cannot optimize away the calculation like msvc7.1's could. I also doubt that the code would work if I ever had to port it to another compiler. I tried using simple TMP techniques I've used in the past but couldn't find a way to access the characters of the string. So looking around I came upon boost.crc which is geared towards computing crcs of blocks of data which isn't really what I care about right now. I also found boost.mpl which is very cool but I have a hard time grasping still. The problems seems solvable with mpl if I could transform the data into boost::mpl::list_c<char,'H','E','L','L','O'> but I still coulnd't find a way to access the characters of the string and I think others at my company would rather revert to the old #define CRC( str, crc ) crc unsigned int hash = CRC( "HELLO", 0x58a10fc0 ); than move to that syntax directly. So looking further I found boost.preprocessor which I haven't explored enough to see if there is away to have PP calculate the CRC directly or have it transform the string into the boost::mpl::list_c form. Can anyone offer some advice? Thanks Mike P.S. The old version was actually fairly simple it just used a precomputed static const unsigned int lookup table and did a strlen and a for loop with a lookup into the table for each character (in a __forceinline function that took a const char * const string).
participants (1)
-
Michael Marcin