1 /* { dg-do run } */
2 /* { dg-require-effective-target sse4 } */
3 /* { dg-options "-O2 -msse4.1" } */
4 /* { dg-skip-if "no M_PI" { vxworks_kernel } } */
5
6 #include "sse4_1-check.h"
7
8 #include <smmintrin.h>
9 #include <math.h>
10 #include <string.h>
11
12 #define NUM 64
13
14 static void
init_round(double * src)15 init_round (double *src)
16 {
17 int i, sign = 1;
18 double d = rand ();
19
20 for (i = 0; i < NUM; i++)
21 {
22 src[i] = (i + 1)* d * M_PI * sign;
23 if (i < (NUM / 2))
24 {
25 if ((i % 6) == 0)
26 d = d * src[i];
27 }
28 else if (i == (NUM / 2))
29 d = rand ();
30 else if ((i % 6) == 0)
31 d = 1 / (d * (i + 1) * src[i] * M_PI *sign);
32 sign = -sign;
33 }
34 }
35
36 static double
do_round(double f,int type)37 do_round (double f, int type)
38 {
39 unsigned short saved_cw, new_cw, clr_mask;
40 double ret;
41
42 if ((type & 4))
43 {
44 type = 0;
45 clr_mask = 0xFFFF;
46 }
47 else
48 {
49 type = 0x003F | ((type & 3) << 10);
50 clr_mask = ~0x0C3F;
51 }
52
53 __asm__ ("fnstcw %0" : "=m" (saved_cw));
54
55 new_cw = saved_cw & clr_mask;
56 new_cw |= type;
57
58 __asm__ ("fldcw %2\n\t"
59 "frndint\n\t"
60 "fldcw %3" : "=t" (ret)
61 : "0" (f), "m" (new_cw), "m" (saved_cw));
62 return ret;
63 }
64
65 static void
sse4_1_test(void)66 sse4_1_test (void)
67 {
68 int i;
69 double f;
70 union
71 {
72 __m128d x[NUM / 2];
73 double d[NUM];
74 } dst, src;
75
76 init_round (src.d);
77 memset (&dst, 0, NUM * sizeof(double));
78
79 for (i = 0; i < NUM / 2 ; i++)
80 dst.x[i] = _mm_round_sd (dst.x[i], src.x[i], _MM_FROUND_TRUNC);
81
82 for (i = 0; i < NUM; i += 2)
83 {
84 if (dst.d[i + 1] != 0.0)
85 abort ();
86
87 f = do_round (src.d[i], 0x03);
88 if (f != dst.d[i])
89 abort ();
90 }
91 }
92