forked from idealvin/coost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfast.cc
234 lines (197 loc) · 5.7 KB
/
fast.cc
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
#include "fast.h"
#include "fastring.h"
#include "lru_map.h"
#include <stdio.h>
#include <string.h>
namespace fast {
static void init_itoh_table(uint16* p) {
for (int i = 0; i < 256; ++i) {
char* b = (char*)(p + i);
b[0] = "0123456789abcdef"[i >> 4];
b[1] = "0123456789abcdef"[i & 0x0f];
}
}
static void init_itoa_table(uint32* p) {
for (int i = 0; i < 10000; ++i) {
char* b = (char*)(p + i);
b[3] = (char)(i % 10 + '0');
b[2] = (char)(i % 100 / 10 + '0');
b[1] = (char)(i % 1000 / 100 + '0');
b[0] = (char)(i / 1000);
// digits of i: (b[0] >> 4) + 1
if (i > 999) {
b[0] |= (3 << 4); // 0x30
} else if (i > 99) {
b[0] |= (2 << 4); // 0x20
} else if (i > 9) {
b[0] |= (1 << 4); // 0x10
}
}
}
static inline uint16* create_itoh_table() {
static uint16 itoh_table[256];
init_itoh_table(itoh_table);
return itoh_table;
}
static inline uint32* create_itoa_table() {
static uint32 itoa_table[10000];
init_itoa_table(itoa_table);
return itoa_table;
}
static inline uint16* get_itoh_table() {
static uint16* itoh_table = create_itoh_table();
return itoh_table;
}
static inline uint32* get_itoa_table() {
static uint32* itoa_table = create_itoa_table();
return itoa_table;
}
int u32toh(uint32 v, char* buf) {
static uint16* itoh_table = get_itoh_table();
uint16 b[4], *p = b + 4;
do {
*--p = itoh_table[v & 0xff];
v >>= 8;
} while (v > 0);
buf[0] = '0';
buf[1] = 'x';
int len = (int) ((char*)(b + 4) - (char*)p - (*(char*)p == '0'));
memcpy(buf + 2, (char*)(b + 4) - len, (size_t)len);
return len + 2;
}
int u64toh(uint64 v, char* buf) {
static uint16* itoh_table = get_itoh_table();
uint16 b[8], *p = b + 8;
do {
*--p = itoh_table[v & 0xff];
v >>= 8;
} while (v > 0);
buf[0] = '0';
buf[1] = 'x';
int len = (int) ((char*)(b + 8) - (char*)p - (*(char*)p == '0'));
memcpy(buf + 2, (char*)(b + 8) - len, (size_t)len);
return len + 2;
}
int u32toa(uint32 v, char* buf) {
static uint32* itoa_table = get_itoa_table();
uint32 b[3], *p = b + 2;
if (v > 9999) {
b[1] = v / 10000;
b[2] = itoa_table[v - b[1] * 10000] | 0x30303030;
--p;
} else {
b[2] = itoa_table[v];
goto u32toa_end;
}
if (b[1] > 9999) {
b[0] = b[1] / 10000;
b[1] = itoa_table[b[1] - b[0] * 10000] | 0x30303030;
b[0] = itoa_table[b[0]];
--p;
} else {
b[1] = itoa_table[b[1]];
goto u32toa_end;
}
u32toa_end:
int len = (int) (((char*)b) + 9 + ((*(char*)p) >> 4) - ((char*)p));
memcpy(buf, ((char*)b) + 12 - len, (size_t)len);
return len;
}
int u64toa(uint64 v, char* buf) {
static uint32* itoa_table = get_itoa_table();
uint32 b[5], *p = b + 4;
uint64 x;
if (v > 9999) {
x = v / 10000;
b[4] = itoa_table[v - x * 10000] | 0x30303030;
--p;
} else {
b[4] = itoa_table[v];
goto u64toa_end;
}
if (x > 9999) {
v = x / 10000;
b[3] = itoa_table[x - v * 10000] | 0x30303030;
--p;
} else {
b[3] = itoa_table[x];
goto u64toa_end;
}
if (v > 9999) {
x = v / 10000;
b[2] = itoa_table[v - x * 10000] | 0x30303030;
--p;
} else {
b[2] = itoa_table[v];
goto u64toa_end;
}
if (x > 9999) {
b[0] = (uint32) (x / 10000);
b[1] = itoa_table[x - b[0] * 10000] | 0x30303030;
b[0] = itoa_table[b[0]];
--p;
} else {
b[1] = itoa_table[x];
goto u64toa_end;
}
u64toa_end:
int len = (int) (((char*)b) + 17 + ((*(char*)p) >> 4) - ((char*)p));
memcpy(buf, ((char*)b) + 20 - len, (size_t)len);
return len;
}
#if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wstrict-aliasing"
#endif
int dtoa(float v, char* buf) {
static __thread LruMap<uint32, fastring>* float_cache = 0;
uint32 key = *(uint32*)&v;
if (float_cache) {
auto it = float_cache->find(key);
if (it != float_cache->end()) {
const fastring& s = it->second;
memcpy(buf, s.data(), s.size());
return (int) s.size();
}
} else {
float_cache = new LruMap<uint32, fastring>(1024);
}
#ifdef _WIN32
int r = _snprintf_s(buf, 24, 24, "%.7g", v);
#else
int r = snprintf(buf, 24, "%.7g", v);
#endif
if (r >= 0) {
float_cache->insert(key, fastring(buf, r));
return r;
}
return 0;
}
int dtoa(double v, char* buf) {
static __thread LruMap<uint64, fastring>* double_cache = 0;
uint64 key = *(uint64*)&v;
if (double_cache) {
auto it = double_cache->find(key);
if (it != double_cache->end()) {
const fastring& s = it->second;
memcpy(buf, s.data(), s.size());
return (int) s.size();
}
} else {
double_cache = new LruMap<uint64, fastring>(1024);
}
#ifdef _WIN32
int r = _snprintf_s(buf, 24, 24, "%.7g", v);
#else
int r = snprintf(buf, 24, "%.7g", v);
#endif
if (r >= 0) {
double_cache->insert(key, fastring(buf, r));
return r;
}
return 0;
}
#if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
#pragma GCC diagnostic pop
#endif
} // namespace fast