gem5 v24.0.0.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 {
57 Oct
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 char spaces[fmt.width - flen + 1];
234 std::memset(spaces, ' ', fmt.width - flen);
235 spaces[fmt.width - flen] = 0;
236
237 if (fmt.flushLeft)
238 out << foo.str() << spaces;
239 else
240 out << spaces << foo.str();
241 } else {
242 out << data;
243 }
244 } else {
245 out << data;
246 }
247}
248
250//
251// The code below controls the actual usage of formats for various types
252//
253
254//
255// character formats
256//
257template <typename T>
258static inline void
259formatChar(std::ostream &out, const T &data, Format &fmt)
260{
261 out << "<bad arg type for char format>";
262}
263
264static inline void
265formatChar(std::ostream &out, char data, Format &fmt)
266{
267 _formatChar(out, data, fmt);
268}
269
270static inline void
271formatChar(std::ostream &out, unsigned char data, Format &fmt)
272{
273 _formatChar(out, data, fmt);
274}
275
276static inline void
277formatChar(std::ostream &out, signed char data, Format &fmt)
278{
279 _formatChar(out, data, fmt);
280}
281
282static inline void
283formatChar(std::ostream &out, short data, Format &fmt)
284{
285 _formatChar(out, (char)data, fmt);
286}
287
288static inline void
289formatChar(std::ostream &out, unsigned short data, Format &fmt)
290{
291 _formatChar(out, (char)data, fmt);
292}
293
294static inline void
295formatChar(std::ostream &out, int data, Format &fmt)
296{
297 _formatChar(out, (char)data, fmt);
298}
299
300static inline void
301formatChar(std::ostream &out, unsigned int data, Format &fmt)
302{
303 _formatChar(out, (char)data, fmt);
304}
305
306static inline void
307formatChar(std::ostream &out, long data, Format &fmt)
308{
309 _formatChar(out, (char)data, fmt);
310}
311
312static inline void
313formatChar(std::ostream &out, unsigned long data, Format &fmt)
314{
315 _formatChar(out, (char)data, fmt);
316}
317
318static inline void
319formatChar(std::ostream &out, long long data, Format &fmt)
320{
321 _formatChar(out, (char)data, fmt);
322}
323
324static inline void
325formatChar(std::ostream &out, unsigned long long data, Format &fmt)
326{
327 _formatChar(out, (char)data, fmt);
328}
329
330//
331// integer formats
332//
333template <typename T>
334static inline void
335formatInteger(std::ostream &out, const T &data, Format &fmt)
336{
337 _formatInteger(out, data, fmt);
338}
339static inline void
340formatInteger(std::ostream &out, char data, Format &fmt)
341{
342 _formatInteger(out, (int)data, fmt);
343}
344static inline void
345formatInteger(std::ostream &out, unsigned char data, Format &fmt)
346{
347 _formatInteger(out, (int)data, fmt);
348}
349static inline void
350formatInteger(std::ostream &out, signed char data, Format &fmt)
351{
352 _formatInteger(out, (int)data, fmt);
353}
354static inline void
355formatInteger(std::ostream &out, const unsigned char *data, Format &fmt)
356{
357 _formatInteger(out, (uintptr_t)data, fmt);
358}
359static inline void
360formatInteger(std::ostream &out, const signed char *data, Format &fmt)
361{
362 _formatInteger(out, (uintptr_t)data, fmt);
363}
364
365//
366// floating point formats
367//
368template <typename T>
369static inline void
370formatFloat(std::ostream &out, const T &data, Format &fmt)
371{
372 out << "<bad arg type for float format>";
373}
374
375static inline void
376formatFloat(std::ostream &out, float data, Format &fmt)
377{
378 _formatFloat(out, data, fmt);
379}
380
381static inline void
382formatFloat(std::ostream &out, double data, Format &fmt)
383{
384 _formatFloat(out, data, fmt);
385}
386
387//
388// string formats
389//
390template <typename T>
391static inline void
392formatString(std::ostream &out, const T &data, Format &fmt)
393{
394 _formatString(out, data, fmt);
395}
396
397} // namespace cp
398} // namespace gem5
399
400#endif // __CPRINTF_FORMATS_HH__
const char data[]
uint8_t flags
Definition helpers.cc:87
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 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
Definition binary32.hh:36
enum gem5::cp::Format::@45 floatFormat
enum gem5::cp::Format::@43 base
enum gem5::cp::Format::@44 format

Generated on Tue Jun 18 2024 16:24:00 for gem5 by doxygen 1.11.0