#include <stdio.h>


#include "stdtyp/stdint.h"


/****************************************************************************************/

#define BREAKPOINT asm("   ESTOP0")



/****************************************************************************************/
void Test_abs16_sat( void );

void Test_byte( void );

void Test_norm32( void );



/****************************************************************************************/
void main(void) {
    printf("\n\n\nTEST START\n");


#if 0
    Test_abs16_sat();
#endif

#if 0
    Test_byte();
#endif

#if 1
    Test_norm32();
    // Does not work on compiler version 6.1???
#endif


    for(;;)
    {
        BREAKPOINT;
    }
}

/****************************************************************************************/
void Test_abs16_sat( void )
{
	volatile int16_t a, b, c;
	int16_t i;

	const int16_t vals[] = { -13, 13, INT16_MIN, INT16_MAX };


    BREAKPOINT;
	for( i = 0 ; i < (sizeof(vals)/sizeof(vals[0])) ; i++ )
	{
	    a = vals[i];
	    b = __abs16_sat( a );
	    c = abs(a);
	    printf("__abs16_sat(%6d) => %6d \t abs(%6d) => %6d\n", a, b, a, c );
	}



    /* ==== OUTPUT ====
	    [C28xx] __abs16_sat(   -13) =>     13    abs(   -13) =>     13
        [C28xx] __abs16_sat(    13) =>     13    abs(    13) =>     13
        [C28xx] __abs16_sat(-32768) =>  32767    abs(-32768) => -32768
        [C28xx] __abs16_sat( 32767) =>  32767    abs( 32767) =>  32767
	 */

}
/****************************************************************************************/
void Test_byte( void )
{
    int16_t i;
    int16_t destAry[8];

    int16_t ary[] = {0x1122, 0x3344, 0x5566};
    int16_t aryBytes = 2 * (sizeof(ary) / sizeof(ary[0]));

    BREAKPOINT;
    for( i = 0; i < aryBytes; i++ )
    {
        volatile uint16_t byt;

        byt = __byte( ary, i );
        printf("__byte( ary, %d ) => 0x%04X\n", i, byt );
    }

    /* ==== OUTPUT ====
    [C28xx] __byte( ary, 0 ) => 0x  22
    [C28xx] __byte( ary, 1 ) => 0x  11
    [C28xx] __byte( ary, 2 ) => 0x  44
    [C28xx] __byte( ary, 3 ) => 0x  33
    [C28xx] __byte( ary, 4 ) => 0x  66
    [C28xx] __byte( ary, 5 ) => 0x  55
     */

    for( i = 0; i < 8*2; i++ )
    {
        __byte( destAry, i ) = i;
    }

    for( i = 0; i < 8; i++ )
    {
        printf("destAry[%i] => 0x%04X\n", i, destAry[i] );
    }

    /* ==== OUTPUT ====
        [C28xx] destAry[0] => 0x0100
        [C28xx] destAry[1] => 0x0302
        [C28xx] destAry[2] => 0x0504
        [C28xx] destAry[3] => 0x0706
        [C28xx] destAry[4] => 0x0908
        [C28xx] destAry[5] => 0x0B0A
        [C28xx] destAry[6] => 0x0D0C
        [C28xx] destAry[7] => 0x0F0E
     */


}



/****************************************************************************************/
void Test_norm32( void )
{
    int32_t  ary[] = { 1, -1, (3L<<20), (15L<<16) };
    int16_t  aryLen = sizeof(ary)/sizeof(ary[0]);
    int16_t  i;


    int32_t src;
    int32_t dst;
    int16_t shift;

    BREAKPOINT;
    printf("Test_norm32\n");
    for( i = 0; i < aryLen; i++ )
    {

        src = ary[i];

        BREAKPOINT;
        dst = __norm32( src , &shift );
        BREAKPOINT;


        printf("__norm32( 0x%08lx , &shift ) => 0x%08lx, [shift] => %d\n",
               src, dst, shift );
    }


}





