回答編集履歴

1

加筆

2018/02/25 00:24

投稿

episteme
episteme

スコア16614

test CHANGED
@@ -1,3 +1,85 @@
1
1
  Visual Studio のデバッガでmemcpyに飛び込んでみた。
2
2
 
3
+
4
+
3
5
  ![イメージ説明](2b05e247b144a53fb2955111cfbcc8e5.jpeg)
6
+
7
+
8
+
9
+ かたやコチラ↓が C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.12.25827\crt\src\x64\memcpy.asm
10
+
11
+
12
+
13
+ ```
14
+
15
+ mov r11, rcx ; save destination address
16
+
17
+ mov r10, rdx ; save source address
18
+
19
+ cmp r8, 16 ; if 16 bytes or less
20
+
21
+ jbe MoveBytes16 ; go move them quick
22
+
23
+ cmp r8, 32 ; check for length <= 32 (we know its > 16)
24
+
25
+ jbe Move17to32 ; go handle lengths 17-32 as a special case
26
+
27
+ sub rdx, rcx ; compute offset to source buffer
28
+
29
+ jae CopyUp ; if above or equal, go move up
30
+
31
+ mov rax, r10 ; else check that src+count < dst
32
+
33
+ add rax, r8 ; src + count
34
+
35
+ cmp rcx, rax ; (src + count) < dst
36
+
37
+ jl CopyDown ; no, buffers overlap go move downward
38
+
39
+
40
+
41
+ CopyUp:
42
+
43
+ cmp r8, 128
44
+
45
+ jbe XmmCopySmall
46
+
47
+
48
+
49
+ bt __favor, __FAVOR_ENFSTRG ; check for ENFSTRG (enhanced fast strings)
50
+
51
+ jnc XmmCopyUp ; If Enhanced Fast String not available, use XMM
52
+
53
+
54
+
55
+ ; use Enhanced Fast Strings
56
+
57
+ ; but first align the destination dst to 16 byte alignment
58
+
59
+ StringCopy:
60
+
61
+ mov rax, r11 ; return original destination pointer
62
+
63
+ mov r11, rdi ; save rdi in r11
64
+
65
+ mov rdi, rcx ; move destination pointer to rdi
66
+
67
+ mov rcx, r8 ; move length to rcx
68
+
69
+ mov r8, rsi ; save rsi in r8
70
+
71
+ mov rsi, r10 ; move source pointer to rsi
72
+
73
+ rep movsb ; copy source to destination buffer
74
+
75
+ mov rsi, r8 ; restore rsi
76
+
77
+ mov rdi, r11 ; restore rdi
78
+
79
+ ret
80
+
81
+ ```
82
+
83
+
84
+
85
+ 同じもんみたいやね、ッタリマエだけど。