gem5  v21.0.1.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
utility.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2007 MIPS Technologies, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met: redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer;
9  * redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution;
12  * neither the name of the copyright holders nor the names of its
13  * contributors may be used to endorse or promote products derived from
14  * this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include "arch/mips/utility.hh"
30 
31 #include <cmath>
32 
33 #include "arch/mips/isa_traits.hh"
34 #include "arch/mips/registers.hh"
35 #include "base/bitfield.hh"
36 #include "base/logging.hh"
37 #include "cpu/static_inst.hh"
38 #include "cpu/thread_context.hh"
39 #include "sim/serialize.hh"
40 
41 using namespace MipsISA;
42 
43 namespace MipsISA {
44 
45 uint64_t
46 fpConvert(ConvertType cvt_type, double fp_val)
47 {
48 
49  switch (cvt_type)
50  {
51  case SINGLE_TO_DOUBLE:
52  {
53  double sdouble_val = fp_val;
54  void *sdouble_ptr = &sdouble_val;
55  uint64_t sdp_bits = *(uint64_t *) sdouble_ptr;
56  return sdp_bits;
57  }
58 
59  case SINGLE_TO_WORD:
60  {
61  int32_t sword_val = (int32_t) fp_val;
62  void *sword_ptr = &sword_val;
63  uint64_t sword_bits= *(uint32_t *) sword_ptr;
64  return sword_bits;
65  }
66 
67  case WORD_TO_SINGLE:
68  {
69  float wfloat_val = fp_val;
70  void *wfloat_ptr = &wfloat_val;
71  uint64_t wfloat_bits = *(uint32_t *) wfloat_ptr;
72  return wfloat_bits;
73  }
74 
75  case WORD_TO_DOUBLE:
76  {
77  double wdouble_val = fp_val;
78  void *wdouble_ptr = &wdouble_val;
79  uint64_t wdp_bits = *(uint64_t *) wdouble_ptr;
80  return wdp_bits;
81  }
82 
83  default:
84  panic("Invalid Floating Point Conversion Type (%d). See \"types.hh\" for List of Conversions\n",cvt_type);
85  return 0;
86  }
87 }
88 
89 double
90 roundFP(double val, int digits)
91 {
92  double digit_offset = pow(10.0,digits);
93  val = val * digit_offset;
94  val = val + 0.5;
95  val = floor(val);
96  val = val / digit_offset;
97  return val;
98 }
99 
100 double
101 truncFP(double val)
102 {
103  int trunc_val = (int) val;
104  return (double) trunc_val;
105 }
106 
107 bool
108 getCondCode(uint32_t fcsr, int cc_idx)
109 {
110  int shift = (cc_idx == 0) ? 23 : cc_idx + 24;
111  bool cc_val = (fcsr >> shift) & 0x00000001;
112  return cc_val;
113 }
114 
115 uint32_t
116 genCCVector(uint32_t fcsr, int cc_num, uint32_t cc_val)
117 {
118  int cc_idx = (cc_num == 0) ? 23 : cc_num + 24;
119 
120  fcsr = bits(fcsr, 31, cc_idx + 1) << (cc_idx + 1) |
121  cc_val << cc_idx |
122  bits(fcsr, cc_idx - 1, 0);
123 
124  return fcsr;
125 }
126 
127 uint32_t
128 genInvalidVector(uint32_t fcsr_bits)
129 {
130  //Set FCSR invalid in "flag" field
131  int invalid_offset = Invalid + Flag_Field;
132  fcsr_bits = fcsr_bits | (1 << invalid_offset);
133 
134  //Set FCSR invalid in "cause" flag
135  int cause_offset = Invalid + Cause_Field;
136  fcsr_bits = fcsr_bits | (1 << cause_offset);
137 
138  return fcsr_bits;
139 }
140 
141 bool
142 isNan(void *val_ptr, int size)
143 {
144  switch (size)
145  {
146  case 32:
147  {
148  uint32_t val_bits = *(uint32_t *) val_ptr;
149  return (bits(val_bits, 30, 23) == 0xFF);
150  }
151 
152  case 64:
153  {
154  uint64_t val_bits = *(uint64_t *) val_ptr;
155  return (bits(val_bits, 62, 52) == 0x7FF);
156  }
157 
158  default:
159  panic("Type unsupported. Size mismatch\n");
160  }
161 }
162 
163 
164 bool
165 isQnan(void *val_ptr, int size)
166 {
167  switch (size)
168  {
169  case 32:
170  {
171  uint32_t val_bits = *(uint32_t *) val_ptr;
172  return (bits(val_bits, 30, 22) == 0x1FE);
173  }
174 
175  case 64:
176  {
177  uint64_t val_bits = *(uint64_t *) val_ptr;
178  return (bits(val_bits, 62, 51) == 0xFFE);
179  }
180 
181  default:
182  panic("Type unsupported. Size mismatch\n");
183  }
184 }
185 
186 bool
187 isSnan(void *val_ptr, int size)
188 {
189  switch (size)
190  {
191  case 32:
192  {
193  uint32_t val_bits = *(uint32_t *) val_ptr;
194  return (bits(val_bits, 30, 22) == 0x1FF);
195  }
196 
197  case 64:
198  {
199  uint64_t val_bits = *(uint64_t *) val_ptr;
200  return (bits(val_bits, 62, 51) == 0xFFF);
201  }
202 
203  default:
204  panic("Type unsupported. Size mismatch\n");
205  }
206 }
207 
208 void
210 {
211  // First loop through the integer registers.
212  for (int i = 0; i < NumIntRegs; i++)
213  dest->setIntRegFlat(i, src->readIntRegFlat(i));
214 
215  // Then loop through the floating point registers.
216  for (int i = 0; i < NumFloatRegs; i++)
217  dest->setFloatRegFlat(i, src->readFloatRegFlat(i));
218 
219  // Would need to add condition-code regs if implemented
220  assert(NumCCRegs == 0);
221 
222  // Copy misc. registers
223  for (int i = 0; i < NumMiscRegs; i++)
225 
226  // Copy over the PC State
227  dest->pcState(src->pcState());
228 }
229 
230 void
232 {
233  panic("Copy Misc. Regs Not Implemented Yet\n");
234 }
235 
236 } // namespace MipsISA
ThreadContext::readMiscRegNoEffect
virtual RegVal readMiscRegNoEffect(RegIndex misc_reg) const =0
ThreadContext::readIntRegFlat
virtual RegVal readIntRegFlat(RegIndex idx) const =0
Flat register interfaces.
MipsISA::genCCVector
uint32_t genCCVector(uint32_t fcsr, int cc_num, uint32_t cc_val)
Definition: utility.cc:116
MipsISA::truncFP
double truncFP(double val)
Definition: utility.cc:101
serialize.hh
registers.hh
MipsISA::NumIntRegs
const int NumIntRegs
Definition: registers.hh:50
MipsISA::NumCCRegs
const int NumCCRegs
Definition: registers.hh:56
MipsISA::WORD_TO_DOUBLE
@ WORD_TO_DOUBLE
Definition: types.hh:59
MipsISA::Cause_Field
@ Cause_Field
Definition: registers.hh:81
MipsISA::SINGLE_TO_DOUBLE
@ SINGLE_TO_DOUBLE
Definition: types.hh:45
MipsISA::roundFP
double roundFP(double val, int digits)
Definition: utility.cc:90
ThreadContext::setFloatRegFlat
virtual void setFloatRegFlat(RegIndex idx, RegVal val)=0
MipsISA
Definition: decoder.cc:31
ThreadContext
ThreadContext is the external interface to all thread state for anything outside of the CPU.
Definition: thread_context.hh:88
bitfield.hh
ArmISA::shift
Bitfield< 6, 5 > shift
Definition: types.hh:126
ThreadContext::setIntRegFlat
virtual void setIntRegFlat(RegIndex idx, RegVal val)=0
MipsISA::fpConvert
uint64_t fpConvert(ConvertType cvt_type, double fp_val)
Definition: utility.cc:46
MipsISA::WORD_TO_SINGLE
@ WORD_TO_SINGLE
Definition: types.hh:58
MipsISA::isNan
bool isNan(void *val_ptr, int size)
Definition: utility.cc:142
utility.hh
static_inst.hh
ThreadContext::readFloatRegFlat
virtual RegVal readFloatRegFlat(RegIndex idx) const =0
X86ISA::val
Bitfield< 63 > val
Definition: misc.hh:769
MipsISA::NumFloatRegs
const int NumFloatRegs
Definition: registers.hh:51
ThreadContext::pcState
virtual TheISA::PCState pcState() const =0
MipsISA::Invalid
@ Invalid
Definition: registers.hh:74
MipsISA::SINGLE_TO_WORD
@ SINGLE_TO_WORD
Definition: types.hh:46
MipsISA::i
Bitfield< 2 > i
Definition: pra_constants.hh:276
MipsISA::NumMiscRegs
const int NumMiscRegs
Definition: registers.hh:269
MipsISA::ConvertType
ConvertType
Definition: types.hh:44
MipsISA::getCondCode
bool getCondCode(uint32_t fcsr, int cc_idx)
Definition: utility.cc:108
MipsISA::copyMiscRegs
void copyMiscRegs(ThreadContext *src, ThreadContext *dest)
Definition: utility.cc:231
MipsISA::copyRegs
void copyRegs(ThreadContext *src, ThreadContext *dest)
Definition: utility.cc:209
logging.hh
isa_traits.hh
bits
constexpr T bits(T val, unsigned first, unsigned last)
Extract the bitfield from position 'first' to 'last' (inclusive) from 'val' and right justify it.
Definition: bitfield.hh:73
MipsISA::isQnan
bool isQnan(void *val_ptr, int size)
Definition: utility.cc:165
ThreadContext::setMiscRegNoEffect
virtual void setMiscRegNoEffect(RegIndex misc_reg, RegVal val)=0
MipsISA::isSnan
bool isSnan(void *val_ptr, int size)
Definition: utility.cc:187
MipsISA::Flag_Field
@ Flag_Field
Definition: registers.hh:79
thread_context.hh
MipsISA::genInvalidVector
uint32_t genInvalidVector(uint32_t fcsr_bits)
Definition: utility.cc:128
panic
#define panic(...)
This implements a cprintf based panic() function.
Definition: logging.hh:171

Generated on Tue Jun 22 2021 15:28:21 for gem5 by doxygen 1.8.17