1/* { dg-do run } */
2/* { dg-require-effective-target ssse3 } */
3/* { dg-options "-O2 -fno-strict-aliasing -mssse3" } */
4
5#ifndef CHECK_H
6#define CHECK_H "ssse3-check.h"
7#endif
8
9#ifndef TEST
10#define TEST ssse3_test
11#endif
12
13#include CHECK_H
14
15#include "ssse3-vals.h"
16
17#include <tmmintrin.h>
18
19#ifndef __AVX__
20/* Test the 64-bit form */
21static void
22ssse3_test_phsubsw (int *i1, int *i2, int *r)
23{
24  __m64 t1 = *(__m64 *) i1;
25  __m64 t2 = *(__m64 *) i2;
26
27  *(__m64 *) r = _mm_hsubs_pi16 (t1, t2);
28
29  _mm_empty ();
30}
31#endif
32
33/* Test the 128-bit form */
34static void
35ssse3_test_phsubsw128 (int *i1, int *i2, int *r)
36{
37  /* Assumes incoming pointers are 16-byte aligned */
38  __m128i t1 = *(__m128i *) i1;
39  __m128i t2 = *(__m128i *) i2;
40  *(__m128i *) r = _mm_hsubs_epi16 (t1, t2);
41}
42
43static short
44signed_saturate_to_word (int x)
45{
46  if (x > (int )0x7fff)
47    return 0x7fff;
48
49  if (x < (int) 0xffff8000)
50    return 0x8000;
51
52  return (short)x;
53}
54
55/* Routine to manually compute the results */
56static void
57compute_correct_result (int *i1, int *i2, int *r)
58{
59  short *s1 = (short *) i1;
60  short *s2 = (short *) i2;
61  short *sout = (short *) r;
62  int i;
63
64  for (i = 0; i < 4; i++)
65    sout[i] = signed_saturate_to_word (s1[2 * i] - s1[2 * i + 1]);
66
67  for (i = 0; i < 4; i++)
68    sout[i + 4] = signed_saturate_to_word (s2[2 * i] - s2[2 * i + 1]);
69}
70
71static void
72TEST (void)
73{
74  int i;
75  int r [4] __attribute__ ((aligned(16)));
76  int ck [4];
77  int fail = 0;
78
79  for (i = 0; i < 256; i += 8)
80    {
81      /* Manually compute the result */
82      compute_correct_result (&vals[i + 0], &vals[i + 4], ck);
83
84#ifndef __AVX__
85      /* Run the 64-bit tests */
86      ssse3_test_phsubsw (&vals[i + 0], &vals[i + 2], &r[0]);
87      ssse3_test_phsubsw (&vals[i + 4], &vals[i + 6], &r[2]);
88      fail += chk_128 (ck, r);
89#endif
90
91      /* Run the 128-bit tests */
92      ssse3_test_phsubsw128 (&vals[i + 0], &vals[i + 4], r);
93      fail += chk_128 (ck, r);
94    }
95
96  if (fail != 0)
97    abort ();
98}
99