-
-
Notifications
You must be signed in to change notification settings - Fork 270
/
Copy pathcompare258.c
186 lines (145 loc) · 5.55 KB
/
compare258.c
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
/* compare258.c -- aligned and unaligned versions of compare258
* Copyright (C) 2020 Nathan Moinvaziri
* For conditions of distribution and use, see copyright notice in zlib.h
*/
#include "zbuild.h"
#include "zutil.h"
#include "fallback_builtins.h"
/* ALIGNED, byte comparison */
static inline int32_t compare256_c_static(const unsigned char *src0, const unsigned char *src1) {
int32_t len = 0;
do {
if (*src0 != *src1)
return len + (*src0 == *src1);
src0 += 1, src1 += 1, len += 1;
if (*src0 != *src1)
return len + (*src0 == *src1);
src0 += 1, src1 += 1, len += 1;
if (*src0 != *src1)
return len + (*src0 == *src1);
src0 += 1, src1 += 1, len += 1;
if (*src0 != *src1)
return len + (*src0 == *src1);
src0 += 1, src1 += 1, len += 1;
if (*src0 != *src1)
return len + (*src0 == *src1);
src0 += 1, src1 += 1, len += 1;
if (*src0 != *src1)
return len + (*src0 == *src1);
src0 += 1, src1 += 1, len += 1;
if (*src0 != *src1)
return len + (*src0 == *src1);
src0 += 1, src1 += 1, len += 1;
if (*src0 != *src1)
return len + (*src0 == *src1);
src0 += 1, src1 += 1, len += 1;
} while (len < 256);
return 256;
}
static inline int32_t compare258_c_static(const unsigned char *src0, const unsigned char *src1) {
if (*src0 != *src1)
return 0;
src0 += 1, src1 += 1;
if (*src0 != *src1)
return 1;
src0 += 1, src1 += 1;
return compare256_c_static(src0, src1) + 2;
}
ZLIB_INTERNAL int32_t compare258_c(const unsigned char *src0, const unsigned char *src1) {
return compare258_c_static(src0, src1);
}
#define LONGEST_MATCH longest_match_c
#define COMPARE256 compare256_c_static
#define COMPARE258 compare258_c_static
#include "match_tpl.h"
#ifdef UNALIGNED_OK
/* UNALIGNED_OK, 16-bit integer comparison */
static inline int32_t compare256_unaligned_16_static(const unsigned char *src0, const unsigned char *src1) {
int32_t len = 0;
do {
if (*(uint16_t *)src0 != *(uint16_t *)src1)
return len + (*src0 == *src1);
src0 += 2, src1 += 2, len += 2;
if (*(uint16_t *)src0 != *(uint16_t *)src1)
return len + (*src0 == *src1);
src0 += 2, src1 += 2, len += 2;
if (*(uint16_t *)src0 != *(uint16_t *)src1)
return len + (*src0 == *src1);
src0 += 2, src1 += 2, len += 2;
if (*(uint16_t *)src0 != *(uint16_t *)src1)
return len + (*src0 == *src1);
src0 += 2, src1 += 2, len += 2;
} while (len < 256);
return 256;
}
static inline int32_t compare258_unaligned_16_static(const unsigned char *src0, const unsigned char *src1) {
if (*(uint16_t *)src0 != *(uint16_t *)src1)
return (*src0 == *src1);
return compare256_unaligned_16_static(src0+2, src1+2) + 2;
}
ZLIB_INTERNAL int32_t compare258_unaligned_16(const unsigned char *src0, const unsigned char *src1) {
return compare258_unaligned_16_static(src0, src1);
}
#define LONGEST_MATCH longest_match_unaligned_16
#define COMPARE256 compare256_unaligned_16_static
#define COMPARE258 compare258_unaligned_16_static
#include "match_tpl.h"
#ifdef HAVE_BUILTIN_CTZ
/* UNALIGNED_OK, 32-bit integer comparison */
static inline int32_t compare256_unaligned_32_static(const unsigned char *src0, const unsigned char *src1) {
int32_t len = 0;
do {
uint32_t sv = *(uint32_t *)src0;
uint32_t mv = *(uint32_t *)src1;
uint32_t diff = sv ^ mv;
if (diff) {
uint32_t match_byte = __builtin_ctz(diff) / 8;
return (int32_t)(len + match_byte);
}
src0 += 4, src1 += 4, len += 4;
} while (len < 256);
return 256;
}
static inline int32_t compare258_unaligned_32_static(const unsigned char *src0, const unsigned char *src1) {
if (*(uint16_t *)src0 != *(uint16_t *)src1)
return (*src0 == *src1);
return compare256_unaligned_32_static(src0+2, src1+2) + 2;
}
ZLIB_INTERNAL int32_t compare258_unaligned_32(const unsigned char *src0, const unsigned char *src1) {
return compare258_unaligned_32_static(src0, src1);
}
#define LONGEST_MATCH longest_match_unaligned_32
#define COMPARE256 compare256_unaligned_32_static
#define COMPARE258 compare258_unaligned_32_static
#include "match_tpl.h"
#endif
#if defined(UNALIGNED64_OK) && defined(HAVE_BUILTIN_CTZLL)
/* UNALIGNED64_OK, 64-bit integer comparison */
static inline int32_t compare256_unaligned_64_static(const unsigned char *src0, const unsigned char *src1) {
int32_t len = 0;
do {
uint64_t sv = *(uint64_t *)src0;
uint64_t mv = *(uint64_t *)src1;
uint64_t diff = sv ^ mv;
if (diff) {
uint64_t match_byte = __builtin_ctzll(diff) / 8;
return (int32_t)(len + match_byte);
}
src0 += 8, src1 += 8, len += 8;
} while (len < 256);
return 256;
}
static inline int32_t compare258_unaligned_64_static(const unsigned char *src0, const unsigned char *src1) {
if (*(uint16_t *)src0 != *(uint16_t *)src1)
return (*src0 == *src1);
return compare256_unaligned_64_static(src0+2, src1+2) + 2;
}
ZLIB_INTERNAL int32_t compare258_unaligned_64(const unsigned char *src0, const unsigned char *src1) {
return compare258_unaligned_64_static(src0, src1);
}
#define LONGEST_MATCH longest_match_unaligned_64
#define COMPARE256 compare256_unaligned_64_static
#define COMPARE258 compare258_unaligned_64_static
#include "match_tpl.h"
#endif
#endif