table generation with recursive templates

W

W Karas

I wrote this code to generate an intialized array of 256 32-bit words:

typedef unsigned char u_int8;
typedef unsigned long u_int32;

template <unsigned Bits, class Start>
struct TG
{
typedef TG<Bits - 1, Start> Sub_start1;
typedef TG<Bits - 1, Sub_start1> Sub_start2;

Sub_start1 s1;
Sub_start2 s2;

static const u_int8 Val1 = Sub_start2::Val1;
static const u_int8 Val2 = Sub_start2::Val2;
static const u_int8 Val3 = Sub_start2::Val3;
static const u_int8 Val4 = Sub_start2::Val4;
};

struct TG_base
{
static const u_int8 Val1 = 17;
static const u_int8 Val2 = 19;
static const u_int8 Val3 = 23;
static const u_int8 Val4 = 29;
};

template <class Start>
struct TG<0, Start>
{
static const u_int32 Val =
(((((Start::Val4 << 8) | Start::Val3) << 8) |
Start::Val2) << 8) | Start::Val1;

const u_int32 val;

TG(void) : val(Val) { }

static const u_int8 Val1 = static_cast<u_int8>(Start::Val1 +
TG_base::Val1);
static const u_int8 Val2 = static_cast<u_int8>(Start::Val2 +
TG_base::Val2);
static const u_int8 Val3 = static_cast<u_int8>(Start::Val3 +
TG_base::Val3);
static const u_int8 Val4 = static_cast<u_int8>(Start::Val4 +
TG_base::Val4);
};

TG<8, TG_base> table_data;

const u_int32 * const Table =
reinterpret_cast<const u_int32 *>(&table_data);

When I compiled with assembler output, I found that the table_data
structure was not being statically initialized. I tried with two
different compilers, with optimization enabled, and both generated a
constructor with store instructions to initialize table_data . So I
discarded the templates and used macros instead. Does anyone know of
a compiler that would realize it could statically initialize
table_data in this example?
 
S

SG

I wrote this code to generate an intialized array of 256 32-bit words:
[...]
When I compiled with assembler output, I found that the table_data
structure was not being statically initialized.

Right. I also spent a while trying to figure out if I could let the
compiler generate a table statically via templates (or something
else). I don't think it's possible.

What I did instead was I wrote a little command line program that
generated a file "tables.inc" (lookup tables in C++ syntax) which is
just included in a C++ file. It's not difficult to write a proper
Makefile for this. You just add a target "tables.inc" and some other
dependencies. ;-)

Cheers!
SG
 
M

Michael Mol

I wrote this code to generate an intialized array of 256 32-bit words:
[...]
When I compiled with assembler output, I found that the table_data
structure was not being statically initialized.

Right. I also spent a while trying to figure out if I could let the
compiler generate a table statically via templates (or something
else). I don't think it's possible.

What I did instead was I wrote a little command line program that
generated a file "tables.inc" (lookup tables in C++ syntax) which is
just included in a C++ file. It's not difficult to write a proper
Makefile for this. You just add a target "tables.inc" and some other
dependencies. ;-)

Cheers!
SG

http://99-bottles-of-beer.net/language-c++-108.html

Extreme template metaprogramming. Go to town and scare your
coworkers. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top