Merge remote-tracking branch 'remotes/dgilbert-gitlab/tags/pull-migration-20210726a...
[qemu/armbru.git] / target / riscv / bitmanip_helper.c
blob5b2f795d03a753ce425a3379f8c931d46866cfe6
1 /*
2 * RISC-V Bitmanip Extension Helpers for QEMU.
4 * Copyright (c) 2020 Kito Cheng, kito.cheng@sifive.com
5 * Copyright (c) 2020 Frank Chang, frank.chang@sifive.com
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms and conditions of the GNU General Public License,
9 * version 2 or later, as published by the Free Software Foundation.
11 * This program is distributed in the hope it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
16 * You should have received a copy of the GNU General Public License along with
17 * this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "qemu/osdep.h"
21 #include "qemu/host-utils.h"
22 #include "exec/exec-all.h"
23 #include "exec/helper-proto.h"
24 #include "tcg/tcg.h"
26 static const uint64_t adjacent_masks[] = {
27 dup_const(MO_8, 0x55),
28 dup_const(MO_8, 0x33),
29 dup_const(MO_8, 0x0f),
30 dup_const(MO_16, 0xff),
31 dup_const(MO_32, 0xffff),
32 UINT32_MAX
35 static inline target_ulong do_swap(target_ulong x, uint64_t mask, int shift)
37 return ((x & mask) << shift) | ((x & ~mask) >> shift);
40 static target_ulong do_grev(target_ulong rs1,
41 target_ulong rs2,
42 int bits)
44 target_ulong x = rs1;
45 int i, shift;
47 for (i = 0, shift = 1; shift < bits; i++, shift <<= 1) {
48 if (rs2 & shift) {
49 x = do_swap(x, adjacent_masks[i], shift);
53 return x;
56 target_ulong HELPER(grev)(target_ulong rs1, target_ulong rs2)
58 return do_grev(rs1, rs2, TARGET_LONG_BITS);
61 target_ulong HELPER(grevw)(target_ulong rs1, target_ulong rs2)
63 return do_grev(rs1, rs2, 32);
66 static target_ulong do_gorc(target_ulong rs1,
67 target_ulong rs2,
68 int bits)
70 target_ulong x = rs1;
71 int i, shift;
73 for (i = 0, shift = 1; shift < bits; i++, shift <<= 1) {
74 if (rs2 & shift) {
75 x |= do_swap(x, adjacent_masks[i], shift);
79 return x;
82 target_ulong HELPER(gorc)(target_ulong rs1, target_ulong rs2)
84 return do_gorc(rs1, rs2, TARGET_LONG_BITS);
87 target_ulong HELPER(gorcw)(target_ulong rs1, target_ulong rs2)
89 return do_gorc(rs1, rs2, 32);