dump.cpp
10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
* Copyright Andrey Semashev 2007 - 2021.
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE_1_0.txt or copy at
* http://www.boost.org/LICENSE_1_0.txt)
*/
/*!
* \file dump.cpp
* \author Andrey Semashev
* \date 03.05.2013
*
* \brief This header is the Boost.Log library implementation, see the library documentation
* at http://www.boost.org/doc/libs/release/libs/log/doc/html/index.html.
*/
#include <boost/log/detail/config.hpp>
#include <ostream>
#include <boost/cstdint.hpp>
#include <boost/log/utility/manipulators/dump.hpp>
#if defined(_MSC_VER) && (defined(BOOST_LOG_USE_SSSE3) || defined(BOOST_LOG_USE_AVX2))
#include <boost/winapi/dll.hpp>
#include <intrin.h> // __cpuid
#endif
#include <boost/log/detail/header.hpp>
namespace boost {
BOOST_LOG_OPEN_NAMESPACE
namespace aux {
#if defined(BOOST_LOG_USE_SSSE3)
extern dump_data_char_t dump_data_char_ssse3;
extern dump_data_wchar_t dump_data_wchar_ssse3;
#if !defined(BOOST_NO_CXX11_CHAR16_T)
extern dump_data_char16_t dump_data_char16_ssse3;
#endif
#if !defined(BOOST_NO_CXX11_CHAR32_T)
extern dump_data_char32_t dump_data_char32_ssse3;
#endif
extern dump_data_char_t dump_data_char_ssse3_slow_pshufb;
extern dump_data_wchar_t dump_data_wchar_ssse3_slow_pshufb;
#if !defined(BOOST_NO_CXX11_CHAR16_T)
extern dump_data_char16_t dump_data_char16_ssse3_slow_pshufb;
#endif
#if !defined(BOOST_NO_CXX11_CHAR32_T)
extern dump_data_char32_t dump_data_char32_ssse3_slow_pshufb;
#endif
#endif
#if defined(BOOST_LOG_USE_AVX2)
extern dump_data_char_t dump_data_char_avx2;
extern dump_data_wchar_t dump_data_wchar_avx2;
#if !defined(BOOST_NO_CXX11_CHAR16_T)
extern dump_data_char16_t dump_data_char16_avx2;
#endif
#if !defined(BOOST_NO_CXX11_CHAR32_T)
extern dump_data_char32_t dump_data_char32_avx2;
#endif
#endif
enum { stride = 256 };
BOOST_ALIGNMENT(16) extern const char g_hex_char_table[2][16] =
{
{ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' },
{ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }
};
template< typename CharT >
void dump_data_generic(const void* data, std::size_t size, std::basic_ostream< CharT >& strm)
{
typedef CharT char_type;
char_type buf[stride * 3u];
const char* const char_table = g_hex_char_table[(strm.flags() & std::ios_base::uppercase) != 0];
const std::size_t stride_count = size / stride, tail_size = size % stride;
const uint8_t* p = static_cast< const uint8_t* >(data);
char_type* buf_begin = buf + 1u; // skip the first space of the first chunk
char_type* buf_end = buf + sizeof(buf) / sizeof(*buf);
for (std::size_t i = 0; i < stride_count; ++i)
{
char_type* b = buf;
for (unsigned int j = 0; j < stride; ++j, b += 3u, ++p)
{
uint32_t n = *p;
b[0] = static_cast< char_type >(' ');
b[1] = static_cast< char_type >(char_table[n >> 4]);
b[2] = static_cast< char_type >(char_table[n & 0x0F]);
}
strm.write(buf_begin, buf_end - buf_begin);
buf_begin = buf;
}
if (tail_size > 0)
{
char_type* b = buf;
unsigned int i = 0;
do
{
uint32_t n = *p;
b[0] = static_cast< char_type >(' ');
b[1] = static_cast< char_type >(char_table[n >> 4]);
b[2] = static_cast< char_type >(char_table[n & 0x0F]);
++i;
++p;
b += 3u;
}
while (i < tail_size);
strm.write(buf_begin, b - buf_begin);
}
}
BOOST_LOG_API dump_data_char_t* dump_data_char = &dump_data_generic< char >;
BOOST_LOG_API dump_data_wchar_t* dump_data_wchar = &dump_data_generic< wchar_t >;
#if !defined(BOOST_NO_CXX11_CHAR16_T)
BOOST_LOG_API dump_data_char16_t* dump_data_char16 = &dump_data_generic< char16_t >;
#endif
#if !defined(BOOST_NO_CXX11_CHAR32_T)
BOOST_LOG_API dump_data_char32_t* dump_data_char32 = &dump_data_generic< char32_t >;
#endif
#if defined(BOOST_LOG_USE_SSSE3) || defined(BOOST_LOG_USE_AVX2)
BOOST_LOG_ANONYMOUS_NAMESPACE {
struct function_pointer_initializer
{
function_pointer_initializer()
{
// First, let's check for the max supported cpuid function
uint32_t eax = 0, ebx = 0, ecx = 0, edx = 0;
cpuid(eax, ebx, ecx, edx);
const uint32_t max_cpuid_function = eax;
const uint32_t cpu_vendor[3u] = { ebx, edx, ecx };
if (max_cpuid_function >= 1)
{
eax = 1;
ebx = ecx = edx = 0;
cpuid(eax, ebx, ecx, edx);
// Check for SSSE3 support
if (ecx & (1u << 9))
{
const uint32_t family = ((eax >> 8) & 0x0000000F) + ((eax >> 20) & 0x000000FF);
const uint32_t model = ((eax >> 4) & 0x0000000F) | ((eax >> 12) & 0x000000F0);
// Check if the CPU has slow pshufb. Some old Intel Atoms prior to Silvermont.
if (cpu_vendor[0] == 0x756e6547 && cpu_vendor[1] == 0x49656e69 && cpu_vendor[2] == 0x6c65746e &&
family == 6 && (model == 28 || model == 38 || model == 39 || model == 53 || model == 54))
{
enable_ssse3_slow_pshufb();
}
else
{
enable_ssse3();
}
}
#if defined(BOOST_LOG_USE_AVX2)
if (max_cpuid_function >= 7)
{
// To check for AVX2 availability we also need to verify that OS supports it
// Check that OSXSAVE is supported by CPU
if (ecx & (1u << 27))
{
// Check that it is used by the OS
bool mmstate = false;
#if defined(__GNUC__)
// Get the XFEATURE_ENABLED_MASK register
__asm__ __volatile__
(
"xgetbv\n\t"
: "=a" (eax), "=d" (edx)
: "c" (0)
);
mmstate = (eax & 6u) == 6u;
#elif defined(BOOST_WINDOWS)
// MSVC does not have an intrinsic for xgetbv, we have to query OS
boost::winapi::HMODULE_ hKernel32 = boost::winapi::GetModuleHandleW(L"kernel32.dll");
if (hKernel32)
{
typedef uint64_t (BOOST_WINAPI_WINAPI_CC* get_enabled_extended_features_t)(uint64_t);
get_enabled_extended_features_t get_enabled_extended_features = (get_enabled_extended_features_t)boost::winapi::get_proc_address(hKernel32, "GetEnabledExtendedFeatures");
if (get_enabled_extended_features)
{
// XSTATE_MASK_LEGACY_SSE | XSTATE_MASK_GSSE == 6
mmstate = get_enabled_extended_features(6u) == 6u;
}
}
#endif
if (mmstate)
{
// Finally, check for AVX2 support in CPU
eax = 7;
ebx = ecx = edx = 0;
cpuid(eax, ebx, ecx, edx);
if (ebx & (1u << 5))
enable_avx2();
}
}
}
#endif // defined(BOOST_LOG_USE_AVX2)
}
}
private:
static void cpuid(uint32_t& eax, uint32_t& ebx, uint32_t& ecx, uint32_t& edx)
{
#if defined(__GNUC__)
#if (defined(__i386__) || defined(__VXWORKS__)) && (defined(__PIC__) || defined(__PIE__)) && !(defined(__clang__) || (defined(BOOST_GCC) && BOOST_GCC >= 50100))
// Unless the compiler can do it automatically, we have to backup ebx in 32-bit PIC/PIE code because it is reserved by the ABI.
// For VxWorks ebx is reserved on 64-bit as well.
#if defined(__x86_64__)
uint64_t rbx = ebx;
__asm__ __volatile__
(
"xchgq %%rbx, %0\n\t"
"cpuid\n\t"
"xchgq %%rbx, %0\n\t"
: "+DS" (rbx), "+a" (eax), "+c" (ecx), "+d" (edx)
);
ebx = static_cast< uint32_t >(rbx);
#else // defined(__x86_64__)
__asm__ __volatile__
(
"xchgl %%ebx, %0\n\t"
"cpuid\n\t"
"xchgl %%ebx, %0\n\t"
: "+DS" (ebx), "+a" (eax), "+c" (ecx), "+d" (edx)
);
#endif // defined(__x86_64__)
#else
__asm__ __volatile__
(
"cpuid\n\t"
: "+a" (eax), "+b" (ebx), "+c" (ecx), "+d" (edx)
);
#endif
#elif defined(_MSC_VER)
int regs[4] = {};
__cpuid(regs, eax);
eax = regs[0];
ebx = regs[1];
ecx = regs[2];
edx = regs[3];
#else
#error Boost.Log: Unexpected compiler
#endif
}
static void enable_ssse3_slow_pshufb()
{
dump_data_char = &dump_data_char_ssse3_slow_pshufb;
dump_data_wchar = &dump_data_wchar_ssse3_slow_pshufb;
#if !defined(BOOST_NO_CXX11_CHAR16_T)
dump_data_char16 = &dump_data_char16_ssse3_slow_pshufb;
#endif
#if !defined(BOOST_NO_CXX11_CHAR32_T)
dump_data_char32 = &dump_data_char32_ssse3_slow_pshufb;
#endif
}
static void enable_ssse3()
{
dump_data_char = &dump_data_char_ssse3;
dump_data_wchar = &dump_data_wchar_ssse3;
#if !defined(BOOST_NO_CXX11_CHAR16_T)
dump_data_char16 = &dump_data_char16_ssse3;
#endif
#if !defined(BOOST_NO_CXX11_CHAR32_T)
dump_data_char32 = &dump_data_char32_ssse3;
#endif
}
#if defined(BOOST_LOG_USE_AVX2)
static void enable_avx2()
{
dump_data_char = &dump_data_char_avx2;
dump_data_wchar = &dump_data_wchar_avx2;
#if !defined(BOOST_NO_CXX11_CHAR16_T)
dump_data_char16 = &dump_data_char16_avx2;
#endif
#if !defined(BOOST_NO_CXX11_CHAR32_T)
dump_data_char32 = &dump_data_char32_avx2;
#endif
}
#endif // defined(BOOST_LOG_USE_AVX2)
};
static function_pointer_initializer g_function_pointer_initializer;
} // namespace
#endif // defined(BOOST_LOG_USE_SSSE3) || defined(BOOST_LOG_USE_AVX2)
} // namespace aux
BOOST_LOG_CLOSE_NAMESPACE // namespace log
} // namespace boost
#include <boost/log/detail/footer.hpp>