summaryrefslogtreecommitdiff
path: root/libraries/ghc-prim/cbits/pdep.c
blob: 2769008f676f734fa00e677bb120e63846ce66ea (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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include "Rts.h"
#include "MachDeps.h"

StgWord64
hs_pdep64(StgWord64 src, StgWord64 mask)
{
  uint64_t m0, mk, mp, mv, t;
  uint64_t array[6];

  m0 = mask;
  mk = ~mask << 1;

  for (int i = 0; i < 6 ; i++) {
    mp = mk ^ (mk << 1);
    mp = mp ^ (mp << 2);
    mp = mp ^ (mp << 4);
    mp = mp ^ (mp << 8);
    mp = mp ^ (mp << 16);
    mp = mp ^ (mp << 32);
    mv = mp & mask;
    array[i] = mv;
    mask = (mask ^ mv) | (mv >> (1 << i));
    mk = mk & ~mp;
  }

  for (int i = 5; i >= 0; i--) {
    mv = array[i];
    t = src << (1 << i);
    src = (src & ~ mv) | (t & mv);
  }
  return src & m0;
}

StgWord
hs_pdep32(StgWord src, StgWord mask)
{
  uint32_t m0, mk, mp, mv, t;
  uint32_t array[5];

  m0 = mask;
  mk = ~mask << 1;

  for (int i = 0; i < 5 ; i++) {
    mp = mk ^ (mk << 1);
    mp = mp ^ (mp << 2);
    mp = mp ^ (mp << 4);
    mp = mp ^ (mp << 8);
    mp = mp ^ (mp << 16);
    mv = mp & mask;
    array[i] = mv;
    mask = (mask ^ mv) | (mv >> (1 << i));
    mk = mk & ~mp;
  }

  for (int i = 4; i >= 0; i--) {
    mv = array[i];
    t = src << (1 << i);
    src = (src & ~ mv) | (t & mv);
  }
  return src & m0;
}

StgWord
hs_pdep16(StgWord src, StgWord mask)
{
  uint16_t m0, mk, mp, mv, t;
  uint16_t array[4];

  m0 = mask;
  mk = ~mask << 1;

  for (int i = 0; i < 4 ; i++) {
    mp = mk ^ (mk << 1);
    mp = mp ^ (mp << 2);
    mp = mp ^ (mp << 4);
    mp = mp ^ (mp << 8);
    mv = mp & mask;
    array[i] = mv;
    mask = (mask ^ mv) | (mv >> (1 << i));
    mk = mk & ~mp;
  }

  for (int i = 3; i >= 0; i--) {
    mv = array[i];
    t = src << (1 << i);
    src = (src & ~ mv) | (t & mv);
  }
  return src & m0;
}

StgWord
hs_pdep8(StgWord src, StgWord mask)
{
  uint8_t m0, mk, mp, mv, t;
  uint8_t array[3];

  m0 = mask;
  mk = ~mask << 1;

  for (int i = 0; i < 3 ; i++) {
    mp = mk ^ (mk << 1);
    mp = mp ^ (mp << 2);
    mp = mp ^ (mp << 4);
    mv = mp & mask;
    array[i] = mv;
    mask = (mask ^ mv) | (mv >> (1 << i));
    mk = mk & ~mp;
  }

  for (int i = 2; i >= 0; i--) {
    mv = array[i];
    t = src << (1 << i);
    src = (src & ~ mv) | (t & mv);
  }
  return src & m0;
}