master
c 136 lines 3.63 KB
Raw
1 /*
2 * RISC-V Bitmanip Extension Helpers for QEMU.
3 *
4 * Copyright (c) 2020 Kito Cheng, kito.cheng@sifive.com
5 * Copyright (c) 2020 Frank Chang, frank.chang@sifive.com
6 * Copyright (c) 2021 Philipp Tomsich, philipp.tomsich@vrull.eu
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms and conditions of the GNU General Public License,
10 * version 2 or later, as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "qemu/osdep.h"
22 #include "qemu/host-utils.h"
23 #include "exec/target_long.h"
24 #include "exec/helper-proto.h"
25 #include "tcg/tcg.h"
26 #include "qemu/crc32.h"
27 #include "qemu/crc32c.h"
28
29 target_ulong HELPER(clmul)(target_ulong rs1, target_ulong rs2)
30 {
31 target_ulong result = 0;
32
33 for (int i = 0; i < TARGET_LONG_BITS; i++) {
34 if ((rs2 >> i) & 1) {
35 result ^= (rs1 << i);
36 }
37 }
38
39 return result;
40 }
41
42 target_ulong HELPER(clmulr)(target_ulong rs1, target_ulong rs2)
43 {
44 target_ulong result = 0;
45
46 for (int i = 0; i < TARGET_LONG_BITS; i++) {
47 if ((rs2 >> i) & 1) {
48 result ^= (rs1 >> (TARGET_LONG_BITS - i - 1));
49 }
50 }
51
52 return result;
53 }
54
55 static const uint64_t shuf_masks[] = {
56 dup_const(MO_8, 0x44),
57 dup_const(MO_8, 0x30),
58 dup_const(MO_16, 0x0f00),
59 dup_const(MO_32, 0xff0000)
60 };
61
62 static inline target_ulong do_shuf_stage(target_ulong src, uint64_t maskL,
63 uint64_t maskR, int shift)
64 {
65 target_ulong x = src & ~(maskL | maskR);
66
67 x |= ((src << shift) & maskL) | ((src >> shift) & maskR);
68 return x;
69 }
70
71 target_ulong HELPER(unzip)(target_ulong rs1)
72 {
73 target_ulong x = rs1;
74
75 x = do_shuf_stage(x, shuf_masks[0], shuf_masks[0] >> 1, 1);
76 x = do_shuf_stage(x, shuf_masks[1], shuf_masks[1] >> 2, 2);
77 x = do_shuf_stage(x, shuf_masks[2], shuf_masks[2] >> 4, 4);
78 x = do_shuf_stage(x, shuf_masks[3], shuf_masks[3] >> 8, 8);
79 return x;
80 }
81
82 target_ulong HELPER(zip)(target_ulong rs1)
83 {
84 target_ulong x = rs1;
85
86 x = do_shuf_stage(x, shuf_masks[3], shuf_masks[3] >> 8, 8);
87 x = do_shuf_stage(x, shuf_masks[2], shuf_masks[2] >> 4, 4);
88 x = do_shuf_stage(x, shuf_masks[1], shuf_masks[1] >> 2, 2);
89 x = do_shuf_stage(x, shuf_masks[0], shuf_masks[0] >> 1, 1);
90 return x;
91 }
92
93 static inline target_ulong do_xperm(target_ulong rs1, target_ulong rs2,
94 uint32_t sz_log2)
95 {
96 target_ulong r = 0;
97 target_ulong sz = 1LL << sz_log2;
98 target_ulong mask = (1LL << sz) - 1;
99 target_ulong pos;
100
101 for (int i = 0; i < TARGET_LONG_BITS; i += sz) {
102 pos = ((rs2 >> i) & mask) << sz_log2;
103 if (pos < sizeof(target_ulong) * 8) {
104 r |= ((rs1 >> pos) & mask) << i;
105 }
106 }
107 return r;
108 }
109
110 target_ulong HELPER(xperm4)(target_ulong rs1, target_ulong rs2)
111 {
112 return do_xperm(rs1, rs2, 2);
113 }
114
115 target_ulong HELPER(xperm8)(target_ulong rs1, target_ulong rs2)
116 {
117 return do_xperm(rs1, rs2, 3);
118 }
119
120 target_ulong HELPER(crc32)(target_ulong rs1, target_ulong sz)
121 {
122 for (target_ulong i = 0; i < sz; i++) {
123 rs1 = crc32_table[rs1 & 0xFF] ^ (rs1 >> 8);
124 }
125
126 return rs1;
127 }
128
129 target_ulong HELPER(crc32c)(target_ulong rs1, target_ulong sz)
130 {
131 for (target_ulong i = 0; i < sz; i++) {
132 rs1 = crc32c_table[rs1 & 0xFF] ^ (rs1 >> 8);
133 }
134
135 return rs1;
136 }