M
mast4as
Hi everyone
It seems that one day I saw some code where the developper managed to
use bit operations to avoid using a switch which I would like to avoid
for performance reasons. Here is what I am trying to do.
typedef float color[3];
typedef struct Test
{
float a;
float b;
color c;
float d;
};
typedef enum Type
{
TYPE_A = 1 << 0,
TYPE_B = 1 << 1,
TYPE_C = 1 << 2,
TYPE_D = 1 << 3,
};
int main( int argc, char **argv )
{
Type type = TYPE_B;
Test test;
memset( &test.a, 0x0, sizeof( Test ) );
switch ( type )
{
case TYPE_A: test.a += 1; break;
case TYPE_B: test.b -= 1; break;
case TYPE_C: test.c[ 0 ] = 1; break;
case TYPE_D: test.d += 2; break;
defaullt: ;
}
}
Basically I would like to avoid the switch statetement. What I am
trying to do in the code is add the result to the appropriate element
of the variable "test" of type Test based on the type of the variable
"type". Do you guys see a super efficient way of doing this ;-)
Many thanks -coralie
It seems that one day I saw some code where the developper managed to
use bit operations to avoid using a switch which I would like to avoid
for performance reasons. Here is what I am trying to do.
typedef float color[3];
typedef struct Test
{
float a;
float b;
color c;
float d;
};
typedef enum Type
{
TYPE_A = 1 << 0,
TYPE_B = 1 << 1,
TYPE_C = 1 << 2,
TYPE_D = 1 << 3,
};
int main( int argc, char **argv )
{
Type type = TYPE_B;
Test test;
memset( &test.a, 0x0, sizeof( Test ) );
switch ( type )
{
case TYPE_A: test.a += 1; break;
case TYPE_B: test.b -= 1; break;
case TYPE_C: test.c[ 0 ] = 1; break;
case TYPE_D: test.d += 2; break;
defaullt: ;
}
}
Basically I would like to avoid the switch statetement. What I am
trying to do in the code is add the result to the appropriate element
of the variable "test" of type Test based on the type of the variable
"type". Do you guys see a super efficient way of doing this ;-)
Many thanks -coralie