xref: /qemu/target/riscv/bitmanip_helper.c (revision 0955d66e)
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/exec-all.h"
24 #include "exec/helper-proto.h"
25 #include "tcg/tcg.h"
26 
27 target_ulong HELPER(clmul)(target_ulong rs1, target_ulong rs2)
28 {
29     target_ulong result = 0;
30 
31     for (int i = 0; i < TARGET_LONG_BITS; i++) {
32         if ((rs2 >> i) & 1) {
33             result ^= (rs1 << i);
34         }
35     }
36 
37     return result;
38 }
39 
40 target_ulong HELPER(clmulr)(target_ulong rs1, target_ulong rs2)
41 {
42     target_ulong result = 0;
43 
44     for (int i = 0; i < TARGET_LONG_BITS; i++) {
45         if ((rs2 >> i) & 1) {
46             result ^= (rs1 >> (TARGET_LONG_BITS - i - 1));
47         }
48     }
49 
50     return result;
51 }
52