-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathfixed-memcmp.cpp
129 lines (103 loc) · 3.84 KB
/
fixed-memcmp.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
// #define USE_SIMPLE_MEMCMP // when defined simpler expressions are used
namespace {
MAYBE_UNUSED
bool always_true(const char*, const char*) {
return true;
}
MAYBE_UNUSED
bool memcmp1(const char* a, const char* b) {
return a[0] == b[0];
}
MAYBE_UNUSED
bool memcmp2(const char* a, const char* b) {
const uint16_t A = *reinterpret_cast<const uint16_t*>(a);
const uint16_t B = *reinterpret_cast<const uint16_t*>(b);
return A == B;
}
MAYBE_UNUSED
bool memcmp3(const char* a, const char* b) {
#ifdef USE_SIMPLE_MEMCMP
return memcmp2(a, b) && memcmp1(a + 2, b + 2);
#else
const uint32_t A = *reinterpret_cast<const uint32_t*>(a);
const uint32_t B = *reinterpret_cast<const uint32_t*>(b);
return (A & 0x00ffffff) == (B & 0x00ffffff);
#endif
}
MAYBE_UNUSED
bool memcmp4(const char* a, const char* b) {
const uint32_t A = *reinterpret_cast<const uint32_t*>(a);
const uint32_t B = *reinterpret_cast<const uint32_t*>(b);
return A == B;
}
MAYBE_UNUSED
bool memcmp5(const char* a, const char* b) {
#ifdef USE_SIMPLE_MEMCMP
return memcmp4(a, b) && memcmp1(a + 4, b + 4);
#else
const uint64_t A = *reinterpret_cast<const uint64_t*>(a);
const uint64_t B = *reinterpret_cast<const uint64_t*>(b);
return ((A ^ B) & 0x000000fffffffffflu) == 0;
#endif
}
MAYBE_UNUSED
bool memcmp6(const char* a, const char* b) {
#ifdef USE_SIMPLE_MEMCMP
return memcmp4(a, b) && memcmp2(a + 4, b + 4);
#else
const uint64_t A = *reinterpret_cast<const uint64_t*>(a);
const uint64_t B = *reinterpret_cast<const uint64_t*>(b);
return ((A ^ B) & 0x0000fffffffffffflu) == 0;
#endif
}
MAYBE_UNUSED
bool memcmp7(const char* a, const char* b) {
#ifdef USE_SIMPLE_MEMCMP
return memcmp4(a, b) && memcmp3(a + 4, b + 4);
#else
const uint64_t A = *reinterpret_cast<const uint64_t*>(a);
const uint64_t B = *reinterpret_cast<const uint64_t*>(b);
return ((A ^ B) & 0x00fffffffffffffflu) == 0;
#endif
}
MAYBE_UNUSED
bool memcmp8(const char* a, const char* b) {
const uint64_t A = *reinterpret_cast<const uint64_t*>(a);
const uint64_t B = *reinterpret_cast<const uint64_t*>(b);
return A == B;
}
MAYBE_UNUSED
bool memcmp9(const char* a, const char* b) {
const uint64_t A = *reinterpret_cast<const uint64_t*>(a);
const uint64_t B = *reinterpret_cast<const uint64_t*>(b);
return (A == B) & (a[8] == b[8]);
}
MAYBE_UNUSED
bool memcmp10(const char* a, const char* b) {
const uint64_t Aq = *reinterpret_cast<const uint64_t*>(a);
const uint64_t Bq = *reinterpret_cast<const uint64_t*>(b);
const uint16_t Aw = *reinterpret_cast<const uint16_t*>(a + 8);
const uint16_t Bw = *reinterpret_cast<const uint16_t*>(b + 8);
return (Aq == Bq) & (Aw == Bw);
}
MAYBE_UNUSED
bool memcmp11(const char* a, const char* b) {
#ifdef USE_SIMPLE_MEMCMP
return memcmp8(a, b) && memcmp3(a + 8, b + 8);
#else
const uint64_t Aq = *reinterpret_cast<const uint64_t*>(a);
const uint64_t Bq = *reinterpret_cast<const uint64_t*>(b);
const uint32_t Ad = *reinterpret_cast<const uint32_t*>(a + 8);
const uint32_t Bd = *reinterpret_cast<const uint32_t*>(b + 8);
return (Aq == Bq) & ((Ad & 0x00ffffff) == (Bd & 0x00ffffff));
#endif
}
MAYBE_UNUSED
bool memcmp12(const char* a, const char* b) {
const uint64_t Aq = *reinterpret_cast<const uint64_t*>(a);
const uint64_t Bq = *reinterpret_cast<const uint64_t*>(b);
const uint32_t Ad = *reinterpret_cast<const uint32_t*>(a + 8);
const uint32_t Bd = *reinterpret_cast<const uint32_t*>(b + 8);
return (Aq == Bq) & (Ad == Bd);
}
}