aboutsummaryrefslogtreecommitdiff
path: root/modules/arch/x86/tests/ssse3.c
blob: 419a12711ef8977e70e4a0da5a89609e6bbb22b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include <assert.h>
#include <stdlib.h>
#include <string.h>

#define SAT(x) (((x) < -32768) ? -32768 : (((x) > 32767) ? 32767 : (x)))

static void test_pabsb_c(char *pDst, char *pSrc, int xmm)
{
   int i;

   for ( i = 0; i < (8 << xmm); i++ )
      pDst[ i ] = pSrc[ i ] > 0 ? pSrc[i ] : -pSrc[ i ];
}

static void test_pabsw_c(short *pDst, short *pSrc, int xmm)
{
   int i;

   for ( i = 0; i < (4 << xmm); i++ )
      pDst[ i ] = pSrc[ i ] > 0 ? pSrc[i ] : -pSrc[ i ];
}

static void test_pabsd_c(int *pDst, int *pSrc, int xmm)
{
   int i;

   for ( i = 0; i < (2 << xmm); i++ )
      pDst[ i ] = pSrc[ i ] > 0 ? pSrc[i ] : -pSrc[ i ];
}

static void test_psignb_c(char *pDst, char *pSrc, int xmm)
{
   int i;

   for ( i = 0; i < (8 << xmm); i++ )
      pDst[ i ] = pSrc[i] ? ( pSrc[ i ] >= 0 ? pDst[i ] : -pDst[ i ] ) : 0;
}

static void test_psignw_c(short *pDst, short *pSrc, int xmm)
{
   int i;

   for ( i = 0; i < (4 << xmm); i++ )
      pDst[ i ] = pSrc[i] ? ( pSrc[ i ] >= 0 ? pDst[i ] : -pDst[ i ] ) : 0;
}

static void test_psignd_c(int *pDst, int *pSrc, int xmm)
{
   int i;

   for ( i = 0; i < (2 << xmm); i++ )
      pDst[ i ] = pSrc[i] ? ( pSrc[ i ] >= 0 ? pDst[i ] : -pDst[ i ] ) : 0;
}

static void test_phaddw_c(unsigned short *pDst,unsigned short *pSrc, int xmm)
{
   int i;

   for ( i = 0; i < (2 << xmm); i++ )
      pDst[ i ] = pDst[ i * 2 ] + pDst[ i * 2 + 1 ];

   for ( i = 0; i < (2 << xmm); i++ )
      pDst[ i + (2 << xmm) ] = pSrc[ i * 2 ] + pSrc[ i * 2 + 1 ];
}

static void test_phaddsw_c(short *pDst, short *pSrc, int xmm)
{
   int i;

   for ( i = 0; i < (2 << xmm); i++ )
      pDst[ i ] = SAT( pDst[ i * 2 ] + pDst[ i * 2 + 1 ] );

   for ( i = 0; i < (2 << xmm); i++ )
      pDst[ i + (2 << xmm) ] = SAT( pSrc[ i * 2 ] + pSrc[ i * 2 + 1 ] );
}

static void test_phaddd_c(unsigned int *pDst, unsigned int *pSrc, int xmm)
{
   int i;

   for ( i = 0; i < (1 << xmm); i++ )
      pDst[ i ] = pDst[ i * 2 ] + pDst[ i * 2 + 1 ];

   for ( i = 0; i < (1 << xmm); i++ )
      pDst[ i + (1 << xmm) ] = pSrc[ i * 2 ] + pSrc[ i * 2 + 1 ];
}

static void test_phsubw_c(unsigned short *pDst,unsigned short *pSrc, int xmm)
{
   int i;

   for ( i = 0; i < (2 << xmm); i++ )
      pDst[ i ] = pDst[ i * 2 ] - pDst[ i * 2 + 1 ];

   for ( i = 0; i < (2 << xmm); i++ )
      pDst[ i + (2 << xmm) ] = pSrc[ i * 2 ] - pSrc[ i * 2 + 1 ];
}

static void test_phsubsw_c(short *pDst, short *pSrc, int xmm)
{
   int i;

   for ( i = 0; i < (2 << xmm); i++ )
      pDst[ i ] = SAT( pDst[ i * 2 ] - pDst[ i * 2 + 1 ] );

   for ( i = 0; i < (2 << xmm); i++ )
      pDst[ i + (2 << xmm) ] = SAT( pSrc[ i * 2 ] - pSrc[ i * 2 + 1 ] );
}

