I want to create a quick lookup table to return unsigned chars for log (base 2) of 2,4,8,16,32,64,128 and avoid calculating (unsigned char)log(x)/log(2.0) thousands of times per second.
A simple solution seems to be an array of unsigned chars which would use 129 bytes ("wasting" 122 of them, but who cares).
I can use a const array, initialized in a header file so it will be available to public static methods of class U. (No "U" object is ever instantiated; it just contains several static methods that are used by various objects.)
Is there a nicer way to do this than listing all 129 elements as in
so that, for example mylog[64] = (unsigned char)6? I'm not worried about all those 0's because I'll never look up mylog of anything other than those specific 7 values.Code:const unsigned char mylog[129] = { 0,0,1,0,2,0,0,0,3,0,0,0,0,0,0,0,4,0 ... 0,7};
I read somewhere that there is a gcc extension that would allow initialization by writingand so on, but it doesn't seem to work in gcc 4.2 or 3.3. In 4.2 I get "error: expected primary-expression before '[' token". In 3.3 I get "syntax error before '=' token". (Anyway, I'd rather not use something that's not portable.)Code:const int mylog[129] = { [2]=1, [4]=2, [8]=3, ... };
Alternatively, can I use map or vector? Is there a way to initialize a map or a vector in a header (since I never call a constructor for the class that will be using the table)?