Skip to content

Commit ba5ebd3

Browse files
nadime15charmitro
andcommitted
Add support for Zvkg extension
Add following instructions: vghsh.vv vgmul.vv Co-authored-by: Charalampos Mitrodimas <[email protected]>
1 parent e145e11 commit ba5ebd3

7 files changed

+141
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ Supported RISC-V ISA features
119119
- Zvbb extension for vector basic bit-manipulation, v1.0
120120
- Zvbc extension for vector carryless multiplication, v1.0
121121
- Zvkb extension for vector cryptography bit-manipulation, v1.0
122+
- Zvkg extension for vector GCM/GMAC, v1.0
122123
- Machine, Supervisor, and User modes
123124
- Smcntrpmf extension for cycle and instret privilege mode filtering, v1.0
124125
- Sscofpmf extension for Count Overflow and Mode-Based Filtering, v1.0

model/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ foreach (xlen IN ITEMS 32 64)
8080
"riscv_insts_zicboz.sail"
8181
"riscv_insts_zvbb.sail"
8282
"riscv_insts_zvbc.sail"
83+
"riscv_insts_zvkg.sail"
8384
# Zimop and Zcmop should be at the end so they can be overridden by earlier extensions
8485
"riscv_insts_zimop.sail"
8586
"riscv_insts_zcmop.sail"
@@ -172,6 +173,7 @@ foreach (xlen IN ITEMS 32 64)
172173
${sail_vm_srcs}
173174
# Shared/common code for the cryptography extension.
174175
"riscv_types_kext.sail"
176+
"riscv_types_zvk.sail"
175177
)
176178

177179
if (variant STREQUAL "rvfi")

model/arithmetic.sail

+9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@
66
/* SPDX-License-Identifier: BSD-2-Clause */
77
/*=======================================================================================*/
88

