#include <stdio.h>
unsigned long long rdtsc(void)
{
unsigned long long tmp;
__asm__ volatile("rdtsc;":"=A"(tmp));
return tmp;
}
#define N 1000000
void sse_f(void)
{
volatile float __attribute__((aligned(16))) a[4] = {
123.456,
123.456,
123.456,
123.456
};
int i = N / 4;
while(i-->0) {
__asm__ volatile(
"movaps (%0), %%xmm0;"
"addps %%xmm0, %%xmm0;"
"mulps %%xmm0, %%xmm0;"
"subps %%xmm0, %%xmm0;"
"divps %%xmm0, %%xmm0;"
"movaps %%xmm0, (%0);"
:
:"r"(a)
:"memory"
);
}
}
void sse_d(void)
{
volatile double __attribute__((aligned(16))) a[2] = {
123.456,
123.456
};
int i = N / 2;
while(i-->0) {
__asm__ volatile(
"movdqa (%0), %%xmm0;"
"addpd %%xmm0, %%xmm0;"
"mulpd %%xmm0, %%xmm0;"
"subpd %%xmm0, %%xmm0;"
"divpd %%xmm0, %%xmm0;"
"movdqa %%xmm0, (%0);"
:
:"r"(a)
:"memory"
);
}
}
void f(void)
{
int i = N;
volatile float a=123.456;
while(i-->0) {a+=a; a*=a; a-=a; a/=a;}
}
void d(void)
{
int i=N;
volatile double a=123.456;
while(i-->0) {a+=a; a*=a; a-=a; a/=a;}
}
typedef void (*func)(void);
void C(func f, char* str)
{
unsigned long long n = rdtsc();
f();
printf("%s\t:\t%llu\n", str, rdtsc() - n);
}
void main()
{
int i = 3;
while(i-->0) {
C(f, "fpu float");
C(d, "fpu double");
C(sse_f, "sse float");
C(sse_d, "sse double");
putchar('\n');
}
}