/*
 * Test code generation for 64-bit shifts by a number of places
 * specified by a variable.
 *
 * Fails on Maverick for ASL with shift counts 32-64 (max is 31)
 * Fails on x86 for shift counts of 64.
 * There is no Cirrus instruction coded for arithmetic (sign-extending)
 * right shifts, either by a constant or by a register. It may be that the
 * UI bit in the status word affects this. Even if it does, it's expensive to
 * change that bit on the fly, as doing so requires clobbering one maverick
 * and one ARM register.
 */

/* Which tests do you want? */
//#define ASL
//#define LSR
#define ASR

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

typedef unsigned long long ui64;
typedef long long i64;

/* Use patterns with top bit set to verify sign extension in ASR */
#define UI64  ((ui64) 0xdeadb0d101234567ULL)
#define  I64  ((i64) 0xdeadb0d101234567LL)

#ifdef ASL
const ui64 left[65] = {
	UI64 << 0, UI64 << 1, UI64 << 2, UI64 << 3, UI64 << 4,
	UI64 << 5, UI64 << 6, UI64 << 7, UI64 << 8, UI64 << 9,
	UI64 << 10, UI64 << 11, UI64 << 12, UI64 << 13, UI64 << 14,
	UI64 << 15, UI64 << 16, UI64 << 17, UI64 << 18, UI64 << 19,
	UI64 << 20, UI64 << 21, UI64 << 22, UI64 << 23, UI64 << 24,
	UI64 << 25, UI64 << 26, UI64 << 27, UI64 << 28, UI64 << 29,
	UI64 << 30, UI64 << 31, UI64 << 32, UI64 << 33, UI64 << 34,
	UI64 << 35, UI64 << 36, UI64 << 37, UI64 << 38, UI64 << 39,
	UI64 << 40, UI64 << 41, UI64 << 42, UI64 << 43, UI64 << 44,
	UI64 << 45, UI64 << 46, UI64 << 47, UI64 << 48, UI64 << 49,
	UI64 << 50, UI64 << 51, UI64 << 52, UI64 << 53, UI64 << 54,
	UI64 << 55, UI64 << 56, UI64 << 57, UI64 << 58, UI64 << 59,
	UI64 << 60, UI64 << 61, UI64 << 62, UI64 << 63, UI64 << 64,
};
#endif

#ifdef LSR
const ui64 lright[65] = {
	UI64 >> 0, UI64 >> 1, UI64 >> 2, UI64 >> 3, UI64 >> 4,
	UI64 >> 5, UI64 >> 6, UI64 >> 7, UI64 >> 8, UI64 >> 9,
	UI64 >> 10, UI64 >> 11, UI64 >> 12, UI64 >> 13, UI64 >> 14,
	UI64 >> 15, UI64 >> 16, UI64 >> 17, UI64 >> 18, UI64 >> 19,
	UI64 >> 20, UI64 >> 21, UI64 >> 22, UI64 >> 23, UI64 >> 24,
	UI64 >> 25, UI64 >> 26, UI64 >> 27, UI64 >> 28, UI64 >> 29,
	UI64 >> 30, UI64 >> 31, UI64 >> 32, UI64 >> 33, UI64 >> 34,
	UI64 >> 35, UI64 >> 36, UI64 >> 37, UI64 >> 38, UI64 >> 39,
	UI64 >> 40, UI64 >> 41, UI64 >> 42, UI64 >> 43, UI64 >> 44,
	UI64 >> 45, UI64 >> 46, UI64 >> 47, UI64 >> 48, UI64 >> 49,
	UI64 >> 50, UI64 >> 51, UI64 >> 52, UI64 >> 53, UI64 >> 54,
	UI64 >> 55, UI64 >> 56, UI64 >> 57, UI64 >> 58, UI64 >> 59,
	UI64 >> 60, UI64 >> 61, UI64 >> 62, UI64 >> 63, UI64 >> 64,
};
#endif

#ifdef ASR
const i64 aright[65] = {
	I64 >> 0, I64 >> 1, I64 >> 2, I64 >> 3, I64 >> 4,
	I64 >> 5, I64 >> 6, I64 >> 7, I64 >> 8, I64 >> 9,
	I64 >> 10, I64 >> 11, I64 >> 12, I64 >> 13, I64 >> 14,
	I64 >> 15, I64 >> 16, I64 >> 17, I64 >> 18, I64 >> 19,
	I64 >> 20, I64 >> 21, I64 >> 22, I64 >> 23, I64 >> 24,
	I64 >> 25, I64 >> 26, I64 >> 27, I64 >> 28, I64 >> 29,
	I64 >> 30, I64 >> 31, I64 >> 32, I64 >> 33, I64 >> 34,
	I64 >> 35, I64 >> 36, I64 >> 37, I64 >> 38, I64 >> 39,
	I64 >> 40, I64 >> 41, I64 >> 42, I64 >> 43, I64 >> 44,
	I64 >> 45, I64 >> 46, I64 >> 47, I64 >> 48, I64 >> 49,
	I64 >> 50, I64 >> 51, I64 >> 52, I64 >> 53, I64 >> 54,
	I64 >> 55, I64 >> 56, I64 >> 57, I64 >> 58, I64 >> 59,
	I64 >> 60, I64 >> 61, I64 >> 62, I64 >> 63, I64 >> 64,
};
#endif

extern uitestasl(ui64 v);
extern uitestlsr(ui64 v);
extern itestasr(i64 v);

main()
{
	uitestasl(UI64);
	uitestlsr(UI64);
	itestasr(I64);
	exit(0);
}

uitestasl(ui64 va)
{
#ifdef ASL
	register int i;
	register ui64 v
#ifdef __MAVERICK__
			asm("mvdx0")
#endif
				    = va;
	/* test 64-bit left shifts by a variable amount */
	for (i=0; i<=64; i++)
	  if (v << i != left[i])
	    printf("ASL failed at %02d: 0x%016llx should be 0x%016llx\n", i, v<<i, left[i]);
#endif
}

uitestlsr(ui64 va)
{
#ifdef LSR
	register int i;
	register ui64 v
#ifdef __MAVERICK__
			asm("mvdx0")
#endif
				    = va;

	/* test 64-bit logical right shifts by a variable amount */
	for (i=0; i<=64; i++)
	  if (v >> i != lright[i])
	    printf("LSR failed at %02d: 0x%016llx should be 0x%016llx\n", i, v>>i, lright[i]);
#endif
}

itestasr(i64 va)
{
#ifdef ASR
	register int i;
	register i64 v
#ifdef __MAVERICK__
			asm("mvdx0")
#endif
				    = va;

	/* test 64-bit arithmetic right shifts by a variable amount */
	for (i=0; i<=64; i++)
	  if (v >> i != aright[i])
	    printf("ASR failed at %02d: 0x%016llx should be 0x%016llx\n", i, v>>i, aright[i]);
#endif
}
