SSE optimization algorithm:
Function cgposcharsse (subchar: Char; srcstring: pchar; Len: integer; Order: Boolean = true): integer;
// Subchar-> Al; srcstring-> edX; Len-> ECx order-> [EBP + 8]
ASM
Push ESI
Push EBX
Test ECx, ECx
JZ @ notfound
Test edX, EDX
JZ @ notfound
Xor esi, ESI
MoV ah, Al
Movd xmm1, eax
Pshuflw xmm1, xmm1, 0
Pshufd xmm1, xmm1, 0
MoV eax, [EBP + 8]
If test eax and eax // is 0, order = false
Je @ reverse // 0 Reverse Lookup
{--------------- Sequential query ------------------}
@ Ordercmp:
Movups xmm0, [edX + esi]
Pcmpeqb xmm0, xmm1
Pmovmskb eax, xmm0
Test eax, eax
Jnz @ orderfound
Add ESI, $10
Cmp esi, ECx
Jl @ ordercmp
JMP @ notfound
{--------------- Reverse query ------------------}
@ Reverse:
MoV ESI, ECx
Sub ESI, $10
@ Reversecmp:
Movups xmm0, [edX + esi]
Pcmpeqb xmm0, xmm1
Pmovmskb eax, xmm0
Test eax, eax
Jnz @ reversefound
Sub ESI, $10
Cmp esi,-$10
Jl @ reversecmp
@ Notfound:
XOR eax, eax
JMP @ exit
@ Orderfound:
BSF eax, eax
JMP @ setret
@ Reversefound:
BSR eax, eax
@ Setret:
SHR eax, 3
Add eax, ESI
Add eax, 1
CMP eax, ECx
JG @ notfound // The out-of-range value is greater than the length.
CMP eax, 1
Jl @ notfound // The cross-border value is less than 1.
@ Exit:
Pop EBX
Pop ESI
End;
General optimization algorithms:
Function cgposchar (subchar: Char; srcstring: pchar; Len: integer; Order: Boolean = true): integer;
// Subchar-> Al; srcstring-> edX; Len-> ECx order-> [EBP + 8]
ASM
Push ESI
Push EBX
Push edX
Push EDI
Test ECx, ECx
JZ @ notfound
Test edX, EDX
JZ @ notfound
Xor ebx, EBX
MoV ah, Al
MoV BX, ax
SHL eax, $10
Or EBX, eax
Xor esi, ESI
MoV eax, [EBP + 8]
If test eax and eax // is 0, order = false
Je @ reverse // 0 Reverse Lookup
{--------------- Sequential query ------------------}
@ Ordercmp:
MoV eax, [edX + esi]
XOR eax, EBX
Lea EDI, [eax-$01010101]
Not eax
And eax, EDI
And eax, $80808080
Jnz @ orderfound
Add ESI, 4
Cmp esi, ECx
Jl @ ordercmp
JMP @ notfound
{--------------- Reverse query ------------------}
@ Reverse:
MoV ESI, ECx
Sub ESI, 4
@ Reversecmp:
MoV eax, [edX + esi]
XOR eax, EBX
Lea EDI, [eax-$01010101]
Not eax
And eax, EDI
And eax, $80808080
Jnz @ reversefound
Sub ESI, 4
Cmp esi,-4
JG @ reversecmp
@ Notfound:
XOR eax, eax
JMP @ exit
@ Orderfound:
BSF eax, eax
JMP @ setret
@ Reversefound:
BSR eax, eax
@ Setret:
SHR eax, 3
Add eax, ESI
Add eax, 1
CMP eax, ECx
JG @ notfound // The out-of-range value is greater than the length.
CMP eax, 1
Jl @ notfound // The cross-border value is less than 1.
@ Exit:
Pop EDI
Pop edX
Pop EBX
Pop ESI
End;