ETISS 0.8.0
Extendable Translating Instruction Set Simulator (version 0.8.0)
FastString.h
Go to the documentation of this file.
1 /*
2 
3  @copyright
4 
5  <pre>
6 
7  Copyright 2018 Infineon Technologies AG
8 
9  This file is part of ETISS tool, see <https://github.com/tum-ei-eda/etiss>.
10 
11  The initial version of this software has been created with the funding support by the German Federal
12  Ministry of Education and Research (BMBF) in the project EffektiV under grant 01IS13022.
13 
14  Redistribution and use in source and binary forms, with or without modification, are permitted
15  provided that the following conditions are met:
16 
17  1. Redistributions of source code must retain the above copyright notice, this list of conditions and
18  the following disclaimer.
19 
20  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
21  and the following disclaimer in the documentation and/or other materials provided with the distribution.
22 
23  3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse
24  or promote products derived from this software without specific prior written permission.
25 
26  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
27  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
28  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
29  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  POSSIBILITY OF SUCH DAMAGE.
34 
35  </pre>
36 
37  @author Marc Greim <marc.greim@mytum.de>, Chair of Electronic Design Automation, TUM
38 
39  @version 0.1
40 
41 */
42 
43 #ifndef ETISS_FASTSTRING_H_
44 #define ETISS_FASTSTRING_H_
45 
46 #include <inttypes.h>
47 #include <cstdlib>
48 #include <type_traits>
49 #if ETISS
50 #include "etiss/Misc.h"
51 #endif
52 
53 namespace etiss
54 {
55 
56 namespace string
57 {
58 
59 template <unsigned maxlen_plus1>
63 void strcpy(char *dst, const char *src)
64 {
65  size_t pos = 0;
66  while (pos < maxlen_plus1)
67  {
68  const char sc = src[pos];
69  if (unlikely(sc == 0))
70  break;
71  dst[pos++] = sc;
72  }
73  dst[pos] = 0;
74 }
75 
81 {
82  private:
83  char *buffer;
84  size_t size;
85  std::vector<std::pair<size_t, size_t>> fields;
86 
87  private:
89  size_t char_size(unsigned, size_t *, bool *) { return 0; }
90  template <typename... O>
91  size_t char_size(unsigned pos, size_t *size, bool *isPlaceholder, const char *s, O... others)
92  {
93  size[pos] = strlen(s);
94  isPlaceholder[pos] = false;
95  return size[pos] + char_size(pos + 1, size, isPlaceholder, others...);
96  }
97  template <typename... O>
98  size_t char_size(unsigned pos, size_t *size, bool *isPlaceholder, size_t len, O... others)
99  {
100  size[pos] = len;
101  isPlaceholder[pos] = true;
102  return size[pos] + char_size(pos + 1, size, isPlaceholder, others...);
103  }
104 
105  private:
107  void char_copy(unsigned, char *, size_t *) {}
108  template <typename... O>
109  void char_copy(unsigned pos, char *buf, size_t *size, const char *s, O... others)
110  {
111  memcpy(buf, s, size[pos]); // copy string
112  char_copy(pos + 1, buf + size[pos], size, others...);
113  }
114  template <typename... O>
115  void char_copy(unsigned pos, char *buf, size_t *size, size_t /*ignored*/, O... others)
116  {
117  memset(buf, ' ', size[pos]); // fill with spaces (keep string valid)
118  char_copy(pos + 1, buf + size[pos], size, others...);
119  }
120 
121  public:
122  template <typename... T>
124  {
125 
126  size_t element[sizeof...(str)];
127  bool placeholder[sizeof...(str)];
128  size_t total = char_size(0, element, placeholder, str...); // calculate total length
129 
130  buffer = new char[total + 1]; // allocate buffer
131  size = total;
132 
133  char_copy(0, buffer, element, str...); // copy strings, fill placeholders with ' '
134 
135  buffer[size] = 0; // terminating 0
136 
137  size_t offset = 0;
138  for (size_t i = 0; i < sizeof...(str); i++)
139  {
140  if (placeholder[i])
141  { // placeholder field
142  fields.push_back(std::make_pair(offset, element[i])); // store offset and length
143  }
144  offset = offset + element[i];
145  }
146  }
147 
148  template <typename T>
149  std::enable_if<std::is_integral<T>::value, void> write_dec(unsigned field, T value)
150  {
151  std::pair<size_t, size_t> f = fields[field];
152  size_t pos = f.first + f.second;
153  do
154  {
155 #if DEBUG
156  if (pos < f.first)
157  {
158 #if ETISS
159  etiss::log(etiss::FATALERROR, "out of range buffer operation", ETISS_SRCLOC);
160 #else
161  std::cerr << "out of range buffer operation" << std::endl;
162  abort();
163 #endif
164  }
165 #endif
166  T tmp = value / 10;
167  char c = (char)(value % 10); // the compiler should detect the division and use the remainder here without
168  // calculating modulo
169  value = tmp;
170  buffer[pos] = '0' + c;
171  pos--;
172  } while (value);
173  while (pos >= f.first)
174  {
175  buffer[pos] = ' ';
176  }
177  }
178 
179  inline void write(unsigned field, const char *buf, size_t length)
180  {
181  auto f = fields[field];
182 #if DEBUG
183  if (length > f.second)
184  {
185 #if ETISS
186  etiss::log(etiss::FATALERROR, "out of range buffer operation", ETISS_SRCLOC);
187 #else
188  std::cerr << "out of range buffer operation" << std::endl;
189  abort();
190 #endif
191  }
192 #endif
193  memcpy(buffer + f.first, buf, length);
194  memset(buffer + f.first, ' ', f.second - length);
195  }
196  inline void writet(unsigned field, const char *buf, size_t length, char terminator = ' ', char fill = ' ')
197  {
198  auto f = fields[field];
199 #if DEBUG
200  if (length + 2 > f.second)
201  {
202 #if ETISS
203  etiss::log(etiss::FATALERROR, "out of range buffer operation", ETISS_SRCLOC);
204 #else
205  std::cerr << "out of range buffer operation" << std::endl;
206  abort();
207 #endif
208  }
209 #endif
210  memcpy(buffer + f.first, buf, length);
211  buffer[f.first + length] = terminator;
212  memset(buffer + f.first + length + 1, fill, f.second - length - 1);
213  }
214 
215  inline char *c_str() { return buffer; }
216  inline const char *c_str() const { return buffer; }
217 };
218 
219 std::ostream &operator<<(std::ostream &os, const form_string &fs)
220 {
221  os << fs.c_str();
222  return os;
223 }
224 
225 } // namespace string
226 
227 } // namespace etiss
228 
229 #endif // ETISS_FASTSTRING_H_
general configuration and logging
#define ETISS_SRCLOC
Definition: Misc.h:239
__DEVICE__ void * memset(void *__a, int __b, size_t __c)
__DEVICE__ void * memcpy(void *__a, const void *__b, size_t __c)
__device__ __2f16 float c
__device__ __2f16 float bool s
#define unlikely(x)
Definition: types.h:74
void write(unsigned field, const char *buf, size_t length)
Definition: FastString.h:179
size_t char_size(unsigned pos, size_t *size, bool *isPlaceholder, const char *s, O... others)
Definition: FastString.h:91
std::enable_if< std::is_integral< T >::value, void > write_dec(unsigned field, T value)
Definition: FastString.h:149
const char * c_str() const
Definition: FastString.h:216
size_t char_size(unsigned, size_t *, bool *)
terminates template recursion
Definition: FastString.h:89
size_t char_size(unsigned pos, size_t *size, bool *isPlaceholder, size_t len, O... others)
Definition: FastString.h:98
std::vector< std::pair< size_t, size_t > > fields
list of field in the form_string. (offset,length) pairs
Definition: FastString.h:85
void writet(unsigned field, const char *buf, size_t length, char terminator=' ', char fill=' ')
Definition: FastString.h:196
void char_copy(unsigned, char *, size_t *)
terminates template recursion
Definition: FastString.h:107
void char_copy(unsigned pos, char *buf, size_t *size, const char *s, O... others)
Definition: FastString.h:109
void char_copy(unsigned pos, char *buf, size_t *size, size_t, O... others)
Definition: FastString.h:115
void strcpy(char *dst, const char *src)
Definition: FastString.h:63
std::ostream & operator<<(std::ostream &os, const form_string &fs)
Definition: FastString.h:219
Page Table Entry (PTE) defines the composition of Page Frame Number (PFN) and relavant flags.
Definition: Benchmark.h:53
@ FATALERROR
Definition: Misc.h:126
void log(Verbosity level, std::string msg)
write log message at the given level.
Definition: Misc.cpp:125
float __ovld __cnfn length(float p)
Return the length of vector p, i.e., sqrt(p.x2 + p.y 2 + ...)