forked from zlib-ng/zlib-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
memcopy.h
377 lines (331 loc) · 10.1 KB
/
memcopy.h
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
375
376
377
/* memcopy.h -- inline functions to copy small data chunks.
* For conditions of distribution and use, see copyright notice in zlib.h
*/
#ifndef MEMCOPY_H_
#define MEMCOPY_H_
/* Load 64 bits from IN and place the bytes at offset BITS in the result. */
static inline uint64_t load_64_bits(const unsigned char *in, unsigned bits) {
uint64_t chunk;
MEMCPY(&chunk, in, sizeof(chunk));
#if BYTE_ORDER == LITTLE_ENDIAN
return chunk << bits;
#else
return ZSWAP64(chunk) << bits;
#endif
}
static inline unsigned char *copy_1_bytes(unsigned char *out, unsigned char *from) {
*out++ = *from;
return out;
}
static inline unsigned char *copy_2_bytes(unsigned char *out, unsigned char *from) {
uint16_t chunk;
unsigned sz = sizeof(chunk);
MEMCPY(&chunk, from, sz);
MEMCPY(out, &chunk, sz);
return out + sz;
}
static inline unsigned char *copy_3_bytes(unsigned char *out, unsigned char *from) {
out = copy_1_bytes(out, from);
return copy_2_bytes(out, from + 1);
}
static inline unsigned char *copy_4_bytes(unsigned char *out, unsigned char *from) {
uint32_t chunk;
unsigned sz = sizeof(chunk);
MEMCPY(&chunk, from, sz);
MEMCPY(out, &chunk, sz);
return out + sz;
}
static inline unsigned char *copy_5_bytes(unsigned char *out, unsigned char *from) {
out = copy_1_bytes(out, from);
return copy_4_bytes(out, from + 1);
}
static inline unsigned char *copy_6_bytes(unsigned char *out, unsigned char *from) {
out = copy_2_bytes(out, from);
return copy_4_bytes(out, from + 2);
}
static inline unsigned char *copy_7_bytes(unsigned char *out, unsigned char *from) {
out = copy_3_bytes(out, from);
return copy_4_bytes(out, from + 3);
}
static inline unsigned char *copy_8_bytes(unsigned char *out, unsigned char *from) {
uint64_t chunk;
unsigned sz = sizeof(chunk);
MEMCPY(&chunk, from, sz);
MEMCPY(out, &chunk, sz);
return out + sz;
}
/* Copy LEN bytes (7 or fewer) from FROM into OUT. Return OUT + LEN. */
static inline unsigned char *copy_bytes(unsigned char *out, unsigned char *from, unsigned len) {
Assert(len < 8, "copy_bytes should be called with less than 8 bytes");
#ifndef UNALIGNED_OK
while (len--) {
*out++ = *from++;
}
return out;
#else
switch (len) {
case 7:
return copy_7_bytes(out, from);
case 6:
return copy_6_bytes(out, from);
case 5:
return copy_5_bytes(out, from);
case 4:
return copy_4_bytes(out, from);
case 3:
return copy_3_bytes(out, from);
case 2:
return copy_2_bytes(out, from);
case 1:
return copy_1_bytes(out, from);
case 0:
return out;
default:
Assert(0, "should not happen");
}
return out;
#endif /* UNALIGNED_OK */
}
/* Copy LEN bytes (7 or fewer) from FROM into OUT. Return OUT + LEN. */
static inline unsigned char *set_bytes(unsigned char *out, unsigned char *from, unsigned dist, unsigned len) {
Assert(len < 8, "set_bytes should be called with less than 8 bytes");
#ifndef UNALIGNED_OK
while (len--) {
*out++ = *from++;
}
return out;
#else
if (dist >= len)
return copy_bytes(out, from, len);
switch (dist) {
case 6:
Assert(len == 7, "len should be exactly 7");
out = copy_6_bytes(out, from);
return copy_1_bytes(out, from);
case 5:
Assert(len == 6 || len == 7, "len should be either 6 or 7");
out = copy_5_bytes(out, from);
return copy_bytes(out, from, len - 5);
case 4:
Assert(len == 5 || len == 6 || len == 7, "len should be either 5, 6, or 7");
out = copy_4_bytes(out, from);
return copy_bytes(out, from, len - 4);
case 3:
Assert(4 <= len && len <= 7, "len should be between 4 and 7");
out = copy_3_bytes(out, from);
switch (len) {
case 7:
return copy_4_bytes(out, from);
case 6:
return copy_3_bytes(out, from);
case 5:
return copy_2_bytes(out, from);
case 4:
return copy_1_bytes(out, from);
default:
Assert(0, "should not happen");
break;
}
case 2:
Assert(3 <= len && len <= 7, "len should be between 3 and 7");
out = copy_2_bytes(out, from);
switch (len) {
case 7:
out = copy_4_bytes(out, from);
out = copy_1_bytes(out, from);
return out;
case 6:
out = copy_4_bytes(out, from);
return out;
case 5:
out = copy_2_bytes(out, from);
out = copy_1_bytes(out, from);
return out;
case 4:
out = copy_2_bytes(out, from);
return out;
case 3:
out = copy_1_bytes(out, from);
return out;
default:
Assert(0, "should not happen");
break;
}
case 1:
Assert(2 <= len && len <= 7, "len should be between 2 and 7");
unsigned char c = *from;
switch (len) {
case 7:
MEMSET(out, c, 7);
return out + 7;
case 6:
MEMSET(out, c, 6);
return out + 6;
case 5:
MEMSET(out, c, 5);
return out + 5;
case 4:
MEMSET(out, c, 4);
return out + 4;
case 3:
MEMSET(out, c, 3);
return out + 3;
case 2:
MEMSET(out, c, 2);
return out + 2;
default:
Assert(0, "should not happen");
break;
}
}
return out;
#endif /* UNALIGNED_OK */
}
/* Byte by byte semantics: copy LEN bytes from OUT + DIST and write them to OUT. Return OUT + LEN. */
static inline unsigned char *chunk_memcpy(unsigned char *out, unsigned char *from, unsigned len) {
unsigned sz = sizeof(uint64_t);
Assert(len >= sz, "chunk_memcpy should be called on larger chunks");
/* Copy a few bytes to make sure the loop below has a multiple of SZ bytes to be copied. */
copy_8_bytes(out, from);
unsigned rem = len % sz;
len /= sz;
out += rem;
from += rem;
unsigned by8 = len % sz;
len -= by8;
switch (by8) {
case 7:
out = copy_8_bytes(out, from);
from += sz;
case 6:
out = copy_8_bytes(out, from);
from += sz;
case 5:
out = copy_8_bytes(out, from);
from += sz;
case 4:
out = copy_8_bytes(out, from);
from += sz;
case 3:
out = copy_8_bytes(out, from);
from += sz;
case 2:
out = copy_8_bytes(out, from);
from += sz;
case 1:
out = copy_8_bytes(out, from);
from += sz;
}
while (len) {
out = copy_8_bytes(out, from);
from += sz;
out = copy_8_bytes(out, from);
from += sz;
out = copy_8_bytes(out, from);
from += sz;
out = copy_8_bytes(out, from);
from += sz;
out = copy_8_bytes(out, from);
from += sz;
out = copy_8_bytes(out, from);
from += sz;
out = copy_8_bytes(out, from);
from += sz;
out = copy_8_bytes(out, from);
from += sz;
len -= 8;
}
return out;
}
/* Memset LEN bytes in OUT with the value at OUT - 1. Return OUT + LEN. */
static inline unsigned char *byte_memset(unsigned char *out, unsigned len) {
unsigned sz = sizeof(uint64_t);
Assert(len >= sz, "byte_memset should be called on larger chunks");
unsigned char *from = out - 1;
unsigned char c = *from;
/* First, deal with the case when LEN is not a multiple of SZ. */
MEMSET(out, c, sz);
unsigned rem = len % sz;
len /= sz;
out += rem;
unsigned by8 = len % 8;
len -= by8;
switch (by8) {
case 7:
MEMSET(out, c, sz);
out += sz;
case 6:
MEMSET(out, c, sz);
out += sz;
case 5:
MEMSET(out, c, sz);
out += sz;
case 4:
MEMSET(out, c, sz);
out += sz;
case 3:
MEMSET(out, c, sz);
out += sz;
case 2:
MEMSET(out, c, sz);
out += sz;
case 1:
MEMSET(out, c, sz);
out += sz;
}
while (len) {
/* When sz is a constant, the compiler replaces __builtin_memset with an
inline version that does not incur a function call overhead. */
MEMSET(out, c, sz);
out += sz;
MEMSET(out, c, sz);
out += sz;
MEMSET(out, c, sz);
out += sz;
MEMSET(out, c, sz);
out += sz;
MEMSET(out, c, sz);
out += sz;
MEMSET(out, c, sz);
out += sz;
MEMSET(out, c, sz);
out += sz;
MEMSET(out, c, sz);
out += sz;
len -= 8;
}
return out;
}
/* Copy DIST bytes from OUT - DIST into OUT + DIST * k, for 0 <= k < LEN/DIST. Return OUT + LEN. */
static inline unsigned char *chunk_memset(unsigned char *out, unsigned char *from, unsigned dist, unsigned len) {
if (dist >= len)
return chunk_memcpy(out, from, len);
Assert(len >= sizeof(uint64_t), "chunk_memset should be called on larger chunks");
/* Double up the size of the memset pattern until reaching the largest pattern of size less than SZ. */
unsigned sz = sizeof(uint64_t);
while (dist < len && dist < sz) {
copy_8_bytes(out, from);
out += dist;
len -= dist;
dist += dist;
/* Make sure the next memcpy has at least SZ bytes to be copied. */
if (len < sz)
/* Finish up byte by byte when there are not enough bytes left. */
return set_bytes(out, from, dist, len);
}
return chunk_memcpy(out, from, len);
}
/* Byte by byte semantics: copy LEN bytes from FROM and write them to OUT. Return OUT + LEN. */
static inline unsigned char *chunk_copy(unsigned char *out, unsigned char *from, int dist, unsigned len) {
if (len < sizeof(uint64_t)) {
if (dist > 0)
return set_bytes(out, from, dist, len);
return copy_bytes(out, from, len);
}
if (dist == 1)
return byte_memset(out, len);
if (dist > 0)
return chunk_memset(out, from, dist, len);
return chunk_memcpy(out, from, len);
}
#endif /* MEMCOPY_H_ */