ETISS 0.11.2
ExtendableTranslatingInstructionSetSimulator(version0.11.2)
Loading...
Searching...
No Matches
FastString.h
Go to the documentation of this file.
1// SPDX-License-Identifier: BSD-3-Clause
2//
3// This file is part of ETISS. It is licensed under the BSD 3-Clause License; you may not use this file except in
4// compliance with the License. You should have received a copy of the license along with this project. If not, see the
5// LICENSE file.
6
7#ifndef ETISS_FASTSTRING_H_
8#define ETISS_FASTSTRING_H_
9
10#include <inttypes.h>
11#include <cstdlib>
12#include <type_traits>
13#include <vector>
14#include <string.h>
15#include <ostream>
16
17#include "etiss/jit/types.h"
18#if ETISS
19#include "etiss/Misc.h"
20#endif
21
22namespace etiss
23{
24
25namespace string
26{
27
28template <unsigned maxlen_plus1>
32void strcpy(char *dst, const char *src)
33{
34 size_t pos = 0;
35 while (pos < maxlen_plus1)
36 {
37 const char sc = src[pos];
38 if (unlikely(sc == 0))
39 break;
40 dst[pos++] = sc;
41 }
42 dst[pos] = 0;
43}
44
50{
51 private:
52 char *buffer;
53 size_t size;
54 std::vector<std::pair<size_t, size_t>> fields;
55
56 private:
58 size_t char_size(unsigned, size_t *, bool *) { return 0; }
59 template <typename... O>
60 size_t char_size(unsigned pos, size_t *size, bool *isPlaceholder, const char *s, O... others)
61 {
62 size[pos] = strlen(s);
63 isPlaceholder[pos] = false;
64 return size[pos] + char_size(pos + 1, size, isPlaceholder, others...);
65 }
66 template <typename... O>
67 size_t char_size(unsigned pos, size_t *size, bool *isPlaceholder, size_t len, O... others)
68 {
69 size[pos] = len;
70 isPlaceholder[pos] = true;
71 return size[pos] + char_size(pos + 1, size, isPlaceholder, others...);
72 }
73
74 private:
76 void char_copy(unsigned, char *, size_t *) {}
77 template <typename... O>
78 void char_copy(unsigned pos, char *buf, size_t *size, const char *s, O... others)
79 {
80 memcpy(buf, s, size[pos]); // copy string
81 char_copy(pos + 1, buf + size[pos], size, others...);
82 }
83 template <typename... O>
84 void char_copy(unsigned pos, char *buf, size_t *size, size_t /*ignored*/, O... others)
85 {
86 memset(buf, ' ', size[pos]); // fill with spaces (keep string valid)
87 char_copy(pos + 1, buf + size[pos], size, others...);
88 }
89
90 public:
91 template <typename... T>
92 form_string(T... str)
93 {
94
95 size_t element[sizeof...(str)];
96 bool placeholder[sizeof...(str)];
97 size_t total = char_size(0, element, placeholder, str...); // calculate total length
98
99 buffer = new char[total + 1]; // allocate buffer
100 size = total;
101
102 char_copy(0, buffer, element, str...); // copy strings, fill placeholders with ' '
103
104 buffer[size] = 0; // terminating 0
105
106 size_t offset = 0;
107 for (size_t i = 0; i < sizeof...(str); i++)
108 {
109 if (placeholder[i])
110 { // placeholder field
111 fields.push_back(std::make_pair(offset, element[i])); // store offset and length
112 }
113 offset = offset + element[i];
114 }
115 }
116
117 template <typename T>
118 std::enable_if<std::is_integral<T>::value, void> write_dec(unsigned field, T value)
119 {
120 std::pair<size_t, size_t> f = fields[field];
121 size_t pos = f.first + f.second;
122 do
123 {
124#if DEBUG
125 if (pos < f.first)
126 {
127#if ETISS
128 etiss::log(etiss::FATALERROR, "out of range buffer operation", ETISS_SRCLOC);
129#else
130 std::cerr << "out of range buffer operation" << std::endl;
131 abort();
132#endif
133 }
134#endif
135 T tmp = value / 10;
136 char c = (char)(value % 10); // the compiler should detect the division and use the remainder here without
137 // calculating modulo
138 value = tmp;
139 buffer[pos] = '0' + c;
140 pos--;
141 } while (value);
142 while (pos >= f.first)
143 {
144 buffer[pos] = ' ';
145 }
146 }
147
148 inline void write(unsigned field, const char *buf, size_t length)
149 {
150 auto f = fields[field];
151#if DEBUG
152 if (length > f.second)
153 {
154#if ETISS
155 etiss::log(etiss::FATALERROR, "out of range buffer operation", ETISS_SRCLOC);
156#else
157 std::cerr << "out of range buffer operation" << std::endl;
158 abort();
159#endif
160 }
161#endif
162 memcpy(buffer + f.first, buf, length);
163 memset(buffer + f.first, ' ', f.second - length);
164 }
165 inline void writet(unsigned field, const char *buf, size_t length, char terminator = ' ', char fill = ' ')
166 {
167 auto f = fields[field];
168#if DEBUG
169 if (length + 2 > f.second)
170 {
171#if ETISS
172 etiss::log(etiss::FATALERROR, "out of range buffer operation", ETISS_SRCLOC);
173#else
174 std::cerr << "out of range buffer operation" << std::endl;
175 abort();
176#endif
177 }
178#endif
179 memcpy(buffer + f.first, buf, length);
180 buffer[f.first + length] = terminator;
181 memset(buffer + f.first + length + 1, fill, f.second - length - 1);
182 }
183
184 inline char *c_str() { return buffer; }
185 inline const char *c_str() const { return buffer; }
186};
187
188std::ostream &operator<<(std::ostream &os, const form_string &fs)
189{
190 os << fs.c_str();
191 return os;
192}
193
194} // namespace string
195
196} // namespace etiss
197
198#endif // ETISS_FASTSTRING_H_
general configuration and logging
#define ETISS_SRCLOC
Definition Misc.h:202
__DEVICE__ void * memcpy(void *__a, const void *__b, size_t __c)
__DEVICE__ void * memset(void *__a, int __b, size_t __c)
__device__ __2f16 float c
__device__ __2f16 float bool s
#define unlikely(x)
Definition types.h:36
void write(unsigned field, const char *buf, size_t length)
Definition FastString.h:148
size_t char_size(unsigned pos, size_t *size, bool *isPlaceholder, const char *s, O... others)
Definition FastString.h:60
const char * c_str() const
Definition FastString.h:185
size_t char_size(unsigned, size_t *, bool *)
terminates template recursion
Definition FastString.h:58
size_t char_size(unsigned pos, size_t *size, bool *isPlaceholder, size_t len, O... others)
Definition FastString.h:67
std::vector< std::pair< size_t, size_t > > fields
list of field in the form_string. (offset,length) pairs
Definition FastString.h:54
std::enable_if< std::is_integral< T >::value, void > write_dec(unsigned field, T value)
Definition FastString.h:118
void writet(unsigned field, const char *buf, size_t length, char terminator=' ', char fill=' ')
Definition FastString.h:165
void char_copy(unsigned, char *, size_t *)
terminates template recursion
Definition FastString.h:76
void char_copy(unsigned pos, char *buf, size_t *size, const char *s, O... others)
Definition FastString.h:78
void char_copy(unsigned pos, char *buf, size_t *size, size_t, O... others)
Definition FastString.h:84
std::ostream & operator<<(std::ostream &os, const form_string &fs)
Definition FastString.h:188
void strcpy(char *dst, const char *src)
Definition FastString.h:32
forwards: include/jit/*
Definition Benchmark.h:17
@ FATALERROR
Definition Misc.h:84
void log(Verbosity level, std::string msg)
write log message at the given level.
Definition Misc.cpp:94
float __ovld __cnfn length(float p)
Return the length of vector p, i.e., sqrt(p.x2 + p.y 2 + ...)