/*
 * Test asm code for 64-bit shifts by a number of places
 * specified by a variable.
 */

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

typedef unsigned long long ui64;
typedef long long i64;


/* Choose implementation to test/measure */
#ifdef __MAVERICK__
#define asl64(v,s) asl64_1(v,s)
#define lsr64(v,s) lsr64_1(v,s)
#define asr64(v,s) asr64_1(v,s)
#else
#define asl64(v,s) asl64_0(v,s)
#define lsr64(v,s) lsr64_0(v,s)
#define asr64(v,s) asr64_0(v,s)
#endif

#ifndef __MAVERICK__

ui64  /* When compiled without -mcirrus-di this takes 0.000 000 083s */
asl64_0(ui64 value, int shift)
{
	return value << shift;
}

#else

ui64 	/* This works and takes 0.000 000 06s */
asl64_1(ui64 value, int shift)
{
	register ui64 mvd1 asm("mvd1") = value; /* value to shift */
	register ui64 mvd0 asm("mvd0");	 	/* the result */
	register int r2 asm("r2") = shift;	/* how many bits to shift by */
	register int r3 asm("r3");		/* tmp */

	/* The following works from 0 to 64 places as required by C */
	// First, do (shift_count & 31) places
	asm("and	r3, r2, #31"	  : "=r"(r3)	: "r"(r2) );
	asm("cfrshl64	mvdx1, mvdx0, r3" : "=v"(mvd0)	: "v"(mvd1),"r"(r3) );
	asm("nop");							// Avoid erratum 2
	// Special case: When shift count == 64, result is 0
	asm("cmp	r2,#64"		  : 		: "r"(r2) );	// If arg >= 64
	asm("cfsub64ge	mvdx0, mvdx0, mvdx0": "=v"(mvd0));		// result == 0.
	// Now do the other 32 bit shift if the arg was 32..63
	asm("ands	r3, r2, #32"	  : "=r"(r3) : "r"(r2) );
	asm("cfsh64ne	mvdx0, mvdx0, #16": "=v"(mvd0)	: "v"(mvd0) );
	asm("cfsh64ne	mvdx0, mvdx0, #16": "=v"(mvd0)	: "v"(mvd0) );

	return mvd0;
}

ui64 	/* This works and takes 0.000 000 08s */
asl64_2(ui64 value, int shift)
{
	register volatile ui64 mvd1 asm("mvd1") = value; /* value to shift */
	register volatile ui64 mvd0 asm("mvd0");	 /* the result */
	register volatile int r2 asm("r2") = shift; // how many bits to shift by
	int shift31;	    /* shift31 */
	int shift62;	    /* shift62 */
	register volatile int resid asm("r3");	// shift mod 31

	/* Stragety: we want to end up performing:
	 * 0-31 places:		cfrshl64  v1, v0, r2	; v0=v1<<r2
	 *
	 * 32-62 places:	cfrshl64  v1, v0, r2-31
	 *			cfsh64	  v0, v0, #31
	 *
	 * 63 places:		cfrshl64  v1, v0, r2-62
	 *			cfsh64	  v0, v0, #31
	 *			cfsh64	  v0, v0, #31
	 *
	 *	resid = shift;
	 *	if (shift > 31) resid -= 31;
	 *	if (shift > 62) resid -= 31;
	 *	v0 = v1 << resid;
	 *	if (shift > 31) v1 <<= 31;
	 *	if (shift > 62) v1 <<= 31;
	 */
	
	asm("mov	r3, r2");	// resid = shift
	asm("cmp	r2, #31");
	asm("subgt	r3, r3, #31");
	asm("cmp	r2, #62");
	asm("subgt	r3, r3, #31");
	asm("cfrshl64	mvdx1, mvdx0, r3"); // shift by shift % 31
	asm("cmp	r2, #62");
	asm("cfsh64gt	mvdx0, mvdx0, #31");
	asm("cmp	r2, #31");
	asm("cmp	r2, #31");
	asm("cfsh64gt	mvdx0, mvdx0, #31");

	return mvd0;
}