static void test_phsubd_c(unsigned int *pDst, unsigned int *pSrc, int xmm)
{
   int i;

   for ( i = 0; i < (1 << xmm); i++ )
      pDst[ i ] = pDst[ i * 2 ] - pDst[ i * 2 + 1 ];

   for ( i = 0; i < (1 << xmm); i++ )
      pDst[ i + (1 << xmm) ] = pSrc[ i * 2 ] - pSrc[ i * 2 + 1 ];
}

static void test_pmulhrsw_c(short *pDst, short *pSrc, int xmm)
{
   int i;

   for ( i = 0; i < (4 << xmm); i++ )
   {
      int a = pSrc[ i ] * pDst[ i ];
         pDst[i] = (short)(((a >> 14) + 1) >> 1);
   }
}

static void test_pmaddubsw_c(unsigned char *pDst, signed char *pSrc, int xmm)
{
   int i;

   for ( i = 0; i < (4 << xmm); i++ )
   {
      int a = pSrc[ 2 * i ] * pDst[ 2 * i ] + pSrc[ 2 * i + 1 ] * pDst[ 2 * i + 1];
         ((signed short *)pDst)[i] = SAT(a);
   }
}

static void test_pshufb_c(unsigned char *pDst, unsigned char *pSrc, int xmm)
{
   unsigned char bla[16];
   int i;

   memcpy( bla, pDst, ( 8 << xmm ) );

   for ( i = 0; i < (8 << xmm); i++ )
      pDst[ i ] = (pSrc[ i ] >= 0x80) ? 0 : bla[ pSrc[ i ] & ((1 << (xmm + 3)) - 1) ];
}

static void test_palignr_c(unsigned char *pDst, unsigned char *pSrc, int xmm)
{
   int i;

   for ( i = 0; i < 3; i++ )
      pDst[ i + (8 << xmm) - 3 ] = pDst[ i ];

   for ( i = 3; i < (8 << xmm); i++ )
      pDst[ i - 3 ] = pSrc[ i ];
}

static void randomize_args(unsigned char *pDst, unsigned char *pSrc)
{
   int j; 
   for ( j = 0; j < 16; j++ )
   {
      pDst[ j ] = rand() % 256;
      pSrc[ j ] = rand() % 256;
   }
}

#define CHECK_FUNCTION(instruction, extension, additionnal, pDst, pSrc) \
   do { \
      unsigned char temp_dst[16]; \
      unsigned char temp_src[16]; \
      randomize_args( pDst, pSrc ); \
      memcpy( temp_dst, pDst, 16 ); \
      memcpy( temp_src, pSrc, 16 ); \
      test_##instruction##_c( pDst, pSrc, additionnal ); \
      test_##instruction##_##extension( temp_dst, temp_src ); \
      assert( !memcmp( pDst, temp_dst, (8 << additionnal) ) ); \
   } while( 0 )

#define CHECK_FUNCTIONS(instruction) \
   CHECK_FUNCTION(instruction, mmx, 0, pDst, pSrc); \
   CHECK_FUNCTION(instruction, xmm, 1, pDst, pSrc)

      
void main(int nArgC, char *pArgv[])
{
   void *pSrc = malloc(16);
   void *pDst = malloc(16);
   int nIter = atoi( pArgv[ 1 ] );
   int i;

   for ( i = 0; i < nIter; i++ )
   {
      CHECK_FUNCTIONS( psignb );
      CHECK_FUNCTIONS( psignw );
      CHECK_FUNCTIONS( psignd );

      CHECK_FUNCTIONS( pabsb );
      CHECK_FUNCTIONS( pabsw );
      CHECK_FUNCTIONS( pabsd );

      CHECK_FUNCTIONS( phaddw );
      CHECK_FUNCTIONS( phaddsw );
      CHECK_FUNCTIONS( phaddd );

      CHECK_FUNCTIONS( phsubw );
      CHECK_FUNCTIONS( phsubsw );
      CHECK_FUNCTIONS( phsubd );

      CHECK_FUNCTIONS( pmulhrsw );
         CHECK_FUNCTIONS( pmaddubsw );
      
         CHECK_FUNCTIONS( pshufb );
      CHECK_FUNCTIONS( palignr );
   }
}