Home | History | Annotate | Download | only in tests
      1 #include <assert.h>
      2 #include <stdlib.h>
      3 #include <string.h>
      4 
      5 #define SAT(x) (((x) < -32768) ? -32768 : (((x) > 32767) ? 32767 : (x)))
      6 
      7 static void test_pabsb_c(char *pDst, char *pSrc, int xmm)
      8 {
      9    int i;
     10 
     11    for ( i = 0; i < (8 << xmm); i++ )
     12       pDst[ i ] = pSrc[ i ] > 0 ? pSrc[i ] : -pSrc[ i ];
     13 }
     14 
     15 static void test_pabsw_c(short *pDst, short *pSrc, int xmm)
     16 {
     17    int i;
     18 
     19    for ( i = 0; i < (4 << xmm); i++ )
     20       pDst[ i ] = pSrc[ i ] > 0 ? pSrc[i ] : -pSrc[ i ];
     21 }
     22 
     23 static void test_pabsd_c(int *pDst, int *pSrc, int xmm)
     24 {
     25    int i;
     26 
     27    for ( i = 0; i < (2 << xmm); i++ )
     28       pDst[ i ] = pSrc[ i ] > 0 ? pSrc[i ] : -pSrc[ i ];
     29 }
     30 
     31 static void test_psignb_c(char *pDst, char *pSrc, int xmm)
     32 {
     33    int i;
     34 
     35    for ( i = 0; i < (8 << xmm); i++ )
     36       pDst[ i ] = pSrc[i] ? ( pSrc[ i ] >= 0 ? pDst[i ] : -pDst[ i ] ) : 0;
     37 }
     38 
     39 static void test_psignw_c(short *pDst, short *pSrc, int xmm)
     40 {
     41    int i;
     42 
     43    for ( i = 0; i < (4 << xmm); i++ )
     44       pDst[ i ] = pSrc[i] ? ( pSrc[ i ] >= 0 ? pDst[i ] : -pDst[ i ] ) : 0;
     45 }
     46 
     47 static void test_psignd_c(int *pDst, int *pSrc, int xmm)
     48 {
     49    int i;
     50 
     51    for ( i = 0; i < (2 << xmm); i++ )
     52       pDst[ i ] = pSrc[i] ? ( pSrc[ i ] >= 0 ? pDst[i ] : -pDst[ i ] ) : 0;
     53 }
     54 
     55 static void test_phaddw_c(unsigned short *pDst,unsigned short *pSrc, int xmm)
     56 {
     57    int i;
     58 
     59    for ( i = 0; i < (2 << xmm); i++ )
     60       pDst[ i ] = pDst[ i * 2 ] + pDst[ i * 2 + 1 ];
     61 
     62    for ( i = 0; i < (2 << xmm); i++ )
     63       pDst[ i + (2 << xmm) ] = pSrc[ i * 2 ] + pSrc[ i * 2 + 1 ];
     64 }
     65 
     66 static void test_phaddsw_c(short *pDst, short *pSrc, int xmm)
     67 {
     68    int i;
     69 
     70    for ( i = 0; i < (2 << xmm); i++ )
     71       pDst[ i ] = SAT( pDst[ i * 2 ] + pDst[ i * 2 + 1 ] );
     72 
     73    for ( i = 0; i < (2 << xmm); i++ )
     74       pDst[ i + (2 << xmm) ] = SAT( pSrc[ i * 2 ] + pSrc[ i * 2 + 1 ] );
     75 }
     76 
     77 static void test_phaddd_c(unsigned int *pDst, unsigned int *pSrc, int xmm)
     78 {
     79    int i;
     80 
     81    for ( i = 0; i < (1 << xmm); i++ )
     82       pDst[ i ] = pDst[ i * 2 ] + pDst[ i * 2 + 1 ];
     83 
     84    for ( i = 0; i < (1 << xmm); i++ )
     85       pDst[ i + (1 << xmm) ] = pSrc[ i * 2 ] + pSrc[ i * 2 + 1 ];
     86 }
     87 
     88 static void test_phsubw_c(unsigned short *pDst,unsigned short *pSrc, int xmm)
     89 {
     90    int i;
     91 
     92    for ( i = 0; i < (2 << xmm); i++ )
     93       pDst[ i ] = pDst[ i * 2 ] - pDst[ i * 2 + 1 ];
     94 
     95    for ( i = 0; i < (2 << xmm); i++ )
     96       pDst[ i + (2 << xmm) ] = pSrc[ i * 2 ] - pSrc[ i * 2 + 1 ];
     97 }
     98 
     99 static void test_phsubsw_c(short *pDst, short *pSrc, int xmm)
    100 {
    101    int i;
    102 
    103    for ( i = 0; i < (2 << xmm); i++ )
    104       pDst[ i ] = SAT( pDst[ i * 2 ] - pDst[ i * 2 + 1 ] );
    105 
    106    for ( i = 0; i < (2 << xmm); i++ )
    107       pDst[ i + (2 << xmm) ] = SAT( pSrc[ i * 2 ] - pSrc[ i * 2 + 1 ] );
    108 }
    109 
    110 static void test_phsubd_c(unsigned int *pDst, unsigned int *pSrc, int xmm)
    111 {
    112    int i;
    113 
    114    for ( i = 0; i < (1 << xmm); i++ )
    115       pDst[ i ] = pDst[ i * 2 ] - pDst[ i * 2 + 1 ];
    116 
    117    for ( i = 0; i < (1 << xmm); i++ )
    118       pDst[ i + (1 << xmm) ] = pSrc[ i * 2 ] - pSrc[ i * 2 + 1 ];
    119 }
    120 
    121 static void test_pmulhrsw_c(short *pDst, short *pSrc, int xmm)
    122 {
    123    int i;
    124 
    125    for ( i = 0; i < (4 << xmm); i++ )
    126    {
    127       int a = pSrc[ i ] * pDst[ i ];
    128          pDst[i] = (short)(((a >> 14) + 1) >> 1);
    129    }
    130 }
    131 
    132 static void test_pmaddubsw_c(unsigned char *pDst, signed char *pSrc, int xmm)
    133 {
    134    int i;
    135 
    136    for ( i = 0; i < (4 << xmm); i++ )
    137    {
    138       int a = pSrc[ 2 * i ] * pDst[ 2 * i ] + pSrc[ 2 * i + 1 ] * pDst[ 2 * i + 1];
    139          ((signed short *)pDst)[i] = SAT(a);
    140    }
    141 }
    142 
    143 static void test_pshufb_c(unsigned char *pDst, unsigned char *pSrc, int xmm)
    144 {
    145    unsigned char bla[16];
    146    int i;
    147 
    148    memcpy( bla, pDst, ( 8 << xmm ) );
    149 
    150    for ( i = 0; i < (8 << xmm); i++ )
    151       pDst[ i ] = (pSrc[ i ] >= 0x80) ? 0 : bla[ pSrc[ i ] & ((1 << (xmm + 3)) - 1) ];
    152 }
    153 
    154 static void test_palignr_c(unsigned char *pDst, unsigned char *pSrc, int xmm)
    155 {
    156    int i;
    157 
    158    for ( i = 0; i < 3; i++ )
    159       pDst[ i + (8 << xmm) - 3 ] = pDst[ i ];
    160 
    161    for ( i = 3; i < (8 << xmm); i++ )
    162       pDst[ i - 3 ] = pSrc[ i ];
    163 }
    164 
    165 static void randomize_args(unsigned char *pDst, unsigned char *pSrc)
    166 {
    167    int j;
    168    for ( j = 0; j < 16; j++ )
    169    {
    170       pDst[ j ] = rand() % 256;
    171       pSrc[ j ] = rand() % 256;
    172    }
    173 }
    174 
    175 #define CHECK_FUNCTION(instruction, extension, additionnal, pDst, pSrc) \
    176    do { \
    177       unsigned char temp_dst[16]; \
    178       unsigned char temp_src[16]; \
    179       randomize_args( pDst, pSrc ); \
    180       memcpy( temp_dst, pDst, 16 ); \
    181       memcpy( temp_src, pSrc, 16 ); \
    182       test_##instruction##_c( pDst, pSrc, additionnal ); \
    183       test_##instruction##_##extension( temp_dst, temp_src ); \
    184       assert( !memcmp( pDst, temp_dst, (8 << additionnal) ) ); \
    185    } while( 0 )
    186 
    187 #define CHECK_FUNCTIONS(instruction) \
    188    CHECK_FUNCTION(instruction, mmx, 0, pDst, pSrc); \
    189    CHECK_FUNCTION(instruction, xmm, 1, pDst, pSrc)
    190 
    191 
    192 void main(int nArgC, char *pArgv[])
    193 {
    194    void *pSrc = malloc(16);
    195    void *pDst = malloc(16);
    196    int nIter = atoi( pArgv[ 1 ] );
    197    int i;
    198 
    199    for ( i = 0; i < nIter; i++ )
    200    {
    201       CHECK_FUNCTIONS( psignb );
    202       CHECK_FUNCTIONS( psignw );
    203       CHECK_FUNCTIONS( psignd );
    204 
    205       CHECK_FUNCTIONS( pabsb );
    206       CHECK_FUNCTIONS( pabsw );
    207       CHECK_FUNCTIONS( pabsd );
    208 
    209       CHECK_FUNCTIONS( phaddw );
    210       CHECK_FUNCTIONS( phaddsw );
    211       CHECK_FUNCTIONS( phaddd );
    212 
    213       CHECK_FUNCTIONS( phsubw );
    214       CHECK_FUNCTIONS( phsubsw );
    215       CHECK_FUNCTIONS( phsubd );
    216 
    217       CHECK_FUNCTIONS( pmulhrsw );
    218          CHECK_FUNCTIONS( pmaddubsw );
    219 
    220          CHECK_FUNCTIONS( pshufb );
    221       CHECK_FUNCTIONS( palignr );
    222    }
    223 }