testsuite: Add testcase for #17206
[ghc.git] / libraries / ghc-prim / cbits / pdep.c
1 #include "Rts.h"
2 #include "MachDeps.h"
3
4 StgWord64
5 hs_pdep64(StgWord64 src, StgWord64 mask)
6 {
7 uint64_t result = 0;
8
9 while (1) {
10 // Mask out all but the lowest bit
11 const uint64_t lowest = (-mask & mask);
12
13 if (lowest == 0) {
14 break;
15 }
16
17 const uint64_t lsb = (uint64_t)((int64_t)(src << 63) >> 63);
18
19 result |= lsb & lowest;
20 mask &= ~lowest;
21 src >>= 1;
22 }
23
24 return result;
25 }
26
27 StgWord
28 hs_pdep32(StgWord src, StgWord mask)
29 {
30 return hs_pdep64(src, mask);
31 }
32
33 StgWord
34 hs_pdep16(StgWord src, StgWord mask)
35 {
36 return hs_pdep64(src, mask);
37 }
38
39 StgWord
40 hs_pdep8(StgWord src, StgWord mask)
41 {
42 return hs_pdep64(src, mask);
43 }