1 /* { dg-do run { target { powerpc*-*-* && p9vector_hw } } } */
2 /* { dg-skip-if "do not override -mcpu" { powerpc*-*-* } { "-mcpu=*" } { "-mcpu=power9" } } */
3 /* { dg-require-effective-target powerpc_p9vector_ok } */
4 /* { dg-options "-mcpu=power9 -O2" } */
5 
6 #include <altivec.h>
7 #define TRUE 1
8 #define FALSE 0
9 
10 #ifdef DEBUG
11 #include <stdio.h>
12 #endif
13 
14 #define EXTRACT 0
15 
16 void abort (void);
17 
result_wrong_ull(vector unsigned long long vec_expected,vector unsigned long long vec_actual)18 int result_wrong_ull (vector unsigned long long vec_expected,
19 		      vector unsigned long long vec_actual)
20 {
21   int i;
22 
23   for (i = 0; i < 2; i++)
24     if (vec_expected[i] != vec_actual[i])
25       return TRUE;
26 
27   return FALSE;
28 }
29 
result_wrong_uc(vector unsigned char vec_expected,vector unsigned char vec_actual)30 int result_wrong_uc (vector unsigned char vec_expected,
31 		     vector unsigned char vec_actual)
32 {
33   int i;
34 
35   for (i = 0; i < 16; i++)
36     if (vec_expected[i] != vec_actual[i])
37       return TRUE;
38 
39   return FALSE;
40 }
41 
42 #ifdef DEBUG
print_ull(vector unsigned long long vec_expected,vector unsigned long long vec_actual)43 void print_ull (vector unsigned long long vec_expected,
44 		vector unsigned long long vec_actual)
45 {
46   int i;
47 
48   printf("expected unsigned long long data\n");
49   for (i = 0; i < 2; i++)
50     printf(" %lld,", vec_expected[i]);
51 
52   printf("\nactual signed char data\n");
53   for (i = 0; i < 2; i++)
54     printf(" %lld,", vec_actual[i]);
55   printf("\n");
56 }
57 
print_uc(vector unsigned char vec_expected,vector unsigned char vec_actual)58 void print_uc (vector unsigned char vec_expected,
59 	       vector unsigned char vec_actual)
60 {
61   int i;
62 
63   printf("expected unsigned char data\n");
64   for (i = 0; i < 16; i++)
65     printf(" %d,", vec_expected[i]);
66 
67   printf("\nactual unsigned char data\n");
68   for (i = 0; i < 16; i++)
69     printf(" %d,", vec_actual[i]);
70   printf("\n");
71 }
72 #endif
73 
74 #if EXTRACT
75 vector unsigned long long
vext(vector unsigned char * vc)76 vext (vector unsigned char *vc)
77 {
78   return vextract_si_vchar (*vc, 5);
79 }
80 #endif
81 
main()82 int main()
83 {
84    vector signed int vsi_arg;
85    vector unsigned char vec_uc_arg, vec_uc_result, vec_uc_expected;
86    vector unsigned long long vec_ull_result, vec_ull_expected;
87    unsigned long long ull_result, ull_expected;
88 
89    vec_uc_arg = (vector unsigned char){1, 2, 3, 4,
90 				       5, 6, 7, 8,
91 				       9, 10, 11, 12,
92 				       13, 14, 15, 16};
93 
94    vsi_arg = (vector signed int){0xA, 0xB, 0xC, 0xD};
95 
96    vec_uc_expected = (vector unsigned char){0xC, 0, 0, 0,
97 					    5, 6, 7, 8,
98 					    9, 10, 11, 12,
99 					    13, 14, 15, 16};
100    /* Test vec_insert4b() */
101    /* Insert into char 0 location */
102    vec_uc_result = vec_insert4b (vsi_arg, vec_uc_arg, 0);
103 
104    if (result_wrong_uc(vec_uc_expected, vec_uc_result))
105      {
106 #ifdef DEBUG
107         printf("Error: vec_insert4b pos 0, result does not match expected result\n");
108 	print_uc (vec_uc_expected, vec_uc_result);
109 #else
110         abort();
111 #endif
112       }
113 
114    /* insert into char 4 location */
115    vec_uc_expected = (vector unsigned char){1, 2, 3, 4,
116 					    0xC, 0, 0, 0,
117 					    9, 10, 11, 12,
118 					    13, 14, 15, 16};
119    vec_uc_result = vec_insert4b (vsi_arg, vec_uc_arg, 4);
120 
121    if (result_wrong_uc(vec_uc_expected, vec_uc_result))
122      {
123 #ifdef DEBUG
124         printf("Error: vec_insert4b pos 4, result does not match expected result\n");
125 	print_uc (vec_uc_expected, vec_uc_result);
126 #else
127         abort();
128 #endif
129       }
130 
131    /* Test vec_extract4b() */
132    /* Extract 4b, from char 0 location */
133    vec_uc_arg = (vector unsigned char){10, 0, 0, 0,
134 				       20, 0, 0, 0,
135 				       30, 0, 0, 0,
136 				       40, 0, 0, 0};
137 
138    vec_ull_expected = (vector unsigned long long){0, 10};
139    vec_ull_result = vec_extract4b(vec_uc_arg, 0);
140 
141    if (result_wrong_ull(vec_ull_expected, vec_ull_result))
142      {
143 #ifdef DEBUG
144         printf("Error: vec_extract4b pos 0, result does not match expected result\n");
145 	print_ull (vec_ull_expected, vec_ull_result);
146 #else
147         abort();
148 #endif
149       }
150 
151    /* Extract 4b, from char 12 location */
152    vec_uc_arg = (vector unsigned char){10, 0, 0, 0,
153 				       20, 0, 0, 0,
154 				       30, 0, 0, 0,
155 				       40, 0, 0, 0};
156 
157    vec_ull_expected = (vector unsigned long long){0, 40};
158    vec_ull_result = vec_extract4b(vec_uc_arg, 12);
159 
160    if (result_wrong_ull(vec_ull_expected, vec_ull_result))
161      {
162 #ifdef DEBUG
163         printf("Error: vec_extract4b pos 12, result does not match expected result\n");
164 	print_ull (vec_ull_expected, vec_ull_result);
165 #else
166         abort();
167 #endif
168       }
169 }
170