在博文(1)和(2)里分别用了4中方式写一个素数筛选的算法,分别是javascript in browser、node.js、ruby和c;最终的结果是c最快,node.js其次,js in b虽然也不慢,但极不稳定,所以排在第三,ruby最慢。
现在我们在linux64中用汇编语言重写sieve算法,看看动用最终的武器:汇编语言,我们能不能进一步优化素数筛选算法。
如果忘了算法逻辑,不要紧,下面分别再次贴出node.js、ruby以及c的sieve代码:
首先是node.js:
function sieve(n){
var a = new Int8Array(n+1);
var max = Math.floor(Math.sqrt(n));
var p = 2;
while(p <= max){
for(var i=2*p;i<=n;i+=p)
a[i] = 1;
while(a[++p]); /* empty */
}
while(a[n]) n--;
return n;
}
然后是ruby:
def sieve(n)
a = Array.new(n+1);
max = Math.sqrt(n).to_i;
p = 2;
while p<=max do
i = 2*p
while i<=n do
a[i] = 1
i+=p
end
while a[p+=1] == 1 do end
end
while a[n] do n-=1 end
n
end
最后是c的代码:
ULL sieve(ULL n)
{
char *a = malloc(n+1);
if(!a) return 0;
memset(a,0,n+1);
ULL max = sqrtl(n);
ULL p = 2;
while(p <= max){
for(ULL i=2*p;i<=n;i+=p)
a[i] = 1;
while(a[++p]); /* empty */
}
while(a[n]) n--;
return n;
}
下面尝试用汇编重写sieve函数,需要注意的几点是:
要说明的是该段代码肯定还可以进一步优化,但本猫就到这里为止了,希望能够抛砖引玉。先把结果说一下吧:用汇编写的sieve版本是最快的,超过了c代码,在本猫 Intel(R) Core(TM)2 Duo CPU T7100 @ 1.80GHz上跑出了最快的37xx毫秒,比c版的平均要快100-200毫秒,而且非常稳定。
最后贴出C的main.c和汇编的sieve.s代码:
main.c:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
typedef unsigned long long ULL;
ULL sieve(ULL n);
int main(int argc,char **argv){
ULL n = 0;
if(argc < 2){
printf("usage %s n\n",argv[0]);
return 1;
}
sscanf(argv[1],"%llu",&n);
if(n == 0){
puts("wrong number format");
return 2;
}
else if(n < 0){
puts("must + number");
return 3;
}
int start = clock();
ULL result = sieve(n);
if(result == -1){
puts("sieve calc failed!");
return 4;
}
double end = ((1.0 * (clock() - start)) / CLOCKS_PER_SEC) * 1000.0;
printf("max p is %llu (take %f ms)\n",result,end);
return 0;
}
汇编的sieve.s:
section .data
n:dq 0
len:dq 0
addr: dq 0
p:dq 2
max:dq 0
i:dq 2
section .text
global sieve
sieve:
push rbp
push rbx
push rcx
mov rbp,rsp
mov [n],rdi ;save 1st arg to n
inc rdi
mov [len],rdi ;mmap len = n + 1
mov eax,9 ;call syscall mmap
mov rdi,0
mov rsi,[len]
mov rdx,3
mov r10,33
mov r8,-1
mov r9,0
syscall
cmp rax,0xfffffffffffff001 ;mmap error
jb next
mov rax,-1 ;return -1
jmp quit
next: ;save mmap return addr
;FIXME:mmap space always 0 ???
fild qword [n] ;calc sqrt(n) and save result to max
fsqrt
fistp qword [max]
mov r15,[p] ;r15 = p
mov r14,[max] ;r14 = max
mov r13,[n] ;r13 = n
mov r12,[i] ;r12 = i
enter_while:
cmp r15,r14 ;if p<=max
ja quit_while
mov rbx,r15
shl rbx,1
mov r12,rbx
enter_for:
cmp r12,r13
ja quit_for
mov byte [rax + r12],1
add r12,r15
jmp enter_for
quit_for:
inc r15
mov cl,byte [rax + r15]
test cl,cl
jnz quit_for
jmp enter_while
quit_while:
mov cl,byte [rax + r13]
test cl,cl
jz pre_quit
dec r13
jmp quit_while
pre_quit:
mov rax,r13
quit:
mov rsp,rbp
pop rcx
pop rbx
pop rbp
ret