-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmemcpyd.d
374 lines (339 loc) · 8.88 KB
/
memcpyd.d
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
import std.datetime.stopwatch;
import core.stdc.string;
import std.random;
import std.traits;
import std.stdio;
struct S(size_t Size)
{
ubyte[Size] x;
}
bool isPowerOf2(T)(T x)
if (isIntegral!T)
{
return (x != 0) && ((x & (x - 1)) == 0);
}
void memcpyC(T)(ref T dst, const ref T src)
{
pragma(inline, true)
memcpy(&dst, &src, T.sizeof);
}
void memcpyD(T)(ref T dst, const ref T src)
if (isScalarType!T)
{
pragma(inline, true)
dst = src;
}
void memcpyDRepMovsb(T)(ref T dst, const ref T src)
{
// pragma(inline, true) // cannot inline a function with ASM in DMD
asm pure nothrow @nogc
{
mov RSI, dst;
mov RDI, src;
cld;
mov RCX, T.sizeof;
rep;
movsb;
}
}
// This implementation handles type sizes that are not powers of 2
// This implementation can't be @safe because it does pointer arithmetic
pragma(inline, true);
private void memcpyDUnsafe(T)(ref T dst, const ref T src) @trusted
if (is(T == struct))
{
import core.bitop: bsr;
static assert(T.sizeof != 0);
enum prevPowerOf2 = 1LU << bsr(T.sizeof);
alias TRemainder = S!(T.sizeof - prevPowerOf2);
auto s = cast(const S!prevPowerOf2*)(&src);
auto d = cast(S!prevPowerOf2*)(&dst);
memcpyD(*d, *s);
memcpyD(*cast(TRemainder*)(d + 1), *cast(const TRemainder*)(s + 1));
}
void memcpyD(T)(ref T dst, const ref T src)
if (is(T == struct))
{
static if (T.sizeof == 1)
{
pragma(inline, true)
memcpyD(*cast(ubyte*)(&dst), *cast(const ubyte*)(&src));
return;
}
else static if (T.sizeof == 2)
{
pragma(inline, true)
memcpyD(*cast(ushort*)(&dst), *cast(const ushort*)(&src));
return;
}
else static if (T.sizeof == 4)
{
pragma(inline, true)
memcpyD(*cast(uint*)(&dst), *cast(const uint*)(&src));
return;
}
else static if (T.sizeof == 8)
{
pragma(inline, true)
memcpyD(*cast(ulong*)(&dst), *cast(const ulong*)(&src));
return;
}
else static if (T.sizeof == 16)
{
pragma(inline, true)
version(D_SIMD)
{
pragma(msg, "SIMD");
import core.simd: void16, storeUnaligned, loadUnaligned;
storeUnaligned(cast(void16*)(&dst), loadUnaligned(cast(const void16*)(&src)));
}
else
{
static foreach(i; 0 .. T.sizeof/8)
{
memcpyD(*(cast(ulong*)(&dst) + i), *(cast(const ulong*)src + i));
}
}
return;
}
else static if (T.sizeof == 32)
{
pragma(inline, true)
// AVX implementation is unstable in DMD. It sporadically fails.
version(D_AVX)
{
import core.simd: void32;
*(cast(void32*)(&src)) = *(cast(const void32*)(&dst));
}
else
{
static foreach(i; 0 .. T.sizeof/16)
{
memcpyD(*(cast(S!16*)(&dst) + i), *(cast(const S!16*)(&src) + i));
}
}
return;
}
else static if (T.sizeof < 64 && !isPowerOf2(T.sizeof))
{
//pragma(inline, true)
memcpyDUnsafe(dst, src);
return;
}
else
{
version(D_AVX)
{
pragma(msg, "AVX");
static foreach(i; 0 .. T.sizeof/32)
{
memcpyD(*(cast(S!32*)(&dst) + i), *(cast(const S!32*)&src + i));
}
return;
}
else
version(D_SIMD)
{
static if (T.sizeof <= 1024)
{
import core.simd: void16, storeUnaligned, loadUnaligned;
static foreach(i; 0 .. T.sizeof/16)
{
// This won't inline in DMD for some reason
// pragma(inline, true)
// memcpyD((cast(const S16*)dst) + i, (cast(S16*)src) + i);
storeUnaligned((cast(void16*)(&dst)) + i, loadUnaligned((cast(const void16*)(&src)) + i));
}
return;
}
else
{
pragma(inline, true)
memcpyDRepMovsb(dst, src);
return;
}
}
else
{
pragma(inline, true)
memcpyDRepMovsb(dst, src);
return;
}
}
}
// The following 2 functions are an attempt to prevent the compiler
// from removing code when compiling with optimizations. See
// https://stackoverflow.com/questions/40122141/preventing-compiler-optimizations-while-benchmarking
void use(void* p)
{
version(LDC)
{
import ldc.llvmasm;
__asm("", "r,~{memory}", p);
}
version(GNU)
{
asm { "" : : "g" p : "memory"; }
}
}
void clobber()
{
version(LDC)
{
import ldc.llvmasm;
__asm("", "~{memory}");
}
version(GNU)
{
asm { "" : : : "memory"; }
}
}
Duration benchmark(T, alias f)(ref T dst, ref T src, ulong* bytesCopied)
{
enum iterations = 2^^20 / T.sizeof;
Duration result;
auto swt = StopWatch(AutoStart.yes);
swt.reset();
while(swt.peek().total!"msecs" < 200)
{
auto sw = StopWatch(AutoStart.yes);
sw.reset();
foreach (_; 0 .. iterations)
{
use(&dst); // So optimizer doesn't remove code
f(dst, src);
use(&src); // So optimizer doesn't remove code
}
result += sw.peek();
*bytesCopied += (iterations * T.sizeof);
}
return result;
}
void init(T)(ref T v)
{
static if (is (T == float))
{
v = uniform(0.0f, 9_999_999.0f);
}
else static if (is(T == double))
{
v = uniform(0.0, 9_999_999.0);
}
else static if (is(T == real))
{
v = uniform(0.0L, 9_999_999.0L);
}
else
{
auto m = (cast(ubyte*)(&v))[0 .. T.sizeof];
for(int i = 0; i < m.length; i++)
{
m[i] = uniform!byte;
}
}
}
void verify(T)(const ref T a, const ref T b)
{
auto aa = (cast(ubyte*)(&a))[0..T.sizeof];
auto bb = (cast(ubyte*)(&b))[0..T.sizeof];
for(int i = 0; i < T.sizeof; i++)
{
assert(aa[i] == bb[i]);
}
}
bool average;
void test(T)()
{
// Just an arbitrarily sized buffer big enough to store test data
// We will offset from this buffer to create unaligned data
ubyte[66000] buf1;
ubyte[66000] buf2;
double TotalGBperSec1 = 0.0;
double TotalGBperSec2 = 0.0;
enum alignments = 16;
// test align(0) through align(32) for now
foreach(i; 0..alignments)
{
{
static if (T.sizeof < 32)
{
auto d = cast(T*)(&buf1[i]);
auto s = cast(T*)(&buf2[i]);
}
else // AVX code crashes on misalignment, so for now, always align(32)
{
auto _d = cast(size_t)(&buf1[32]);
auto d = cast(T*)(_d &= ~0b11111);
auto _s = cast(size_t)(&buf2[32]);
auto s = cast(T*)(_s &= ~0b11111);
}
ulong bytesCopied1;
ulong bytesCopied2;
init(*d);
init(*s);
immutable d1 = benchmark!(T, memcpyC)(*d, *s, &bytesCopied1);
verify(*d, *s);
init(*d);
init(*s);
immutable d2 = benchmark!(T, memcpyD)(*d, *s, &bytesCopied2);
verify(*d, *s);
auto secs1 = (cast(double)(d1.total!"nsecs")) / 1_000_000_000.0;
auto secs2 = (cast(double)(d2.total!"nsecs")) / 1_000_000_000.0;
auto GB1 = (cast(double)bytesCopied1) / 1_000_000_000.0;
auto GB2 = (cast(double)bytesCopied2) / 1_000_000_000.0;
auto GBperSec1 = GB1 / secs1;
auto GBperSec2 = GB2 / secs2;
if (average)
{
TotalGBperSec1 += GBperSec1;
TotalGBperSec2 += GBperSec2;
}
else
{
writeln(T.sizeof, " ", GBperSec1, " ", GBperSec2);
stdout.flush();
}
}
}
if (average)
{
writeln(T.sizeof, " ", TotalGBperSec1 / alignments, " ", TotalGBperSec2 / alignments);
stdout.flush();
}
}
void main(string[] args)
{
average = args.length >= 2;
// For performing benchmarks
writeln("size(bytes) memcpyC(GB/s) memcpyD(GB/s)");
stdout.flush();
static foreach(i; 1..65)
{
test!(S!i);
}
test!(S!128);
test!(S!256);
test!(S!512);
test!(S!1024);
test!(S!2048);
test!(S!4096);
test!(S!8192);
test!(S!16384);
test!(S!32768);
test!(S!65536);
// For testing integrity
// writeln("");
// writeln("size(bytes) memcpyC(GB/s) memcpyD(GB/s)");
// test!bool;
// test!ubyte;
// test!byte;
// test!ushort;
// test!short;
// test!uint;
// test!int;
// test!ulong;
// test!long;
// test!float;
// test!double;
// test!real;
}