#include <stdlib.h>
#include <stdio.h>
#include <ppu_intrinsics.h>

extern void *memcpy(void *, const void *, size_t);

int main() {
	unsigned char *s, *d;
	register unsigned long long start, finish,  i;
	if(posix_memalign((void *)&s, 128, 16384)) {
		printf("failed\n");
		goto end;
	}
	for(i = 128, j = 0; i > 0; i--, j += 128) {		/* clear s from cache */
		__dcbf(s+j);
	}
	if(posix_memalign((void *)&d, 128, 16384)) {
		printf("failed\n");
		goto end;
	}
	for(i = 128, j = 0; i > 0; i--, j += 128) {		/* clear d from cache */
		__dcbf(s+j);
	}

	__dcbt(memcpy);		/* prefetch memcpy */
	__dcbt(memcpy+128);
	__dcbt(memcpy+256);
	__dcbt(memcpy+384);
	__dcbt(memcpy+512);
	__dcbt(memcpy+640);
	__dcbt(memcpy+768);
	__dcbt(memcpy+896);
	__dcbt(memcpy+1024);
	__dcbt(memcpy+1152);
	__dcbt(memcpy+1280);
	__dcbt(memcpy+1408);
	__dcbt(memcpy+1536);
	__dcbt(memcpy+1664);
	__dcbt(memcpy+1792);
	__dcbt(memcpy+1920);
	__dcbt(memcpy+2048);
	__dcbt(memcpy+2176);
	__dcbt(memcpy+2304);
	__dcbt(memcpy+2432);
	__sync();

	start = __mftb();
	memcpy(d, s, 4096);
	finish = __mftb();

	printf("%lld\n", finish - start);

	free(s);
	free(d);
end:
	return 0;
}
