以下这个例子很简单,分别有两个双精度浮点类型的二维数组:double[512][1024]
,共4MB数据,分为512行1024列,每列位8个字节。现在将每对浮点数组的每一行的第一个元素相加,然后将结果存到一个在栈上分配的数组。
算法的汇编文件如下所示:
// asmtest.S
.align 2
.text
.globl _fast_int_sqrt
.globl _get_cycles
.globl _naive_calc
.globl _opt_calc
_get_cycles:
rdtsc
shl $32, %rdx
or %rdx, %rax
ret
_naive_calc:
mov $100, %r9
native_loop:
mov $512, %r8
xor %rcx, %rcx
xor %rax, %rax
naive_process:
fldl (%rsi, %rcx)
fldl (%rdx, %rcx)
fadd %st(0), %st(1)
fcmove %st(1), %st(0)
faddp %st(0), %st(1)
fstpl (%rdi, %rax)
add $(8 * 1024), %rcx
add $8, %rax
sub $1, %r8
jne naive_process
sub $1, %r9
jne native_loop
ret
_opt_calc:
mov $100, %r9
opt_loop:
mov $512, %r8
xor %rcx, %rcx
xor %rax, %rax
opt_process:
prefetcht0 (8 * 1024)(%rsi, %rcx)
prefetcht0 (8 * 1024)(%rdx, %rcx)
fldl (%rsi, %rcx)
fldl (%rdx, %rcx)
fadd %st(0), %st(1)
fcmove %st(1), %st(0)
faddp %st(0), %st(1)
fstpl (%rdi, %rax)
add $(8 * 1024), %rcx
add $8, %rax
sub $1, %r8
jne opt_process
sub $1, %r9
jne opt_loop
ret
上面可以看到 opt_calc 与 naive_calc 的唯一区别就是 opt_calc 多了两条 PREFETCH
指令。
下面是C语言的测试函数:
// main.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
extern unsigned long get_cycles(void);
// Calculate 2 * (src1 + src2)
extern void naive_calc(void *dst, const void *src1, const void *src2);
extern void opt_calc(void *dst, const void *src1, const void *src2);
extern void hey(int y);
void hey(int y)
{
// Initialize the buffers
void *src1 = malloc(8 * 1024 * 512);
void *src2 = malloc(8 * 1024 * 512);
double dst[512];
memset(src1, 0, 8 * 1024 * 512);
memset(src2, 0, 8 * 1024 * 512);
double *p = (double*)src1;
*p = 3.141;
p = (double*)src2;
*p = 2.163;
// naive
unsigned long t1 = get_cycles();
naive_calc(dst, src1, src2); // rdi, rsi, rdx
unsigned long t2 = get_cycles();
// opt
unsigned long t3 = get_cycles();
opt_calc(dst, src1, src2);
unsigned long t4 = get_cycles();
printf("Naive cycles is: %lu\n", t2 - t1);
printf("Opt cycles: %lu\n", t4 - t3);
printf("The value is: %f\n", dst[0]);
free(src1);
free(src2);
}
这里为了增加些计算周期,所以用了乘以2的方式。
俺在Mac Mini CPU为P7350,2.00GHz的双核基于45nm技术的Intel Core架构的处理器下测试,添加 opt_calc 的性能大约能增长8%到20%。