9+
/* Reverse bits in each byte */
10+
val brev8 : forall 'm, 'm >= 0 & mod('m, 8) == 0. (bits('m)) -> bits('m)
11+
function brev8(input) = {
12+
var output : bits('m) = zeros();
13+
foreach (i from 0 to ('m - 8) by 8)
14+
output[i+7..i] = reverse(input[i+7..i]);
15+
output
16+
}
17+
918
/* Carry-less multiply. */
1019
val carryless_mul : forall 'n, 'n > 0. (bits('n), bits('n)) -> bits(2 * 'n)
1120
function carryless_mul(a, b) = {

model/riscv_extensions.sail

+2
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ enum clause extension = Ext_Zvbb
113113
enum clause extension = Ext_Zvkb
114114
// Vector Carryless Multiplication
115115
enum clause extension = Ext_Zvbc
116+
// Vector GCM/GMAC
117+
enum clause extension = Ext_Zvkg
116118

117119
// Count Overflow and Mode-Based Filtering
118120
enum clause extension = Ext_Sscofpmf

model/riscv_insts_vext_utils.sail

+11
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,17 @@ function get_scalar(rs1, SEW) = {
172172
}
173173
}
174174

175+
/* Extracts 4 consecutive vector elements starting from index 4*i */
176+
val get_velem_4 : forall 'n 'm 'p, 'n > 0 & 8 <= 'm <= 64 & 'p >= 0 & 4 * 'p + 3 < 'n. (vector('n, bits('m)), int('p)) -> bits(4 * 'm)
177+
function get_velem_4(v, i) = v[4 * i + 3] @ v[4 * i + 2] @ v[4 * i + 1] @ v[4 * i]
178+
179+
/* Divide the input bitvector into 4 equal slices and store them in vd starting at position 4*i */
180+
val set_velem_4 : forall 'p 'n 'm, 8 <= 'n <= 64 & 'm > 0 & 'p >= 0. (vregidx, int('n), bits('m), int('p)) -> unit
181+
function set_velem_4(vd, SEW, input, i) = {
182+
foreach(j from 0 to 3)
183+
write_single_element(SEW, 4 * i + j, vd, slice(input, j * SEW, SEW));
184+
}
185+
175186
/* Get the starting element index from csr vtype */
176187
val get_start_element : unit -> nat
177188
function get_start_element() = {

model/riscv_insts_zvkg.sail

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*=======================================================================================*/
2+
/* This Sail RISC-V architecture model, comprising all files and */
3+
/* directories except where otherwise noted is subject the BSD */
4+
/* two-clause license in the LICENSE file. */
5+
/* */
6+
/* SPDX-License-Identifier: BSD-2-Clause */
7+
/*=======================================================================================*/
8+
9+
function clause extensionEnabled(Ext_Zvkg) = extensionEnabled(Ext_V)
10+
11+
union clause ast = VGHSH_VV : (vregidx, vregidx, vregidx)
12+
13+
mapping clause encdec = VGHSH_VV(vs2, vs1, vd)
14+
<-> 0b1011001 @ encdec_vreg(vs2) @ encdec_vreg(vs1) @ 0b010 @ encdec_vreg(vd) @ 0b1110111
15+
when extensionEnabled(Ext_Zvkg) & get_sew() == 32 & zvk_check_encdec(128, 4)
16+
17+
mapping clause assembly = VGHSH_VV(vs2, vs1, vd)
18+
<-> "vghsh.vv" ^ spc() ^ vreg_name(vd) ^ sep() ^ vreg_name(vs2)^ sep() ^ vreg_name(vs1)
19+
20+
function clause execute (VGHSH_VV(vs2, vs1, vd)) = {
21+
let SEW = get_sew();
22+
let LMUL_pow = get_lmul_pow();
23+
let num_elem = get_num_elem(LMUL_pow, SEW);
24+
25+
assert(SEW == 32);
26+
27+
let 'n = num_elem;
28+
let 'm = SEW;
29+
30+
let vs2_val : vector('n, bits('m)) = read_vreg(num_elem, SEW, LMUL_pow, vs2);
31+
let vs1_val : vector('n, bits('m)) = read_vreg(num_elem, SEW, LMUL_pow, vs1);
32+
let vd_val : vector('n, bits('m)) = read_vreg(num_elem, SEW, LMUL_pow, vd);
33+
34+
let eg_len = (unsigned(vl) / 4);
35+
let eg_start = (unsigned(vstart) / 4);
36+
37+
foreach (i from eg_start to (eg_len - 1)) {
38+
assert(0 <= i & ((i * 4) + 3) < 'n);
39+
40+
let Y : bits(128) = get_velem_4(vd_val, i);
41+
let X : bits(128) = get_velem_4(vs1_val, i);
42+
var H : bits(128) = brev8(get_velem_4(vs2_val, i));
43+
44+
var Z : bits(128) = zeros();
45+
let S : bits(128) = brev8(Y ^ X);
46+
47+
foreach (b from 0 to 127) {
48+
if S[b] == bitone then Z = Z ^ H;
49+
50+
let reduce = H[127] == bitone;
51+
H = H << 1;
52+
if reduce then H = H[127..8] @ (H[7..0] ^ 0x87);
53+
};
54+
55+
set_velem_4(vd, SEW, brev8(Z), i);
56+
};
57+
58+
set_vstart(zeros());
59+
RETIRE_SUCCESS
60+
}
61+
62+
union clause ast = VGMUL_VV : (vregidx, vregidx)
63+
64+
mapping clause encdec = VGMUL_VV(vs2, vd)
65+
<-> 0b1010001 @ encdec_vreg(vs2) @ 0b10001 @ 0b010 @ encdec_vreg(vd) @ 0b1110111
66+
when extensionEnabled(Ext_Zvkg) & get_sew() == 32 & zvk_check_encdec(128, 4)
67+
68+
mapping clause assembly = VGMUL_VV(vs2, vd)
69+
<-> "vgmul.vv" ^ spc() ^ vreg_name(vd) ^ sep() ^ vreg_name(vs2)
70+
71+
function clause execute (VGMUL_VV(vs2, vd)) = {
72+
let SEW = get_sew();
73+
let LMUL_pow = get_lmul_pow();
74+
let num_elem = get_num_elem(LMUL_pow, SEW);
75+
76+
assert(SEW == 32);
77+
78+
let 'n = num_elem;
79+
let 'm = SEW;
80+
81+
let vs2_val : vector('n, bits('m)) = read_vreg(num_elem, SEW, LMUL_pow, vs2);
82+
let vd_val : vector('n, bits('m)) = read_vreg(num_elem, SEW, LMUL_pow, vd);
83+
84+
let eg_len = (unsigned(vl) / 4);
85+
let eg_start = (unsigned(vstart) / 4);
86+
87+
foreach (i from eg_start to (eg_len - 1)) {
88+
assert(0 <= i & ((i * 4) + 3) < 'n);
89+
90+
let Y : bits(128) = brev8(get_velem_4(vd_val, i));
91+
var H : bits(128) = brev8(get_velem_4(vs2_val, i));
92+
var Z : bits(128) = zeros();
93+
94+
foreach (b from 0 to 127) {
95+
if Y[b] == bitone then Z = Z ^ H;
96+
97+
let reduce = H[127] == bitone;
98+
H = H << 1;
99+
if reduce then H = H[127..8] @ (H[7..0] ^ 0x87);
100+
};
101+
102+
set_velem_4(vd, SEW, brev8(Z), i);
103+
};
104+
105+
set_vstart(zeros());
106+
RETIRE_SUCCESS
107+
}

model/riscv_types_zvk.sail

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/*=======================================================================================*/
2+
/* This Sail RISC-V architecture model, comprising all files and */
3+
/* directories except where otherwise noted is subject the BSD */
4+
/* two-clause license in the LICENSE file. */
5+
/* */
6+
/* SPDX-License-Identifier: BSD-2-Clause */
7+
/*=======================================================================================*/
8+
9+
function zvk_check_encdec(EGW: int, EGS: {4}) -> bool = (unsigned(vl) % EGS == 0) & (unsigned(vstart) % EGS == 0) & (2 ^ get_lmul_pow() * VLEN) >= EGW

0 commit comments

Comments
 (0)