Partially SIMDed without trailing ... and it beat all 🙂
const
{ Source Data Format : Imm8[1:0] }
DF_UNSIGNED_BYTES = 0;
DF_UNSIGNED_WORDS = 1;
DF_SIGNED_BYTES = 2;
DF_SIGNED_WORDS = 3;
{ Aggregation Operation : Imm8[3:2] }
AGGREGATION_OP_EQUAL_ANY = 0 shl 2;
AGGREGATION_OP_RANGES = 1 shl 2;
AGGREGATION_OP_EQUAL_EACH = 2 shl 2;
AGGREGATION_OP_EQUAL_ORDERED = 3 shl 2;
{ Polarity : Imm8[5:4] }
POLARITY_POSITIVE = 0 shl 4;
POLARITY_NEGATIVE = 1 shl 4;
POLARITY_MASKED_POSITIVE = 2 shl 4;
POLARITY_MASKED_NEGATIVE = 3 shl 4;
{ Output Selection : Imm8[6] }
OS_LSI = 0 shl 6;
OS_MSI = 1 shl 6;
OS_BIT_MASK = 0 shl 6;
OS_BYTE_WORD_MASK = 1 shl 6;
const
[Align(16)]
Range: array [0 .. 7] of Char = '09afAF' + #00;
function IsValidHex(P: Pointer): Boolean;
asm
movdqa xmm1, [Range]
sub eax, 16
@@SimdLoop:
add eax, 16
movdqu xmm2, [eax]
pcmpistri xmm1, xmm2, DF_UNSIGNED_WORDS or AGGREGATION_OP_RANGES or POLARITY_NEGATIVE
ja @@SimdLoop
test Word [eax + ecx *2], -1
setz al
end;
function HexToBinMahdiOneShot(const HexValue: string): string;
type
TChar4 = array [0 .. 3] of Char;
PChar4 = ^TChar4;
const
Table: array [0 .. 25] of TChar4 = ('0000', '1010', '1011', '1100', '1101', '1110', '1111', '0000', '0000', '0000', '0000', '0000', '0000', '0000', '0000',
'0000', '0000', '0001', '0010', '0011', '0100', '0101', '0110', '0111', '1000', '1001');
var
P: PChar4;
I, Len: Integer;
begin
SetLength(Result, Length(HexValue) * 4);
P := PChar4(Result);
if IsValidHex(Pointer(HexValue)) then
begin
Len := Length(HexValue);
for I := 1 to Len do
begin
P^ := Table[Ord(HexValue[I]) and 31];
Inc(P);
end;
end
else
raise EConvertError.CreateFmt('Invalid hex : %s', [HexValue]);
end;