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?
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?