gem5 v24.0.0.0
Loading...
Searching...
No Matches
mxfp.hh
Go to the documentation of this file.
1/*
2 * Copyright (c) 2024 Advanced Micro Devices, 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 met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its
16 * contributors may be used to endorse or promote products derived from this
17 * software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#ifndef __ARCH_AMDGPU_COMMON_DTYPE_MXFP_HH__
33#define __ARCH_AMDGPU_COMMON_DTYPE_MXFP_HH__
34
35#include <cmath>
36#include <cstdint>
37#include <iostream>
38
40
41namespace gem5
42{
43
44namespace AMDGPU
45{
46
47// Base class for all microscaling types. The sizes of everything are
48// determined by the enum fields in the FMT struct. All of these share the
49// same operator overloads which convert to float before arithmetic and
50// convert back if assigned to a microscaling type.
51template<typename FMT>
52class mxfp
53{
54 public:
55 mxfp() = default;
57 {
59 }
60
61 // Set raw bits, used by gem5 to set a raw value read from VGPRs.
62 mxfp(const uint32_t& raw)
63 {
64 // The info unions end up being "left" aligned. For example, in FP4
65 // only the bits 31:28 are used. Shift the input by the storage size
66 // of 32 by the type size (sign + exponent + mantissa bits).
67 data = raw;
68 data <<= (32 - int(FMT::sbits) - int(FMT::ebits) - int(FMT::mbits));
69 }
70
71 mxfp(const mxfp& f)
72 {
73 FMT conv_out;
74 conv_out = convertMXFP<FMT, decltype(f.getFmt())>(f.getFmt());
75 data = conv_out.storage;
76 }
77
78 mxfp&
79 operator=(const float& f)
80 {
82 return *this;
83 }
84
85 mxfp&
87 {
88 FMT conv_out;
89 conv_out = convertMXFP<FMT, decltype(f.getFmt())>(f.getFmt());
90 data = conv_out.storage;
91 return *this;
92 }
93
94 operator float() const
95 {
96 binary32 out;
97 FMT in;
98 in.storage = data;
100
101 return out.fp32;
102 }
103
104 constexpr static int
106 {
107 return int(FMT::mbits) + int(FMT::ebits) + int(FMT::sbits);
108 }
109
110 // Intentionally use storage > size() so that a storage type is not needed
111 // as a template parameter.
112 uint32_t data = 0;
113
114 FMT
115 getFmt() const
116 {
117 FMT out;
118 out.storage = data;
119 return out;
120 }
121
122 void
123 setFmt(FMT in)
124 {
125 data = in.storage;
126 }
127
128 void
129 scale(const float& f)
130 {
131 binary32 bfp;
132 bfp.fp32 = f;
133 int scale_val = bfp.exp - bfp.bias;
134
135 // Scale value of 0xFF is NaN. Scaling by NaN returns NaN.
136 // In this implementation, types without NaN define it as zero.
137 if (scale_val == 0xFF) {
138 data = FMT::nan;
139 return;
140 }
141
142 FMT in = getFmt();
143 int exp = in.exp;
144
145 if (exp + scale_val > max_exp<FMT>()) {
146 in.exp = max_exp<FMT>();
147 } else if (exp + scale_val < min_exp<FMT>()) {
148 in.exp = min_exp<FMT>();
149 } else {
150 in.exp = exp + scale_val;
151 }
152
153 data = in.storage;
154 }
155
156 private:
158
159 uint32_t
161 {
162 if (std::isinf(f)) {
163 assert(std::numeric_limits<FMT>::has_infinity);
164 return FMT::inf;
165 }
166
167 if (std::isnan(f)) {
168 assert(std::numeric_limits<FMT>::has_quiet_NaN);
169 return FMT::nan;
170 }
171
172 return float_to_mxfp_nocheck(f);
173 }
174
175 uint32_t
177 {
178 binary32 in;
179 in.fp32 = f;
180
181 FMT out;
182 out.storage = 0;
183
185
186 return out.storage;
187 }
188};
189
190// Unary operators
191template<typename T>
192inline T operator+(T a)
193{
194 return a;
195}
196
197template<typename T>
198inline T operator-(T a)
199{
200 // Flip sign bit
201 a.data ^= 0x80000000;
202 return a;
203}
204
205template<typename T>
206inline T operator++(T a)
207{
208 a = a + T(1.0f);
209 return a;
210}
211
212template<typename T>
213inline T operator--(T a)
214{
215 a = a - T(1.0f);
216 return a;
217}
218
219template<typename T>
220inline T operator++(T a, int)
221{
222 T original = a;
223 ++a;
224 return original;
225}
226
227template<typename T>
228inline T operator--(T a, int)
229{
230 T original = a;
231 --a;
232 return original;
233}
234
235// Math operators
236template<typename T>
237inline T operator+(T a, T b)
238{
239 return T(float(a) + float(b));
240}
241
242template<typename T>
243inline T operator-(T a, T b)
244{
245 return T(float(a) - float(b));
246}
247
248template<typename T>
249inline T operator*(T a, T b)
250{
251 return T(float(a) * float(b));
252}
253
254template<typename T>
255inline T operator/(T a, T b)
256{
257 return T(float(a) / float(b));
258}
259
260template<typename T>
261inline T operator+=(T &a, T b)
262{
263 a = a + b;
264 return a;
265}
266
267template<typename T>
268inline T operator-=(T &a, T b)
269{
270 a = a - b;
271 return a;
272}
273
274template<typename T>
275inline T operator*=(T &a, T b)
276{
277 a = a * b;
278 return a;
279}
280
281template<typename T>
282inline T operator/=(T &a, T b)
283{
284 a = a / b;
285 return a;
286}
287
288// Comparison operators
289template<typename T>
290inline bool operator<(T a, T b)
291{
292 return float(a) < float(b);
293}
294
295template<typename T>
296inline bool operator>(T a, T b)
297{
298 return float(a) > float(b);
299}
300
301template<typename T>
302inline bool operator<=(T a, T b)
303{
304 return float(a) <= float(b);
305}
306
307template<typename T>
308inline bool operator>=(T a, T b)
309{
310 return float(a) >= float(b);
311}
312
313template<typename T>
314inline bool operator==(T a, T b)
315{
316 return float(a) == float(b);
317}
318
319template<typename T>
320inline bool operator!=(T a, T b)
321{
322 return float(a) != float(b);
323}
324
325} // namespace AMDGPU
326
327} // namespace gem5
328
329#endif // __ARCH_AMDGPU_COMMON_DTYPE_MXFP_HH__
mxfp(const mxfp &f)
Definition mxfp.hh:71
FMT getFmt() const
Definition mxfp.hh:115
void setFmt(FMT in)
Definition mxfp.hh:123
mxfp & operator=(const mxfp &f)
Definition mxfp.hh:86
uint32_t float_to_mxfp(float f)
Definition mxfp.hh:160
static constexpr int size()
Definition mxfp.hh:105
mxfp(const uint32_t &raw)
Definition mxfp.hh:62
mxfpRoundingMode mode
Definition mxfp.hh:157
void scale(const float &f)
Definition mxfp.hh:129
uint32_t float_to_mxfp_nocheck(float f)
Definition mxfp.hh:176
mxfp & operator=(const float &f)
Definition mxfp.hh:79
mxfp(float f)
Definition mxfp.hh:56
uint32_t data
Definition mxfp.hh:112
T operator-=(T &a, T b)
Definition mxfp.hh:268
bool operator<(T a, T b)
Definition mxfp.hh:290
bool operator==(T a, T b)
Definition mxfp.hh:314
T operator*=(T &a, T b)
Definition mxfp.hh:275
T operator--(T a)
Definition mxfp.hh:213
T operator++(T a)
Definition mxfp.hh:206
T operator*(T a, T b)
Definition mxfp.hh:249
T operator-(T a)
Definition mxfp.hh:198
T operator+(T a)
Definition mxfp.hh:192
T operator+=(T &a, T b)
Definition mxfp.hh:261
dFMT convertMXFP(sFMT in, mxfpRoundingMode mode=roundTiesToEven, uint32_t seed=0)
bool operator!=(T a, T b)
Definition mxfp.hh:320
bool operator<=(T a, T b)
Definition mxfp.hh:302
T operator/(T a, T b)
Definition mxfp.hh:255
bool operator>=(T a, T b)
Definition mxfp.hh:308
bool operator>(T a, T b)
Definition mxfp.hh:296
T operator/=(T &a, T b)
Definition mxfp.hh:282
Bitfield< 7 > b
Bitfield< 8 > a
Definition misc_types.hh:66
Bitfield< 6 > f
Definition misc_types.hh:68
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
Definition binary32.hh:36
constexpr bool isinf(gem5::AMDGPU::fp16_e5m10_info a)
Definition fp16_e5m10.hh:78
constexpr bool isnan(gem5::AMDGPU::fp16_e5m10_info a)
Definition fp16_e5m10.hh:83

Generated on Tue Jun 18 2024 16:23:39 for gem5 by doxygen 1.11.0