Repository navigation
Expand file tree
/
Copy pathmem.cpp
More file actions
143 lines (116 loc) · 3.34 KB
/
Copy pathmem.cpp
File metadata and controls
143 lines (116 loc) · 3.34 KB
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
#include "mem.h"
uint64_t GetMemorySize(EFI_MEMORY_DESCRIPTOR* Map, uint64_t MapEntries, uint64_t DescriptorSize) {
static uint64_t memoryMapSize = 0;
EFI_MEMORY_DESCRIPTOR* desc;
if(memoryMapSize > 0) return memoryMapSize;
for (int i = 0; i < MapEntries; i++) {
desc = (EFI_MEMORY_DESCRIPTOR*)((uint64_t)Map + (i * DescriptorSize));
memoryMapSize += desc->numPages * 4096;
}
return memoryMapSize;
}
// void* memset(void* start, uint8_t value, size_t len) {
// unsigned char* p = (unsigned char*)start;
// unsigned char v = value & 0xff;
// while(len--) {
// *p++ = v;
// }
// return start;
// }
static inline void *small_memset(void *s, int c, size_t n) {
if (n < 5) {
if (n == 0)
return s;
char *p = (char*)s;
p[0] = c;
p[n - 1] = c;
if (n <= 2)
return s;
p[1] = c;
p[2] = c;
return s;
}
if (n <= 16) {
uint64_t val8 = ((uint64_t)0x0101010101010101L * ((uint8_t)c));
if (n >= 8) {
char *first = (char*)s;
char *last = (char*)s + n - 8;
*((u64 *)first) = val8;
*((u64 *)last) = val8;
return s;
}
uint32_t val4 = val8;
char *first = (char*)s;
char *last = (char*)s + n - 4;
*((u32 *)first) = val4;
*((u32 *)last) = val4;
return s;
}
char X = c;
char *p = (char*)s;
char16 val16 = {X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X};
char *last = (char*)s + n - 16;
*((char16 *)last) = val16;
*((char16 *)p) = val16;
return s;
}
static inline void *huge_memset(void *s, int c, size_t n) {
char *p = (char*)s;
char X = c;
char32 val32 = {X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X,
X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X};
// Stamp the first 32byte store.
*((char32 *)p) = val32;
char *first_aligned = p + 32 - ((uint64_t)p % 32);
char *buffer_end = p + n;
char *last_word = buffer_end - 32;
// Align the next stores.
p = first_aligned;
// Unroll the body of the loop to increase parallelism.
while (p + (32 * 5) < buffer_end) {
*((char32a *)p) = val32;
p += 32;
*((char32a *)p) = val32;
p += 32;
*((char32a *)p) = val32;
p += 32;
*((char32a *)p) = val32;
p += 32;
*((char32a *)p) = val32;
p += 32;
}
// Complete the last few iterations:
#define TRY_STAMP_32_BYTES \
if (p < last_word) { \
*((char32a *)p) = val32; \
p += 32; \
}
TRY_STAMP_32_BYTES
TRY_STAMP_32_BYTES
TRY_STAMP_32_BYTES
TRY_STAMP_32_BYTES
// Stamp the last unaligned word.
*((char32 *)last_word) = val32;
return s;
}
void *memset(void *s, uint8_t c, size_t n) {
char *p = (char*)s;
char X = c;
if (n < 32) {
return small_memset(s, c, n);
}
if (n > 160) {
return huge_memset(s, c, n);
}
char32 val32 = {X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X,
X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X};
char *last_word = (char*)s + n - 32;
// Stamp the 32-byte chunks.
do {
*((char32 *)p) = val32;
p += 32;
} while (p < last_word);
// Stamp the last unaligned 32 bytes of the buffer.
*((char32 *)last_word) = val32;
return s;
}