gem5 [DEVELOP-FOR-25.0]
Loading...
Searching...
No Matches
cprintf_formats.hh
Go to the documentation of this file.
1/*
2 * Copyright (c) 2003-2005 The Regents of The University of Michigan
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#ifndef __BASE_CPRINTF_FORMATS_HH__
30#define __BASE_CPRINTF_FORMATS_HH__
31
32#include <cstdint>
33#include <cstring>
34#include <ostream>
35#include <sstream>
36
37#include "base/stl_helpers.hh"
38
39namespace gem5
40{
41
42namespace cp
43{
44
45struct Format
46{
53 enum
54 {
59 enum
60 {
67 enum
68 {
74 int width;
77
78 Format() { clear(); }
79
80 void
82 {
83 alternateForm = false;
84 flushLeft = false;
85 printSign = false;
86 blankSpace = false;
87 fillZero = false;
88 uppercase = false;
89 base = Dec;
90 format = None;
92 precision = -1;
93 width = 0;
94 getPrecision = false;
95 getWidth = false;
96 }
97};
98
99template <typename T>
100static inline void
101_formatChar(std::ostream &out, const T &data, Format &fmt)
102{
103 out << data;
104}
105
106template <typename T>
107static inline void
108_formatInteger(std::ostream &out, const T &data, Format &fmt)
109{
110 std::ios::fmtflags flags(out.flags());
111
112 switch (fmt.base) {
113 case Format::Hex:
114 out.setf(std::ios::hex, std::ios::basefield);
115 break;
116
117 case Format::Oct:
118 out.setf(std::ios::oct, std::ios::basefield);
119 break;
120
121 case Format::Dec:
122 out.setf(std::ios::dec, std::ios::basefield);
123 break;
124 }
125
126 if (fmt.alternateForm) {
127 if (!fmt.fillZero) {
128 out.setf(std::ios::showbase);
129 } else {
130 switch (fmt.base) {
131 case Format::Hex:
132 out << "0x";
133 fmt.width -= 2;
134 break;
135 case Format::Oct:
136 out << "0";
137 fmt.width -= 1;
138 break;
139 case Format::Dec:
140 break;
141 }
142 }
143 }
144
145 if (fmt.fillZero)
146 out.fill('0');
147
148 if (fmt.width > 0)
149 out.width(fmt.width);
150
151 if (fmt.flushLeft && !fmt.fillZero)
152 out.setf(std::ios::left);
153
154 if (fmt.printSign)
155 out.setf(std::ios::showpos);
156
157 if (fmt.uppercase)
158 out.setf(std::ios::uppercase);
159
160 out << data;
161
162 out.flags(flags);
163}
164
165template <typename T>
166static inline void
167_formatFloat(std::ostream &out, const T &data, Format &fmt)
168{
169 std::ios::fmtflags flags(out.flags());
170
171 if (fmt.fillZero)
172 out.fill('0');
173
174 switch (fmt.floatFormat) {
176 if (fmt.precision != -1) {
177 if (fmt.width > 0)
178 out.width(fmt.width);
179
180 if (fmt.precision == 0)
181 fmt.precision = 1;
182 else
183 out.setf(std::ios::scientific);
184
185 out.precision(fmt.precision);
186 } else if (fmt.width > 0) {
187 out.width(fmt.width);
188 }
189
190 if (fmt.uppercase)
191 out.setf(std::ios::uppercase);
192 break;
193
194 case Format::Fixed:
195 if (fmt.precision != -1) {
196 if (fmt.width > 0)
197 out.width(fmt.width);
198
199 out.setf(std::ios::fixed);
200 out.precision(fmt.precision);
201 } else if (fmt.width > 0) {
202 out.width(fmt.width);
203 }
204
205 break;
206
207 default:
208 if (fmt.precision != -1)
209 out.precision(fmt.precision);
210
211 if (fmt.width > 0)
212 out.width(fmt.width);
213
214 break;
215 }
216
217 out << data;
218
219 out.flags(flags);
220}
221
222template <typename T>
223static inline void
224_formatString(std::ostream &out, const T &data, Format &fmt)
225{
226 using stl_helpers::operator<<;
227 if (fmt.width > 0) {
228 std::stringstream foo;
229 foo << data;
230 int flen = foo.str().size();
231
232 if (fmt.width > flen) {
233 std::string spaces(fmt.width - flen, ' ');
234 if (fmt.flushLeft)
235 out << foo.str() << spaces;
236 else
237 out << spaces << foo.str();
238 } else {
239 out << data;
240 }
241 } else {
242 out << data;
243 }
244}
245
247//
248// The code below controls the actual usage of formats for various types
249//
250
251//
252// character formats
253//
254template <typename T>
255static inline void
256formatChar(std::ostream &out, const T &data, Format &fmt)
257{
258 out << "<bad arg type for char format>";
259}
260
261static inline void
262formatChar(std::ostream &out, char data, Format &fmt)
263{
264 _formatChar(out, data, fmt);
265}
266
267static inline void
268formatChar(std::ostream &out, unsigned char data, Format &fmt)
269{
270 _formatChar(out, data, fmt);
271}
272
273static inline void
274formatChar(std::ostream &out, signed char data, Format &fmt)
275{
276 _formatChar(out, data, fmt);
277}
278
279static inline void
280formatChar(std::ostream &out, short data, Format &fmt)
281{
282 _formatChar(out, (char)data, fmt);
283}
284
285static inline void
286formatChar(std::ostream &out, unsigned short data, Format &fmt)
287{
288 _formatChar(out, (char)data, fmt);
289}
290
291static inline void
292formatChar(std::ostream &out, int data, Format &fmt)
293{
294 _formatChar(out, (char)data, fmt);
295}
296
297static inline void
298formatChar(std::ostream &out, unsigned int data, Format &fmt)
299{
300 _formatChar(out, (char)data, fmt);
301}
302
303static inline void
304formatChar(std::ostream &out, long data, Format &fmt)
305{
306 _formatChar(out, (char)data, fmt);
307}
308
309static inline void
310formatChar(std::ostream &out, unsigned long data, Format &fmt)
311{
312 _formatChar(out, (char)data, fmt);
313}
314
315static inline void
316formatChar(std::ostream &out, long long data, Format &fmt)
317{
318 _formatChar(out, (char)data, fmt);
319}
320
321static inline void
322formatChar(std::ostream &out, unsigned long long data, Format &fmt)
323{
324 _formatChar(out, (char)data, fmt);
325}
326
327//
328// integer formats
329//
330template <typename T>
331static inline void
332formatInteger(std::ostream &out, const T &data, Format &fmt)
333{
334 _formatInteger(out, data, fmt);
335}
336static inline void
337formatInteger(std::ostream &out, char data, Format &fmt)
338{
339 _formatInteger(out, (int)data, fmt);
340}
341static inline void
342formatInteger(std::ostream &out, unsigned char data, Format &fmt)
343{
344 _formatInteger(out, (int)data, fmt);
345}
346static inline void
347formatInteger(std::ostream &out, signed char data, Format &fmt)
348{
349 _formatInteger(out, (int)data, fmt);
350}
351static inline void
352formatInteger(std::ostream &out, const unsigned char *data, Format &fmt)
353{
354 _formatInteger(out, (uintptr_t)data, fmt);
355}
356static inline void
357formatInteger(std::ostream &out, const signed char *data, Format &fmt)
358{
359 _formatInteger(out, (uintptr_t)data, fmt);
360}
361
362//
363// floating point formats
364//
365template <typename T>
366static inline void
367formatFloat(std::ostream &out, const T &data, Format &fmt)
368{
369 out << "<bad arg type for float format>";
370}
371
372static inline void
373formatFloat(std::ostream &out, float data, Format &fmt)
374{
375 _formatFloat(out, data, fmt);
376}
377
378static inline void
379formatFloat(std::ostream &out, double data, Format &fmt)
380{
381 _formatFloat(out, data, fmt);
382}
383
384//
385// string formats
386//
387template <typename T>
388static inline void
389formatString(std::ostream &out, const T &data, Format &fmt)
390{
391 _formatString(out, data, fmt);
392}
393
394} // namespace cp
395} // namespace gem5
396
397#endif // __CPRINTF_FORMATS_HH__
const char data[]
Bitfield< 31, 29 > format
static void _formatChar(std::ostream &out, const T &data, Format &fmt)
static void formatChar(std::ostream &out, const T &data, Format &fmt)
static void _formatInteger(std::ostream &out, const T &data, Format &fmt)
static void _formatFloat(std::ostream &out, const T &data, Format &fmt)
static void formatInteger(std::ostream &out, const T &data, Format &fmt)
static void _formatString(std::ostream &out, const T &data, Format &fmt)
static void formatString(std::ostream &out, const T &data, Format &fmt)
static void formatFloat(std::ostream &out, const T &data, Format &fmt)
Copyright (c) 2024 Arm Limited All rights reserved.
Definition binary32.hh:36
enum gem5::cp::Format::@053233316303234150347110140122163024114136372335 base
enum gem5::cp::Format::@342060060230017250137312363150313136323064143172 floatFormat
enum gem5::cp::Format::@054275224036272101153033032335116326275214075001 format

Generated on Mon May 26 2025 09:19:06 for gem5 by doxygen 1.13.2