-
Notifications
You must be signed in to change notification settings - Fork 203
/
Copy pathriscv_insts_vext_red.sail
executable file
·137 lines (114 loc) · 5.82 KB
/
riscv_insts_vext_red.sail
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*=======================================================================================*/
/* This Sail RISC-V architecture model, comprising all files and */
/* directories except where otherwise noted is subject the BSD */
/* two-clause license in the LICENSE file. */
/* */
/* SPDX-License-Identifier: BSD-2-Clause */
/*=======================================================================================*/
/* ******************************************************************************* */
/* This file implements part of the vector extension. */
/* Chapter 14: Vector Reduction Instructions */
/* ******************************************************************************* */
/* ********************* OPIVV (Widening Integer Reduction) ********************** */
union clause ast = RIVVTYPE : (rivvfunct6, bits(1), vregidx, vregidx, vregidx)
mapping encdec_rivvfunct6 : rivvfunct6 <-> bits(6) = {
IVV_VWREDSUMU <-> 0b110000,
IVV_VWREDSUM <-> 0b110001
}
mapping clause encdec = RIVVTYPE(funct6, vm, vs2, vs1, vd)
<-> encdec_rivvfunct6(funct6) @ vm @ encdec_vreg(vs2) @ encdec_vreg(vs1) @ 0b000 @ encdec_vreg(vd) @ 0b1010111
when extensionEnabled(Ext_V)
function clause execute(RIVVTYPE(funct6, vm, vs2, vs1, vd)) = {
let SEW = get_sew();
let LMUL_pow = get_lmul_pow();
let 'SEW_widen = SEW * 2;
let LMUL_pow_widen = LMUL_pow + 1;
let num_elem_vs = get_num_elem(LMUL_pow, SEW);
let num_elem_vd = get_num_elem(0, SEW_widen); /* vd regardless of LMUL setting */
if illegal_reduction_widen(SEW_widen, LMUL_pow_widen) then { handle_illegal(); return RETIRE_FAIL };
if unsigned(vl) == 0 then return RETIRE_SUCCESS; /* if vl=0, no operation is performed */
let vm_val = read_vmask(num_elem_vs, vm, zvreg);
let vd_val = read_vreg(num_elem_vd, SEW_widen, 0, vd);
let vs2_val = read_vreg(num_elem_vs, SEW, LMUL_pow, vs2);
let mask = init_masked_source(num_elem_vs, LMUL_pow, vm_val);
var sum = read_single_element(SEW_widen, 0, vs1); /* vs1 regardless of LMUL setting */
foreach (i from 0 to (num_elem_vs - 1)) {
if mask[i] == bitone then {
let elem : bits('SEW_widen) = match funct6 {
IVV_VWREDSUMU => to_bits(SEW_widen, unsigned(vs2_val[i])),
IVV_VWREDSUM => to_bits(SEW_widen, signed(vs2_val[i]))
};
sum = sum + elem
}
};
write_single_element(SEW_widen, 0, vd, sum);
/* other elements in vd are treated as tail elements, currently remain unchanged */
/* TODO: configuration support for agnostic behavior */
set_vstart(zeros());
RETIRE_SUCCESS
}
mapping rivvtype_mnemonic : rivvfunct6 <-> string = {
IVV_VWREDSUMU <-> "vwredsumu.vs",
IVV_VWREDSUM <-> "vwredsum.vs"
}
mapping clause assembly = RIVVTYPE(funct6, vm, vs2, vs1, vd)
<-> rivvtype_mnemonic(funct6) ^ spc() ^ vreg_name(vd) ^ sep() ^ vreg_name(vs2) ^ sep() ^ vreg_name(vs1) ^ maybe_vmask(vm)
/* ******************* OPMVV (Single-Width Integer Reduction) ******************** */
union clause ast = RMVVTYPE : (rmvvfunct6, bits(1), vregidx, vregidx, vregidx)
mapping encdec_rmvvfunct6 : rmvvfunct6 <-> bits(6) = {
MVV_VREDSUM <-> 0b000000,
MVV_VREDAND <-> 0b000001,
MVV_VREDOR <-> 0b000010,
MVV_VREDXOR <-> 0b000011,
MVV_VREDMINU <-> 0b000100,
MVV_VREDMIN <-> 0b000101,
MVV_VREDMAXU <-> 0b000110,
MVV_VREDMAX <-> 0b000111
}
mapping clause encdec = RMVVTYPE(funct6, vm, vs2, vs1, vd)
<-> encdec_rmvvfunct6(funct6) @ vm @ encdec_vreg(vs2) @ encdec_vreg(vs1) @ 0b010 @ encdec_vreg(vd) @ 0b1010111
when extensionEnabled(Ext_V)
function clause execute(RMVVTYPE(funct6, vm, vs2, vs1, vd)) = {
let SEW = get_sew();
let LMUL_pow = get_lmul_pow();
let num_elem_vs = get_num_elem(LMUL_pow, SEW);
let num_elem_vd = get_num_elem(0, SEW); /* vd regardless of LMUL setting */
if illegal_reduction() then { handle_illegal(); return RETIRE_FAIL };
if unsigned(vl) == 0 then return RETIRE_SUCCESS; /* if vl=0, no operation is performed */
let vm_val = read_vmask(num_elem_vs, vm, zvreg);
let vd_val = read_vreg(num_elem_vd, SEW, 0, vd);
let vs2_val = read_vreg(num_elem_vs, SEW, LMUL_pow, vs2);
let mask = init_masked_source(num_elem_vs, LMUL_pow, vm_val);
var sum = read_single_element(SEW, 0, vs1); /* vs1 regardless of LMUL setting */
foreach (i from 0 to (num_elem_vs - 1)) {
if mask[i] == bitone then {
sum = match funct6 {
MVV_VREDSUM => sum + vs2_val[i],
MVV_VREDAND => sum & vs2_val[i],
MVV_VREDOR => sum | vs2_val[i],
MVV_VREDXOR => sum ^ vs2_val[i],
MVV_VREDMIN => to_bits(SEW, min(signed(vs2_val[i]), signed(sum))),
MVV_VREDMINU => to_bits(SEW, min(unsigned(vs2_val[i]), unsigned(sum))),
MVV_VREDMAX => to_bits(SEW, max(signed(vs2_val[i]), signed(sum))),
MVV_VREDMAXU => to_bits(SEW, max(unsigned(vs2_val[i]), unsigned(sum)))
}
}
};
write_single_element(SEW, 0, vd, sum);
/* other elements in vd are treated as tail elements, currently remain unchanged */
/* TODO: configuration support for agnostic behavior */
set_vstart(zeros());
RETIRE_SUCCESS
}
mapping rmvvtype_mnemonic : rmvvfunct6 <-> string = {
MVV_VREDSUM <-> "vredsum.vs",
MVV_VREDAND <-> "vredand.vs",
MVV_VREDOR <-> "vredor.vs",
MVV_VREDXOR <-> "vredxor.vs",
MVV_VREDMINU <-> "vredminu.vs",
MVV_VREDMIN <-> "vredmin.vs",
MVV_VREDMAXU <-> "vredmaxu.vs",
MVV_VREDMAX <-> "vredmax.vs"
}
mapping clause assembly = RMVVTYPE(funct6, vm, vs2, vs1, vd)
<-> rmvvtype_mnemonic(funct6) ^ spc() ^ vreg_name(vd) ^ sep() ^ vreg_name(vs2) ^ sep() ^ vreg_name(vs1) ^ maybe_vmask(vm)