Add new mbmi and mbmi2 compiler flags
[ghc.git] / libraries / ghc-prim / cbits / pdep.c
1 #include "Rts.h"
2 #include "MachDeps.h"
3
4 extern StgWord hs_pdep64(StgWord64 src, StgWord mask);
5 StgWord
6 hs_pdep64(StgWord src, StgWord mask)
7 {
8 uint64_t result = 0;
9
10 while (1) {
11 // Mask out all but the lowest bit
12 const uint64_t lowest = (-mask & mask);
13
14 if (lowest == 0) {
15 break;
16 }
17
18 const uint64_t lsb = (uint64_t)((int64_t)(src << 63) >> 63);
19
20 result |= lsb & lowest;
21 mask &= ~lowest;
22 src >>= 1;
23 }
24
25 return result;
26 }
27
28 extern StgWord hs_pdep32(StgWord src, StgWord mask);
29 StgWord
30 hs_pdep32(StgWord src, StgWord mask)
31 {
32 return hs_pdep64(src, mask);
33 }
34
35 extern StgWord hs_pdep16(StgWord src, StgWord mask);
36 StgWord
37 hs_pdep16(StgWord src, StgWord mask)
38 {
39 return hs_pdep64(src, mask);
40 }
41
42 extern StgWord hs_pdep8(StgWord src, StgWord mask);
43 StgWord
44 hs_pdep8(StgWord src, StgWord mask)
45 {
46 return hs_pdep64(src, mask);
47 }
48
49 #if WORD_SIZE_IN_BITS == 32
50
51 extern StgWord hs_pdep(StgWord src, StgWord mask);
52 StgWord
53 hs_pdep(StgWord src, StgWord mask)
54 {
55 return hs_pdep64(src, mask);
56 }
57
58 #elif WORD_SIZE_IN_BITS == 64
59
60 extern StgWord hs_pdep(StgWord src, StgWord mask);
61 StgWord
62 hs_pdep(StgWord src, StgWord mask)
63 {
64 return hs_pdep64(src, mask);
65 }
66
67 #else
68
69 #error Unknown machine word size
70
71 #endif