1831ec7f3SFrank Chang /*
2831ec7f3SFrank Chang * RISC-V Bitmanip Extension Helpers for QEMU.
3831ec7f3SFrank Chang *
4831ec7f3SFrank Chang * Copyright (c) 2020 Kito Cheng, kito.cheng@sifive.com
5831ec7f3SFrank Chang * Copyright (c) 2020 Frank Chang, frank.chang@sifive.com
6fd4b81a3SPhilipp Tomsich * Copyright (c) 2021 Philipp Tomsich, philipp.tomsich@vrull.eu
7831ec7f3SFrank Chang *
8831ec7f3SFrank Chang * This program is free software; you can redistribute it and/or modify it
9831ec7f3SFrank Chang * under the terms and conditions of the GNU General Public License,
10831ec7f3SFrank Chang * version 2 or later, as published by the Free Software Foundation.
11831ec7f3SFrank Chang *
12831ec7f3SFrank Chang * This program is distributed in the hope it will be useful, but WITHOUT
13831ec7f3SFrank Chang * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14831ec7f3SFrank Chang * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15831ec7f3SFrank Chang * more details.
16831ec7f3SFrank Chang *
17831ec7f3SFrank Chang * You should have received a copy of the GNU General Public License along with
18831ec7f3SFrank Chang * this program. If not, see <http://www.gnu.org/licenses/>.
19831ec7f3SFrank Chang */
20831ec7f3SFrank Chang
21831ec7f3SFrank Chang #include "qemu/osdep.h"
22831ec7f3SFrank Chang #include "qemu/host-utils.h"
23*2ec0dc24SPierrick Bouvier #include "exec/target_long.h"
24831ec7f3SFrank Chang #include "exec/helper-proto.h"
25831ec7f3SFrank Chang #include "tcg/tcg.h"
26831ec7f3SFrank Chang
HELPER(clmul)27fd4b81a3SPhilipp Tomsich target_ulong HELPER(clmul)(target_ulong rs1, target_ulong rs2)
28fd4b81a3SPhilipp Tomsich {
29fd4b81a3SPhilipp Tomsich target_ulong result = 0;
30fd4b81a3SPhilipp Tomsich
31fd4b81a3SPhilipp Tomsich for (int i = 0; i < TARGET_LONG_BITS; i++) {
32fd4b81a3SPhilipp Tomsich if ((rs2 >> i) & 1) {
33fd4b81a3SPhilipp Tomsich result ^= (rs1 << i);
34fd4b81a3SPhilipp Tomsich }
35fd4b81a3SPhilipp Tomsich }
36fd4b81a3SPhilipp Tomsich
37fd4b81a3SPhilipp Tomsich return result;
38fd4b81a3SPhilipp Tomsich }
39fd4b81a3SPhilipp Tomsich
HELPER(clmulr)40fd4b81a3SPhilipp Tomsich target_ulong HELPER(clmulr)(target_ulong rs1, target_ulong rs2)
41fd4b81a3SPhilipp Tomsich {
42fd4b81a3SPhilipp Tomsich target_ulong result = 0;
43fd4b81a3SPhilipp Tomsich
44fd4b81a3SPhilipp Tomsich for (int i = 0; i < TARGET_LONG_BITS; i++) {
45fd4b81a3SPhilipp Tomsich if ((rs2 >> i) & 1) {
46fd4b81a3SPhilipp Tomsich result ^= (rs1 >> (TARGET_LONG_BITS - i - 1));
47fd4b81a3SPhilipp Tomsich }
48fd4b81a3SPhilipp Tomsich }
49fd4b81a3SPhilipp Tomsich
50fd4b81a3SPhilipp Tomsich return result;
51fd4b81a3SPhilipp Tomsich }
52d8e81e3cSWeiwei Li
do_swap(target_ulong x,uint64_t mask,int shift)53d8e81e3cSWeiwei Li static inline target_ulong do_swap(target_ulong x, uint64_t mask, int shift)
54d8e81e3cSWeiwei Li {
55d8e81e3cSWeiwei Li return ((x & mask) << shift) | ((x & ~mask) >> shift);
56d8e81e3cSWeiwei Li }
57d8e81e3cSWeiwei Li
HELPER(brev8)58d8e81e3cSWeiwei Li target_ulong HELPER(brev8)(target_ulong rs1)
59d8e81e3cSWeiwei Li {
60d8e81e3cSWeiwei Li target_ulong x = rs1;
61d8e81e3cSWeiwei Li
62d8e81e3cSWeiwei Li x = do_swap(x, 0x5555555555555555ull, 1);
63d8e81e3cSWeiwei Li x = do_swap(x, 0x3333333333333333ull, 2);
64d8e81e3cSWeiwei Li x = do_swap(x, 0x0f0f0f0f0f0f0f0full, 4);
65d8e81e3cSWeiwei Li return x;
66d8e81e3cSWeiwei Li }
67d8e81e3cSWeiwei Li
68d8e81e3cSWeiwei Li static const uint64_t shuf_masks[] = {
69d8e81e3cSWeiwei Li dup_const(MO_8, 0x44),
70d8e81e3cSWeiwei Li dup_const(MO_8, 0x30),
71d8e81e3cSWeiwei Li dup_const(MO_16, 0x0f00),
72d8e81e3cSWeiwei Li dup_const(MO_32, 0xff0000)
73d8e81e3cSWeiwei Li };
74d8e81e3cSWeiwei Li
do_shuf_stage(target_ulong src,uint64_t maskL,uint64_t maskR,int shift)75d8e81e3cSWeiwei Li static inline target_ulong do_shuf_stage(target_ulong src, uint64_t maskL,
76d8e81e3cSWeiwei Li uint64_t maskR, int shift)
77d8e81e3cSWeiwei Li {
78d8e81e3cSWeiwei Li target_ulong x = src & ~(maskL | maskR);
79d8e81e3cSWeiwei Li
80d8e81e3cSWeiwei Li x |= ((src << shift) & maskL) | ((src >> shift) & maskR);
81d8e81e3cSWeiwei Li return x;
82d8e81e3cSWeiwei Li }
83d8e81e3cSWeiwei Li
HELPER(unzip)84d8e81e3cSWeiwei Li target_ulong HELPER(unzip)(target_ulong rs1)
85d8e81e3cSWeiwei Li {
86d8e81e3cSWeiwei Li target_ulong x = rs1;
87d8e81e3cSWeiwei Li
88d8e81e3cSWeiwei Li x = do_shuf_stage(x, shuf_masks[0], shuf_masks[0] >> 1, 1);
89d8e81e3cSWeiwei Li x = do_shuf_stage(x, shuf_masks[1], shuf_masks[1] >> 2, 2);
90d8e81e3cSWeiwei Li x = do_shuf_stage(x, shuf_masks[2], shuf_masks[2] >> 4, 4);
91d8e81e3cSWeiwei Li x = do_shuf_stage(x, shuf_masks[3], shuf_masks[3] >> 8, 8);
92d8e81e3cSWeiwei Li return x;
93d8e81e3cSWeiwei Li }
94d8e81e3cSWeiwei Li
HELPER(zip)95d8e81e3cSWeiwei Li target_ulong HELPER(zip)(target_ulong rs1)
96d8e81e3cSWeiwei Li {
97d8e81e3cSWeiwei Li target_ulong x = rs1;
98d8e81e3cSWeiwei Li
99d8e81e3cSWeiwei Li x = do_shuf_stage(x, shuf_masks[3], shuf_masks[3] >> 8, 8);
100d8e81e3cSWeiwei Li x = do_shuf_stage(x, shuf_masks[2], shuf_masks[2] >> 4, 4);
101d8e81e3cSWeiwei Li x = do_shuf_stage(x, shuf_masks[1], shuf_masks[1] >> 2, 2);
102d8e81e3cSWeiwei Li x = do_shuf_stage(x, shuf_masks[0], shuf_masks[0] >> 1, 1);
103d8e81e3cSWeiwei Li return x;
104d8e81e3cSWeiwei Li }
10504963896SWeiwei Li
do_xperm(target_ulong rs1,target_ulong rs2,uint32_t sz_log2)10604963896SWeiwei Li static inline target_ulong do_xperm(target_ulong rs1, target_ulong rs2,
10704963896SWeiwei Li uint32_t sz_log2)
10804963896SWeiwei Li {
10904963896SWeiwei Li target_ulong r = 0;
11004963896SWeiwei Li target_ulong sz = 1LL << sz_log2;
11104963896SWeiwei Li target_ulong mask = (1LL << sz) - 1;
11204963896SWeiwei Li target_ulong pos;
11304963896SWeiwei Li
11404963896SWeiwei Li for (int i = 0; i < TARGET_LONG_BITS; i += sz) {
11504963896SWeiwei Li pos = ((rs2 >> i) & mask) << sz_log2;
11604963896SWeiwei Li if (pos < sizeof(target_ulong) * 8) {
11704963896SWeiwei Li r |= ((rs1 >> pos) & mask) << i;
11804963896SWeiwei Li }
11904963896SWeiwei Li }
12004963896SWeiwei Li return r;
12104963896SWeiwei Li }
12204963896SWeiwei Li
HELPER(xperm4)12304963896SWeiwei Li target_ulong HELPER(xperm4)(target_ulong rs1, target_ulong rs2)
12404963896SWeiwei Li {
12504963896SWeiwei Li return do_xperm(rs1, rs2, 2);
12604963896SWeiwei Li }
12704963896SWeiwei Li
HELPER(xperm8)12804963896SWeiwei Li target_ulong HELPER(xperm8)(target_ulong rs1, target_ulong rs2)
12904963896SWeiwei Li {
13004963896SWeiwei Li return do_xperm(rs1, rs2, 3);
13104963896SWeiwei Li }
132