(no commit message)
[libreriscv.git] / openpower / sv / bitmanip.mdwn
1 # bit to byte permute
2
3 do j = 0 to 7
4 do k = 0 to 7
5 b = VSR[VRB+32].dword[i].byte[k].bit[j]
6 VSR[VRT+32].dword[i].byte[j].bit[k] = b
7
8 # vector bit deposit
9
10 vpdepd VRT,VRA,VRB
11
12 do while(m < 64)
13 if VSR[VRB+32].dword[i].bit[63-m]=1 then do
14 result = VSR[VRA+32].dword[i].bit[63-k]
15 VSR[VRT+32].dword[i].bit[63-m] = result
16 k = k + 1
17 m = m + 1
18
19 # vector bit extract
20
21 other way round
22
23 # single bit set
24
25 based on RV bitmanip
26
27 ```
28 uint_xlen_t sbset(uint_xlen_t rs1, uint_xlen_t rs2) { int shamt = rs2 & (XLEN - 1);
29 }
30 return rs1 | (uint_xlen_t(1) << shamt);
31 uint_xlen_t sbclr(uint_xlen_t rs1, uint_xlen_t rs2) { int shamt = rs2 & (XLEN - 1);
32 }
33 return rs1 & ~(uint_xlen_t(1) << shamt);
34 uint_xlen_t sbinv(uint_xlen_t rs1, uint_xlen_t rs2) { int shamt = rs2 & (XLEN - 1);
35 }
36 return rs1 ^ (uint_xlen_t(1) << shamt);
37 uint_xlen_t sbext(uint_xlen_t rs1, uint_xlen_t rs2) { int shamt = rs2 & (XLEN - 1);
38 }
39 return 1 & (rs1 >> shamt);
40 ```
41
42 # shuffle / unshuffle
43
44 based on RV bitmanip
45
46 ```
47 uint64_t shuffle64_stage(uint64_t src, uint64_t maskL, uint64_t maskR, int N) { uint64_t x = src & ~(maskL | maskR);
48 }
49 x |= ((src << N) & maskL) | ((src >> N) & maskR); return x;
50 uint64_t shfl64(uint64_t rs1, uint64_t rs2) { uint64_t x = rs1;
51 }
52 int shamt = rs2 & 31;
53 if (shamt & 16) x = shuffle64_stage(x, 0x0000ffff00000000LL,
54 0x00000000ffff0000LL, 16); if (shamt & 8) x = shuffle64_stage(x, 0x00ff000000ff0000LL,
55 0x0000ff000000ff00LL, 8); if (shamt & 4) x = shuffle64_stage(x, 0x0f000f000f000f00LL,
56 0x00f000f000f000f0LL, 4); if (shamt & 2) x = shuffle64_stage(x, 0x3030303030303030LL,
57 0x0c0c0c0c0c0c0c0cLL, 2); if (shamt & 1) x = shuffle64_stage(x, 0x4444444444444444LL,
58 return x;
59 0x2222222222222222LL, 1);
60 uint64_t unshfl64(uint64_t rs1, uint64_t rs2) { uint64_t x = rs1;
61 }
62 int shamt = rs2 & 31;
63 if (shamt & 1) x = shuffle64_stage(x, 0x4444444444444444LL,
64 0x2222222222222222LL, 1); if (shamt & 2) x = shuffle64_stage(x, 0x3030303030303030LL,
65 0x0c0c0c0c0c0c0c0cLL, 2); if (shamt & 4) x = shuffle64_stage(x, 0x0f000f000f000f00LL,
66 0x00f000f000f000f0LL, 4); if (shamt & 8) x = shuffle64_stage(x, 0x00ff000000ff0000LL,
67 0x0000ff000000ff00LL, 8); if (shamt & 16) x = shuffle64_stage(x, 0x0000ffff00000000LL,
68 return x;
69 0x00000000ffff0000LL, 16);
70 ```
71
72 # xperm
73
74 based on RV bitmanip
75
76 ```
77 uint_xlen_t xperm(uint_xlen_t rs1, uint_xlen_t rs2, int sz_log2) { uint_xlen_t r = 0;
78 }
79 uint_xlen_t sz = 1LL << sz_log2; uint_xlen_t mask = (1LL << sz) - 1; for (int i = 0; i < XLEN; i += sz) { uint_xlen_t pos = ((rs2 >> i) & mask) << sz_log2; if (pos < XLEN)
80 r |= ((rs1 >> pos) & mask) << i; }return r;
81 uint_xlen_t xperm_n (uint_xlen_t rs1, uint_xlen_t rs2) { return xperm(rs1, rs2, 2); } uint_xlen_t xperm_b (uint_xlen_t rs1, uint_xlen_t rs2) { return xperm(rs1, rs2, 3); } uint_xlen_t xperm_h (uint_xlen_t rs1, uint_xlen_t rs2) { return xperm(rs1, rs2, 4); } uint_xlen_t xperm_w (uint_xlen_t rs1, uint_xlen_t rs2) { return xperm(rs1, rs2, 5); }
82 ```
83
84 # gorc
85
86 based on RV bitmanip
87
88 ```
89 uint32_t gorc32(uint32_t rs1, uint32_t rs2) { uint32_t x = rs1;
90 }
91 int shamt = rs2 & 31;
92 if (shamt & 1) x |= ((x & 0x55555555) << 1) | ((x & 0xAAAAAAAA) >> 1); if (shamt & 2) x |= ((x & 0x33333333) << 2) | ((x & 0xCCCCCCCC) >> 2); if (shamt & 4) x |= ((x & 0x0F0F0F0F) << 4) | ((x & 0xF0F0F0F0) >> 4); if (shamt & 8) x |= ((x & 0x00FF00FF) << 8) | ((x & 0xFF00FF00) >> 8); if (shamt & 16) x |= ((x & 0x0000FFFF) << 16) | ((x & 0xFFFF0000) >> 16); return x;
93 uint64_t gorc64(uint64_t rs1, uint64_t rs2) { uint64_t x = rs1;
94 }
95 int shamt = rs2 & 63;
96 if (shamt & 1) x |= ((x & 0x5555555555555555LL) << 1) |
97 ((x & 0xAAAAAAAAAAAAAAAALL) >> 1); if (shamt & 2) x |= ((x & 0x3333333333333333LL) << 2) |
98 ((x & 0xCCCCCCCCCCCCCCCCLL) >> 2); if (shamt & 4) x |= ((x & 0x0F0F0F0F0F0F0F0FLL) << 4) |
99 ((x & 0xF0F0F0F0F0F0F0F0LL) >> 4); if (shamt & 8) x |= ((x & 0x00FF00FF00FF00FFLL) << 8) |
100 ((x & 0xFF00FF00FF00FF00LL) >> 8); if (shamt & 16) x |= ((x & 0x0000FFFF0000FFFFLL) << 16) |
101 ((x & 0xFFFF0000FFFF0000LL) >> 16); if (shamt & 32) x |= ((x & 0x00000000FFFFFFFFLL) << 32) |
102 return x;
103 ((x & 0xFFFFFFFF00000000LL) >> 32);
104 ```
105
106 # cmix
107
108 based on RV bitmanip
109
110 ```
111 uint_xlen_t cmix(uint_xlen_t rs1, uint_xlen_t rs2, uint_xlen_t rs3) { return (rs1 & rs2) | (rs3 & ~rs2);
112 }
113 ```