Hello,
So far this is (I think) my fastest way to chop of accessive bits from a
byte (in Delphi).
// Mission:
// Value contains a binary value/code.
// BitCount specifies which bits of the value starting from bit position
0/lsb should remain.
// so BitCount 5 means: Bits 0 to 4 must remain. Bits 5 to 7 must be
cleared.
// The accessive bits beyond bit count are to be zero-ed/cleared.
var
Bitcount : byte;
Value : byte;
Mask : byte;
begin
Value := 255; // could be anything
BitCount := 5; // could be zero to 8
// 5 instructions, no memory usage, best one so far.
Mask := not (255 shl BitCount);
Value := Value and Mask;
writeln( Value ); // displays 31.
readln;
end.
// generated assembler for the method:
Project1.dpr.18: Mask := not (255 shl BitCount);
00409151 8BC8 mov ecx,eax
00409153 B0FF mov al,$ff
00409155 D2E0 shl al,cl
00409157 F6D0 not al
Project1.dpr.19: Value := Value and Mask;
00409159 22D8 and bl,al
I am pretty sure there is a faster way in assembler.
I am mostly looking for high level code Delphi code.
But it could still be interesting to see any faster low level assembler
code
in case I want to write an assembler routine in the future.
Who can optimize it ? :):):)
I tried different high level techniques, I already tried one high level
technique using a larger mask and ****fting the low bits into the high bits
and then use the high byte to immediatly mask.. reducing the not
instruction... but delphi compiler generated/used memory access which is
probably worse.
But when coding directly in assembler it's probably possible to prevent
the
memory access ;)
I haven't tried yet in assembler because I leave that fun to you guys the
assembler-fan-boys :)
Can you write something better than the delphi compiler ? ;)
Bye,
Skybuck.


|