Y
yves
Hello,
I'm trying to check if only the lsb 8 bits of an integer are used.
bool Func(int x)
{
if ((x & 0xFFFFFF00) == 0)
{
return true;
}
else
{
return false;
}
}
This actually comes down to checking if an integer is in the range
[0,255]. Alternatively I could do:
bool Func(int x)
{
if (x >= 0 && x <= 255)
{
return true;
}
else
{
return false;
}
}
Since the first function only contains one conditional jump and the
second one contains two, the first one should be faster. Oddly enough
when doing some performance tests I found out that the first one (with
only one condition/jump) is doing worse than the second one (with two
conditions/jumps). I've looked at the MSIL to try to figure out why and
found out that the first function is actually translated as:
ldarg.0
conv.i8
ldc.i4 0xffffff00
conv.u8
and
ldc.i4.0
conv.i8
bne.un.s ...
This code contains a number of 'conv' instructions which are causing
the overhead. So I'm wondering which value should be used so that only
4 bytes are used and the 24 MSB are set to 1 while the others are 0?
0xffffff00 is seen as an unsigned value while I want it to be seen as a
signed one of 4 bytes. Any ideas?
TIA
Yves
I'm trying to check if only the lsb 8 bits of an integer are used.
bool Func(int x)
{
if ((x & 0xFFFFFF00) == 0)
{
return true;
}
else
{
return false;
}
}
This actually comes down to checking if an integer is in the range
[0,255]. Alternatively I could do:
bool Func(int x)
{
if (x >= 0 && x <= 255)
{
return true;
}
else
{
return false;
}
}
Since the first function only contains one conditional jump and the
second one contains two, the first one should be faster. Oddly enough
when doing some performance tests I found out that the first one (with
only one condition/jump) is doing worse than the second one (with two
conditions/jumps). I've looked at the MSIL to try to figure out why and
found out that the first function is actually translated as:
ldarg.0
conv.i8
ldc.i4 0xffffff00
conv.u8
and
ldc.i4.0
conv.i8
bne.un.s ...
This code contains a number of 'conv' instructions which are causing
the overhead. So I'm wondering which value should be used so that only
4 bytes are used and the 24 MSB are set to 1 while the others are 0?
0xffffff00 is seen as an unsigned value while I want it to be seen as a
signed one of 4 bytes. Any ideas?
TIA
Yves