#endif // __MAVERICK__

#ifndef __MAVERICK__

i64  /* When compiled without -mcirrus-di this takes 0.000 000 s */
asr64_0(i64 value, int shift)
{
	return value >> shift;
}

#else

// The crunch always does sign extension.
// Even if you set the Usigned Integer bit in DSPSP.
ui64
asr64_1(ui64 value, int shift)
{
	register volatile ui64 mvd1 asm("mvd1") = value; /* value to shift */
	register volatile ui64 mvd0 asm("mvd0");	 /* the result */
	register volatile int r2 asm("r2") = shift; // how many bits to shift by
	register volatile int r3 asm("r3");	    /* scratch */
	
	asm("negs	r3, r2");		//  1..32 -> -1..-32, 0->0
	asm("orrne	r3, #32");		// -64..-33 -> -32..-1, 0->0
	asm("cfrshl64	mvdx1, mvdx0, r3");	// writes to mvd0
	asm("nop");				// Avoid erratum 2.
	asm("cmp	r2, #32");		// If shift_count >= 32
	asm("cfsh64gt	mvdx0, mvdx0, #-32");	// do the other 32 bits

	return mvd0;
}

#endif // __MAVERICK__

#ifndef __MAVERICK__
ui64
lsr64_0(ui64 value, int shift)
{
	return value >> shift;
}

#else

/* The crunch always does sign extension, even if you set the UI bit in DSPSP,
 * so this doesn't work.
 * It looks like we'll have to shift it by one place, clear the top bit,
 * then do the other N-1 places.  Quite how to do that with the limited
 * instruction available...
	asm("cfsub64	mvdx2, mvdx2, mvdx2");	// mvdx0 = 0
	asm("cfcmp64	r15, mvdx1, mvdx2");	// if value == 0 {
	asm("cfsh64eq	mvdx0, mvdx2, #0");	//   result = 0;
	asm("beq	out");			//   return

	asm("cfsh64	mvdx2, mvdx1, #-1");
	asm("cfcmp64	r15, mvdx2, mvdx1");
	asm("bvs	noclear");
	// Clear top bit of value>>1
	asm(".......
	// Subtract one from shift count
 */
ui64
lsr64_1(ui64 value, int shift)
{
	register volatile ui64 mvd1 asm("mvd1") = value; /* value to shift */
	register volatile ui64 mvd0 asm("mvd0");	 /* the result */
	register volatile ui64 mvd2 asm("mvd2");	 /* scratch */
	register volatile int r2 asm("r2") = shift; // how many bits to shift by
	register volatile int r3 asm("r3");	    /* tmp */

	// TODO: If the top bit is set, do something else

	// The shift proper
	asm("negs	r3, r2");		// 1..32 -> -1..-32, 0->0
	asm("orrne	r3, #32");		// -64..-33 -> -32..-1, 0->0
	asm("cfrshl64	mvdx1, mvdx0, r3");	// writes to mvd0
	asm("nop");				// Avoid erratum 2
	asm("cmp	r2, #32");		// if shift_count >= 32
	asm("cfsh64gt	mvdx0, mvdx0, #-32");	// do the other 32 bits

	return mvd0;
}

#endif


/* Test harness */

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

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,
};

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,
};

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,
};

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

uitestlsr(ui64 va)
{
	register int i;
	register ui64 v;

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

itestasr(i64 va)
{
	register int i;
	register i64 v;

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

main(int argc, char **argv)
{
	if (argc == 2) {
		/* Speed test */
		ui64 total = argc;
		unsigned niter = atol(argv[1]);
		unsigned i;
		for (i=niter; i>0; i--) {
			total = asl64(total, (i&63));
		}
		return(total);
	} else {
		/* Correctness test */
		uitestasl(UI64);
		//uitestlsr(UI64);	// fails
		itestasr(I64);
	}
	exit(0);
}
