/*
 * Tickle the new erratum 16, whereby the result of a cfaddd is picked up wrong by cfstrd
 * if the cfstrd occurs in exactly the moment when the result is(n't!) ready.
 */

/* Invent a sum that will leave something interesting in both 32-bit halves of the result */
volatile double a = 42.0, b = 4200000000.0, c = 0;

/* Do c = a * b
 * The time between the add and the result being taken by cfstrd is critical
 * so ensure there are no wait states involved by preloading all arguments in advance.
 */
void
erratum16()
{
	register volatile double *ap asm("r0") = &a;
	register volatile double *bp asm("r1") = &b;
	register volatile double *cp asm("r2") = &c;
	/* We don't bother declaring the maverick registers as mvd0-8 are function scratch
	 * registers and nothing else should use them */

	asm("cfldrd	mvd0, [r0]" : : "r"(ap));
	asm("cfldrd	mvd1, [r1]" : : "r"(bp));
	/* wait until they are definitely in the registers: 16 ARM cycles */
	asm("nop;nop;nop;nop;nop;nop;nop;nop");
	asm("nop;nop;nop;nop;nop;nop;nop;nop");
	asm("cfaddd	mvd2, mvd1, mvd0");
	asm("nop;nop;nop;nop;nop;nop;nop;nop");		/* The critical delay */
	asm("cfstrd	mvd2, [r2]" : : "r"(cp) );
	asm("nop;nop;nop;nop");			/* Just for luck */
}

#include <stdio.h>

main()
{
	erratum16();

	exit (c != 4200000042.0);	/* 0 in success, 1 on failure *.
}
