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
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"
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),
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
,
47 for (i
= 0, shift
= 1; shift
< bits
; i
++, shift
<<= 1) {
49 x
= do_swap(x
, adjacent_masks
[i
], shift
);
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
,
73 for (i
= 0, shift
= 1; shift
< bits
; i
++, shift
<<= 1) {
75 x
|= do_swap(x
, adjacent_masks
[i
], shift
);
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);