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
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/*
* Implementions of functions found in $(D_PSYMBOL tanya.memory.op) for x64.
*
* Copyright: Eugene Wissner 2017.
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
* Mozilla Public License, v. 2.0).
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
* Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/memory/arch/x86_64.d,
* tanya/memory/arch/x86_64.d)
*/
module tanya.memory.arch.x86_64;
import tanya.memory.op;
version (D_InlineAsm_X86_64):
pragma(inline, true)
package (tanya.memory) void copy(const void[] source, void[] target)
pure nothrow @system @nogc
{
asm pure nothrow @nogc
{
naked;
// RDI and RSI should be preserved.
mov RAX, RDI;
mov R8, RSI;
// RDX - source length.
// RCX - source data.
// RDI - target length
// RSI - target data.
mov RDI, RSI;
mov RSI, RCX;
cmp RDX, 0x08;
jc aligned_1;
test EDI, 0x07;
jz aligned_8;
naligned:
movsb;
dec RDX;
test EDI, 0x07;
jnz naligned;
aligned_8:
mov RCX, RDX;
shr RCX, 0x03;
rep;
movsq;
and EDX, 0x07;
jz end;
aligned_1:
// Write the remaining bytes.
mov RCX, RDX;
rep;
movsb;
end: // Restore registers.
mov RSI, R8;
mov RDI, RAX;
ret;
}
}
pragma(inline, true)
package (tanya.memory) void fill(void[], ulong) pure nothrow @system @nogc
{
asm pure nothrow @nogc
{
naked;
// Check for zero length.
test RSI, RSI;
jz end;
/*
* RDX - pointer.
* RSI - length.
* RDI - value filled with a byte.
*/
mov RAX, RSI;
mov R8, RDX;
movq XMM0, RDI;
movlhps XMM0, XMM0;
// Check if the pointer is aligned to a 16-byte boundary.
and R8, -0x10;
// Compute the number of misaligned bytes.
mov R9, RDX;
sub R9, R8;
test R9, R9;
jz aligned;
// Get the number of bytes to be written until we are aligned.
mov RCX, 0x10;
sub RCX, R9;
mov R8, RDX;
naligned:
mov [ R8 ], DIL; // Write a byte.
// Advance the pointer. Decrease the total number of bytes
// and the misaligned ones.
inc R8;
dec RCX;
dec RAX;
// Checks if we are aligned.
test RCX, RCX;
jnz naligned;
aligned:
// Checks if we're done writing bytes.
test RAX, RAX;
jz end;
// Write 1 byte at a time.
cmp RAX, 8;
jl aligned_1;
// Write 8 bytes at a time.
cmp RAX, 16;
jl aligned_8;
// Write 16 bytes at a time.
cmp RAX, 32;
jl aligned_16;
// Write 32 bytes at a time.
cmp RAX, 64;
jl aligned_32;
aligned_64:
movdqa [ R8 ], XMM0;
movdqa [ R8 + 16 ], XMM0;
movdqa [ R8 + 32 ], XMM0;
movdqa [ R8 + 48 ], XMM0;
add R8, 64;
sub RAX, 64;
cmp RAX, 64;
jge aligned_64;
// Checks if we're done writing bytes.
test RAX, RAX;
jz end;
// Write 1 byte at a time.
cmp RAX, 8;
jl aligned_1;
// Write 8 bytes at a time.
cmp RAX, 16;
jl aligned_8;
// Write 16 bytes at a time.
cmp RAX, 32;
jl aligned_16;
aligned_32:
movdqa [ R8 ], XMM0;
movdqa [ R8 + 16 ], XMM0;
add R8, 32;
sub RAX, 32;
// Checks if we're done writing bytes.
test RAX, RAX;
jz end;
// Write 1 byte at a time.
cmp RAX, 8;
jl aligned_1;
// Write 8 bytes at a time.
cmp RAX, 16;
jl aligned_8;
aligned_16:
movdqa [ R8 ], XMM0;
add R8, 16;
sub RAX, 16;
// Checks if we're done writing bytes.
test RAX, RAX;
jz end;
// Write 1 byte at a time.
cmp RAX, 8;
jl aligned_1;
aligned_8:
mov [ R8 ], RDI;
add R8, 8;
sub RAX, 8;
// Checks if we're done writing bytes.
test RAX, RAX;
jz end;
aligned_1:
mov [ R8 ], DIL;
inc R8;
dec RAX;
test RAX, RAX;
jnz aligned_1;
end:
ret;
}
}
pragma(inline, true)
package (tanya.memory) void copyBackward(const void[] source, void[] target)
pure nothrow @system @nogc
{
asm pure nothrow @nogc
{
naked;
// Save the registers should be restored.
mov R8, RSI;
mov R9, RDI;
// RDX - source length.
// RCX - source data.
// RDI - target length
// RSI - target data.
lea RDI, [ RSI + RDX - 1 ];
lea RSI, [ RCX + RDX - 1 ];
mov RCX, RDX;
std; // Set the direction flag.
rep;
movsb;
cld; // Clear the direction flag.
// Restore registers.
mov RDI, R9;
mov RSI, R8;
ret;
}
}
pragma(inline, true)
package (tanya.memory) int cmp(const void[] r1, const void[] r2)
pure nothrow @system @nogc
{
asm pure nothrow @nogc
{
naked;
// RDI and RSI should be preserved.
mov R9, RDI;
mov R8, RSI;
// RDX - r1 length.
// RCX - r1 data.
// RDI - r2 length
// RSI - r2 data.
mov RSI, RCX;
mov RCX, RDI;
mov RDI, R8;
// Compare the lengths.
cmp RDX, RCX;
jl less;
jg greater;
// Check if we're aligned.
cmp RDX, 0x08;
jc aligned_1;
test EDI, 0x07;
jz aligned_8;
naligned:
cmpsb;
jl less;
jg greater;
dec RDX;
test EDI, 0x07;
jnz naligned;
aligned_8:
mov RCX, RDX;
shr RCX, 0x03;
repe;
cmpsq;
jl less;
jg greater;
and EDX, 0x07;
jz equal;
aligned_1: // Compare the remaining bytes.
mov RCX, RDX;
repe;
cmpsb;
jl less;
jg greater;
equal:
xor RAX, RAX; // Return 0.
jmp end;
greater:
mov RAX, 1;
jmp end;
less:
mov RAX, -1;
jmp end;
end: // Restore registers.
mov RSI, R8;
mov RDI, R9;
ret;
}
}
|