-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmemory.cpp
220 lines (201 loc) · 3.78 KB
/
memory.cpp
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
#include <stdlib.h>
#include "memory.h"
#ifdef __BORLANDC__
#include <dos.h>
#else
#include "string.h"
#endif
void memory::mask_memcpy(ptr_t dest, ptr_t src, size_t size, unsigned char mask)
{
#ifdef __BORLANDC__
_DI=FP_OFF(dest);
_SI=FP_OFF(src);
_BX=FP_SEG(src);
_DS=_BX;
_AX=FP_SEG(dest);
_ES=_AX;
_CX=size;
_BL=mask;
bytecopy:
asm {
lodsb // 5
cmp al,bl // 2
jz nobytecopy // 3
stosb // 4
dec cx
jnz bytecopy // 15
jmp endbytecopy
}
nobytecopy:
asm {
inc di
dec cx
jnz bytecopy
}
endbytecopy:
#else
ptr_t s,p,e;
// ptr_t *sp[2];
// sp[0] = &s;
// sp[1] = &p;
// p = dest;
// s = src;
// e = dest + size;
// do
// {
// *p = *(*sp[*s == mask]);
// p++;
// s++;
// } while (p < e);
// p=dest;
// s=src;
// e=dest+size;
// while(p<e) {
// if(*s!=mask) *p=*s;
// p++;
// s++;
// }
p=dest;
s=src;
e=dest+size;
do {
if(*s!=mask) *p=*s;
p++;
s++;
} while(p<e);
#endif
}
void memory::fast_memcpy(void *dest, void *src, size_t size)
{
#ifdef __BORLANDC__
#ifdef i86
if (size > 2) { // long way default, byte copy the long way
_DI=FP_OFF(dest);
_SI=FP_OFF(src);
_BX=FP_SEG(src);
_DS=_BX;
_AX=FP_SEG(dest);
_ES=_AX;
asm {
mov bx,size // count down size
mov cx, di // dest
test cx, 1 // byte offset?
jz pre_word // no, goto work offset
movsb // copy byte
dec bx // 1 byte copied
}
pre_word:
asm {
movsw // addr must be dw aligned
sub bx,2 // 2 bytes copied, we are word aligned now
mov cx,bx
shr cx,1 // divide by 2 to get words remaining
jz post // no words left
mov ax,cx
shl ax,1
sub bx,ax // subtract dwords copied from total
cld
rep movsw // copy dwords
}
post:
asm {
test bx,1 // test number of bytes remaining
jz post_end // no bytes remaining
movsb // copy bytes
}
post_end:
#else
if (size > 4) { // long way default, byte copy the long way
_DI=FP_OFF(dest);
_SI=FP_OFF(src);
_BX=FP_SEG(src);
_DS=_BX;
_AX=FP_SEG(dest);
_ES=_AX;
asm {
mov bx,size // count down size
mov cx, di // dest
test cx, 1 // byte offset?
jz pre_word // no, goto work offset
movsb // copy byte
dec bx // 1 byte copied
}
pre_word:
asm {
test cx, 2 // word offset?
jz pre_dword// no, go to dword
movsw // copy word
sub bx,2 // 2 bytes copied
}
pre_dword:
asm {
movsd // addr must be dw aligned
sub bx,4 // 4 bytes copied, we are dw aligned now
mov cx,bx
shr cx,2 // divide by 4 to get dw remaining
jz post // no dwords left
mov ax,cx
shl ax,2
sub bx,ax // subtract dwords copied from total
cld
rep movsd // copy dwords
}
post:
asm {
test bx,2 // test number of words remaining
jz post_byte// no words remaining
movsw // copy word
}
post_byte:
asm {
test bx,1 // test number of bytes remaining
jz post_end // no bytes remaining
movsb // copy bytes
}
post_end:
#endif
} else {
_CX=size;
_DI=FP_OFF(dest);
_SI=FP_OFF(src);
_BX=FP_SEG(src);
_DS=_BX;
_AX=FP_SEG(dest);
_ES=_AX;
asm {
rep movsb
}
}
#else
memcpy(dest,src,size);
#endif
}
/*
* Implements a fast dword aligned copy, assumes large buffer copy, fastest way to copy memory on 386
*/
void memory::blit(ptr_t dest, ptr_t src, size_t size)
{
#ifdef __BORLANDC__
_DI=FP_OFF(dest);
_SI=FP_OFF(src);
_BX=FP_SEG(src);
_DS=_BX;
_AX=FP_SEG(dest);
_ES=_AX;
#ifdef i86
asm {
mov cx,size
shr cx,1
rep movsw
}
#else
asm {
mov cx,size
shr cx,2
rep movsd
}
#endif
#else
memcpy(dest,src,size);
#endif
}