summaryrefslogtreecommitdiff
path: root/libraries/ghc-prim/cbits/pext.c
blob: 9cddedef1a2194ccf3d629a9e4512f7db117078e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include "Rts.h"
#include "MachDeps.h"

StgWord64
hs_pext64(StgWord64 src, StgWord64 mask)
{
  uint64_t result = 0;
  int offset = 0;

  for (int bit = 0; bit != sizeof(uint64_t) * 8; ++bit) {
    const uint64_t src_bit = (src >> bit) & 1;
    const uint64_t mask_bit = (mask >> bit) & 1;

    if (mask_bit) {
      result |= (uint64_t)(src_bit) << offset;
      ++offset;
    }
  }

  return result;
}

StgWord
hs_pext32(StgWord src, StgWord mask)
{
  return hs_pext64(src, mask);
}

StgWord
hs_pext16(StgWord src, StgWord mask)
{
  return hs_pext64(src, mask);
}

StgWord
hs_pext8(StgWord src, StgWord mask)
{
  return hs_pext64(src, mask);
}