|
|||||||||||||||||||||
|
|||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
BackgroundA while ago I was working on a program that used a large lookup table to do some computations. The contents of the table depended on a large number of parameters that I had to tweak to get optimal performance from my code. And every time I changed one parameter the whole table had to be recalculated... I had written a function that dumped the content of the table to standard output. That way I could compile my program with the new parameters set, cut-and-paste the table from screen and into my source code and finally recompile the whole project. I didn't mind doing that - the first twenty times. After that I figured there had to be a better way. There was. Enter Template Metaprogramming. What is Template Metaprogramming (TMP)Template Metaprogramming is a generic programming technique that uses extremely early binding. The compiler acts as an interpreter or a "virtual computer" that emits the instructions that make up the final program. It can be used for static configuration, adaptive programs, optimization and much more. In this article I intend to explain how to use C++ template classes to produce compile-time generated code. Different kind of MetatemplatesI make a difference between two kinds on Metatemplates - ones that calculate a constant value and ones that produce code. The difference is that the first kind should never produce instructions that are executed at runtime.Templates that calculate a valueAssume you want to calculate the number of bits set in an byte. If you do this at runtime you might have a function looking like this:int bits_set(unsigned char byte) {int count = 0; for (int i = 0; i < 8; i++) if ( (0x1L << i) & byte ) count++; return count; }In cases where the byte is known at compile time this can also be done by the compiler, using TMP: template< unsigned char byte > class BITS_SET { public: enum { B0 = (byte & 0x01) ? 1:0, B1 = (byte & 0x02) ? 1:0, B2 = (byte & 0x04) ? 1:0, B3 = (byte & 0x08) ? 1:0, B4 = (byte & 0x10) ? 1:0, B5 = (byte & 0x20) ? 1:0, B6 = (byte & 0x40) ? 1:0, B7 = (byte & 0x80) ? 1:0 }; public: enum{RESULT = B0+B1+B2+B3+B4+B5+B6+B7}; }; I have used an You can now use template< int i > class FACTOR{ public: enum {RESULT = i * FACTOR<I-1>::RESULT}; }; class FACTOR< 1 >{ public: enum {RESULT = 1}; };If we for example write this: int j = FACTOR< 5 >::RESULT; somewhere in our code the compiler will generate something like the following line of assembler code: ; int j = FACTOR< 5 >::RESULT;
mov DWORD PTR _j$[ebp], 120 ; 00000078H - a constant value!
How does this work? As we instantiate Templates that unroll loops/specialize functionsTemplate metaprograms can generate useful code when interpreted by the compiler, for example a massively inlined algorithm that has its loops unrolled. The result is usually a large speed increase in the application. For example, look at the following code that calculates the sum of the numbers 1..1000: int sum = 0; for (int i = 1 ; i <= 1000; i++) sum += i; We are actually performing 2000 additions, rather than 1000 (as we have to increment int sum = 0; sum += 1; sum += 2; ... sum += 1000; This is the way a Template Metaprogram would expand a loop. Now we perform exactly a thousand additions, but this method also has a prize. The code size increase, meaning that we theoretically could take a performance hit by increasing the number of page faults. In practice, though, code is often invoked multiple times and already loaded in cache. Loop unrollingLoop unrolling is easily defined using recursive templates, similar to calculating a value: template< int i > class LOOP{ public: static inline void EXEC(){ cout << "A-" << i << " "; LOOP< i-1 >::EXEC(); cout << "B-" << i << " "; } }; class LOOP< 0 >{ public: static inline void EXEC(){ cout << "A-" << i; cout << "\n"; cout << "B-" << i; } }; The output of A-8 A-7 A-6 A-5 A-4 A-3 A-2 A-1 A-0 Again, the thing to notice is that there is no loop in the resulting binary code. The loop unrolls itself to produce code like: cout << "A-" << 8 << " "; cout << "A-" << 7 << " "; ... cout << "A-" << 0; cout << "\n"; cout << "B-" << 0; ... cout << "B-" << 7 << " "; cout << "B-" << 8 << " "; An unrelated, but interesting thing can be found in class Beside loops other statements can be constructed: IF - statementtemplate< bool Condition > class IF { public: static inline void EXEC(){ cout << "Statement is true"; } }; class IF< false > { public: static inline void EXEC(){ cout << "Statement is false"; } }; SWITCH - statementtemplate< int _case > class SWITCH { public: static inline void EXEC(){ cout << " SWITCH - default "; } }; class SWITCH< 1 > { public: static inline void EXEC(){ cout << " SWITCH - 1 "; } }; class SWITCH< 2 > { public: static inline void EXEC(){ cout << " SWITCH - 2 "; } }; ... Example of usage of the two classes: SWITCH< 2 > myTwoSwitch; // store for delayed execution myTwoSwitch.EXEC(); IF< false >::EXEC(); myTwoSwitch.EXEC(); The output will be: " SWITCH - 2 Statement is false SWITCH - 2 " Using Meta-MetatemplatesIt is possible to define a generic template for a special kind of operation, like an if- or for-statement. I like to call this a Meta-Metatemplate since the operation is defined in a class outside the template itself. Even if this might be useful it can make the code very hard to understand in complex cases. Also it will probably take a few years before compilers will be able to take full advantage of these kinds of constructs. For now I prefer to use specialized templates, but for simple cases Meta-Metatemplates might be useful. Sample code for the simplest of these constructs, the if-statement is given below:template< bool Condition, class THEN, class ELSE > struct IF { template< bool Condition > struct selector {typedef THEN SELECT_CLASS;}; struct selector< false > {typedef ELSE SELECT_CLASS;}; typedef selector< Condition >::SELECT_CLASS RESULT; }; Example of usage: struct THEN { static int func() {cout << "Inside THEN"; return 42; } }; struct ELSE { static int func() {cout << "Inside ELSE"; return 0; } }; int main(int argc, char* argv[]) { int result = IF< 4 == sizeof(int), THEN, ELSE >::RESULT::func(); cout << " - returning: " << result; } On 32-bit architectures this will print "Inside THEN - returning: 42" to standard output. Note that if A real world exampleIncluded as a sample is a small class that does generic CRC (Cyclic Redundancy Codes - a set of simple hash algorithms) calculations. The class uses a set of parameters that are The class generate a lookup table with 256 entries at compile time. The implementation is pretty straightforward, and I hope that the comments in the source is enough to explain usage of the class. In some cases I have used macros where TMP would have been a possible solution. The reason for this is readability, an important factor when choosing which technique to use. One thing to remember when compiling this is that the source is using a lot of compiler heap memory. I had to increase the memory allocation limit by using the compiler option '/Zm400'. This is one drawback of TMP - it really pushes the compiler to the limit. Coding conventionsThe compiler doesn't like being abused as described above. Not a bit. And it will put up a struggle. Warnings and errors will range from cryptic to C1001 - INTERNAL COMPILER ERROR. And you can't debug a Metatemplate program like a runtime program. For those reasons using well defined coding conventions is more important with TMP than with other programming techniques. Below I give some rules that I've found useful. General suggestionsAs template metaprograms are somewhat similar to macros I prefer to give all TMP classes uppercase names. Also try to make the name as descriptive as possible. The main reason for this is that public variables/functions usually have non-descriptive names (see below). A TMP class is the one defining the operation, not the member functions/variables. Also try to limit a TMP class to a single operation. Template Metaprogramming is challenging enough without trying to generalize the class to support multiple operations. Often this will only result in code bloat. Variable/functions namesI usually prefer to have only one of two possible public operations defined in a TMP class:
Should I use TMP in my program?Template Metaprogramming is a great technique when used correctly. On the other hand it might result in code bloat and performance decrease. Below are some rules of thumb when to use TMP.Use TMP when:
Don't use TMP when:
Acknowledgements and references
| ||||||||||||||||||||