gem5 v23.0.0.1
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
37namespace gem5
38{
39
40namespace cp
41{
42
43struct Format
44{
51 enum
52 {
55 Oct
57 enum
58 {
65 enum
66 {
72 int width;
75
76 Format() { clear(); }
77
78 void
80 {
81 alternateForm = false;
82 flushLeft = false;
83 printSign = false;
84 blankSpace = false;
85 fillZero = false;
86 uppercase = false;
87 base = Dec;
88 format = None;
90 precision = -1;
91 width = 0;
92 getPrecision = false;
93 getWidth = false;
94 }
95};
96
97template <typename T>
98static inline void
99_formatChar(std::ostream &out, const T &data, Format &fmt)
100{
101 out << data;
102}
103
104template <typename T>
105static inline void
106_formatInteger(std::ostream &out, const T &data, Format &fmt)
107{
108 std::ios::fmtflags flags(out.flags());
109
110 switch (fmt.base) {
111 case Format::Hex:
112 out.setf(std::ios::hex, std::ios::basefield);
113 break;
114
115 case Format::Oct:
116 out.setf(std::ios::oct, std::ios::basefield);
117 break;
118
119 case Format::Dec:
120 out.setf(std::ios::dec, std::ios::basefield);
121 break;
122 }
123
124 if (fmt.alternateForm) {
125 if (!fmt.fillZero) {
126 out.setf(std::ios::showbase);
127 } else {
128 switch (fmt.base) {
129 case Format::Hex:
130 out << "0x";
131 fmt.width -= 2;
132 break;
133 case Format::Oct:
134 out << "0";
135 fmt.width -= 1;
136 break;
137 case Format::Dec:
138 break;
139 }
140 }
141 }
142
143 if (fmt.fillZero)
144 out.fill('0');
145
146 if (fmt.width > 0)
147 out.width(fmt.width);
148
149 if (fmt.flushLeft && !fmt.fillZero)
150 out.setf(std::ios::left);
151
152 if (fmt.printSign)
153 out.setf(std::ios::showpos);
154
155 if (fmt.uppercase)
156 out.setf(std::ios::uppercase);
157
158 out << data;
159
160 out.flags(flags);
161}
162
163template <typename T>
164static inline void
165_formatFloat(std::ostream &out, const T &data, Format &fmt)
166{
167 std::ios::fmtflags flags(out.flags());
168
169 if (fmt.fillZero)
170 out.fill('0');
171
172 switch (fmt.floatFormat) {
174 if (fmt.precision != -1) {
175 if (fmt.width > 0)
176 out.width(fmt.width);
177
178 if (fmt.precision == 0)
179 fmt.precision = 1;
180 else
181 out.setf(std::ios::scientific);
182
183 out.precision(fmt.precision);
184 } else if (fmt.width > 0) {
185 out.width(fmt.width);
186 }
187
188 if (fmt.uppercase)
189 out.setf(std::ios::uppercase);
190 break;
191
192 case Format::Fixed:
193 if (fmt.precision != -1) {
194 if (fmt.width > 0)
195 out.width(fmt.width);
196
197 out.setf(std::ios::fixed);
198 out.precision(fmt.precision);
199 } else if (fmt.width > 0) {
200 out.width(fmt.width);
201 }
202
203 break;
204
205 default:
206 if (fmt.precision != -1)
207 out.precision(fmt.precision);
208
209 if (fmt.width > 0)
210 out.width(fmt.width);
211
212 break;
213 }
214
215 out << data;
216
217 out.flags(flags);
218}
219
220template <typename T>
221static inline void
222_formatString(std::ostream &out, const T &data, Format &fmt)
223{
224 if (fmt.width > 0) {
225 std::stringstream foo;
226 foo << data;
227 int flen = foo.str().size();
228
229 if (fmt.width > flen) {
230 char spaces[fmt.width - flen + 1];
231 std::memset(spaces, ' ', fmt.width - flen);
232 spaces[fmt.width - flen] = 0;
233
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[]
uint8_t flags
Definition helpers.cc:66
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)
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
enum gem5::cp::Format::@34 format
enum gem5::cp::Format::@35 floatFormat
enum gem5::cp::Format::@33 base

Generated on Mon Jul 10 2023 15:32:00 for gem5 by doxygen 1.9.7