快速FastPosChar算法比目前算法快500%以上具有实用价值!

Reverse.King 2010-07-29 10:54:52
加精
应群友cjc要求改写了一个poschar的快速算法.
实测速度CGPosChar比系统自带pos要快40%-60%以上,
而CGPosCharSSE则比系统自带pos要快500%以上,这些都是归功于sse指令集发挥的作用。
SSE版本的需要CPU支持SSE2,附带一个测试CPU是否支持SSE的函数。


SSE优化算法:
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]
test eax,eax //为0则表示Order =false
je @Reverse //为0倒序查找

{---------------顺序查找------------------}
@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:
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:
add eax,esi
add eax,1
cmp eax,ecx
jg @Notfound //越界大于长度
cmp eax,1
jl @Notfound //越界小于1
@Exit:
pop ebx
pop esi
end;

常规优化算法:

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]
test eax,eax //为0则表示Order =false
je @Reverse //为0倒序查找

{---------------顺序查找------------------}
@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:
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 //越界大于长度
cmp eax,1
jl @Notfound //越界小于1
@Exit:
pop edi
pop edx
pop ebx
pop esi
end;

CPU SSE支持检测函数:

function CheckSupportSSE(SupportFlag: Byte): Boolean;
// SupportFlag in:[1-6] is check SSE1,SSE2,SSE3,SSSE3,SSE41,SSE42.
function GetCpuId: DWORD;
asm
push ecx
push edx
mov eax,1
cpuid
mov eax,edx //RetValue
pop edx
pop ecx
end;
const
_SSE_FLAG: array[0..5] of DWORD = ($2000000, $4000000, 1, $200, $80000, $100000);
var
_Flag: DWORD;
begin
Result := False;
_Flag := GetCpuId;
Result:= (_SSE_FLAG[SupportFlag] and _Flag) <> 0;
end;

...全文
7284 256 打赏 收藏 转发到动态 举报
写回复
用AI写文章
256 条回复
切换为时间正序
请发表友善的回复…
发表回复
chrile 2010-09-02
  • 打赏
  • 举报
回复
先收藏再说了
tayu0214 2010-08-24
  • 打赏
  • 举报
回复
学习了,不错!!
ymenking 2010-08-20
  • 打赏
  • 举报
回复
看了很震憾
courage3000 2010-08-08
  • 打赏
  • 举报
回复
汇编看不懂
yudonk 2010-08-08
  • 打赏
  • 举报
回复
不懂
不懂
shell2522 2010-08-08
  • 打赏
  • 举报
回复
楼主头像牛
joejoe1991 2010-08-07
  • 打赏
  • 举报
回复
为了挣十分。。
wuaj1980 2010-08-06
  • 打赏
  • 举报
回复
TNND.咱一点都看不懂呢
yuntian666 2010-08-06
  • 打赏
  • 举报
回复
[Quote=引用 241 楼 codegame 的回复:]
引用 234 楼 smltq 的回复:
引用 72 楼 gqqnb 的回复:
FastPosChar是什么算法啊

同问,FastPosChar是什么算法?


FastPosChar 函数功能是从一串字串中查找一个字char返回该字的位置。
[/Quote]
如果原来字符串是无序的,难道有比O(n)时间复杂度还要低的算法?
habour2010 2010-08-06
  • 打赏
  • 举报
回复
很厉害 学习了
Reverse.King 2010-08-06
  • 打赏
  • 举报
回复
[Quote=引用 243 楼 smltq 的回复:]
引用 241 楼 codegame 的回复:
引用 234 楼 smltq 的回复:
引用 72 楼 gqqnb 的回复:
FastPosChar是什么算法啊

同问,FastPosChar是什么算法?


FastPosChar 函数功能是从一串字串中查找一个字char返回该字的位置。

如果原来字符串是无序的,难道有比O(n)时间复杂度还要低的算法?
[/Quote]
都是无序的算法就是顺序对比,优化是做指令优化不是逻辑优化。
sinton 2010-08-06
  • 打赏
  • 举报
回复
不错,好强的算法子啊
lhj33852626 2010-08-06
  • 打赏
  • 举报
回复
牛人啊!汇编搞这么好!
sharpidd 2010-08-06
  • 打赏
  • 举报
回复
牛哦,过来学习下!
lsq726 2010-08-05
  • 打赏
  • 举报
回复
拿分。。
看不懂。
zidane1000 2010-08-05
  • 打赏
  • 举报
回复
每天回帖即可获得10分可用分
yuntian666 2010-08-05
  • 打赏
  • 举报
回复
[Quote=引用 72 楼 gqqnb 的回复:]
FastPosChar是什么算法啊
[/Quote]
同问,FastPosChar是什么算法?
Reverse.King 2010-08-05
  • 打赏
  • 举报
回复
[Quote=引用 234 楼 smltq 的回复:]
引用 72 楼 gqqnb 的回复:
FastPosChar是什么算法啊

同问,FastPosChar是什么算法?
[/Quote]

FastPosChar 函数功能是从一串字串中查找一个字char返回该字的位置。
  • 打赏
  • 举报
回复
ni niu
Q287413288 2010-08-05
  • 打赏
  • 举报
回复



加载更多回复(204)

16,748

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