-
Notifications
You must be signed in to change notification settings - Fork 63
/
ualgobase.cc
284 lines (257 loc) · 8.6 KB
/
ualgobase.cc
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
// This file is part of the uSTL library, an STL implementation.
//
// Copyright (c) 2005 by Mike Sharov <[email protected]>
// This file is free software, distributed under the MIT License.
#ifndef NDEBUG // Optimized code here. asserts slow it down, and are checked elsewhere.
#define NDEBUG
#endif
#include "ualgo.h"
namespace ustl {
// Generic version for implementing fill_nX_fast on non-i386 architectures.
template <typename T> static inline void stosv (T*& p, size_t n, T v)
{ while (n--) *p++ = v; }
#if __i386__ || __x86_64__
//----------------------------------------------------------------------
// Copy functions
//----------------------------------------------------------------------
static inline void movsb_dir_up (void) { asm volatile ("cld"); }
static inline void movsb_dir_down (void) { asm volatile ("std"); }
static inline void movsb (const void*& src, size_t nBytes, void*& dest)
{
asm volatile ("rep;\n\tmovsb"
: "=&S"(src), "=&D"(dest), "=&c"(nBytes)
: "0"(src), "1"(dest), "2"(nBytes)
: "memory");
}
static inline void movsd (const void*& src, size_t nWords, void*& dest)
{
asm volatile ("rep;\n\tmovsl"
: "=&S"(src), "=&D"(dest), "=&c"(nWords)
: "0"(src), "1"(dest), "2"(nWords)
: "memory");
}
template <> inline void stosv (uint8_t*& p, size_t n, uint8_t v)
{ asm volatile ("rep;\n\tstosb" : "=&D"(p), "=c"(n) : "0"(p), "1"(n), "a"(v) : "memory"); }
template <> inline void stosv (uint16_t*& p, size_t n, uint16_t v)
{ asm volatile ("rep;\n\tstosw" : "=&D"(p), "=c"(n) : "0"(p), "1"(n), "a"(v) : "memory"); }
template <> inline void stosv (uint32_t*& p, size_t n, uint32_t v)
{ asm volatile ("rep;\n\tstosl" : "=&D"(p), "=c"(n) : "0"(p), "1"(n), "a"(v) : "memory"); }
#if CPU_HAS_MMX
#define MMX_ALIGN 16U // Data must be aligned on this grain
#define MMX_BS 32U // Assembly routines copy data this many bytes at a time.
static inline void simd_block_copy (const void* src, void* dest)
{
const char* csrc ((const char*) src);
char* cdest ((char*) dest);
#if CPU_HAS_SSE
asm (
"movaps\t%2, %%xmm0 \n\t"
"movaps\t%3, %%xmm1 \n\t"
"movntps\t%%xmm0, %0 \n\t"
"movntps\t%%xmm1, %1"
: "=m"(cdest[0]), "=m"(cdest[16])
: "m"(csrc[0]), "m"(csrc[16])
: "xmm0", "xmm1", "memory");
#else
asm (
"movq %4, %%mm0 \n\t"
"movq %5, %%mm1 \n\t"
"movq %6, %%mm2 \n\t"
"movq %7, %%mm3 \n\t"
"movq %%mm0, %0 \n\t"
"movq %%mm1, %1 \n\t"
"movq %%mm2, %2 \n\t"
"movq %%mm3, %3"
: "=m"(cdest[0]), "=m"(cdest[8]), "=m"(cdest[16]), "=m"(cdest[24])
: "m"(csrc[0]), "m"(csrc[8]), "m"(csrc[16]), "m"(csrc[24])
: "mm0", "mm1", "mm2", "mm3", "st", "st(1)", "st(2)", "st(3)", "memory");
#endif
}
static inline void simd_block_store (uint8_t* dest)
{
#if CPU_HAS_SSE
asm volatile (
"movntq %%mm0, %0\n\t"
"movntq %%mm0, %1\n\t"
"movntq %%mm0, %2\n\t"
"movntq %%mm0, %3"
: "=m"(dest[0]), "=m"(dest[8]), "=m"(dest[16]), "=m"(dest[24])
:: "memory");
#else
asm volatile (
"movq %%mm0, %0 \n\t"
"movq %%mm0, %1 \n\t"
"movq %%mm0, %2 \n\t"
"movq %%mm0, %3"
: "=m"(dest[0]), "=m"(dest[8]), "=m"(dest[16]), "=m"(dest[24])
:: "memory");
#endif
}
static inline void simd_block_cleanup (void)
{
#if !CPU_HAS_SSE
simd::reset_mmx();
#endif
asm volatile ("sfence");
}
/// The fastest optimized raw memory copy.
void copy_n_fast (const void* src, size_t nBytes, void* dest) noexcept
{
movsb_dir_up();
size_t nHeadBytes = Align(uintptr_t(src), MMX_ALIGN) - uintptr_t(src);
nHeadBytes = min (nHeadBytes, nBytes);
movsb (src, nHeadBytes, dest);
nBytes -= nHeadBytes;
if (!(uintptr_t(dest) % MMX_ALIGN)) {
const size_t nMiddleBlocks = nBytes / MMX_BS;
for (uoff_t i = 0; i < nMiddleBlocks; ++ i) {
prefetch (advance (src, 512), 0, 0);
simd_block_copy (src, dest);
src = advance (src, MMX_BS);
dest = advance (dest, MMX_BS);
}
simd_block_cleanup();
nBytes %= MMX_BS;
}
movsb (src, nBytes, dest);
}
#endif // CPU_HAS_MMX
/// The fastest optimized backwards raw memory copy.
void copy_backward_fast (const void* first, const void* last, void* result) noexcept
{
prefetch (first, 0, 0);
prefetch (result, 1, 0);
size_t nBytes (distance (first, last));
movsb_dir_down();
size_t nHeadBytes = uintptr_t(last) % 4;
last = advance (last, -1);
result = advance (result, -1);
movsb (last, nHeadBytes, result);
nBytes -= nHeadBytes;
if (uintptr_t(result) % 4 == 3) {
const size_t nMiddleBlocks = nBytes / 4;
last = advance (last, -3);
result = advance (result, -3);
movsd (last, nMiddleBlocks, result);
nBytes %= 4;
}
movsb (last, nBytes, result);
movsb_dir_up();
}
#endif // __i386__
//----------------------------------------------------------------------
// Fill functions
//----------------------------------------------------------------------
#if CPU_HAS_MMX
template <typename T> static inline void build_block (T) {}
template <> inline void build_block (uint8_t v)
{
asm volatile (
"movd %0, %%mm0\n\tpunpcklbw %%mm0, %%mm0\n\tpshufw $0, %%mm0, %%mm0"
: : "g"(uint32_t(v)) : "mm0");
}
template <> inline void build_block (uint16_t v)
{
asm volatile (
"movd %0, %%mm0\n\tpshufw $0, %%mm0, %%mm0"
: : "g"(uint32_t(v)) : "mm0");
}
template <> inline void build_block (uint32_t v)
{
asm volatile (
"movd %0, %%mm0\n\tpunpckldq %%mm0, %%mm0"
: : "g"(uint32_t(v)) : "mm0");
}
static inline void simd_block_fill_loop (uint8_t*& dest, size_t count)
{
prefetch (advance (dest, 512), 1, 0);
for (const uint8_t* destEnd = dest + count * MMX_BS; dest < destEnd; dest += MMX_BS)
simd_block_store (dest);
simd_block_cleanup();
simd::reset_mmx();
}
template <typename T>
static inline void fill_n_fast (T* dest, size_t count, T v)
{
size_t nHead = Align(uintptr_t(dest), MMX_ALIGN) - uintptr_t(dest) / sizeof(T);
nHead = min (nHead, count);
stosv (dest, nHead, v);
count -= nHead;
build_block (v);
uint8_t* bdest = (uint8_t*) dest;
simd_block_fill_loop (bdest, count * sizeof(T) / MMX_BS);
count %= MMX_BS;
dest = (T*) bdest;
stosv (dest, count, v);
}
void fill_n8_fast (uint8_t* dest, size_t count, uint8_t v) noexcept
{ fill_n_fast (dest, count, v); }
void fill_n16_fast (uint16_t* dest, size_t count, uint16_t v) noexcept
{ fill_n_fast (dest, count, v); }
void fill_n32_fast (uint32_t* dest, size_t count, uint32_t v) noexcept
{ fill_n_fast (dest, count, v); }
#else
void fill_n8_fast (uint8_t* dest, size_t count, uint8_t v) noexcept { memset (dest, v, count); }
void fill_n16_fast (uint16_t* dest, size_t count, uint16_t v) noexcept { stosv (dest, count, v); }
void fill_n32_fast (uint32_t* dest, size_t count, uint32_t v) noexcept { stosv (dest, count, v); }
#endif // CPU_HAS_MMX
/// Exchanges ranges [first, middle) and [middle, last)
void rotate_fast (void* first, void* middle, void* last) noexcept
{
#if HAVE_ALLOCA_H
const size_t half1 (distance (first, middle)), half2 (distance (middle, last));
const size_t hmin (min (half1, half2));
if (!hmin)
return;
void* buf = alloca (hmin);
if (buf) {
if (half2 < half1) {
copy_n_fast (middle, half2, buf);
copy_backward_fast (first, middle, last);
copy_n_fast (buf, half2, first);
} else {
copy_n_fast (first, half1, buf);
copy_n_fast (middle, half2, first);
copy_n_fast (buf, half1, advance (first, half2));
}
} else
#else
if (first == middle || middle == last)
return;
#endif
{
char* f = (char*) first;
char* m = (char*) middle;
char* l = (char*) last;
reverse (f, m);
reverse (m, l);
while (f != m && m != l)
iter_swap (f++, --l);
reverse (f, (f == m ? l : m));
}
}
#if __GNUC__ < 4
size_t popcount (uint32_t v) noexcept
{
const uint32_t w = v - ((v >> 1) & 0x55555555); // Algorithm from AMD optimization guide
const uint32_t x = (w & 0x33333333) + ((w >> 2) & 0x33333333);
return (((x + (x >> 4) & 0x0F0F0F0F) * 0x01010101) >> 24);
}
#if HAVE_INT64_T
/// \brief Returns the number of 1s in \p v in binary.
size_t popcount (uint64_t v) noexcept
{
v -= (v >> 1) & UINT64_C(0x5555555555555555); // Algorithm from Wikipedia
v = (v & UINT64_C(0x3333333333333333)) + ((v >> 2) & UINT64_C(0x3333333333333333));
v = (v + (v >> 4)) & UINT64_C(0x0F0F0F0F0F0F0F0F);
return ((v * UINT64_C(0x0101010101010101)) >> 56);
}
#endif // HAVE_INT64_T
#endif // !__GNUC__
//----------------------------------------------------------------------
// Miscellaneous instantiated stuff from headers which don't have enough
// to warrant creation of a separate file.cc
//----------------------------------------------------------------------
// Used in uspecial to print printable characters
const char _FmtPrtChr[2][8]={"'%c'","%d"};
} // namespace ustl