xref: /openbmc/qemu/target/riscv/vcrypto_helper.c (revision e13c7d3b)
1 /*
2  * RISC-V Vector Crypto Extension Helpers for QEMU.
3  *
4  * Copyright (C) 2023 SiFive, Inc.
5  * Written by Codethink Ltd and SiFive.
6  *
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.
10  *
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.
15  *
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/>.
18  */
19 
20 #include "qemu/osdep.h"
21 #include "qemu/host-utils.h"
22 #include "qemu/bitops.h"
23 #include "cpu.h"
24 #include "exec/memop.h"
25 #include "exec/exec-all.h"
26 #include "exec/helper-proto.h"
27 #include "internals.h"
28 #include "vector_internals.h"
29 
30 static uint64_t clmul64(uint64_t y, uint64_t x)
31 {
32     uint64_t result = 0;
33     for (int j = 63; j >= 0; j--) {
34         if ((y >> j) & 1) {
35             result ^= (x << j);
36         }
37     }
38     return result;
39 }
40 
41 static uint64_t clmulh64(uint64_t y, uint64_t x)
42 {
43     uint64_t result = 0;
44     for (int j = 63; j >= 1; j--) {
45         if ((y >> j) & 1) {
46             result ^= (x >> (64 - j));
47         }
48     }
49     return result;
50 }
51 
52 RVVCALL(OPIVV2, vclmul_vv, OP_UUU_D, H8, H8, H8, clmul64)
53 GEN_VEXT_VV(vclmul_vv, 8)
54 RVVCALL(OPIVX2, vclmul_vx, OP_UUU_D, H8, H8, clmul64)
55 GEN_VEXT_VX(vclmul_vx, 8)
56 RVVCALL(OPIVV2, vclmulh_vv, OP_UUU_D, H8, H8, H8, clmulh64)
57 GEN_VEXT_VV(vclmulh_vv, 8)
58 RVVCALL(OPIVX2, vclmulh_vx, OP_UUU_D, H8, H8, clmulh64)
59 GEN_VEXT_VX(vclmulh_vx, 8)
60