Fast fastposchar algorithm (improved version) by codegame

Source: Internet
Author: User

 

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;

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.