gem5 v23.0.0.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
utility.hh
Go to the documentation of this file.
1/*
2 * Copyright (c) 2013 ARM Limited
3 * Copyright (c) 2014-2015 Sven Karlsson
4 * Copyright (c) 2018 TU Dresden
5 * Copyright (c) 2020 Barkhausen Institut
6 * All rights reserved
7 *
8 * The license below extends only to copyright in the software and shall
9 * not be construed as granting a license to any other intellectual
10 * property including but not limited to intellectual property relating
11 * to a hardware implementation of the functionality of the software
12 * licensed hereunder. You may use the software subject to the license
13 * terms below provided that you ensure that this notice is replicated
14 * unmodified and in its entirety in all distributions of the software,
15 * modified or unmodified, in source code or in binary form.
16 *
17 * Copyright (c) 2016-2017 The University of Virginia
18 * All rights reserved.
19 *
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions are
22 * met: redistributions of source code must retain the above copyright
23 * notice, this list of conditions and the following disclaimer;
24 * redistributions in binary form must reproduce the above copyright
25 * notice, this list of conditions and the following disclaimer in the
26 * documentation and/or other materials provided with the distribution;
27 * neither the name of the copyright holders nor the names of its
28 * contributors may be used to endorse or promote products derived from
29 * this software without specific prior written permission.
30 *
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
37 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
38 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
39 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
40 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
41 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 */
43
44#ifndef __ARCH_RISCV_UTILITY_HH__
45#define __ARCH_RISCV_UTILITY_HH__
46
47#include <cmath>
48#include <cstdint>
49#include <sstream>
50#include <string>
51
54#include "base/types.hh"
55#include "cpu/reg_class.hh"
56#include "cpu/static_inst.hh"
57#include "cpu/thread_context.hh"
58#include "enums/RiscvType.hh"
59#include "rvk.hh"
60
61namespace gem5
62{
63
64namespace RiscvISA
65{
66
67template<typename T> inline bool
69{
70 return false;
71}
72
73template<> inline bool
75{
76 return std::isnan(val)
77 && (reinterpret_cast<uint32_t&>(val)&0x00400000);
78}
79
80template<> inline bool
82{
83 return std::isnan(val)
84 && (reinterpret_cast<uint64_t&>(val)&0x0008000000000000ULL);
85}
86
87template<typename T> inline bool
89{
90 return false;
91}
92
93template<> inline bool
95{
96 return std::isnan(val)
97 && (reinterpret_cast<uint32_t&>(val)&0x00200000);
98}
99
100template<> inline bool
102{
103 return std::isnan(val)
104 && (reinterpret_cast<uint64_t&>(val)&0x0004000000000000ULL);
105}
106
107inline std::string
109{
110 if (reg.is(IntRegClass)) {
111 if (reg.index() >= int_reg::NumArchRegs) {
112 /*
113 * This should only happen if a instruction is being speculatively
114 * executed along a not-taken branch, and if that instruction's
115 * width was incorrectly predecoded (i.e., it was predecoded as a
116 * full instruction rather than a compressed one or vice versa).
117 * It also should only happen if a debug flag is on that prints
118 * disassembly information, so rather than panic the incorrect
119 * value is printed for debugging help.
120 */
121 std::stringstream str;
122 str << "?? (x" << reg.index() << ')';
123 return str.str();
124 }
125 return int_reg::RegNames[reg.index()];
126 } else if (reg.is(FloatRegClass)) {
127 if (reg.index() >= float_reg::NumRegs) {
128 std::stringstream str;
129 str << "?? (f" << reg.index() << ')';
130 return str.str();
131 }
132 return float_reg::RegNames[reg.index()];
133 } else {
134 /* It must be an InvalidRegClass, in RISC-V we should treat it as a
135 * zero register for the disassembler to work correctly.
136 */
137 return int_reg::RegNames[reg.index()];
138 }
139}
140
141inline uint32_t
142mulhu_32(uint32_t rs1, uint32_t rs2)
143{
144 return ((uint64_t)rs1 * rs2) >> 32;
145}
146
147inline uint64_t
148mulhu_64(uint64_t rs1, uint64_t rs2)
149{
150 uint64_t rs1_lo = (uint32_t)rs1;
151 uint64_t rs1_hi = rs1 >> 32;
152 uint64_t rs2_lo = (uint32_t)rs2;
153 uint64_t rs2_hi = rs2 >> 32;
154
155 uint64_t hi = rs1_hi * rs2_hi;
156 uint64_t mid1 = rs1_hi * rs2_lo;
157 uint64_t mid2 = rs1_lo * rs2_hi;
158 uint64_t lo = rs1_lo * rs2_lo;
159 uint64_t carry = ((uint64_t)(uint32_t)mid1
160 + (uint64_t)(uint32_t)mid2
161 + (lo >> 32)) >> 32;
162
163 return hi + (mid1 >> 32) + (mid2 >> 32) + carry;
164}
165
166inline int32_t
167mulh_32(int32_t rs1, int32_t rs2)
168{
169 return ((int64_t)rs1 * rs2) >> 32;
170}
171
172inline int64_t
173mulh_64(int64_t rs1, int64_t rs2)
174{
175 bool negate = (rs1 < 0) != (rs2 < 0);
176 uint64_t res = mulhu_64(std::abs(rs1), std::abs(rs2));
177 return negate ? ~res + (rs1 * rs2 == 0 ? 1 : 0) : res;
178}
179
180inline int32_t
181mulhsu_32(int32_t rs1, uint32_t rs2)
182{
183 return ((int64_t)rs1 * rs2) >> 32;
184}
185
186inline int64_t
187mulhsu_64(int64_t rs1, uint64_t rs2)
188{
189 bool negate = rs1 < 0;
190 uint64_t res = mulhu_64(std::abs(rs1), rs2);
191 return negate ? ~res + (rs1 * rs2 == 0 ? 1 : 0) : res;
192}
193
194template<typename T> inline T
196{
197 constexpr T kRsMin = std::numeric_limits<T>::min();
198 if (rs2 == 0) {
199 return -1;
200 } else if (rs1 == kRsMin && rs2 == -1) {
201 return kRsMin;
202 } else {
203 return rs1 / rs2;
204 }
205}
206
207template<typename T> inline T
209{
210 if (rs2 == 0) {
211 return std::numeric_limits<T>::max();
212 } else {
213 return rs1 / rs2;
214 }
215}
216
217template<typename T> inline T
219{
220 constexpr T kRsMin = std::numeric_limits<T>::min();
221 if (rs2 == 0) {
222 return rs1;
223 } else if (rs1 == kRsMin && rs2 == -1) {
224 return 0;
225 } else {
226 return rs1 % rs2;
227 }
228}
229
230template<typename T> inline T
232{
233 return (rs2 == 0) ? rs1 : rs1 % rs2;
234}
235
236} // namespace RiscvISA
237} // namespace gem5
238
239#endif // __ARCH_RISCV_UTILITY_HH__
Defines global host-dependent types: Counter, Tick, and (indirectly) {int,uint}{8,...
Register ID: describe an architectural register with its class and index.
Definition reg_class.hh:93
Bitfield< 19, 16 > lo
const std::vector< std::string > RegNames
Definition float.hh:201
const std::vector< std::string > RegNames
Definition int.hh:125
int32_t mulh_32(int32_t rs1, int32_t rs2)
Definition utility.hh:167
bool issignalingnan< double >(double val)
Definition utility.hh:101
Bitfield< 24, 20 > rs2
Definition types.hh:81
uint64_t mulhu_64(uint64_t rs1, uint64_t rs2)
Definition utility.hh:148
bool isquietnan< double >(double val)
Definition utility.hh:81
T div(T rs1, T rs2)
Definition utility.hh:195
bool issignalingnan(T val)
Definition utility.hh:88
bool isquietnan< float >(float val)
Definition utility.hh:74
T rem(T rs1, T rs2)
Definition utility.hh:218
std::string registerName(RegId reg)
Definition utility.hh:108
int32_t mulhsu_32(int32_t rs1, uint32_t rs2)
Definition utility.hh:181
Bitfield< 19, 15 > rs1
Definition types.hh:80
int64_t mulhsu_64(int64_t rs1, uint64_t rs2)
Definition utility.hh:187
T remu(T rs1, T rs2)
Definition utility.hh:231
T divu(T rs1, T rs2)
Definition utility.hh:208
uint32_t mulhu_32(uint32_t rs1, uint32_t rs2)
Definition utility.hh:142
bool issignalingnan< float >(float val)
Definition utility.hh:94
int64_t mulh_64(int64_t rs1, int64_t rs2)
Definition utility.hh:173
bool isquietnan(T val)
Definition utility.hh:68
Bitfield< 5, 3 > reg
Definition types.hh:92
Bitfield< 63 > val
Definition misc.hh:776
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
@ FloatRegClass
Floating-point register.
Definition reg_class.hh:61
@ IntRegClass
Integer register.
Definition reg_class.hh:60

Generated on Mon Jul 10 2023 14:24:26 for gem5 by doxygen 1.9.7