1 /* { dg-do run { target bmi2 } } */
2 /* { dg-options "-mbmi2 -O2" } */
3 
4 #include <x86intrin.h>
5 #include "bmi2-check.h"
6 
7 __attribute__((noinline))
8 unsigned
calc_pext_u32(unsigned a,unsigned mask)9 calc_pext_u32 (unsigned a, unsigned mask)
10 {
11   unsigned res = 0;
12   int i, k = 0;
13 
14   for (i = 0; i < 32; ++i)
15     if (mask & (1 << i)) {
16       res |= ((a & (1 << i)) >> i) << k;
17       ++k;
18     }
19 
20   return res;
21 }
22 
23 static void
bmi2_test()24 bmi2_test ()
25 {
26   unsigned i;
27   unsigned src = 0xce7acc;
28   unsigned res, res_ref;
29 
30   for (i = 0; i < 5; ++i) {
31     src = src * (i + 1);
32 
33     res_ref = calc_pext_u32 (src, ~(i * 3));
34     res = _pext_u32 (src, ~(i * 3));
35 
36     if (res != res_ref)
37       abort();
38   }
39 }
40