-
Notifications
You must be signed in to change notification settings - Fork 14
/
ascii.cpp
222 lines (173 loc) · 5.04 KB
/
ascii.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
221
222
#include <sys/time.h>
#include <algorithm>
#include <cassert>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <vector>
static inline int ascii_std(const uint8_t *data, int len) {
return !std::any_of(data, data + len, [](int8_t b) { return b < 0; });
}
static inline int ascii_u64(const uint8_t *data, int len) {
uint8_t orall = 0;
if (len >= 16) {
uint64_t or1 = 0, or2 = 0;
const uint8_t *data2 = data + 8;
do {
or1 |= *(const uint64_t *)data;
or2 |= *(const uint64_t *)data2;
data += 16;
data2 += 16;
len -= 16;
} while (len >= 16);
/*
* Idea from Benny Halevy <[email protected]>
* - 7-th bit set ==> orall = !(non-zero) - 1 = 0 - 1 = 0xFF
* - 7-th bit clear ==> orall = !0 - 1 = 1 - 1 = 0x00
*/
orall = !((or1 | or2) & 0x8080808080808080ULL) - 1;
}
while (len--) orall |= *data++;
return orall < 0x80;
}
#if defined(__x86_64__)
#include <x86intrin.h>
static inline int ascii_simd(const uint8_t *data, int len) {
if (len >= 32) {
const uint8_t *data2 = data + 16;
__m128i or1 = _mm_set1_epi8(0), or2 = or1;
while (len >= 32) {
__m128i input1 = _mm_loadu_si128((const __m128i *)data);
__m128i input2 = _mm_loadu_si128((const __m128i *)data2);
or1 = _mm_or_si128(or1, input1);
or2 = _mm_or_si128(or2, input2);
data += 32;
data2 += 32;
len -= 32;
}
or1 = _mm_or_si128(or1, or2);
if (_mm_movemask_epi8(_mm_cmplt_epi8(or1, _mm_set1_epi8(0)))) return 0;
}
return ascii_u64(data, len);
}
#elif defined(__aarch64__)
#include <arm_neon.h>
static inline int ascii_simd(const uint8_t *data, int len) {
if (len >= 32) {
const uint8_t *data2 = data + 16;
uint8x16_t or1 = vdupq_n_u8(0), or2 = or1;
while (len >= 32) {
const uint8x16_t input1 = vld1q_u8(data);
const uint8x16_t input2 = vld1q_u8(data2);
or1 = vorrq_u8(or1, input1);
or2 = vorrq_u8(or2, input2);
data += 32;
data2 += 32;
len -= 32;
}
or1 = vorrq_u8(or1, or2);
if (vmaxvq_u8(or1) >= 0x80) return 0;
}
return ascii_u64(data, len);
}
#endif
struct ftab {
const char *name;
int (*func)(const uint8_t *data, int len);
};
static const std::vector<ftab> _f = {
{
.name = "std",
.func = ascii_std,
},
{
.name = "u64",
.func = ascii_u64,
},
{
.name = "simd",
.func = ascii_simd,
},
};
static void load_test_buf(uint8_t *data, int len) {
uint8_t v = 0;
for (int i = 0; i < len; ++i) {
data[i] = v++;
v &= 0x7F;
}
}
static void bench(const struct ftab &f, const uint8_t *data, int len) {
const int loops = 1024 * 1024 * 1024 / len;
int ret = 1;
double time_aligned, time_unaligned, size;
struct timeval tv1, tv2;
fprintf(stderr, "bench %s (%d bytes)... ", f.name, len);
/* aligned */
gettimeofday(&tv1, 0);
for (int i = 0; i < loops; ++i) ret &= f.func(data, len);
gettimeofday(&tv2, 0);
time_aligned = tv2.tv_usec - tv1.tv_usec;
time_aligned = time_aligned / 1000000 + tv2.tv_sec - tv1.tv_sec;
/* unaligned */
gettimeofday(&tv1, 0);
for (int i = 0; i < loops; ++i) ret &= f.func(data + 1, len);
gettimeofday(&tv2, 0);
time_unaligned = tv2.tv_usec - tv1.tv_usec;
time_unaligned = time_unaligned / 1000000 + tv2.tv_sec - tv1.tv_sec;
printf("%s ", ret ? "pass" : "FAIL");
size = ((double)len * loops) / (1024 * 1024);
printf("%.0f/%.0f MB/s\n", size / time_aligned, size / time_unaligned);
}
static void test(const struct ftab &f, uint8_t *data, int len) {
int error = 0;
fprintf(stderr, "test %s (%d bytes)... ", f.name, len);
/* positive */
error |= !f.func(data, len);
/* negative */
if (len < 100 * 1024) {
for (int i = 0; i < len; ++i) {
data[i] += 0x80;
error |= f.func(data, len);
data[i] -= 0x80;
}
}
printf("%s\n", error ? "FAIL" : "pass");
}
/* ./ascii [test|bench] [alg] */
int main(int argc, const char *argv[]) {
int do_test = 1, do_bench = 1;
const char *alg = NULL;
if (argc > 1) {
do_bench &= !!strcmp(argv[1], "test");
do_test &= !!strcmp(argv[1], "bench");
}
if (do_bench && argc > 2) alg = argv[2];
const std::vector<int> size = {
9, 16 + 1, 32 - 1, 128 + 1,
1024 + 15, 16 * 1024 + 1, 64 * 1024 + 15, 1024 * 1024};
int max_size = *std::max_element(size.begin(), size.end());
uint8_t *_data = new uint8_t[max_size + 1];
assert(((uintptr_t)_data & 7) == 0);
uint8_t *data = _data + 1; /* Unalign buffer address */
_data[0] = 0;
load_test_buf(data, max_size);
if (do_test) {
printf("==================== Test ====================\n");
for (int sz : size) {
for (auto &f : _f) {
test(f, data, sz);
}
}
}
if (do_bench) {
printf("==================== Bench ====================\n");
for (int sz : size) {
for (auto &f : _f) {
if (!alg || strcmp(alg, f.name) == 0) bench(f, _data, sz);
}
printf("-----------------------------------------------\n");
}
}
delete _data;
return 0;
}