| 1 |
// This library is part of PLINK 2.0, copyright (C) 2005-2025 Shaun Purcell, |
| 2 |
// Christopher Chang. |
| 3 |
// |
| 4 |
// This library is free software: you can redistribute it and/or modify it |
| 5 |
// under the terms of the GNU Lesser General Public License as published by the |
| 6 |
// Free Software Foundation; either version 3 of the License, or (at your |
| 7 |
// option) any later version. |
| 8 |
// |
| 9 |
// This library is distributed in the hope that it will be useful, but WITHOUT |
| 10 |
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 |
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License |
| 12 |
// for more details. |
| 13 |
// |
| 14 |
// You should have received a copy of the GNU Lesser General Public License |
| 15 |
// along with this library. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
|
| 17 |
|
| 18 |
#include "plink2_string.h" |
| 19 |
|
| 20 |
#ifdef __cplusplus |
| 21 |
namespace plink2 { |
| 22 |
#endif |
| 23 |
|
| 24 |
#if defined(__LP64__) && !defined(_GNU_SOURCE) |
| 25 |
CXXCONST_VOIDP rawmemchr(const void* ss, int cc) { |
| 26 |
const uintptr_t starting_addr = R_CAST(uintptr_t, ss); |
| 27 |
const VecI8* ss_viter = R_CAST(const VecI8*, RoundDownPow2(starting_addr, kBytesPerVec)); |
| 28 |
const VecI8 vvec_all_needle = veci8_set1(cc); |
| 29 |
VecI8 cur_vvec = *ss_viter; |
| 30 |
VecI8 needle_match_vvec = (cur_vvec == vvec_all_needle); |
| 31 |
const uint32_t leading_byte_ct = starting_addr - R_CAST(uintptr_t, ss_viter); |
| 32 |
# ifndef SIMDE_ARM_NEON_A32V8_NATIVE |
| 33 |
uint32_t matching_bytes = veci8_movemask(needle_match_vvec); |
| 34 |
matching_bytes &= UINT32_MAX << leading_byte_ct; |
| 35 |
// This is typically short-range, so the Memrchr() double-vector strategy is |
| 36 |
// unlikely to be profitable. (todo: experiment with header-inline) |
| 37 |
while (!matching_bytes) { |
| 38 |
++ss_viter; |
| 39 |
cur_vvec = *ss_viter; |
| 40 |
needle_match_vvec = (cur_vvec == vvec_all_needle); |
| 41 |
matching_bytes = veci8_movemask(needle_match_vvec); |
| 42 |
} |
| 43 |
const uint32_t byte_offset_in_vec = ctzu32(matching_bytes); |
| 44 |
# else |
| 45 |
uint64_t matching_nybbles = arm_shrn4_i8(needle_match_vvec); |
| 46 |
matching_nybbles &= UINT64_MAX << (4 * leading_byte_ct); |
| 47 |
// This is typically short-range, so the Memrchr() double-vector strategy is |
| 48 |
// unlikely to be profitable. (todo: experiment with header-inline) |
| 49 |
while (!matching_nybbles) { |
| 50 |
++ss_viter; |
| 51 |
cur_vvec = *ss_viter; |
| 52 |
needle_match_vvec = (cur_vvec == vvec_all_needle); |
| 53 |
matching_nybbles = arm_shrn4_i8(needle_match_vvec); |
| 54 |
} |
| 55 |
const uint32_t byte_offset_in_vec = ctzw(matching_nybbles) / 4; |
| 56 |
# endif |
| 57 |
return R_CAST(CXXCONST_VOIDP, R_CAST(uintptr_t, ss_viter) + byte_offset_in_vec); |
| 58 |
} |
| 59 |
#endif |
| 60 |
|
| 61 |
#ifdef __LP64__ |
| 62 |
CXXCONST_VOIDP rawmemchr2(const void* ss, unsigned char ucc1, unsigned char ucc2) { |
| 63 |
const uintptr_t starting_addr = R_CAST(uintptr_t, ss); |
| 64 |
const VecI8* ss_viter = R_CAST(const VecI8*, RoundDownPow2(starting_addr, kBytesPerVec)); |
| 65 |
const VecI8 vvec_all_ucc1 = veci8_set1(ucc1); |
| 66 |
const VecI8 vvec_all_ucc2 = veci8_set1(ucc2); |
| 67 |
VecI8 cur_vvec = *ss_viter; |
| 68 |
VecI8 ucc1_match_vvec = (cur_vvec == vvec_all_ucc1); |
| 69 |
VecI8 ucc2_match_vvec = (cur_vvec == vvec_all_ucc2); |
| 70 |
const uint32_t leading_byte_ct = starting_addr - R_CAST(uintptr_t, ss_viter); |
| 71 |
# ifndef SIMDE_ARM_NEON_A32V8_NATIVE |
| 72 |
uint32_t matching_bytes = veci8_movemask(ucc1_match_vvec | ucc2_match_vvec); |
| 73 |
matching_bytes &= UINT32_MAX << leading_byte_ct; |
| 74 |
while (!matching_bytes) { |
| 75 |
++ss_viter; |
| 76 |
cur_vvec = *ss_viter; |
| 77 |
ucc1_match_vvec = (cur_vvec == vvec_all_ucc1); |
| 78 |
ucc2_match_vvec = (cur_vvec == vvec_all_ucc2); |
| 79 |
matching_bytes = veci8_movemask(ucc1_match_vvec | ucc2_match_vvec); |
| 80 |
} |
| 81 |
const uint32_t byte_offset_in_vec = ctzu32(matching_bytes); |
| 82 |
# else |
| 83 |
uint64_t matching_nybbles = arm_shrn4_i8(ucc1_match_vvec | ucc2_match_vvec); |
| 84 |
matching_nybbles &= UINT64_MAX << (4 * leading_byte_ct); |
| 85 |
while (!matching_nybbles) { |
| 86 |
++ss_viter; |
| 87 |
cur_vvec = *ss_viter; |
| 88 |
ucc1_match_vvec = (cur_vvec == vvec_all_ucc1); |
| 89 |
ucc2_match_vvec = (cur_vvec == vvec_all_ucc2); |
| 90 |
matching_nybbles = arm_shrn4_i8(ucc1_match_vvec | ucc2_match_vvec); |
| 91 |
} |
| 92 |
const uint32_t byte_offset_in_vec = ctzw(matching_nybbles) / 4; |
| 93 |
# endif |
| 94 |
return &(DowncastToXC(ss_viter)[byte_offset_in_vec]); |
| 95 |
} |
| 96 |
|
| 97 |
CXXCONST_VOIDP rawmemchr3(const void* ss, unsigned char ucc1, unsigned char ucc2, unsigned char ucc3) { |
| 98 |
const uintptr_t starting_addr = R_CAST(uintptr_t, ss); |
| 99 |
const VecI8* ss_viter = R_CAST(const VecI8*, RoundDownPow2(starting_addr, kBytesPerVec)); |
| 100 |
const VecI8 vvec_all_ucc1 = veci8_set1(ucc1); |
| 101 |
const VecI8 vvec_all_ucc2 = veci8_set1(ucc2); |
| 102 |
const VecI8 vvec_all_ucc3 = veci8_set1(ucc3); |
| 103 |
VecI8 cur_vvec = *ss_viter; |
| 104 |
VecI8 ucc1_match_vvec = (cur_vvec == vvec_all_ucc1); |
| 105 |
VecI8 ucc2_match_vvec = (cur_vvec == vvec_all_ucc2); |
| 106 |
VecI8 ucc3_match_vvec = (cur_vvec == vvec_all_ucc3); |
| 107 |
const uint32_t leading_byte_ct = starting_addr - R_CAST(uintptr_t, ss_viter); |
| 108 |
# ifndef SIMDE_ARM_NEON_A32V8_NATIVE |
| 109 |
uint32_t matching_bytes = veci8_movemask(ucc1_match_vvec | ucc2_match_vvec | ucc3_match_vvec); |
| 110 |
matching_bytes &= UINT32_MAX << leading_byte_ct; |
| 111 |
while (!matching_bytes) { |
| 112 |
++ss_viter; |
| 113 |
cur_vvec = *ss_viter; |
| 114 |
ucc1_match_vvec = (cur_vvec == vvec_all_ucc1); |
| 115 |
ucc2_match_vvec = (cur_vvec == vvec_all_ucc2); |
| 116 |
ucc3_match_vvec = (cur_vvec == vvec_all_ucc3); |
| 117 |
matching_bytes = veci8_movemask(ucc1_match_vvec | ucc2_match_vvec | ucc3_match_vvec); |
| 118 |
} |
| 119 |
const uint32_t byte_offset_in_vec = ctzu32(matching_bytes); |
| 120 |
# else |
| 121 |
uint64_t matching_nybbles = arm_shrn4_i8(ucc1_match_vvec | ucc2_match_vvec | ucc3_match_vvec); |
| 122 |
matching_nybbles &= UINT64_MAX << (4 * leading_byte_ct); |
| 123 |
while (!matching_nybbles) { |
| 124 |
++ss_viter; |
| 125 |
cur_vvec = *ss_viter; |
| 126 |
ucc1_match_vvec = (cur_vvec == vvec_all_ucc1); |
| 127 |
ucc2_match_vvec = (cur_vvec == vvec_all_ucc2); |
| 128 |
ucc3_match_vvec = (cur_vvec == vvec_all_ucc3); |
| 129 |
matching_nybbles = arm_shrn4_i8(ucc1_match_vvec | ucc2_match_vvec | ucc3_match_vvec); |
| 130 |
} |
| 131 |
const uint32_t byte_offset_in_vec = ctzw(matching_nybbles) / 4; |
| 132 |
# endif |
| 133 |
return &(DowncastToXC(ss_viter)[byte_offset_in_vec]); |
| 134 |
} |
| 135 |
|
| 136 |
CXXCONST_CP strchrnul3(const char* ss, unsigned char ucc1, unsigned char ucc2, unsigned char ucc3) { |
| 137 |
const uintptr_t starting_addr = R_CAST(uintptr_t, ss); |
| 138 |
const VecI8* ss_viter = R_CAST(const VecI8*, RoundDownPow2(starting_addr, kBytesPerVec)); |
| 139 |
const VecI8 vvec_all_zero = veci8_setzero(); |
| 140 |
const VecI8 vvec_all_ucc1 = veci8_set1(ucc1); |
| 141 |
const VecI8 vvec_all_ucc2 = veci8_set1(ucc2); |
| 142 |
const VecI8 vvec_all_ucc3 = veci8_set1(ucc3); |
| 143 |
VecI8 cur_vvec = *ss_viter; |
| 144 |
VecI8 zero_match_vvec = (cur_vvec == vvec_all_zero); |
| 145 |
VecI8 ucc1_match_vvec = (cur_vvec == vvec_all_ucc1); |
| 146 |
VecI8 ucc2_match_vvec = (cur_vvec == vvec_all_ucc2); |
| 147 |
VecI8 ucc3_match_vvec = (cur_vvec == vvec_all_ucc3); |
| 148 |
const uint32_t leading_byte_ct = starting_addr - R_CAST(uintptr_t, ss_viter); |
| 149 |
# ifndef SIMDE_ARM_NEON_A32V8_NATIVE |
| 150 |
uint32_t matching_bytes = veci8_movemask(zero_match_vvec | ucc1_match_vvec | ucc2_match_vvec | ucc3_match_vvec); |
| 151 |
matching_bytes &= UINT32_MAX << leading_byte_ct; |
| 152 |
while (!matching_bytes) { |
| 153 |
++ss_viter; |
| 154 |
cur_vvec = *ss_viter; |
| 155 |
zero_match_vvec = (cur_vvec == vvec_all_zero); |
| 156 |
ucc1_match_vvec = (cur_vvec == vvec_all_ucc1); |
| 157 |
ucc2_match_vvec = (cur_vvec == vvec_all_ucc2); |
| 158 |
ucc3_match_vvec = (cur_vvec == vvec_all_ucc3); |
| 159 |
matching_bytes = veci8_movemask(zero_match_vvec | ucc1_match_vvec | ucc2_match_vvec | ucc3_match_vvec); |
| 160 |
} |
| 161 |
const uint32_t byte_offset_in_vec = ctzu32(matching_bytes); |
| 162 |
# else |
| 163 |
uint64_t matching_nybbles = arm_shrn4_i8(zero_match_vvec | ucc1_match_vvec | ucc2_match_vvec | ucc3_match_vvec); |
| 164 |
matching_nybbles &= UINT64_MAX << (4 * leading_byte_ct); |
| 165 |
while (!matching_nybbles) { |
| 166 |
++ss_viter; |
| 167 |
cur_vvec = *ss_viter; |
| 168 |
zero_match_vvec = (cur_vvec == vvec_all_zero); |
| 169 |
ucc1_match_vvec = (cur_vvec == vvec_all_ucc1); |
| 170 |
ucc2_match_vvec = (cur_vvec == vvec_all_ucc2); |
| 171 |
ucc3_match_vvec = (cur_vvec == vvec_all_ucc3); |
| 172 |
matching_nybbles = arm_shrn4_i8(zero_match_vvec | ucc1_match_vvec | ucc2_match_vvec | ucc3_match_vvec); |
| 173 |
} |
| 174 |
const uint32_t byte_offset_in_vec = ctzw(matching_nybbles) / 4; |
| 175 |
# endif |
| 176 |
return &(DowncastToXC(ss_viter)[byte_offset_in_vec]); |
| 177 |
} |
| 178 |
#else |
| 179 |
CXXCONST_VOIDP rawmemchr2(const void* ss, unsigned char ucc1, unsigned char ucc2) { |
| 180 |
for (const unsigned char* ss_iter = S_CAST(const unsigned char*, ss); ; ++ss_iter) { |
| 181 |
unsigned char ucc = *ss_iter; |
| 182 |
if ((ucc == ucc1) || (ucc == ucc2)) { |
| 183 |
return S_CAST(CXXCONST_VOIDP, ss_iter); |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
CXXCONST_VOIDP rawmemchr3(const void* ss, unsigned char ucc1, unsigned char ucc2, unsigned char ucc3) { |
| 189 |
for (const unsigned char* ss_iter = S_CAST(const unsigned char*, ss); ; ++ss_iter) { |
| 190 |
unsigned char ucc = *ss_iter; |
| 191 |
if ((ucc == ucc1) || (ucc == ucc2) || (ucc == ucc3)) { |
| 192 |
return S_CAST(CXXCONST_VOIDP, ss_iter); |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
CXXCONST_CP strchrnul3(const char* ss, unsigned char ucc1, unsigned char ucc2, unsigned char ucc3) { |
| 198 |
for (; ; ++ss) { |
| 199 |
unsigned char ucc = *ss; |
| 200 |
// at this point, checking against a precomputed 256-byte array may be |
| 201 |
// better? test this. |
| 202 |
if ((!ucc) || (ucc == ucc1) || (ucc == ucc2) || (ucc == ucc3)) { |
| 203 |
return S_CAST(CXXCONST_CP, ss); |
| 204 |
} |
| 205 |
} |
| 206 |
} |
| 207 |
#endif |
| 208 |
|
| 209 |
void WordWrap(uint32_t suffix_len, char* strbuf) { |
| 210 |
// Input: A null-terminated string with no intermediate newlines. If |
| 211 |
// suffix_len is zero, there should be a terminating \n; otherwise, |
| 212 |
// the last character should be a space. The allocation the string is |
| 213 |
// part of must include at least ~80 bytes past the string end. |
| 214 |
// Effect: Spaces are replaced with newlines in a manner that plays well with |
| 215 |
// 80 column terminal windows. (Multi-space blocks are never |
| 216 |
// collapsed.) |
| 217 |
// Considered UTF-8 awareness, but then decided against it after reading |
| 218 |
// https://denisbider.blogspot.com/2015/09/when-monospace-fonts-arent-unicode.html . |
| 219 |
char* token_start = strbuf; |
| 220 |
char* line_end = &(strbuf[79]); |
| 221 |
char* token_end; |
| 222 |
while (1) { |
| 223 |
while (*token_start == ' ') { |
| 224 |
++token_start; |
| 225 |
} |
| 226 |
if (token_start > line_end) { |
| 227 |
do { |
| 228 |
*line_end = '\n'; |
| 229 |
line_end = &(line_end[80]); |
| 230 |
} while (token_start > line_end); |
| 231 |
} |
| 232 |
token_end = strchr(token_start, ' '); |
| 233 |
if (!token_end) { |
| 234 |
if (&(token_start[79]) == line_end) { |
| 235 |
return; |
| 236 |
} |
| 237 |
token_end = strnul(token_start); |
| 238 |
if (!suffix_len) { |
| 239 |
if (token_end <= &(line_end[1])) { |
| 240 |
// okay if end-of-string is one past the end, because function |
| 241 |
// assumes last character is \n in suffix_len == 0 case |
| 242 |
assert(token_end[-1] == '\n'); |
| 243 |
return; |
| 244 |
} |
| 245 |
} else { |
| 246 |
if (&(token_end[suffix_len]) <= line_end) { |
| 247 |
return; |
| 248 |
} |
| 249 |
// because of terminal space assumption, token_start actually points |
| 250 |
// to the end of the string |
| 251 |
assert(token_start[-1] == ' '); |
| 252 |
} |
| 253 |
token_start[-1] = '\n'; |
| 254 |
return; |
| 255 |
} |
| 256 |
if (token_end > line_end) { |
| 257 |
if (&(token_start[79]) != line_end) { |
| 258 |
token_start[-1] = '\n'; |
| 259 |
line_end = &(token_start[79]); |
| 260 |
if (token_end > line_end) { |
| 261 |
// single really long token, can't do anything beyond putting it on |
| 262 |
// its own line |
| 263 |
*token_end = '\n'; |
| 264 |
line_end = &(token_end[80]); |
| 265 |
} |
| 266 |
} else { |
| 267 |
// single really long token, *and* previous token was either |
| 268 |
// nonexistent or long |
| 269 |
*token_end = '\n'; |
| 270 |
line_end = &(token_end[80]); |
| 271 |
} |
| 272 |
} |
| 273 |
token_start = &(token_end[1]); |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
void WordWrapMultiline(char* strbuf) { |
| 278 |
for (char* line_start = strbuf; *line_start != '\0'; ) { |
| 279 |
char* line_end = strchrnul(line_start, '\n'); |
| 280 |
if (*line_end == '\0') { |
| 281 |
line_end[0] = '\n'; |
| 282 |
line_end[1] = '\0'; |
| 283 |
} |
| 284 |
char* next_line_start = &(line_end[1]); |
| 285 |
// Temporarily clobber first character of next line so WordWrap() |
| 286 |
// conditions are satisfied. |
| 287 |
const char first_char_of_next_line = *next_line_start; |
| 288 |
*next_line_start = '\0'; |
| 289 |
WordWrap(0, line_start); |
| 290 |
*next_line_start = first_char_of_next_line; |
| 291 |
line_start = next_line_start; |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
// This implementation is from Kendall Willets. See |
| 296 |
// https://lemire.me/blog/2021/06/03/computing-the-number-of-digits-of-an-integer-even-faster/ |
| 297 |
|
| 298 |
#ifdef USE_AVX2 |
| 299 |
// bugfix (14 Aug 2022): _lzcnt_u32(0) is 32. |
| 300 |
static const uint64_t kLzcntUintSlenTable[] = |
| 301 |
{42949672960LL, 42949672960LL, 41949672960LL, 41949672960LL, 41949672960LL, |
| 302 |
38554705664LL, 38554705664LL, 38554705664LL, 34349738368LL, 34349738368LL, |
| 303 |
34349738368LL, 34349738368LL, 30063771072LL, 30063771072LL, 30063771072LL, |
| 304 |
25769703776LL, 25769703776LL, 25769703776LL, 21474826480LL, 21474826480LL, |
| 305 |
21474826480LL, 21474826480LL, 17179868184LL, 17179868184LL, 17179868184LL, |
| 306 |
12884901788LL, 12884901788LL, 12884901788LL, 8589934582LL, 8589934582LL, |
| 307 |
8589934582LL, 4294967296LL, 4294967296LL}; |
| 308 |
|
| 309 |
uint32_t UintSlen(uint32_t num) { |
| 310 |
const uint32_t lz_ct = _lzcnt_u32(num); |
| 311 |
return (num + kLzcntUintSlenTable[lz_ct]) >> 32; |
| 312 |
} |
| 313 |
#else |
| 314 |
static const uint64_t kBsrUintSlenTable[] = |
| 315 |
{4294967296LL, 8589934582LL, 8589934582LL, 8589934582LL, 12884901788LL, |
| 316 |
12884901788LL, 12884901788LL, 17179868184LL, 17179868184LL, 17179868184LL, |
| 317 |
21474826480LL, 21474826480LL, 21474826480LL, 21474826480LL, 25769703776LL, |
| 318 |
25769703776LL, 25769703776LL, 30063771072LL, 30063771072LL, 30063771072LL, |
| 319 |
34349738368LL, 34349738368LL, 34349738368LL, 34349738368LL, 38554705664LL, |
| 320 |
38554705664LL, 38554705664LL, 41949672960LL, 41949672960LL, 41949672960LL, |
| 321 |
42949672960LL, 42949672960LL}; |
| 322 |
|
| 323 |
uint32_t UintSlen(uint32_t num) { |
| 324 |
// tried divide-by-10 and divide-by-100 loops, they were slower |
| 325 |
// also tried a hardcoded binary tree, it was better but still slower |
| 326 |
|
| 327 |
// bsru32(0) is undefined |
| 328 |
num |= 1; |
| 329 |
const uint32_t top_bit_pos = bsru32(num); |
| 330 |
return (num + kBsrUintSlenTable[top_bit_pos]) >> 32; |
| 331 |
} |
| 332 |
#endif |
| 333 |
|
| 334 |
// May read (kBytesPerWord - 1) bytes past the end of each string. |
| 335 |
// This can be quite a bit faster than stock strcmp on x86 for sorting, though |
| 336 |
// benchmarking is necessary in general (compiler may perform its own strcmp |
| 337 |
// optimizations). |
| 338 |
int32_t strcmp_overread(const char* s1, const char* s2) { |
| 339 |
#ifndef NO_UNALIGNED |
| 340 |
const uintptr_t* s1_alias = R_CAST(const uintptr_t*, s1); |
| 341 |
const uintptr_t* s2_alias = R_CAST(const uintptr_t*, s2); |
| 342 |
for (uintptr_t widx = 0; ; ++widx) { |
| 343 |
uintptr_t w1 = s1_alias[widx]; |
| 344 |
const uintptr_t zcheck = DetectFirstZeroByte(w1); |
| 345 |
uintptr_t w2 = s2_alias[widx]; |
| 346 |
if (zcheck) { |
| 347 |
// Mask out bytes past the known null. |
| 348 |
const uintptr_t mask = zcheck ^ (zcheck - 1); |
| 349 |
w1 &= mask; |
| 350 |
w2 &= mask; |
| 351 |
if (w1 == w2) { |
| 352 |
return 0; |
| 353 |
} |
| 354 |
} else if (w1 == w2) { |
| 355 |
continue; |
| 356 |
} |
| 357 |
// bugfix (30 Jun 2018): forgot to adhere to strcmp instead of std::sort |
| 358 |
// interface |
| 359 |
# ifdef __LP64__ |
| 360 |
return (__builtin_bswap64(w1) < __builtin_bswap64(w2))? -1 : 1; |
| 361 |
# else |
| 362 |
return (__builtin_bswap32(w1) < __builtin_bswap32(w2))? -1 : 1; |
| 363 |
# endif |
| 364 |
} |
| 365 |
#else // NO_UNALIGNED |
| 366 |
return strcmp(s1, s2); |
| 367 |
#endif |
| 368 |
} |
| 369 |
|
| 370 |
int32_t strcmp_casted(const void* s1, const void* s2) { |
| 371 |
return strcmp(S_CAST(const char*, s1), S_CAST(const char*, s2)); |
| 372 |
} |
| 373 |
|
| 374 |
int32_t strcmp_overread_casted(const void* s1, const void* s2) { |
| 375 |
return strcmp_overread(S_CAST(const char*, s1), S_CAST(const char*, s2)); |
| 376 |
} |
| 377 |
|
| 378 |
// PLINK 2's natural sort uses the following logic: |
| 379 |
// - All alphabetic characters act as if they are capitalized, except for |
| 380 |
// tiebreaking purposes (where ASCII is used). |
| 381 |
// - Numbers are compared by magnitude, with the exception of... |
| 382 |
// - Numbers with leading zero(es). If you're putting extraneous zeroes in |
| 383 |
// front of IDs, we assume they're there to force particular items to be sorted |
| 384 |
// earlier, rather than just appearing at random. So, unlike many natural sort |
| 385 |
// implementations, we sort 00200 < 021 < 20: all numbers with n leading zeroes |
| 386 |
// are sorted before all numbers with (n-1) leading zeroes; magnitude only |
| 387 |
// applies if the leading zero counts match. This handles e.g. subbasement |
| 388 |
// room numbering properly. |
| 389 |
// |
| 390 |
// This won't always do what you want if your IDs have variable-length decimals |
| 391 |
// in them (e.g. it yields 0.99 < 0.101); if you don't want to fall back on |
| 392 |
// ASCII sort, enforce a fixed number of digits after the decimal point. Also |
| 393 |
// note that ASCII sort is outright better for e.g. numbers represented in |
| 394 |
// hexadecimal or base 36. In principle, it's possible to reliably autodetect |
| 395 |
// some of these cases (especially hexadecimal numbers beginning with "0x"), |
| 396 |
// but that'll never be perfect so we just let the user toggle the sort method. |
| 397 |
int32_t strcmp_natural_scan_forward(const char* s1, const char* s2) { |
| 398 |
// assumes s1 and s2 currently point to the middle of a mismatching number, |
| 399 |
// where s1 < s2. |
| 400 |
char c1; |
| 401 |
char c2; |
| 402 |
do { |
| 403 |
c1 = *(++s1); |
| 404 |
c2 = *(++s2); |
| 405 |
if (IsNotDigit(c1)) { |
| 406 |
return -1; |
| 407 |
} |
| 408 |
} while (IsDigit(c2)); |
| 409 |
return 1; |
| 410 |
} |
| 411 |
|
| 412 |
// We have the following major states: |
| 413 |
// 0 (initial): strings perfectly match so far, last char (if any) is |
| 414 |
// nonnumeric. |
| 415 |
// 1: strings perfectly match so far, last char is numeric. |
| 416 |
// 2: strings match except for capitalization, last char is nonnumeric. |
| 417 |
// 3: strings match except for capitalization, last char is numeric. |
| 418 |
// strcmp_natural_tiebroken() expresses the logic for states 2 and 3, while |
| 419 |
// strcmp_natural_uncasted() handles states 0 and 1. |
| 420 |
int32_t strcmp_natural_tiebroken(const char* s1, const char* s2) { |
| 421 |
// assumes ties should be broken in favor of s2. |
| 422 |
unsigned char uc1 = *(++s1); |
| 423 |
unsigned char uc2 = *(++s2); |
| 424 |
while (IsNotNzdigit(uc1) && IsNotNzdigit(uc2)) { |
| 425 |
// state 2 |
| 426 |
strcmp_natural_tiebroken_state_2: |
| 427 |
if (uc1 != uc2) { |
| 428 |
if ((uc1 >= 'a') && (uc1 <= 'z')) { |
| 429 |
uc1 -= 32; |
| 430 |
} |
| 431 |
if ((uc2 >= 'a') && (uc2 <= 'z')) { |
| 432 |
uc2 -= 32; |
| 433 |
} |
| 434 |
if (uc1 < uc2) { |
| 435 |
return -1; |
| 436 |
} |
| 437 |
if (uc1 > uc2) { |
| 438 |
return 1; |
| 439 |
} |
| 440 |
} else if (!uc1) { |
| 441 |
return -1; |
| 442 |
} |
| 443 |
uc1 = *(++s1); |
| 444 |
uc2 = *(++s2); |
| 445 |
} |
| 446 |
if (IsNotNzdigit(uc1) || IsNotNzdigit(uc2)) { |
| 447 |
return (uc1 < uc2)? -1 : 1; |
| 448 |
} |
| 449 |
do { |
| 450 |
// state 3 |
| 451 |
if (uc1 != uc2) { |
| 452 |
if (IsDigit(uc2)) { |
| 453 |
if (uc1 < uc2) { |
| 454 |
return strcmp_natural_scan_forward(s1, s2); |
| 455 |
} |
| 456 |
return -strcmp_natural_scan_forward(s2, s1); |
| 457 |
} |
| 458 |
return 1; |
| 459 |
} |
| 460 |
uc1 = *(++s1); |
| 461 |
uc2 = *(++s2); |
| 462 |
} while (IsDigit(uc1)); |
| 463 |
if (IsDigit(uc2)) { |
| 464 |
return -1; |
| 465 |
} |
| 466 |
// skip the while (is_not_digit...) check |
| 467 |
goto strcmp_natural_tiebroken_state_2; |
| 468 |
} |
| 469 |
|
| 470 |
int32_t strcmp_natural_uncasted(const char* s1, const char* s2) { |
| 471 |
unsigned char uc1 = *s1; |
| 472 |
unsigned char uc2 = *s2; |
| 473 |
while (IsNotNzdigit(uc1) && IsNotNzdigit(uc2)) { |
| 474 |
// state 0 |
| 475 |
strcmp_natural_uncasted_state_0: |
| 476 |
if (uc1 != uc2) { |
| 477 |
if ((uc1 >= 'a') && (uc1 <= 'z')) { |
| 478 |
if (uc2 + 32 == uc1) { |
| 479 |
return -strcmp_natural_tiebroken(s2, s1); |
| 480 |
} |
| 481 |
if ((uc2 < 'a') || (uc2 > 'z')) { |
| 482 |
uc1 -= 32; |
| 483 |
} |
| 484 |
} else if ((uc2 >= 'a') && (uc2 <= 'z')) { |
| 485 |
uc2 -= 32; |
| 486 |
if (uc1 == uc2) { |
| 487 |
return strcmp_natural_tiebroken(s1, s2); |
| 488 |
} |
| 489 |
} |
| 490 |
return (uc1 < uc2)? -1 : 1; |
| 491 |
} |
| 492 |
if (!uc1) { |
| 493 |
return 0; |
| 494 |
} |
| 495 |
uc1 = *(++s1); |
| 496 |
uc2 = *(++s2); |
| 497 |
} |
| 498 |
if (IsNotNzdigit(uc1) || IsNotNzdigit(uc2)) { |
| 499 |
return (uc1 < uc2)? -1 : 1; |
| 500 |
} |
| 501 |
do { |
| 502 |
// state 1 |
| 503 |
if (uc1 != uc2) { |
| 504 |
if (IsDigit(uc2)) { |
| 505 |
if (uc1 < uc2) { |
| 506 |
return strcmp_natural_scan_forward(s1, s2); |
| 507 |
} |
| 508 |
return -strcmp_natural_scan_forward(s2, s1); |
| 509 |
} |
| 510 |
return 1; |
| 511 |
} |
| 512 |
uc1 = *(++s1); |
| 513 |
uc2 = *(++s2); |
| 514 |
} while (IsDigit(uc1)); |
| 515 |
if (IsDigit(uc2)) { |
| 516 |
return -1; |
| 517 |
} |
| 518 |
goto strcmp_natural_uncasted_state_0; |
| 519 |
} |
| 520 |
|
| 521 |
int32_t strcmp_natural(const void* s1, const void* s2) { |
| 522 |
return strcmp_natural_uncasted(S_CAST(const char*, s1), S_CAST(const char*, s2)); |
| 523 |
} |
| 524 |
|
| 525 |
int32_t strcmp_deref(const void* s1, const void* s2) { |
| 526 |
return strcmp(*S_CAST(const char* const*, s1), *S_CAST(const char* const*, s2)); |
| 527 |
} |
| 528 |
|
| 529 |
int32_t strcmp_overread_deref(const void* s1, const void* s2) { |
| 530 |
return strcmp_overread(*S_CAST(const char* const*, s1), *S_CAST(const char* const*, s2)); |
| 531 |
} |
| 532 |
|
| 533 |
int32_t strcmp_natural_deref(const void* s1, const void* s2) { |
| 534 |
return strcmp_natural_uncasted(*S_CAST(const char* const*, s1), *S_CAST(const char* const*, s2)); |
| 535 |
} |
| 536 |
|
| 537 |
#ifdef __cplusplus |
| 538 |
static_assert(sizeof(Strbuf28Ui) == 32, "Strbuf28Ui is not laid out as expected."); |
| 539 |
static_assert(offsetof(Strbuf28Ui, orig_idx) == 28, "Strbuf28Ui is not laid out as expected."); |
| 540 |
static_assert(sizeof(Strbuf60Ui) == 64, "Strbuf60Ui is not laid out as expected."); |
| 541 |
static_assert(offsetof(Strbuf60Ui, orig_idx) == 60, "Strbuf60Ui is not laid out as expected."); |
| 542 |
uintptr_t GetStrboxsortWentryBlen(uintptr_t max_str_blen) { |
| 543 |
if (max_str_blen <= 28) { |
| 544 |
return sizeof(Strbuf28Ui); |
| 545 |
} |
| 546 |
if (max_str_blen <= 60) { |
| 547 |
return sizeof(Strbuf60Ui); |
| 548 |
} |
| 549 |
return max_str_blen; |
| 550 |
} |
| 551 |
#else |
| 552 |
uintptr_t GetStrboxsortWentryBlen(uintptr_t max_str_blen) { |
| 553 |
return MAXV(max_str_blen, sizeof(StrSortIndexedDeref)); |
| 554 |
} |
| 555 |
#endif |
| 556 |
|
| 557 |
// Assumed that sort_wkspace has size >= str_ct * |
| 558 |
// max(sizeof(StrSortIndexedDeref), max_str_blen). |
| 559 |
// Must be ok to overread. |
| 560 |
void SortStrboxIndexed2Fallback(uintptr_t str_ct, uintptr_t max_str_blen, uint32_t use_nsort, char* strbox, uint32_t* id_map, void* sort_wkspace) { |
| 561 |
StrSortIndexedDerefOverread* wkspace_alias = S_CAST(StrSortIndexedDerefOverread*, sort_wkspace); |
| 562 |
for (uintptr_t str_idx = 0; str_idx != str_ct; ++str_idx) { |
| 563 |
wkspace_alias[str_idx].strptr = &(strbox[str_idx * max_str_blen]); |
| 564 |
wkspace_alias[str_idx].orig_idx = id_map[str_idx]; |
| 565 |
} |
| 566 |
if (!use_nsort) { |
| 567 |
STD_SORT_PAR_UNSEQ(str_ct, strcmp_overread_deref, wkspace_alias); |
| 568 |
} else { |
| 569 |
#ifdef __cplusplus |
| 570 |
StrNsortIndexedDeref* wkspace_alias2 = R_CAST(StrNsortIndexedDeref*, wkspace_alias); |
| 571 |
STD_SORT_PAR_UNSEQ(str_ct, nullptr, wkspace_alias2); |
| 572 |
#else |
| 573 |
STD_SORT_PAR_UNSEQ(str_ct, strcmp_natural_deref, wkspace_alias); |
| 574 |
#endif |
| 575 |
} |
| 576 |
for (uintptr_t str_idx = 0; str_idx != str_ct; ++str_idx) { |
| 577 |
id_map[str_idx] = wkspace_alias[str_idx].orig_idx; |
| 578 |
} |
| 579 |
#ifndef __cplusplus |
| 580 |
if (max_str_blen < sizeof(StrSortIndexedDeref)) { |
| 581 |
// actually better to use non-deref sort here, but just get this working |
| 582 |
// properly for now |
| 583 |
for (uint32_t new_idx = 0; new_idx != str_ct; ++new_idx) { |
| 584 |
const char* strptr = wkspace_alias[new_idx].strptr; |
| 585 |
// todo: check whether memcpy(., ., max_str_blen) tends to be better |
| 586 |
strcpy(&(DowncastToC(wkspace_alias)[new_idx * max_str_blen]), strptr); |
| 587 |
} |
| 588 |
} else { |
| 589 |
#endif |
| 590 |
// bugfix: need to handle id_map[str_idx] != str_idx |
| 591 |
uint32_t new_idx = str_ct; |
| 592 |
do { |
| 593 |
--new_idx; |
| 594 |
const char* strptr = wkspace_alias[new_idx].strptr; |
| 595 |
strcpy(&(DowncastToC(wkspace_alias)[new_idx * max_str_blen]), strptr); |
| 596 |
} while (new_idx); |
| 597 |
#ifndef __cplusplus |
| 598 |
} |
| 599 |
#endif |
| 600 |
memcpy(strbox, wkspace_alias, str_ct * max_str_blen); |
| 601 |
} |
| 602 |
|
| 603 |
#ifdef __cplusplus |
| 604 |
typedef struct WordCmp32bStruct { |
| 605 |
uintptr_t words[32 / kBytesPerWord]; |
| 606 |
bool operator<(const struct WordCmp32bStruct& rhs) const { |
| 607 |
uint32_t idx = 0; |
| 608 |
do { |
| 609 |
const uintptr_t cur_word = words[idx]; |
| 610 |
const uintptr_t rhs_word = rhs.words[idx]; |
| 611 |
if (cur_word != rhs_word) { |
| 612 |
# ifdef __LP64__ |
| 613 |
return __builtin_bswap64(cur_word) < __builtin_bswap64(rhs_word); |
| 614 |
# else |
| 615 |
return __builtin_bswap32(cur_word) < __builtin_bswap32(rhs_word); |
| 616 |
# endif |
| 617 |
} |
| 618 |
} while (++idx < (32 / kBytesPerWord)); |
| 619 |
return false; |
| 620 |
} |
| 621 |
} WordCmp32b; |
| 622 |
|
| 623 |
typedef struct WordCmp64bStruct { |
| 624 |
uintptr_t words[64 / kBytesPerWord]; |
| 625 |
bool operator<(const struct WordCmp64bStruct& rhs) const { |
| 626 |
uint32_t idx = 0; |
| 627 |
do { |
| 628 |
const uintptr_t cur_word = words[idx]; |
| 629 |
const uintptr_t rhs_word = rhs.words[idx]; |
| 630 |
if (cur_word != rhs_word) { |
| 631 |
# ifdef __LP64__ |
| 632 |
return __builtin_bswap64(cur_word) < __builtin_bswap64(rhs_word); |
| 633 |
# else |
| 634 |
return __builtin_bswap32(cur_word) < __builtin_bswap32(rhs_word); |
| 635 |
# endif |
| 636 |
} |
| 637 |
} while (++idx < (64 / kBytesPerWord)); |
| 638 |
return false; |
| 639 |
} |
| 640 |
} WordCmp64b; |
| 641 |
|
| 642 |
static_assert(sizeof(WordCmp32b) == 32, "WordCmp32b does not have the expected size."); |
| 643 |
static_assert(sizeof(WordCmp64b) == 64, "WordCmp64b does not have the expected size."); |
| 644 |
|
| 645 |
void SortStrbox32bFinish(uintptr_t str_ct, uintptr_t max_str_blen, uint32_t use_nsort, Strbuf28Ui* filled_wkspace, char* sorted_strbox, uint32_t* id_map) { |
| 646 |
if (!use_nsort) { |
| 647 |
WordCmp32b* wkspace_alias = R_CAST(WordCmp32b*, filled_wkspace); |
| 648 |
STD_SORT_PAR_UNSEQ(str_ct, nullptr, wkspace_alias); |
| 649 |
} else { |
| 650 |
STD_SORT_PAR_UNSEQ(str_ct, nullptr, filled_wkspace); |
| 651 |
} |
| 652 |
for (uintptr_t str_idx = 0; str_idx != str_ct; ++str_idx) { |
| 653 |
strcpy(&(sorted_strbox[str_idx * max_str_blen]), filled_wkspace[str_idx].strbuf); |
| 654 |
id_map[str_idx] = filled_wkspace[str_idx].orig_idx; |
| 655 |
} |
| 656 |
} |
| 657 |
|
| 658 |
void SortStrbox64bFinish(uintptr_t str_ct, uintptr_t max_str_blen, uint32_t use_nsort, Strbuf60Ui* filled_wkspace, char* sorted_strbox, uint32_t* id_map) { |
| 659 |
if (!use_nsort) { |
| 660 |
WordCmp64b* wkspace_alias = R_CAST(WordCmp64b*, filled_wkspace); |
| 661 |
STD_SORT_PAR_UNSEQ(str_ct, nullptr, wkspace_alias); |
| 662 |
} else { |
| 663 |
STD_SORT_PAR_UNSEQ(str_ct, nullptr, filled_wkspace); |
| 664 |
} |
| 665 |
for (uintptr_t str_idx = 0; str_idx != str_ct; ++str_idx) { |
| 666 |
strcpy(&(sorted_strbox[str_idx * max_str_blen]), filled_wkspace[str_idx].strbuf); |
| 667 |
id_map[str_idx] = filled_wkspace[str_idx].orig_idx; |
| 668 |
} |
| 669 |
} |
| 670 |
|
| 671 |
// Normally use SortStrboxIndexed(), but this version is necessary before |
| 672 |
// g_bigstack has been allocated. |
| 673 |
void SortStrboxIndexed2(uintptr_t str_ct, uintptr_t max_str_blen, uint32_t use_nsort, char* strbox, uint32_t* id_map, void* sort_wkspace) { |
| 674 |
if (max_str_blen <= 28) { |
| 675 |
Strbuf28Ui* wkspace_alias = S_CAST(Strbuf28Ui*, sort_wkspace); |
| 676 |
for (uintptr_t str_idx = 0; str_idx != str_ct; ++str_idx) { |
| 677 |
const char* cur_str = &(strbox[str_idx * max_str_blen]); |
| 678 |
strcpy(wkspace_alias[str_idx].strbuf, cur_str); |
| 679 |
wkspace_alias[str_idx].orig_idx = id_map[str_idx]; |
| 680 |
} |
| 681 |
SortStrbox32bFinish(str_ct, max_str_blen, use_nsort, wkspace_alias, strbox, id_map); |
| 682 |
return; |
| 683 |
} |
| 684 |
if (max_str_blen <= 60) { |
| 685 |
Strbuf60Ui* wkspace_alias = S_CAST(Strbuf60Ui*, sort_wkspace); |
| 686 |
for (uintptr_t str_idx = 0; str_idx != str_ct; ++str_idx) { |
| 687 |
const char* cur_str = &(strbox[str_idx * max_str_blen]); |
| 688 |
strcpy(wkspace_alias[str_idx].strbuf, cur_str); |
| 689 |
wkspace_alias[str_idx].orig_idx = id_map[str_idx]; |
| 690 |
} |
| 691 |
SortStrbox64bFinish(str_ct, max_str_blen, use_nsort, wkspace_alias, strbox, id_map); |
| 692 |
return; |
| 693 |
} |
| 694 |
SortStrboxIndexed2Fallback(str_ct, max_str_blen, use_nsort, strbox, id_map, sort_wkspace); |
| 695 |
} |
| 696 |
#endif // __cplusplus |
| 697 |
|
| 698 |
// Must be ok to overread. |
| 699 |
BoolErr SortStrboxIndexedMalloc(uintptr_t str_ct, uintptr_t max_str_blen, char* strbox, uint32_t* id_map) { |
| 700 |
if (str_ct < 2) { |
| 701 |
return 0; |
| 702 |
} |
| 703 |
const uintptr_t wkspace_entry_blen = GetStrboxsortWentryBlen(max_str_blen); |
| 704 |
unsigned char* sort_wkspace; |
| 705 |
if (unlikely(pgl_malloc(str_ct * wkspace_entry_blen, &sort_wkspace))) { |
| 706 |
return 1; |
| 707 |
} |
| 708 |
SortStrboxIndexed2(str_ct, max_str_blen, 0, strbox, id_map, sort_wkspace); |
| 709 |
free(sort_wkspace); |
| 710 |
return 0; |
| 711 |
} |
| 712 |
|
| 713 |
uint32_t CopyAndDedupSortedStrptrsToStrbox(const char* const* sorted_strptrs, uintptr_t str_ct, uintptr_t max_str_blen, char* strbox) { |
| 714 |
if (!str_ct) { |
| 715 |
return 0; |
| 716 |
} |
| 717 |
const char* const* sorted_strptrs_iter = sorted_strptrs; |
| 718 |
const char* const* sorted_strptrs_end = &(sorted_strptrs[str_ct]); |
| 719 |
uintptr_t write_idx = 0; |
| 720 |
uint32_t prev_slen = UINT32_MAX; |
| 721 |
const char* prev_str = nullptr; |
| 722 |
do { |
| 723 |
const char* cur_str = *sorted_strptrs_iter++; |
| 724 |
const uint32_t cur_slen = strlen(cur_str); |
| 725 |
if ((cur_slen != prev_slen) || (!memequal(cur_str, prev_str, prev_slen))) { |
| 726 |
memcpy(&(strbox[write_idx * max_str_blen]), cur_str, cur_slen + 1); |
| 727 |
++write_idx; |
| 728 |
prev_str = cur_str; |
| 729 |
} |
| 730 |
} while (sorted_strptrs_iter != sorted_strptrs_end); |
| 731 |
return write_idx; |
| 732 |
} |
| 733 |
|
| 734 |
|
| 735 |
void StrptrArrSortMain(uintptr_t str_ct, uint32_t overread_ok, uint32_t use_nsort, StrSortIndexedDeref* wkspace_alias) { |
| 736 |
if (!use_nsort) { |
| 737 |
if (overread_ok) { |
| 738 |
#ifdef __cplusplus |
| 739 |
StrSortIndexedDerefOverread* wkspace_alias2 = R_CAST(StrSortIndexedDerefOverread*, wkspace_alias); |
| 740 |
STD_SORT_PAR_UNSEQ(str_ct, nullptr, wkspace_alias2); |
| 741 |
#else |
| 742 |
STD_SORT_PAR_UNSEQ(str_ct, strcmp_overread_deref, wkspace_alias); |
| 743 |
#endif |
| 744 |
} else { |
| 745 |
STD_SORT_PAR_UNSEQ(str_ct, strcmp_deref, wkspace_alias); |
| 746 |
} |
| 747 |
} else { |
| 748 |
#ifdef __cplusplus |
| 749 |
StrNsortIndexedDeref* wkspace_alias2 = R_CAST(StrNsortIndexedDeref*, wkspace_alias); |
| 750 |
STD_SORT_PAR_UNSEQ(str_ct, nullptr, wkspace_alias2); |
| 751 |
#else |
| 752 |
STD_SORT_PAR_UNSEQ(str_ct, strcmp_natural_deref, wkspace_alias); |
| 753 |
#endif |
| 754 |
} |
| 755 |
} |
| 756 |
|
| 757 |
void SortStrptrArrIndexed2(uint32_t str_ct, uint32_t leave_first_alone, uint32_t overread_ok, uint32_t use_nsort, const char** strptrs, uint32_t* new_to_old_idx, uint32_t* old_to_new_idx, void* wkspace) { |
| 758 |
const uint32_t str_sort_ct = str_ct - leave_first_alone; |
| 759 |
if (str_sort_ct < 2) { |
| 760 |
if (new_to_old_idx) { |
| 761 |
for (uint32_t str_idx = 0; str_idx != str_ct; ++str_idx) { |
| 762 |
new_to_old_idx[str_idx] = str_idx; |
| 763 |
} |
| 764 |
} |
| 765 |
if (old_to_new_idx) { |
| 766 |
for (uint32_t str_idx = 0; str_idx != str_ct; ++str_idx) { |
| 767 |
old_to_new_idx[str_idx] = str_idx; |
| 768 |
} |
| 769 |
} |
| 770 |
return; |
| 771 |
} |
| 772 |
StrSortIndexedDeref* wkspace_alias = S_CAST(StrSortIndexedDeref*, wkspace); |
| 773 |
const char** strptrs_to_sort = &(strptrs[leave_first_alone]); |
| 774 |
for (uint32_t str_idx = 0; str_idx != str_sort_ct; ++str_idx) { |
| 775 |
wkspace_alias[str_idx].strptr = strptrs_to_sort[str_idx]; |
| 776 |
wkspace_alias[str_idx].orig_idx = str_idx + leave_first_alone; |
| 777 |
} |
| 778 |
StrptrArrSortMain(str_sort_ct, overread_ok, use_nsort, wkspace_alias); |
| 779 |
if (leave_first_alone) { |
| 780 |
if (new_to_old_idx) { |
| 781 |
new_to_old_idx[0] = 0; |
| 782 |
new_to_old_idx = &(new_to_old_idx[1]); |
| 783 |
} |
| 784 |
if (old_to_new_idx) { |
| 785 |
old_to_new_idx[0] = 0; |
| 786 |
} |
| 787 |
} |
| 788 |
for (uint32_t str_idx = 0; str_idx != str_sort_ct; ++str_idx) { |
| 789 |
strptrs_to_sort[str_idx] = wkspace_alias[str_idx].strptr; |
| 790 |
const uint32_t orig_idx = wkspace_alias[str_idx].orig_idx; |
| 791 |
if (new_to_old_idx) { |
| 792 |
new_to_old_idx[str_idx] = orig_idx; |
| 793 |
} |
| 794 |
if (old_to_new_idx) { |
| 795 |
old_to_new_idx[orig_idx] = str_idx + leave_first_alone; |
| 796 |
} |
| 797 |
} |
| 798 |
} |
| 799 |
|
| 800 |
/* |
| 801 |
uint32_t match_upper(const char* str_iter, const char* fixed_str) { |
| 802 |
char cc = *fixed_str++; |
| 803 |
do { |
| 804 |
if ((((unsigned char)(*str_iter++)) & 0xdf) != ((unsigned char)cc)) { |
| 805 |
return 0; |
| 806 |
} |
| 807 |
cc = *fixed_str++; |
| 808 |
} while (cc); |
| 809 |
return !(*str_iter); |
| 810 |
} |
| 811 |
*/ |
| 812 |
|
| 813 |
/* |
| 814 |
#ifdef __LP64__ |
| 815 |
CXXCONST_CP FirstPrecharFar(const char* str_iter, uint32_t char_code) { |
| 816 |
const uintptr_t starting_addr = R_CAST(uintptr_t, str_iter); |
| 817 |
const VecUc* str_viter = R_CAST(const VecUc*, RoundDownPow2(starting_addr, kBytesPerVec)); |
| 818 |
const VecUc vvec_add = vecuc_set1(128 - char_code); |
| 819 |
VecUc cur_vvec = *str_viter; |
| 820 |
VecUc non_prechar_vvec = vecuc_adds(cur_vvec, vvec_add); |
| 821 |
uint32_t matching_bytes = ~vecuc_movemask(non_prechar_vvec); |
| 822 |
const uint32_t leading_byte_ct = starting_addr - R_CAST(uintptr_t, str_viter); |
| 823 |
matching_bytes &= UINT32_MAX << leading_byte_ct; |
| 824 |
// This typically isn't *that* long-range, so the Memrchr() |
| 825 |
// double-vector strategy is unlikely to be profitable? |
| 826 |
while (!matching_bytes) { |
| 827 |
++str_viter; |
| 828 |
cur_vvec = *str_viter; |
| 829 |
non_prechar_vvec = vecuc_adds(cur_vvec, vvec_add); |
| 830 |
matching_bytes = ~vecuc_movemask(non_prechar_vvec); |
| 831 |
} |
| 832 |
const uint32_t byte_offset_in_vec = ctzu32(matching_bytes); |
| 833 |
return &(DowncastToXC(str_viter)[byte_offset_in_vec]); |
| 834 |
} |
| 835 |
#endif |
| 836 |
*/ |
| 837 |
|
| 838 |
uint32_t MatchUpperCounted(const char* str, const char* fixed_str, uint32_t ct) { |
| 839 |
for (uint32_t uii = 0; uii != ct; ++uii) { |
| 840 |
if ((ctou32(str[uii]) & 0xdf) != ctou32(fixed_str[uii])) { |
| 841 |
return 0; |
| 842 |
} |
| 843 |
} |
| 844 |
return 1; |
| 845 |
} |
| 846 |
|
| 847 |
// might want to make this std::array in the future? |
| 848 |
static const char kToUpper[256] = { |
| 849 |
'\0', '\1', '\2', '\3', '\4', '\5', '\6', '\7', |
| 850 |
'\10', '\11', '\12', '\13', '\14', '\15', '\16', '\17', |
| 851 |
'\20', '\21', '\22', '\23', '\24', '\25', '\26', '\27', |
| 852 |
'\30', '\31', '\32', '\33', '\34', '\35', '\36', '\37', |
| 853 |
'\40', '\41', '\42', '\43', '\44', '\45', '\46', '\47', |
| 854 |
'\50', '\51', '\52', '\53', '\54', '\55', '\56', '\57', |
| 855 |
'\60', '\61', '\62', '\63', '\64', '\65', '\66', '\67', |
| 856 |
'\70', '\71', '\72', '\73', '\74', '\75', '\76', '\77', |
| 857 |
'\100', '\101', '\102', '\103', '\104', '\105', '\106', '\107', |
| 858 |
'\110', '\111', '\112', '\113', '\114', '\115', '\116', '\117', |
| 859 |
'\120', '\121', '\122', '\123', '\124', '\125', '\126', '\127', |
| 860 |
'\130', '\131', '\132', '\133', '\134', '\135', '\136', '\137', |
| 861 |
'\140', '\101', '\102', '\103', '\104', '\105', '\106', '\107', |
| 862 |
'\110', '\111', '\112', '\113', '\114', '\115', '\116', '\117', |
| 863 |
'\120', '\121', '\122', '\123', '\124', '\125', '\126', '\127', |
| 864 |
'\130', '\131', '\132', '\173', '\174', '\175', '\176', '\177', |
| 865 |
'\200', '\201', '\202', '\203', '\204', '\205', '\206', '\207', |
| 866 |
'\210', '\211', '\212', '\213', '\214', '\215', '\216', '\217', |
| 867 |
'\220', '\221', '\222', '\223', '\224', '\225', '\226', '\227', |
| 868 |
'\230', '\231', '\232', '\233', '\234', '\235', '\236', '\237', |
| 869 |
'\240', '\241', '\242', '\243', '\244', '\245', '\246', '\247', |
| 870 |
'\250', '\251', '\252', '\253', '\254', '\255', '\256', '\257', |
| 871 |
'\260', '\261', '\262', '\263', '\264', '\265', '\266', '\267', |
| 872 |
'\270', '\271', '\272', '\273', '\274', '\275', '\276', '\277', |
| 873 |
'\300', '\301', '\302', '\303', '\304', '\305', '\306', '\307', |
| 874 |
'\310', '\311', '\312', '\313', '\314', '\315', '\316', '\317', |
| 875 |
'\320', '\321', '\322', '\323', '\324', '\325', '\326', '\327', |
| 876 |
'\330', '\331', '\332', '\333', '\334', '\335', '\336', '\337', |
| 877 |
'\340', '\341', '\342', '\343', '\344', '\345', '\346', '\347', |
| 878 |
'\350', '\351', '\352', '\353', '\354', '\355', '\356', '\357', |
| 879 |
'\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367', |
| 880 |
'\370', '\371', '\372', '\373', '\374', '\375', '\376', '\377' |
| 881 |
}; |
| 882 |
|
| 883 |
uint32_t strcaseequal(const char* str1, const char* str2, uint32_t ct) { |
| 884 |
for (uint32_t uii = 0; uii != ct; ++uii) { |
| 885 |
if (kToUpper[ctou32(str1[uii])] != kToUpper[ctou32(str2[uii])]) { |
| 886 |
return 0; |
| 887 |
} |
| 888 |
} |
| 889 |
return 1; |
| 890 |
} |
| 891 |
|
| 892 |
/* |
| 893 |
void str_toupper(char* str_iter) { |
| 894 |
while (1) { |
| 895 |
const uint32_t uii = (unsigned char)(*str_iter); |
| 896 |
if (!uii) { |
| 897 |
return; |
| 898 |
} |
| 899 |
if (((uint32_t)(uii - 97)) < 26) { |
| 900 |
// 'a' has ASCII code 97 |
| 901 |
*str_iter = uii - 32; |
| 902 |
} |
| 903 |
++str_iter; |
| 904 |
} |
| 905 |
} |
| 906 |
|
| 907 |
void buf_toupper(uint32_t slen, char* strbuf) { |
| 908 |
for (uint32_t pos = 0; pos != slen; ++pos) { |
| 909 |
const uint32_t uii = (unsigned char)(strbuf[pos]); |
| 910 |
if (((uint32_t)(uii - 97)) < 26) { |
| 911 |
strbuf[pos] = uii - 32; |
| 912 |
} |
| 913 |
} |
| 914 |
} |
| 915 |
|
| 916 |
void strcpy_toupper(char* target, const char* source) { |
| 917 |
while (1) { |
| 918 |
uint32_t uii = (unsigned char)(*source++); |
| 919 |
if (!uii) { |
| 920 |
return; |
| 921 |
} |
| 922 |
if (((uint32_t)(uii - 97)) < 26) { |
| 923 |
uii -= 32; |
| 924 |
} |
| 925 |
*target++ = uii; |
| 926 |
} |
| 927 |
} |
| 928 |
|
| 929 |
char* memcpya_toupper(char* __restrict target, const char* __restrict source, uint32_t slen) { |
| 930 |
for (uint32_t pos = 0; pos != slen; ++pos) { |
| 931 |
uint32_t uii = ctou32(source[pos]); |
| 932 |
if ((uii - 97) < 26) { |
| 933 |
uii -= 32; |
| 934 |
} |
| 935 |
target[pos] = uii; |
| 936 |
} |
| 937 |
return &(target[slen]); |
| 938 |
} |
| 939 |
*/ |
| 940 |
|
| 941 |
|
| 942 |
uint32_t IsAlphanumeric(const char* str_iter) { |
| 943 |
for (; ; ++str_iter) { |
| 944 |
uint32_t uii = ctou32(*str_iter); |
| 945 |
if (!uii) { |
| 946 |
return 1; |
| 947 |
} |
| 948 |
if (((uii - 48) > 9) && (((uii & 0xffffffdfU) - 65) > 25)) { |
| 949 |
return 0; |
| 950 |
} |
| 951 |
} |
| 952 |
} |
| 953 |
|
| 954 |
BoolErr ScanPosintptr(const char* str_iter, uintptr_t* valp) { |
| 955 |
// Reads an integer in [1, 2^kBitsPerWord - 1]. Assumes first character is |
| 956 |
// nonspace. |
| 957 |
assert(ctow(str_iter[0]) > 32); |
| 958 |
uintptr_t val = ctow(*str_iter++) - 48; |
| 959 |
if (val >= 10) { |
| 960 |
#ifdef __LP64__ |
| 961 |
if (unlikely(val != 0xfffffffffffffffbLLU)) { |
| 962 |
return 1; |
| 963 |
} |
| 964 |
#else |
| 965 |
if (unlikely(val != 0xfffffffbU)) { |
| 966 |
return 1; |
| 967 |
} |
| 968 |
#endif |
| 969 |
val = ctow(*str_iter++) - 48; |
| 970 |
if (unlikely(val >= 10)) { |
| 971 |
return 1; |
| 972 |
} |
| 973 |
} |
| 974 |
while (!val) { |
| 975 |
val = ctow(*str_iter++) - 48; |
| 976 |
if (unlikely(val >= 10)) { |
| 977 |
return 1; |
| 978 |
} |
| 979 |
} |
| 980 |
#ifdef __LP64__ |
| 981 |
// limit is 20 digits, we've already read one |
| 982 |
const char* str_limit = &(str_iter[20]); |
| 983 |
#else |
| 984 |
const char* str_limit = &(str_iter[10]); |
| 985 |
#endif |
| 986 |
while (1) { |
| 987 |
const uintptr_t cur_digit = ctow(*str_iter++) - 48; |
| 988 |
if (cur_digit >= 10) { |
| 989 |
*valp = val; |
| 990 |
return 0; |
| 991 |
} |
| 992 |
const uintptr_t cur_digit2 = ctow(*str_iter++) - 48; |
| 993 |
if (str_iter == str_limit) { |
| 994 |
if (unlikely((cur_digit2 < 10) || ((val >= (~k0LU) / 10) && ((val > (~k0LU) / 10) || (cur_digit > (~k0LU) % 10))))) { |
| 995 |
return 1; |
| 996 |
} |
| 997 |
*valp = val * 10 + cur_digit; |
| 998 |
return 0; |
| 999 |
} |
| 1000 |
if (cur_digit2 >= 10) { |
| 1001 |
*valp = val * 10 + cur_digit; |
| 1002 |
return 0; |
| 1003 |
} |
| 1004 |
val = val * 100 + cur_digit * 10 + cur_digit2; |
| 1005 |
} |
| 1006 |
} |
| 1007 |
|
| 1008 |
// Requires (cap+1) * 100 < 2^64. |
| 1009 |
static inline BoolErr ScanmovU64CappedFinish(uint64_t cap, const char** str_iterp, uint64_t* valp) { |
| 1010 |
const char* str_iter = *str_iterp; |
| 1011 |
uint64_t val = *valp; |
| 1012 |
while (1) { |
| 1013 |
// a little bit of unrolling seems to help |
| 1014 |
const uint64_t cur_digit = ctou64(*str_iter++) - 48; |
| 1015 |
if (cur_digit >= 10) { |
| 1016 |
break; |
| 1017 |
} |
| 1018 |
// val = val * 10 + cur_digit; |
| 1019 |
const uint64_t cur_digit2 = ctou64(*str_iter++) - 48; |
| 1020 |
if (cur_digit2 >= 10) { |
| 1021 |
val = val * 10 + cur_digit; |
| 1022 |
if (unlikely(val > cap)) { |
| 1023 |
return 1; |
| 1024 |
} |
| 1025 |
break; |
| 1026 |
} |
| 1027 |
val = val * 100 + cur_digit * 10 + cur_digit2; |
| 1028 |
if (unlikely(val > cap)) { |
| 1029 |
return 1; |
| 1030 |
} |
| 1031 |
} |
| 1032 |
*valp = val; |
| 1033 |
*str_iterp = &(str_iter[-1]); |
| 1034 |
return 0; |
| 1035 |
} |
| 1036 |
|
| 1037 |
BoolErr ScanmovU64Capped(uint64_t cap, const char** str_iterp, uint64_t* valp) { |
| 1038 |
const char* str_iter = *str_iterp; |
| 1039 |
*valp = ctou32(*str_iter++) - 48; |
| 1040 |
if (*valp >= 10) { |
| 1041 |
if (unlikely(*valp != 0xfffffffbU)) { |
| 1042 |
return 1; |
| 1043 |
} |
| 1044 |
*valp = ctou32(*str_iter++) - 48; |
| 1045 |
if (unlikely(*valp >= 10)) { |
| 1046 |
return 1; |
| 1047 |
} |
| 1048 |
} |
| 1049 |
while (!(*valp)) { |
| 1050 |
*valp = ctou32(*str_iter++) - 48; |
| 1051 |
if (unlikely((*valp) >= 10)) { |
| 1052 |
return 1; |
| 1053 |
} |
| 1054 |
} |
| 1055 |
*str_iterp = str_iter; |
| 1056 |
return ScanmovU64CappedFinish(cap, str_iterp, valp); |
| 1057 |
} |
| 1058 |
|
| 1059 |
#ifdef __LP64__ |
| 1060 |
static inline BoolErr ScanmovUintCappedFinish(uint64_t cap, const char** str_iterp, uint32_t* valp) { |
| 1061 |
const char* str_iter = *str_iterp; |
| 1062 |
uint64_t val = *valp; |
| 1063 |
while (1) { |
| 1064 |
// a little bit of unrolling seems to help |
| 1065 |
const uint64_t cur_digit = ctou64(*str_iter++) - 48; |
| 1066 |
if (cur_digit >= 10) { |
| 1067 |
break; |
| 1068 |
} |
| 1069 |
// val = val * 10 + cur_digit; |
| 1070 |
const uint64_t cur_digit2 = ctou64(*str_iter++) - 48; |
| 1071 |
if (cur_digit2 >= 10) { |
| 1072 |
val = val * 10 + cur_digit; |
| 1073 |
if (unlikely(val > cap)) { |
| 1074 |
return 1; |
| 1075 |
} |
| 1076 |
break; |
| 1077 |
} |
| 1078 |
val = val * 100 + cur_digit * 10 + cur_digit2; |
| 1079 |
if (unlikely(val > cap)) { |
| 1080 |
return 1; |
| 1081 |
} |
| 1082 |
} |
| 1083 |
*valp = val; |
| 1084 |
*str_iterp = &(str_iter[-1]); |
| 1085 |
return 0; |
| 1086 |
} |
| 1087 |
|
| 1088 |
BoolErr ScanmovPosintCapped(uint64_t cap, const char** str_iterp, uint32_t* valp) { |
| 1089 |
const char* str_iter = *str_iterp; |
| 1090 |
*valp = ctou32(*str_iter++) - 48; |
| 1091 |
if (*valp >= 10) { |
| 1092 |
if (unlikely(*valp != 0xfffffffbU)) { |
| 1093 |
return 1; |
| 1094 |
} |
| 1095 |
*valp = ctou32(*str_iter++) - 48; |
| 1096 |
if (unlikely(*valp >= 10)) { |
| 1097 |
return 1; |
| 1098 |
} |
| 1099 |
} |
| 1100 |
while (!(*valp)) { |
| 1101 |
*valp = ctou32(*str_iter++) - 48; |
| 1102 |
if (unlikely((*valp) >= 10)) { |
| 1103 |
return 1; |
| 1104 |
} |
| 1105 |
} |
| 1106 |
*str_iterp = str_iter; |
| 1107 |
return ScanmovUintCappedFinish(cap, str_iterp, valp); |
| 1108 |
} |
| 1109 |
|
| 1110 |
BoolErr ScanmovUintCapped(uint64_t cap, const char** str_iterp, uint32_t* valp) { |
| 1111 |
const char* str_iter = *str_iterp; |
| 1112 |
*valp = ctou32(*str_iter++) - 48; |
| 1113 |
if (*valp >= 10) { |
| 1114 |
if (*valp != 0xfffffffbU) { |
| 1115 |
// '-' has ascii code 45, so unsigned 45 - 48 = 0xfffffffdU |
| 1116 |
if (unlikely((*valp != 0xfffffffdU) || (*str_iter != '0'))) { |
| 1117 |
return 1; |
| 1118 |
} |
| 1119 |
// accept "-0", "-00", etc. |
| 1120 |
while (*(++str_iter) == '0'); |
| 1121 |
*valp = 0; |
| 1122 |
*str_iterp = str_iter; |
| 1123 |
return (ctou32(*str_iter) - 48) < 10; |
| 1124 |
} |
| 1125 |
// accept leading '+' |
| 1126 |
*valp = ctou32(*str_iter++) - 48; |
| 1127 |
if (unlikely(*valp >= 10)) { |
| 1128 |
return 1; |
| 1129 |
} |
| 1130 |
} |
| 1131 |
*str_iterp = str_iter; |
| 1132 |
return ScanmovUintCappedFinish(cap, str_iterp, valp); |
| 1133 |
} |
| 1134 |
|
| 1135 |
// 2^{-31} < floor <= 0 <= cap < 2^31 |
| 1136 |
BoolErr ScanmovIntBounded(uint64_t abs_floor, uint64_t cap, const char** str_iterp, int32_t* valp) { |
| 1137 |
const char* str_iter = *str_iterp; |
| 1138 |
*valp = ctou32(*str_iter++) - 48; |
| 1139 |
int32_t sign = 1; |
| 1140 |
if (ctou32(*valp) >= 10) { |
| 1141 |
if (*valp == -3) { |
| 1142 |
sign = -1; |
| 1143 |
cap = abs_floor; |
| 1144 |
} else if (unlikely(*valp != -5)) { // accept leading '+' |
| 1145 |
return 1; |
| 1146 |
} |
| 1147 |
*valp = ctou32(*str_iter++) - 48; |
| 1148 |
if (unlikely(*valp >= 10)) { |
| 1149 |
return 1; |
| 1150 |
} |
| 1151 |
} |
| 1152 |
*str_iterp = str_iter; |
| 1153 |
if (ScanmovUintCappedFinish(cap, str_iterp, I32ToU32(valp))) { |
| 1154 |
return 1; |
| 1155 |
} |
| 1156 |
*valp *= sign; |
| 1157 |
return 0; |
| 1158 |
} |
| 1159 |
#else |
| 1160 |
BoolErr ScanmovPosintCapped32(uint32_t cap_div_10, uint32_t cap_mod_10, const char** str_iterp, uint32_t* valp) { |
| 1161 |
const char* str_iter = *str_iterp; |
| 1162 |
uint32_t val = ctou32(*str_iter++) - 48; |
| 1163 |
if (val >= 10) { |
| 1164 |
if (unlikely(val != 0xfffffffbU)) { |
| 1165 |
return 1; |
| 1166 |
} |
| 1167 |
val = ctou32(*str_iter++) - 48; |
| 1168 |
if (unlikely(val >= 10)) { |
| 1169 |
return 1; |
| 1170 |
} |
| 1171 |
} |
| 1172 |
while (!val) { |
| 1173 |
val = ctou32(*str_iter++); |
| 1174 |
if (unlikely(val >= 10)) { |
| 1175 |
return 1; |
| 1176 |
} |
| 1177 |
} |
| 1178 |
for (; ; ++str_iter) { |
| 1179 |
const uint32_t cur_digit = ctou32(*str_iter) - 48; |
| 1180 |
if (cur_digit >= 10) { |
| 1181 |
*valp = val; |
| 1182 |
*str_iterp = str_iter; |
| 1183 |
return 0; |
| 1184 |
} |
| 1185 |
if (unlikely((val >= cap_div_10) && ((val > cap_div_10) || (cur_digit > cap_mod_10)))) { |
| 1186 |
return 1; |
| 1187 |
} |
| 1188 |
val = val * 10 + cur_digit; |
| 1189 |
} |
| 1190 |
} |
| 1191 |
|
| 1192 |
BoolErr ScanmovUintCapped32(uint32_t cap_div_10, uint32_t cap_mod_10, const char** str_iterp, uint32_t* valp) { |
| 1193 |
const char* str_iter = *str_iterp; |
| 1194 |
uint32_t val = ctou32(*str_iter++) - 48; |
| 1195 |
if (val >= 10) { |
| 1196 |
if (val != 0xfffffffbU) { |
| 1197 |
if (unlikely((val != 0xfffffffd) || (*str_iter != '0'))) { |
| 1198 |
return 1; |
| 1199 |
} |
| 1200 |
while (*(++str_iter) == '0'); |
| 1201 |
*valp = 0; |
| 1202 |
*str_iterp = str_iter; |
| 1203 |
return (ctou32(*str_iter) - 48) < 10; |
| 1204 |
} |
| 1205 |
val = ctou32(*str_iter++) - 48; |
| 1206 |
if (unlikely(val >= 10)) { |
| 1207 |
return 1; |
| 1208 |
} |
| 1209 |
} |
| 1210 |
for (; ; ++str_iter) { |
| 1211 |
const uint32_t cur_digit = ctou32(*str_iter) - 48; |
| 1212 |
if (cur_digit >= 10) { |
| 1213 |
*valp = val; |
| 1214 |
*str_iterp = str_iter; |
| 1215 |
return 0; |
| 1216 |
} |
| 1217 |
if (unlikely((val >= cap_div_10) && ((val > cap_div_10) || (cur_digit > cap_mod_10)))) { |
| 1218 |
return 1; |
| 1219 |
} |
| 1220 |
val = val * 10 + cur_digit; |
| 1221 |
} |
| 1222 |
} |
| 1223 |
|
| 1224 |
BoolErr ScanmovIntBounded32(uint32_t abs_floor_div_10, uint32_t abs_floor_mod_10, uint32_t cap_div_10, uint32_t cap_mod_10, const char** str_iterp, int32_t* valp) { |
| 1225 |
const char* str_iter = *str_iterp; |
| 1226 |
uint32_t val = ctou32(*str_iter++) - 48; |
| 1227 |
int32_t sign = 1; |
| 1228 |
if (val >= 10) { |
| 1229 |
if (val == 0xfffffffdU) { |
| 1230 |
sign = -1; |
| 1231 |
cap_div_10 = abs_floor_div_10; |
| 1232 |
cap_mod_10 = abs_floor_mod_10; |
| 1233 |
} else if (unlikely(val != 0xfffffffbU)) { |
| 1234 |
return 1; |
| 1235 |
} |
| 1236 |
val = ctou32(*str_iter++) - 48; |
| 1237 |
if (unlikely(val >= 10)) { |
| 1238 |
return 1; |
| 1239 |
} |
| 1240 |
} |
| 1241 |
for (; ; ++str_iter) { |
| 1242 |
const uint32_t cur_digit = ctou32(*str_iter) - 48; |
| 1243 |
if (cur_digit >= 10) { |
| 1244 |
*valp = sign * S_CAST(int32_t, val); |
| 1245 |
*str_iterp = str_iter; |
| 1246 |
return 0; |
| 1247 |
} |
| 1248 |
if (unlikely((val >= cap_div_10) && ((val > cap_div_10) || (cur_digit > cap_mod_10)))) { |
| 1249 |
return 1; |
| 1250 |
} |
| 1251 |
val = val * 10 + cur_digit; |
| 1252 |
} |
| 1253 |
} |
| 1254 |
#endif |
| 1255 |
|
| 1256 |
static const double kPositivePow10[] = {1, 1.0e1, 1.0e2, 1.0e3, 1.0e4, 1.0e5, 1.0e6, 1.0e7, 1.0e8, 1.0e9, 1.0e10, 1.0e11, 1.0e12, 1.0e13, 1.0e14, 1.0e15}; |
| 1257 |
static const double kPositivePowTen16[] = {1, 1.0e16, 1.0e32, 1.0e48, 1.0e64, 1.0e80, 1.0e96, 1.0e112, 1.0e128, 1.0e144, 1.0e160, 1.0e176, 1.0e192, 1.0e208, 1.0e224, 1.0e240}; |
| 1258 |
static const double kNegativePow10[] = {1, 1.0e-1, 1.0e-2, 1.0e-3, 1.0e-4, 1.0e-5, 1.0e-6, 1.0e-7, 1.0e-8, 1.0e-9, 1.0e-10, 1.0e-11, 1.0e-12, 1.0e-13, 1.0e-14, 1.0e-15}; |
| 1259 |
static const double kNegativePowTen16[] = {1, 1.0e-16, 1.0e-32, 1.0e-48, 1.0e-64, 1.0e-80, 1.0e-96, 1.0e-112}; |
| 1260 |
|
| 1261 |
CXXCONST_CP ScanadvDouble(const char* str_iter, double* valp) { |
| 1262 |
// requires first character to be nonspace (to succeed; it fails without |
| 1263 |
// segfaulting on space/eoln/null) |
| 1264 |
// don't care about hexadecimal |
| 1265 |
// ok to lose last ~2 bits of precision |
| 1266 |
// ok if behavior undefined on >1GB strings in 32-bit case, >2GB for 64-bit |
| 1267 |
// fail on nan/infinity/overflow instead of usual strtod behavior |
| 1268 |
uint32_t cur_char_code = ctou32(*str_iter); |
| 1269 |
const uint32_t is_negative = (cur_char_code == 45); |
| 1270 |
if (is_negative || (cur_char_code == 43)) { |
| 1271 |
cur_char_code = ctou32(*(++str_iter)); |
| 1272 |
} |
| 1273 |
uint32_t cur_digit = cur_char_code - 48; |
| 1274 |
intptr_t e10 = 0; |
| 1275 |
const char* dot_ptr; |
| 1276 |
int64_t digits; |
| 1277 |
#ifdef __LP64__ |
| 1278 |
if (cur_digit < 10) { |
| 1279 |
// ok, we have at least one digit |
| 1280 |
digits = cur_digit; |
| 1281 |
// to check: best to skip leading zeroes and compare against 17 instead of |
| 1282 |
// 10^16? |
| 1283 |
do { |
| 1284 |
cur_digit = ctou32(*(++str_iter)) - 48; |
| 1285 |
if (cur_digit >= 10) { |
| 1286 |
if (cur_digit == 0xfffffffeU) { |
| 1287 |
dot_ptr = str_iter; |
| 1288 |
goto ScanadvDouble_parse_decimal; |
| 1289 |
} |
| 1290 |
goto ScanadvDouble_parse_exponent; |
| 1291 |
} |
| 1292 |
digits = digits * 10 + cur_digit; |
| 1293 |
} while (digits < 10000000000000000LL); |
| 1294 |
// we have 17 significant digits; count the rest, but don't worry about |
| 1295 |
// contents |
| 1296 |
// (could keep ~19 instead, but if we're systematically losing the last two |
| 1297 |
// bits of precision anyway...) |
| 1298 |
const char* last_sig_fig_ptr = str_iter; |
| 1299 |
do { |
| 1300 |
cur_digit = ctou32(*(++str_iter)) - 48; |
| 1301 |
} while (cur_digit < 10); |
| 1302 |
e10 = S_CAST(intptr_t, str_iter - last_sig_fig_ptr) - 1; |
| 1303 |
if (cur_digit == 0xfffffffeU) { |
| 1304 |
do { |
| 1305 |
cur_digit = ctou32(*(++str_iter)) - 48; |
| 1306 |
} while (cur_digit < 10); |
| 1307 |
} |
| 1308 |
goto ScanadvDouble_parse_exponent; |
| 1309 |
} |
| 1310 |
if (cur_digit != 0xfffffffeU) { |
| 1311 |
return nullptr; |
| 1312 |
} |
| 1313 |
// first (nonsign) character is dot, verify we have a digit after it |
| 1314 |
dot_ptr = str_iter; |
| 1315 |
cur_digit = ctou32(*(++str_iter)) - 48; |
| 1316 |
if (cur_digit >= 10) { |
| 1317 |
return nullptr; |
| 1318 |
} |
| 1319 |
digits = cur_digit; |
| 1320 |
ScanadvDouble_parse_decimal: |
| 1321 |
while (1) { |
| 1322 |
cur_digit = ctou32(*(++str_iter)) - 48; |
| 1323 |
if (cur_digit >= 10) { |
| 1324 |
e10 = 1 - S_CAST(intptr_t, str_iter - dot_ptr); |
| 1325 |
break; |
| 1326 |
} |
| 1327 |
digits = digits * 10 + cur_digit; |
| 1328 |
if (digits >= 10000000000000000LL) { |
| 1329 |
e10 = -S_CAST(intptr_t, str_iter - dot_ptr); |
| 1330 |
do { |
| 1331 |
cur_digit = ctou32(*(++str_iter)) - 48; |
| 1332 |
} while (cur_digit < 10); |
| 1333 |
break; |
| 1334 |
} |
| 1335 |
} |
| 1336 |
ScanadvDouble_parse_exponent: |
| 1337 |
if ((cur_digit & 0xdf) == 21) { // 'E' - '0' is 21 |
| 1338 |
cur_char_code = ctou32(*(++str_iter)); |
| 1339 |
const uint32_t exp_is_negative = (cur_char_code == 45); |
| 1340 |
if (exp_is_negative || (cur_char_code == 43)) { |
| 1341 |
cur_char_code = ctou32(*(++str_iter)); |
| 1342 |
} |
| 1343 |
cur_digit = cur_char_code - 48; |
| 1344 |
int32_t cur_exp = 0; |
| 1345 |
while (cur_digit < 10) { |
| 1346 |
if (cur_exp >= 214748364) { |
| 1347 |
// may as well guard against exponent overflow |
| 1348 |
if (!exp_is_negative) { |
| 1349 |
return nullptr; |
| 1350 |
} |
| 1351 |
*valp = 0; |
| 1352 |
do { |
| 1353 |
cur_digit = ctou32(*(++str_iter)) - 48; |
| 1354 |
} while (cur_digit < 10); |
| 1355 |
return S_CAST(CXXCONST_CP, str_iter); |
| 1356 |
} |
| 1357 |
cur_exp = cur_exp * 10 + cur_digit; |
| 1358 |
cur_digit = ctou32(*(++str_iter)) - 48; |
| 1359 |
} |
| 1360 |
if (exp_is_negative) { |
| 1361 |
cur_exp = -cur_exp; |
| 1362 |
} |
| 1363 |
e10 += cur_exp; |
| 1364 |
} |
| 1365 |
#else // not __LP64__ |
| 1366 |
int32_t digits_short; |
| 1367 |
if (cur_digit < 10) { |
| 1368 |
// ok, we have at least one digit |
| 1369 |
digits_short = cur_digit; |
| 1370 |
// to check: best to skip leading zeroes and compare against 17 instead of |
| 1371 |
// 10^16? |
| 1372 |
do { |
| 1373 |
cur_digit = ctou32(*(++str_iter)) - 48; |
| 1374 |
if (cur_digit >= 10) { |
| 1375 |
if (cur_digit == 0xfffffffeU) { |
| 1376 |
dot_ptr = str_iter; |
| 1377 |
goto ScanadvDouble_parse_decimal; |
| 1378 |
} |
| 1379 |
digits = digits_short; |
| 1380 |
goto ScanadvDouble_parse_exponent; |
| 1381 |
} |
| 1382 |
digits_short = digits_short * 10 + cur_digit; |
| 1383 |
} while (digits_short < 100000000); |
| 1384 |
digits = digits_short; |
| 1385 |
do { |
| 1386 |
cur_digit = ctou32(*(++str_iter)) - 48; |
| 1387 |
if (cur_digit >= 10) { |
| 1388 |
if (cur_digit == 0xfffffffeU) { |
| 1389 |
dot_ptr = str_iter; |
| 1390 |
goto ScanadvDouble_parse_decimal_long; |
| 1391 |
} |
| 1392 |
goto ScanadvDouble_parse_exponent; |
| 1393 |
} |
| 1394 |
digits = digits * 10 + cur_digit; |
| 1395 |
} while (digits < 10000000000000000LL); |
| 1396 |
// we have 17 significant digits; count the rest, but don't worry about |
| 1397 |
// contents |
| 1398 |
const char* last_sig_fig_ptr = str_iter; |
| 1399 |
do { |
| 1400 |
cur_digit = ctou32(*(++str_iter)) - 48; |
| 1401 |
} while (cur_digit < 10); |
| 1402 |
e10 = S_CAST(intptr_t, str_iter - last_sig_fig_ptr) - 1; |
| 1403 |
if (cur_digit == 0xfffffffeU) { |
| 1404 |
do { |
| 1405 |
cur_digit = ctou32(*(++str_iter)) - 48; |
| 1406 |
} while (cur_digit < 10); |
| 1407 |
} |
| 1408 |
goto ScanadvDouble_parse_exponent; |
| 1409 |
} |
| 1410 |
if (cur_digit != 0xfffffffeU) { |
| 1411 |
return nullptr; |
| 1412 |
} |
| 1413 |
// first (nonsign) character is dot, verify we have a digit after it |
| 1414 |
dot_ptr = str_iter; |
| 1415 |
cur_digit = ctou32(*(++str_iter)) - 48; |
| 1416 |
if (cur_digit >= 10) { |
| 1417 |
return nullptr; |
| 1418 |
} |
| 1419 |
digits_short = cur_digit; |
| 1420 |
ScanadvDouble_parse_decimal: |
| 1421 |
while (1) { |
| 1422 |
cur_digit = ctou32(*(++str_iter)) - 48; |
| 1423 |
if (cur_digit >= 10) { |
| 1424 |
e10 = 1 - S_CAST(intptr_t, str_iter - dot_ptr); |
| 1425 |
digits = digits_short; |
| 1426 |
break; |
| 1427 |
} |
| 1428 |
digits_short = digits_short * 10 + cur_digit; |
| 1429 |
if (digits_short >= 100000000) { |
| 1430 |
digits = digits_short; |
| 1431 |
ScanadvDouble_parse_decimal_long: |
| 1432 |
while (1) { |
| 1433 |
cur_digit = ctou32(*(++str_iter)) - 48; |
| 1434 |
if (cur_digit >= 10) { |
| 1435 |
e10 = 1 - S_CAST(intptr_t, str_iter - dot_ptr); |
| 1436 |
goto ScanadvDouble_parse_exponent; |
| 1437 |
} |
| 1438 |
digits = digits * 10 + cur_digit; |
| 1439 |
if (digits >= 10000000000000000LL) { |
| 1440 |
e10 = -S_CAST(intptr_t, str_iter - dot_ptr); |
| 1441 |
do { |
| 1442 |
cur_digit = ctou32(*(++str_iter)) - 48; |
| 1443 |
} while (cur_digit < 10); |
| 1444 |
goto ScanadvDouble_parse_exponent; |
| 1445 |
} |
| 1446 |
} |
| 1447 |
} |
| 1448 |
} |
| 1449 |
ScanadvDouble_parse_exponent: |
| 1450 |
if ((cur_digit & 0xdf) == 21) { // 'E' - '0' is 21 |
| 1451 |
cur_char_code = ctou32(*(++str_iter)); |
| 1452 |
const uint32_t exp_is_negative = (cur_char_code == 45); |
| 1453 |
if (exp_is_negative || (cur_char_code == 43)) { |
| 1454 |
cur_char_code = ctou32(*(++str_iter)); |
| 1455 |
} |
| 1456 |
cur_digit = cur_char_code - 48; |
| 1457 |
int32_t cur_exp = 0; |
| 1458 |
while (cur_digit < 10) { |
| 1459 |
if (cur_exp >= 107374182) { |
| 1460 |
// may as well guard against exponent overflow |
| 1461 |
// 2^30 instead of 2^31 limit since this gets added to e10 |
| 1462 |
if (!exp_is_negative) { |
| 1463 |
return nullptr; |
| 1464 |
} |
| 1465 |
*valp = 0; |
| 1466 |
do { |
| 1467 |
cur_digit = ctou32(*(++str_iter)) - 48; |
| 1468 |
} while (cur_digit < 10); |
| 1469 |
return S_CAST(CXXCONST_CP, str_iter); |
| 1470 |
} |
| 1471 |
cur_exp = cur_exp * 10 + cur_digit; |
| 1472 |
cur_digit = ctou32(*(++str_iter)) - 48; |
| 1473 |
} |
| 1474 |
if (exp_is_negative) { |
| 1475 |
cur_exp = -cur_exp; |
| 1476 |
} |
| 1477 |
e10 += cur_exp; |
| 1478 |
} |
| 1479 |
#endif |
| 1480 |
if (digits == 0) { |
| 1481 |
*valp = 0; |
| 1482 |
return S_CAST(CXXCONST_CP, str_iter); |
| 1483 |
} |
| 1484 |
if (is_negative) { |
| 1485 |
digits = -digits; |
| 1486 |
} |
| 1487 |
double dxx = S_CAST(double, digits); |
| 1488 |
if (e10) { |
| 1489 |
if (e10 < 0) { |
| 1490 |
uint32_t pos_exp = -e10; |
| 1491 |
dxx *= kNegativePow10[pos_exp & 15]; |
| 1492 |
pos_exp /= 16; |
| 1493 |
if (pos_exp) { |
| 1494 |
dxx *= kNegativePowTen16[pos_exp & 7]; |
| 1495 |
if (pos_exp > 7) { |
| 1496 |
if (pos_exp > 23) { |
| 1497 |
dxx = 0; |
| 1498 |
} else if (pos_exp > 15) { |
| 1499 |
dxx *= 1.0e-256; |
| 1500 |
} else { |
| 1501 |
dxx *= 1.0e-128; |
| 1502 |
} |
| 1503 |
} |
| 1504 |
} |
| 1505 |
} else { |
| 1506 |
uint32_t pos_exp = e10; |
| 1507 |
dxx *= kPositivePow10[pos_exp & 15]; |
| 1508 |
pos_exp /= 16; |
| 1509 |
if (pos_exp) { |
| 1510 |
dxx *= kPositivePowTen16[pos_exp & 15]; |
| 1511 |
if (pos_exp > 15) { |
| 1512 |
// overflow check |
| 1513 |
// last digits are "54" instead of "57" since that's the threshold |
| 1514 |
// beyond which multiply-by-1e256 overflows |
| 1515 |
if ((pos_exp > 31) || (dxx > 1.7976931348623154e52)) { |
| 1516 |
return nullptr; |
| 1517 |
} |
| 1518 |
dxx *= 1.0e256; |
| 1519 |
} |
| 1520 |
} |
| 1521 |
} |
| 1522 |
} |
| 1523 |
*valp = dxx; |
| 1524 |
return S_CAST(CXXCONST_CP, str_iter); |
| 1525 |
} |
| 1526 |
|
| 1527 |
CXXCONST_CP ScanadvLn(const char* str_iter, double* ln_ptr) { |
| 1528 |
// revised ScanadvDouble() which currently requires number to be nonnegative |
| 1529 |
// returns -DBL_MAX on 0 |
| 1530 |
uint32_t cur_char_code = ctou32(*str_iter); |
| 1531 |
const uint32_t is_negative = (cur_char_code == 45); |
| 1532 |
if (is_negative || (cur_char_code == 43)) { |
| 1533 |
cur_char_code = ctou32(*(++str_iter)); |
| 1534 |
} |
| 1535 |
uint32_t cur_digit = cur_char_code - 48; |
| 1536 |
intptr_t e10 = 0; |
| 1537 |
const char* dot_ptr; |
| 1538 |
int64_t digits; |
| 1539 |
#ifdef __LP64__ |
| 1540 |
if (cur_digit < 10) { |
| 1541 |
// ok, we have at least one digit |
| 1542 |
digits = cur_digit; |
| 1543 |
// to check: best to skip leading zeroes and compare against 17 instead of |
| 1544 |
// 10^16? |
| 1545 |
do { |
| 1546 |
cur_digit = ctou32(*(++str_iter)) - 48; |
| 1547 |
if (cur_digit >= 10) { |
| 1548 |
if (cur_digit == 0xfffffffeU) { |
| 1549 |
dot_ptr = str_iter; |
| 1550 |
goto ScanadvLn_parse_decimal; |
| 1551 |
} |
| 1552 |
goto ScanadvLn_parse_exponent; |
| 1553 |
} |
| 1554 |
digits = digits * 10 + cur_digit; |
| 1555 |
} while (digits < 10000000000000000LL); |
| 1556 |
// we have 17 significant digits; count the rest, but don't worry about |
| 1557 |
// contents |
| 1558 |
// (could keep ~19 instead, but if we're systematically losing the last two |
| 1559 |
// bits of precision anyway...) |
| 1560 |
const char* last_sig_fig_ptr = str_iter; |
| 1561 |
do { |
| 1562 |
cur_digit = ctou32(*(++str_iter)) - 48; |
| 1563 |
} while (cur_digit < 10); |
| 1564 |
e10 = S_CAST(intptr_t, str_iter - last_sig_fig_ptr) - 1; |
| 1565 |
if (cur_digit == 0xfffffffeU) { |
| 1566 |
do { |
| 1567 |
cur_digit = ctou32(*(++str_iter)) - 48; |
| 1568 |
} while (cur_digit < 10); |
| 1569 |
} |
| 1570 |
goto ScanadvLn_parse_exponent; |
| 1571 |
} |
| 1572 |
if (cur_digit != 0xfffffffeU) { |
| 1573 |
return nullptr; |
| 1574 |
} |
| 1575 |
// first (nonsign) character is dot, verify we have a digit after it |
| 1576 |
dot_ptr = str_iter; |
| 1577 |
cur_digit = ctou32(*(++str_iter)) - 48; |
| 1578 |
if (cur_digit >= 10) { |
| 1579 |
return nullptr; |
| 1580 |
} |
| 1581 |
digits = cur_digit; |
| 1582 |
ScanadvLn_parse_decimal: |
| 1583 |
while (1) { |
| 1584 |
cur_digit = ctou32(*(++str_iter)) - 48; |
| 1585 |
if (cur_digit >= 10) { |
| 1586 |
e10 = 1 - S_CAST(intptr_t, str_iter - dot_ptr); |
| 1587 |
break; |
| 1588 |
} |
| 1589 |
digits = digits * 10 + cur_digit; |
| 1590 |
if (digits >= 10000000000000000LL) { |
| 1591 |
e10 = -S_CAST(intptr_t, str_iter - dot_ptr); |
| 1592 |
do { |
| 1593 |
cur_digit = ctou32(*(++str_iter)) - 48; |
| 1594 |
} while (cur_digit < 10); |
| 1595 |
break; |
| 1596 |
} |
| 1597 |
} |
| 1598 |
ScanadvLn_parse_exponent: |
| 1599 |
if (is_negative && (digits != 0)) { |
| 1600 |
return nullptr; |
| 1601 |
} |
| 1602 |
if ((cur_digit & 0xdf) == 21) { // 'E' - '0' is 21 |
| 1603 |
cur_char_code = ctou32(*(++str_iter)); |
| 1604 |
const uint32_t exp_is_negative = (cur_char_code == 45); |
| 1605 |
if (exp_is_negative || (cur_char_code == 43)) { |
| 1606 |
cur_char_code = ctou32(*(++str_iter)); |
| 1607 |
} |
| 1608 |
cur_digit = cur_char_code - 48; |
| 1609 |
int32_t cur_exp = 0; |
| 1610 |
while (cur_digit < 10) { |
| 1611 |
if (cur_exp >= 214748364) { |
| 1612 |
// may as well guard against exponent overflow |
| 1613 |
if (!exp_is_negative) { |
| 1614 |
return nullptr; |
| 1615 |
} |
| 1616 |
*ln_ptr = -DBL_MAX; |
| 1617 |
do { |
| 1618 |
cur_digit = ctou32(*(++str_iter)) - 48; |
| 1619 |
} while (cur_digit < 10); |
| 1620 |
return S_CAST(CXXCONST_CP, str_iter); |
| 1621 |
} |
| 1622 |
cur_exp = cur_exp * 10 + cur_digit; |
| 1623 |
cur_digit = ctou32(*(++str_iter)) - 48; |
| 1624 |
} |
| 1625 |
if (exp_is_negative) { |
| 1626 |
cur_exp = -cur_exp; |
| 1627 |
} |
| 1628 |
e10 += cur_exp; |
| 1629 |
} |
| 1630 |
#else // not __LP64__ |
| 1631 |
int32_t digits_short; |
| 1632 |
if (cur_digit < 10) { |
| 1633 |
// ok, we have at least one digit |
| 1634 |
digits_short = cur_digit; |
| 1635 |
// to check: best to skip leading zeroes and compare against 17 instead of |
| 1636 |
// 10^16? |
| 1637 |
do { |
| 1638 |
cur_digit = ctou32(*(++str_iter)) - 48; |
| 1639 |
if (cur_digit >= 10) { |
| 1640 |
if (cur_digit == 0xfffffffeU) { |
| 1641 |
dot_ptr = str_iter; |
| 1642 |
goto ScanadvLn_parse_decimal; |
| 1643 |
} |
| 1644 |
digits = digits_short; |
| 1645 |
goto ScanadvLn_parse_exponent; |
| 1646 |
} |
| 1647 |
digits_short = digits_short * 10 + cur_digit; |
| 1648 |
} while (digits_short < 100000000); |
| 1649 |
digits = digits_short; |
| 1650 |
do { |
| 1651 |
cur_digit = ctou32(*(++str_iter)) - 48; |
| 1652 |
if (cur_digit >= 10) { |
| 1653 |
if (cur_digit == 0xfffffffeU) { |
| 1654 |
dot_ptr = str_iter; |
| 1655 |
goto ScanadvLn_parse_decimal_long; |
| 1656 |
} |
| 1657 |
goto ScanadvLn_parse_exponent; |
| 1658 |
} |
| 1659 |
digits = digits * 10 + cur_digit; |
| 1660 |
} while (digits < 10000000000000000LL); |
| 1661 |
// we have 17 significant digits; count the rest, but don't worry about |
| 1662 |
// contents |
| 1663 |
const char* last_sig_fig_ptr = str_iter; |
| 1664 |
do { |
| 1665 |
cur_digit = ctou32(*(++str_iter)) - 48; |
| 1666 |
} while (cur_digit < 10); |
| 1667 |
e10 = S_CAST(intptr_t, str_iter - last_sig_fig_ptr) - 1; |
| 1668 |
if (cur_digit == 0xfffffffeU) { |
| 1669 |
do { |
| 1670 |
cur_digit = ctou32(*(++str_iter)) - 48; |
| 1671 |
} while (cur_digit < 10); |
| 1672 |
} |
| 1673 |
goto ScanadvLn_parse_exponent; |
| 1674 |
} |
| 1675 |
if (cur_digit != 0xfffffffeU) { |
| 1676 |
return nullptr; |
| 1677 |
} |
| 1678 |
// first (nonsign) character is dot, verify we have a digit after it |
| 1679 |
dot_ptr = str_iter; |
| 1680 |
cur_digit = ctou32(*(++str_iter)) - 48; |
| 1681 |
if (cur_digit >= 10) { |
| 1682 |
return nullptr; |
| 1683 |
} |
| 1684 |
digits_short = cur_digit; |
| 1685 |
ScanadvLn_parse_decimal: |
| 1686 |
while (1) { |
| 1687 |
cur_digit = ctou32(*(++str_iter)) - 48; |
| 1688 |
if (cur_digit >= 10) { |
| 1689 |
e10 = 1 - S_CAST(intptr_t, str_iter - dot_ptr); |
| 1690 |
digits = digits_short; |
| 1691 |
break; |
| 1692 |
} |
| 1693 |
digits_short = digits_short * 10 + cur_digit; |
| 1694 |
if (digits_short >= 100000000) { |
| 1695 |
digits = digits_short; |
| 1696 |
ScanadvLn_parse_decimal_long: |
| 1697 |
while (1) { |
| 1698 |
cur_digit = ctou32(*(++str_iter)) - 48; |
| 1699 |
if (cur_digit >= 10) { |
| 1700 |
e10 = 1 - S_CAST(intptr_t, str_iter - dot_ptr); |
| 1701 |
goto ScanadvLn_parse_exponent; |
| 1702 |
} |
| 1703 |
digits = digits * 10 + cur_digit; |
| 1704 |
if (digits >= 10000000000000000LL) { |
| 1705 |
e10 = -S_CAST(intptr_t, str_iter - dot_ptr); |
| 1706 |
do { |
| 1707 |
cur_digit = ctou32(*(++str_iter)) - 48; |
| 1708 |
} while (cur_digit < 10); |
| 1709 |
goto ScanadvLn_parse_exponent; |
| 1710 |
} |
| 1711 |
} |
| 1712 |
} |
| 1713 |
} |
| 1714 |
ScanadvLn_parse_exponent: |
| 1715 |
if (is_negative && (digits != 0)) { |
| 1716 |
return nullptr; |
| 1717 |
} |
| 1718 |
if ((cur_digit & 0xdf) == 21) { // 'E' - '0' is 21 |
| 1719 |
cur_char_code = ctou32(*(++str_iter)); |
| 1720 |
const uint32_t exp_is_negative = (cur_char_code == 45); |
| 1721 |
if (exp_is_negative || (cur_char_code == 43)) { |
| 1722 |
cur_char_code = ctou32(*(++str_iter)); |
| 1723 |
} |
| 1724 |
cur_digit = cur_char_code - 48; |
| 1725 |
int32_t cur_exp = 0; |
| 1726 |
while (cur_digit < 10) { |
| 1727 |
if (cur_exp >= 107374182) { |
| 1728 |
// may as well guard against exponent overflow |
| 1729 |
if (!exp_is_negative) { |
| 1730 |
return nullptr; |
| 1731 |
} |
| 1732 |
*ln_ptr = -DBL_MAX; |
| 1733 |
do { |
| 1734 |
cur_digit = ctou32(*(++str_iter)) - 48; |
| 1735 |
} while (cur_digit < 10); |
| 1736 |
return S_CAST(CXXCONST_CP, str_iter); |
| 1737 |
} |
| 1738 |
cur_exp = cur_exp * 10 + cur_digit; |
| 1739 |
cur_digit = ctou32(*(++str_iter)) - 48; |
| 1740 |
} |
| 1741 |
if (exp_is_negative) { |
| 1742 |
cur_exp = -cur_exp; |
| 1743 |
} |
| 1744 |
e10 += cur_exp; |
| 1745 |
} |
| 1746 |
#endif |
| 1747 |
if (digits == 0) { |
| 1748 |
*ln_ptr = -DBL_MAX; |
| 1749 |
return S_CAST(CXXCONST_CP, str_iter); |
| 1750 |
} |
| 1751 |
double ln_val = log(S_CAST(double, digits)); |
| 1752 |
if (e10) { |
| 1753 |
ln_val += e10 * kLn10; |
| 1754 |
} |
| 1755 |
*ln_ptr = ln_val; |
| 1756 |
return S_CAST(CXXCONST_CP, str_iter); |
| 1757 |
} |
| 1758 |
|
| 1759 |
BoolErr ScanPosintCappedx(const char* str_iter, uint64_t cap, uint32_t* valp) { |
| 1760 |
double val; |
| 1761 |
if ((!ScantokDouble(str_iter, &val)) || (val < 1.0) || (val > S_CAST(double, cap))) { |
| 1762 |
return 1; |
| 1763 |
} |
| 1764 |
*valp = S_CAST(uint32_t, val); |
| 1765 |
return (val != S_CAST(double, *valp)); |
| 1766 |
} |
| 1767 |
|
| 1768 |
BoolErr ScanUintCappedx(const char* str_iter, uint64_t cap, uint32_t* valp) { |
| 1769 |
double val; |
| 1770 |
if ((!ScantokDouble(str_iter, &val)) || (val < 0.0) || (val > S_CAST(double, cap))) { |
| 1771 |
return 1; |
| 1772 |
} |
| 1773 |
*valp = S_CAST(uint32_t, val); |
| 1774 |
return (val != S_CAST(double, *valp)); |
| 1775 |
} |
| 1776 |
|
| 1777 |
BoolErr ScanIntAbsBoundedx(const char* str_iter, int64_t bound, int32_t* valp) { |
| 1778 |
const double bound_d = S_CAST(double, bound); |
| 1779 |
double val; |
| 1780 |
if ((!ScantokDouble(str_iter, &val)) || (val < -bound_d) || (val > bound_d)) { |
| 1781 |
return 1; |
| 1782 |
} |
| 1783 |
*valp = S_CAST(int32_t, val); |
| 1784 |
return (val != S_CAST(double, *valp)); |
| 1785 |
} |
| 1786 |
|
| 1787 |
BoolErr ScanPosintptrx(const char* str_iter, uintptr_t* valp) { |
| 1788 |
double val; |
| 1789 |
if ((!ScantokDouble(str_iter, &val)) || (val < 1.0) || (val > S_CAST(double, ~k0LU))) { |
| 1790 |
return 1; |
| 1791 |
} |
| 1792 |
*valp = S_CAST(uintptr_t, val); |
| 1793 |
return (val != S_CAST(double, *valp)); |
| 1794 |
} |
| 1795 |
|
| 1796 |
void GetTopTwoUi(const uint32_t* __restrict uint_arr, uintptr_t uia_size, uintptr_t* __restrict top_idx_ptr, uintptr_t* __restrict second_idx_ptr) { |
| 1797 |
assert(uia_size > 1); |
| 1798 |
uintptr_t top_idx = (uint_arr[1] > uint_arr[0])? 1 : 0; |
| 1799 |
uintptr_t second_idx = 1 ^ top_idx; |
| 1800 |
uint32_t top_val = uint_arr[top_idx]; |
| 1801 |
uint32_t second_val = uint_arr[second_idx]; |
| 1802 |
uintptr_t cur_idx; |
| 1803 |
uintptr_t cur_val; |
| 1804 |
for (cur_idx = 2; cur_idx != uia_size; ++cur_idx) { |
| 1805 |
cur_val = uint_arr[cur_idx]; |
| 1806 |
if (cur_val > second_val) { |
| 1807 |
if (cur_val > top_val) { |
| 1808 |
second_val = top_val; |
| 1809 |
second_idx = top_idx; |
| 1810 |
top_val = cur_val; |
| 1811 |
top_idx = cur_idx; |
| 1812 |
} else { |
| 1813 |
second_val = cur_val; |
| 1814 |
second_idx = cur_idx; |
| 1815 |
} |
| 1816 |
} |
| 1817 |
} |
| 1818 |
*top_idx_ptr = top_idx; |
| 1819 |
*second_idx_ptr = second_idx; |
| 1820 |
} |
| 1821 |
|
| 1822 |
#ifdef USE_AVX2 |
| 1823 |
CXXCONST_CP NextTokenMultFar(const char* str_iter, uint32_t ct) { |
| 1824 |
// assert(ct); |
| 1825 |
if (!str_iter) { |
| 1826 |
return nullptr; |
| 1827 |
} |
| 1828 |
uint32_t transition_bits_to_skip_p1 = ct * 2; |
| 1829 |
const uintptr_t starting_addr = R_CAST(uintptr_t, str_iter); |
| 1830 |
const VecUc* str_viter_start = R_CAST(const VecUc*, RoundDownPow2(starting_addr, kBytesPerVec)); |
| 1831 |
const VecUc vvec_all_tab = vecuc_set1(9); |
| 1832 |
const VecUc vvec_all_space = vecuc_set1(32); |
| 1833 |
VecUc cur_vvec = *str_viter_start; |
| 1834 |
VecUc tabspace_vvec = (cur_vvec == vvec_all_tab) | (cur_vvec == vvec_all_space); |
| 1835 |
// Underlying intrinsics are _mm256_cmpeq_... and _mm256_cmpgt_... |
| 1836 |
// So we don't really want to use <= or !=. |
| 1837 |
// (Er, there's also an signed vs. unsigned comparison impedence mismatch: |
| 1838 |
// there is no unsigned version of _mm256_cmpgt_epi8. So we don't really |
| 1839 |
// want to use gcc-vector-extension < at all here when we can use a mask to |
| 1840 |
// do the same job.) |
| 1841 |
const VecUc vvec_all96 = vecuc_set1(96); |
| 1842 |
VecUc post31_vvec = vecuc_adds(cur_vvec, vvec_all96); |
| 1843 |
uint32_t delimiter_bytes = vecuc_movemask(tabspace_vvec); |
| 1844 |
const uint32_t initial_nonterminating_bytes = vecuc_movemask(post31_vvec) | delimiter_bytes; |
| 1845 |
const uint32_t leading_byte_ct = starting_addr - R_CAST(uintptr_t, str_viter_start); |
| 1846 |
uint32_t leading_mask = UINT32_MAX << leading_byte_ct; |
| 1847 |
delimiter_bytes &= leading_mask; |
| 1848 |
uint32_t terminating_bytes = leading_mask & (~initial_nonterminating_bytes); |
| 1849 |
uint32_t prev_delim_highbit = 0; |
| 1850 |
for (const VecUc* str_viter = str_viter_start; ; ) { |
| 1851 |
// The number of 0->1 + 1->0 bit transitions in x is |
| 1852 |
// popcount(x ^ (x << 1)). |
| 1853 |
uint32_t cur_transitions = S_CAST(Vec8thUint, delimiter_bytes ^ (delimiter_bytes << 1) ^ prev_delim_highbit); |
| 1854 |
uint32_t cur_transition_ct = PopcountVec8thUint(cur_transitions); |
| 1855 |
if (cur_transition_ct >= transition_bits_to_skip_p1) { |
| 1856 |
cur_transitions = ClearBottomSetBits(transition_bits_to_skip_p1 - 1, cur_transitions); |
| 1857 |
uint32_t byte_offset_in_vec = ctzu32(cur_transitions); |
| 1858 |
if (terminating_bytes << (31 - byte_offset_in_vec)) { |
| 1859 |
return nullptr; |
| 1860 |
} |
| 1861 |
return &(DowncastToXC(str_viter)[byte_offset_in_vec]); |
| 1862 |
} |
| 1863 |
if (terminating_bytes) { |
| 1864 |
return nullptr; |
| 1865 |
} |
| 1866 |
transition_bits_to_skip_p1 -= cur_transition_ct; |
| 1867 |
prev_delim_highbit = transition_bits_to_skip_p1 & 1; |
| 1868 |
++str_viter; |
| 1869 |
cur_vvec = *str_viter; |
| 1870 |
tabspace_vvec = (cur_vvec == vvec_all_tab) | (cur_vvec == vvec_all_space); |
| 1871 |
post31_vvec = vecuc_adds(cur_vvec, vvec_all96); |
| 1872 |
delimiter_bytes = vecuc_movemask(tabspace_vvec); |
| 1873 |
terminating_bytes = ~(vecuc_movemask(post31_vvec) | delimiter_bytes); |
| 1874 |
} |
| 1875 |
} |
| 1876 |
|
| 1877 |
const char* TokenLexK0(const char* str_iter, const uint32_t* col_types, const uint32_t* col_skips, uint32_t relevant_col_ct, const char** token_ptrs, uint32_t* token_slens) { |
| 1878 |
const uint32_t relevant_col_ct_m1 = relevant_col_ct - 1; |
| 1879 |
uint32_t relevant_col_idx = 0; |
| 1880 |
uint32_t transition_bits_to_skip_p1 = col_skips[relevant_col_idx] * 2; |
| 1881 |
const uintptr_t starting_addr = R_CAST(uintptr_t, str_iter); |
| 1882 |
const VecUc* str_viter = R_CAST(const VecUc*, RoundDownPow2(starting_addr, kBytesPerVec)); |
| 1883 |
const VecUc vvec_all_tab = vecuc_set1(9); |
| 1884 |
const VecUc vvec_all_space = vecuc_set1(32); |
| 1885 |
VecUc cur_vvec = *str_viter; |
| 1886 |
VecUc tabspace_vvec = (cur_vvec == vvec_all_tab) | (cur_vvec == vvec_all_space); |
| 1887 |
const VecUc vvec_all96 = vecuc_set1(96); |
| 1888 |
VecUc post31_vvec = vecuc_adds(cur_vvec, vvec_all96); |
| 1889 |
uint32_t delimiter_bytes = vecuc_movemask(tabspace_vvec); |
| 1890 |
const uint32_t initial_nonterminating_bytes = vecuc_movemask(post31_vvec) | delimiter_bytes; |
| 1891 |
const uint32_t leading_byte_ct = starting_addr - R_CAST(uintptr_t, str_viter); |
| 1892 |
uint32_t leading_mask = UINT32_MAX << leading_byte_ct; |
| 1893 |
delimiter_bytes &= leading_mask; |
| 1894 |
uint32_t terminating_bytes = leading_mask & (~initial_nonterminating_bytes); |
| 1895 |
uint32_t prev_delim_highbit = 0; |
| 1896 |
if (!transition_bits_to_skip_p1) { |
| 1897 |
// Special case: col_skips[0] can be zero. When it is, str_iter must point |
| 1898 |
// to the beginning of a token. |
| 1899 |
prev_delim_highbit = -leading_mask; |
| 1900 |
transition_bits_to_skip_p1 = 1; |
| 1901 |
} |
| 1902 |
while (1) { |
| 1903 |
// The number of 0->1 + 1->0 bit transitions in x is |
| 1904 |
// popcount(x ^ (x << 1)). |
| 1905 |
uint32_t cur_transitions = S_CAST(Vec8thUint, delimiter_bytes ^ (delimiter_bytes << 1) ^ prev_delim_highbit); |
| 1906 |
uint32_t cur_transition_ct = PopcountVec8thUint(cur_transitions); |
| 1907 |
while (cur_transition_ct >= transition_bits_to_skip_p1) { |
| 1908 |
cur_transitions = ClearBottomSetBits(transition_bits_to_skip_p1 - 1, cur_transitions); |
| 1909 |
uint32_t byte_offset_in_vec = ctzu32(cur_transitions); |
| 1910 |
const char* token_start = &(DowncastKToC(str_viter)[byte_offset_in_vec]); |
| 1911 |
const uint32_t cur_col_type = col_types[relevant_col_idx]; |
| 1912 |
token_ptrs[cur_col_type] = token_start; |
| 1913 |
// Find end of token. |
| 1914 |
if (relevant_col_idx == relevant_col_ct_m1) { |
| 1915 |
if (terminating_bytes << (31 - byte_offset_in_vec)) { |
| 1916 |
return nullptr; |
| 1917 |
} |
| 1918 |
// Handle this separately, since it's ok for the last token to be |
| 1919 |
// terminated by something other than tab/space. |
| 1920 |
// cur_transitions is a misnomer here; we just need the bottom bit to |
| 1921 |
// tell us where the token end is. |
| 1922 |
|
| 1923 |
// 33..255 is a bit more annoying to check for efficiently than |
| 1924 |
// 32..255. |
| 1925 |
const VecUc vvec_all95 = vecuc_set1(95); |
| 1926 |
cur_transitions &= cur_transitions - 1; |
| 1927 |
cur_transitions |= terminating_bytes; |
| 1928 |
while (!cur_transitions) { |
| 1929 |
++str_viter; |
| 1930 |
cur_vvec = *str_viter; |
| 1931 |
const VecUc postspace_vvec = vecuc_adds(cur_vvec, vvec_all95); |
| 1932 |
cur_transitions = S_CAST(Vec8thUint, ~vecuc_movemask(postspace_vvec)); |
| 1933 |
} |
| 1934 |
byte_offset_in_vec = ctzu32(cur_transitions); |
| 1935 |
const char* token_end = &(DowncastKToC(str_viter)[byte_offset_in_vec]); |
| 1936 |
token_slens[cur_col_type] = token_end - token_start; |
| 1937 |
return token_end; |
| 1938 |
} |
| 1939 |
cur_transition_ct -= transition_bits_to_skip_p1; |
| 1940 |
if (!cur_transition_ct) { |
| 1941 |
// Token end is in a later vector. |
| 1942 |
do { |
| 1943 |
if (terminating_bytes) { |
| 1944 |
return nullptr; |
| 1945 |
} |
| 1946 |
++str_viter; |
| 1947 |
cur_vvec = *str_viter; |
| 1948 |
tabspace_vvec = (cur_vvec == vvec_all_tab) | (cur_vvec == vvec_all_space); |
| 1949 |
post31_vvec = vecuc_adds(cur_vvec, vvec_all96); |
| 1950 |
delimiter_bytes = vecuc_movemask(tabspace_vvec); |
| 1951 |
terminating_bytes = ~(vecuc_movemask(post31_vvec) | delimiter_bytes); |
| 1952 |
} while (!delimiter_bytes); |
| 1953 |
cur_transitions = S_CAST(Vec8thUint, delimiter_bytes ^ (delimiter_bytes << 1)); |
| 1954 |
cur_transition_ct = PopcountVec8thUint(cur_transitions); |
| 1955 |
} else { |
| 1956 |
cur_transitions &= cur_transitions - 1; |
| 1957 |
} |
| 1958 |
byte_offset_in_vec = ctzu32(cur_transitions); |
| 1959 |
const char* token_end = &(DowncastKToC(str_viter)[byte_offset_in_vec]); |
| 1960 |
token_slens[cur_col_type] = token_end - token_start; |
| 1961 |
++relevant_col_idx; |
| 1962 |
transition_bits_to_skip_p1 = 2 * col_skips[relevant_col_idx]; |
| 1963 |
} |
| 1964 |
if (terminating_bytes) { |
| 1965 |
return nullptr; |
| 1966 |
} |
| 1967 |
transition_bits_to_skip_p1 -= cur_transition_ct; |
| 1968 |
prev_delim_highbit = transition_bits_to_skip_p1 & 1; |
| 1969 |
++str_viter; |
| 1970 |
cur_vvec = *str_viter; |
| 1971 |
tabspace_vvec = (cur_vvec == vvec_all_tab) | (cur_vvec == vvec_all_space); |
| 1972 |
post31_vvec = vecuc_adds(cur_vvec, vvec_all96); |
| 1973 |
delimiter_bytes = vecuc_movemask(tabspace_vvec); |
| 1974 |
terminating_bytes = ~(vecuc_movemask(post31_vvec) | delimiter_bytes); |
| 1975 |
} |
| 1976 |
} |
| 1977 |
#endif // USE_AVX2 |
| 1978 |
|
| 1979 |
CXXCONST_CP NextCsvMult(const char* str_iter, uint32_t ct) { |
| 1980 |
if (!str_iter) { |
| 1981 |
return nullptr; |
| 1982 |
} |
| 1983 |
// assumes initial spaces in current token have been skipped |
| 1984 |
// ok if we're at the end of the token |
| 1985 |
unsigned char ucc = *str_iter; |
| 1986 |
assert(ucc != ' '); |
| 1987 |
while (ucc >= ' ') { |
| 1988 |
// avoid strchr to keep "ASCII code < 32 == newline" consistent |
| 1989 |
// (tab handling is quirky right now--permitted at the beginning of a |
| 1990 |
// token, but treated as newline later--but it should never appear so |
| 1991 |
// no point in e.g. adding an extra parameter to FirstNonTspace(); |
| 1992 |
// just need to make sure the quirky behavior is consistent.) |
| 1993 |
if (ucc != ',') { |
| 1994 |
ucc = *(++str_iter); |
| 1995 |
continue; |
| 1996 |
} |
| 1997 |
do { |
| 1998 |
ucc = *(++str_iter); |
| 1999 |
} while ((ucc == ' ') || (ucc == '\t')); |
| 2000 |
if (!(--ct)) { |
| 2001 |
return S_CAST(CXXCONST_CP, str_iter); |
| 2002 |
} |
| 2003 |
} |
| 2004 |
return nullptr; |
| 2005 |
} |
| 2006 |
|
| 2007 |
#ifdef USE_AVX2 |
| 2008 |
const char* CsvLexK(const char* str_iter, const uint32_t* col_types, const uint32_t* col_skips, uint32_t relevant_col_ct, const char** token_ptrs, uint32_t* token_slens) { |
| 2009 |
// Temporary; optimize later. |
| 2010 |
for (uint32_t relevant_col_idx = 0; relevant_col_idx != relevant_col_ct; ++relevant_col_idx) { |
| 2011 |
const uint32_t cur_col_type = col_types[relevant_col_idx]; |
| 2012 |
str_iter = NextCsvMult(str_iter, col_skips[relevant_col_idx]); |
| 2013 |
if (!str_iter) { |
| 2014 |
return nullptr; |
| 2015 |
} |
| 2016 |
token_ptrs[cur_col_type] = str_iter; |
| 2017 |
const char* token_end = CsvFieldEnd(str_iter); |
| 2018 |
token_slens[cur_col_type] = token_end - str_iter; |
| 2019 |
str_iter = token_end; |
| 2020 |
} |
| 2021 |
return str_iter; |
| 2022 |
} |
| 2023 |
#endif |
| 2024 |
|
| 2025 |
uint32_t CountTokens(const char* str_iter) { |
| 2026 |
uint32_t token_ct = 0; |
| 2027 |
str_iter = FirstNonTspace(str_iter); |
| 2028 |
while (!IsEolnKns(*str_iter)) { |
| 2029 |
++token_ct; |
| 2030 |
str_iter = FirstNonTspace(CurTokenEnd(str_iter)); |
| 2031 |
} |
| 2032 |
return token_ct; |
| 2033 |
} |
| 2034 |
|
| 2035 |
/* |
| 2036 |
uint32_t CommaOrSpaceCountTokens(const char* str_iter, uint32_t comma_delim) { |
| 2037 |
if (comma_delim) { |
| 2038 |
// assumes nonempty line (treats trailing empty string as a token). |
| 2039 |
uint32_t token_ct = 1; |
| 2040 |
unsigned char ucc = (unsigned char)(*str_iter++); |
| 2041 |
while (1) { |
| 2042 |
if (ucc < 32) { |
| 2043 |
return token_ct; |
| 2044 |
} |
| 2045 |
if (ucc == ',') { |
| 2046 |
// spelled out due to const qualifier |
| 2047 |
do { |
| 2048 |
ucc = (unsigned char)(*str_iter++); |
| 2049 |
} while ((ucc == ' ') || (ucc == '\t')); |
| 2050 |
token_ct++; |
| 2051 |
continue; |
| 2052 |
} |
| 2053 |
ucc = (unsigned char)(*str_iter++); |
| 2054 |
} |
| 2055 |
} |
| 2056 |
return CountTokens(str_iter); |
| 2057 |
} |
| 2058 |
*/ |
| 2059 |
|
| 2060 |
uint32_t CountAndMeasureMultistr(const char* multistr, uintptr_t* max_blen_ptr) { |
| 2061 |
// Straightforward to accelerate this with movemask/shrn4 if it ever matters. |
| 2062 |
// (Doesn't matter now since it's only used for command-line processing.) |
| 2063 |
uint32_t ct = 0; |
| 2064 |
uintptr_t max_blen = *max_blen_ptr; |
| 2065 |
while (*multistr) { |
| 2066 |
const uintptr_t blen = strlen(multistr) + 1; |
| 2067 |
if (blen > max_blen) { |
| 2068 |
max_blen = blen; |
| 2069 |
} |
| 2070 |
multistr = &(multistr[blen]); |
| 2071 |
++ct; |
| 2072 |
} while (*multistr); |
| 2073 |
*max_blen_ptr = max_blen; |
| 2074 |
return ct; |
| 2075 |
} |
| 2076 |
|
| 2077 |
// number-to-string encoders |
| 2078 |
// u32toa and i64toa moved to plink2_base |
| 2079 |
|
| 2080 |
char* i32toa(int32_t ii, char* start) { |
| 2081 |
uint32_t uii = ii; |
| 2082 |
if (ii < 0) { |
| 2083 |
// -INT_MIN is undefined, but negating the unsigned int equivalent works |
| 2084 |
*start++ = '-'; |
| 2085 |
uii = -uii; |
| 2086 |
} |
| 2087 |
return u32toa(uii, start); |
| 2088 |
} |
| 2089 |
|
| 2090 |
char* u32toa_z5(uint32_t uii, char* start) { |
| 2091 |
uint32_t quotient = uii / 10000; |
| 2092 |
*start++ = '0' + quotient; |
| 2093 |
return uitoa_z4(uii - 10000 * quotient, start); |
| 2094 |
} |
| 2095 |
|
| 2096 |
|
| 2097 |
char* u32toa_trunc4(uint32_t uii, char* start) { |
| 2098 |
uint32_t quotient = uii / 100; |
| 2099 |
memcpy_k(start, &(kDigitPair[quotient]), 2); |
| 2100 |
uii -= 100 * quotient; |
| 2101 |
if (uii) { |
| 2102 |
start += 2; |
| 2103 |
memcpy_k(start, &(kDigitPair[uii]), 2); |
| 2104 |
} |
| 2105 |
if (start[1] != '0') { |
| 2106 |
return &(start[2]); |
| 2107 |
} |
| 2108 |
return &(start[1]); |
| 2109 |
} |
| 2110 |
|
| 2111 |
static inline char* uitoa_trunc6(uint32_t uii, char* start) { |
| 2112 |
uint32_t quotient = uii / 10000; |
| 2113 |
memcpy_k(start, &(kDigitPair[quotient]), 2); |
| 2114 |
uii -= 10000 * quotient; |
| 2115 |
if (uii) { |
| 2116 |
quotient = uii / 100; |
| 2117 |
start += 2; |
| 2118 |
memcpy_k(start, &(kDigitPair[quotient]), 2); |
| 2119 |
uii -= 100 * quotient; |
| 2120 |
if (uii) { |
| 2121 |
start += 2; |
| 2122 |
memcpy_k(start, &(kDigitPair[uii]), 2); |
| 2123 |
} |
| 2124 |
} |
| 2125 |
if (start[1] != '0') { |
| 2126 |
return &(start[2]); |
| 2127 |
} |
| 2128 |
return &(start[1]); |
| 2129 |
} |
| 2130 |
|
| 2131 |
static inline char* uitoa_trunc8(uint32_t uii, char* start) { |
| 2132 |
uint32_t quotient = uii / 1000000; |
| 2133 |
memcpy_k(start, &(kDigitPair[quotient]), 2); |
| 2134 |
uii -= 1000000 * quotient; |
| 2135 |
if (uii) { |
| 2136 |
quotient = uii / 10000; |
| 2137 |
start += 2; |
| 2138 |
memcpy_k(start, &(kDigitPair[quotient]), 2); |
| 2139 |
uii -= 10000 * quotient; |
| 2140 |
if (uii) { |
| 2141 |
quotient = uii / 100; |
| 2142 |
start += 2; |
| 2143 |
memcpy_k(start, &(kDigitPair[quotient]), 2); |
| 2144 |
uii -= 100 * quotient; |
| 2145 |
if (uii) { |
| 2146 |
start += 2; |
| 2147 |
memcpy_k(start, &(kDigitPair[uii]), 2); |
| 2148 |
} |
| 2149 |
} |
| 2150 |
} |
| 2151 |
if (start[1] != '0') { |
| 2152 |
return &(start[2]); |
| 2153 |
} |
| 2154 |
return &(start[1]); |
| 2155 |
} |
| 2156 |
|
| 2157 |
static inline char* rtoa_p5(uint32_t remainder, char* start) { |
| 2158 |
if (!remainder) { |
| 2159 |
return start; |
| 2160 |
} |
| 2161 |
*start++ = '.'; |
| 2162 |
uint32_t quotient = remainder / 1000; |
| 2163 |
memcpy_k(start, &(kDigitPair[quotient]), 2); |
| 2164 |
remainder -= 1000 * quotient; |
| 2165 |
if (remainder) { |
| 2166 |
quotient = remainder / 10; |
| 2167 |
start += 2; |
| 2168 |
memcpy_k(start, &(kDigitPair[quotient]), 2); |
| 2169 |
remainder -= 10 * quotient; |
| 2170 |
if (remainder) { |
| 2171 |
start[2] = '0' + remainder; |
| 2172 |
return &(start[3]); |
| 2173 |
} |
| 2174 |
} |
| 2175 |
if (start[1] != '0') { |
| 2176 |
return &(start[2]); |
| 2177 |
} |
| 2178 |
return &(start[1]); |
| 2179 |
} |
| 2180 |
|
| 2181 |
static inline char* qrtoa_1p5(uint32_t quotient, uint32_t remainder, char* start) { |
| 2182 |
*start++ = '0' + quotient; |
| 2183 |
return rtoa_p5(remainder, start); |
| 2184 |
} |
| 2185 |
|
| 2186 |
static inline char* qrtoa_1p7(uint32_t quotient, uint32_t remainder, char* start) { |
| 2187 |
*start++ = '0' + quotient; |
| 2188 |
if (!remainder) { |
| 2189 |
return start; |
| 2190 |
} |
| 2191 |
*start++ = '.'; |
| 2192 |
quotient = remainder / 100000; |
| 2193 |
memcpy_k(start, &(kDigitPair[quotient]), 2); |
| 2194 |
remainder -= 100000 * quotient; |
| 2195 |
if (remainder) { |
| 2196 |
quotient = remainder / 1000; |
| 2197 |
start += 2; |
| 2198 |
memcpy_k(start, &(kDigitPair[quotient]), 2); |
| 2199 |
remainder -= 1000 * quotient; |
| 2200 |
if (remainder) { |
| 2201 |
quotient = remainder / 10; |
| 2202 |
start += 2; |
| 2203 |
memcpy_k(start, &(kDigitPair[quotient]), 2); |
| 2204 |
remainder -= 10 * quotient; |
| 2205 |
if (remainder) { |
| 2206 |
start[2] = '0' + remainder; |
| 2207 |
return &(start[3]); |
| 2208 |
} |
| 2209 |
} |
| 2210 |
} |
| 2211 |
if (start[1] != '0') { |
| 2212 |
return &(start[2]); |
| 2213 |
} |
| 2214 |
return &(start[1]); |
| 2215 |
} |
| 2216 |
|
| 2217 |
// Okay, time to do banker's rounding when printing doubles. 14 digits of |
| 2218 |
// precision are used in judging equality to 0.5 (actual precision of doubles |
| 2219 |
// is 15-17 digits); the intention is to capture all directly loaded or exactly |
| 2220 |
// computed edge cases (so enough tolerance is needed to survive the internal |
| 2221 |
// multiplications by powers of 10, etc.), while rounding a negligible number |
| 2222 |
// of honest-to-god 0.4999999s up and 0.5000001s down. |
| 2223 |
// To avoid inadvertent printing of an extra digit, there's a deliberate gap |
| 2224 |
// between the 99.9994999...-type bounds and the largest numbers that would |
| 2225 |
// actually round down. |
| 2226 |
static const STD_ARRAY_DECL(double, 2, kBankerRound6) = STD_ARRAY_INIT_START() {0.4999995, 0.5000005} STD_ARRAY_INIT_END(); |
| 2227 |
static const STD_ARRAY_DECL(double, 2, kBankerRound8) = STD_ARRAY_INIT_START() {0.499999995, 0.500000005} STD_ARRAY_INIT_END(); |
| 2228 |
|
| 2229 |
static inline uint32_t BankerRoundD(double dxx, STD_ARRAY_KREF(double, 2) banker_round) { |
| 2230 |
uint32_t result = S_CAST(int32_t, dxx); |
| 2231 |
return result + S_CAST(int32_t, (dxx - u31tod(result)) + banker_round[result & 1]); |
| 2232 |
} |
| 2233 |
|
| 2234 |
// These are separate functions so the compiler can optimize the integer |
| 2235 |
// divisions. |
| 2236 |
static inline void BankerRoundD1(double dxx, STD_ARRAY_KREF(double, 2) banker_round, uint32_t* quotientp, uint32_t* remainderp) { |
| 2237 |
dxx *= 10; |
| 2238 |
uint32_t remainder = S_CAST(int32_t, dxx); |
| 2239 |
remainder += S_CAST(int32_t, (dxx - u31tod(remainder)) + banker_round[remainder & 1]); |
| 2240 |
*quotientp = remainder / 10; |
| 2241 |
*remainderp = remainder - (*quotientp) * 10; |
| 2242 |
} |
| 2243 |
|
| 2244 |
static inline void BankerRoundD2(double dxx, STD_ARRAY_KREF(double, 2) banker_round, uint32_t* quotientp, uint32_t* remainderp) { |
| 2245 |
dxx *= 100; |
| 2246 |
uint32_t remainder = S_CAST(int32_t, dxx); |
| 2247 |
remainder += S_CAST(int32_t, (dxx - u31tod(remainder)) + banker_round[remainder & 1]); |
| 2248 |
*quotientp = remainder / 100; |
| 2249 |
*remainderp = remainder - (*quotientp) * 100; |
| 2250 |
} |
| 2251 |
|
| 2252 |
static inline void BankerRoundD3(double dxx, STD_ARRAY_KREF(double, 2) banker_round, uint32_t* quotientp, uint32_t* remainderp) { |
| 2253 |
dxx *= 1000; |
| 2254 |
uint32_t remainder = S_CAST(int32_t, dxx); |
| 2255 |
remainder += S_CAST(int32_t, (dxx - u31tod(remainder)) + banker_round[remainder & 1]); |
| 2256 |
*quotientp = remainder / 1000; |
| 2257 |
*remainderp = remainder - (*quotientp) * 1000; |
| 2258 |
} |
| 2259 |
|
| 2260 |
static inline void BankerRoundD4(double dxx, STD_ARRAY_KREF(double, 2) banker_round, uint32_t* quotientp, uint32_t* remainderp) { |
| 2261 |
dxx *= 10000; |
| 2262 |
uint32_t remainder = S_CAST(int32_t, dxx); |
| 2263 |
remainder += S_CAST(int32_t, (dxx - u31tod(remainder)) + banker_round[remainder & 1]); |
| 2264 |
*quotientp = remainder / 10000; |
| 2265 |
*remainderp = remainder - (*quotientp) * 10000; |
| 2266 |
} |
| 2267 |
|
| 2268 |
static inline void BankerRoundD5(double dxx, STD_ARRAY_KREF(double, 2) banker_round, uint32_t* quotientp, uint32_t* remainderp) { |
| 2269 |
dxx *= 100000; |
| 2270 |
uint32_t remainder = S_CAST(int32_t, dxx); |
| 2271 |
remainder += S_CAST(int32_t, (dxx - u31tod(remainder)) + banker_round[remainder & 1]); |
| 2272 |
*quotientp = remainder / 100000; |
| 2273 |
*remainderp = remainder - (*quotientp) * 100000; |
| 2274 |
} |
| 2275 |
|
| 2276 |
static inline void BankerRoundD6(double dxx, STD_ARRAY_KREF(double, 2) banker_round, uint32_t* quotientp, uint32_t* remainderp) { |
| 2277 |
dxx *= 1000000; |
| 2278 |
uint32_t remainder = S_CAST(int32_t, dxx); |
| 2279 |
remainder += S_CAST(int32_t, (dxx - u31tod(remainder)) + banker_round[remainder & 1]); |
| 2280 |
*quotientp = remainder / 1000000; |
| 2281 |
*remainderp = remainder - (*quotientp) * 1000000; |
| 2282 |
} |
| 2283 |
|
| 2284 |
static inline void BankerRoundD7(double dxx, STD_ARRAY_KREF(double, 2) banker_round, uint32_t* quotientp, uint32_t* remainderp) { |
| 2285 |
dxx *= 10000000; |
| 2286 |
uint32_t remainder = S_CAST(int32_t, dxx); |
| 2287 |
remainder += S_CAST(int32_t, (dxx - u31tod(remainder)) + banker_round[remainder & 1]); |
| 2288 |
*quotientp = remainder / 10000000; |
| 2289 |
*remainderp = remainder - (*quotientp) * 10000000; |
| 2290 |
} |
| 2291 |
|
| 2292 |
char* dtoa_so6(double dxx, char* start) { |
| 2293 |
// 6 sig fig number, 0.999995 <= dxx < 999999.5 |
| 2294 |
// 'so' = "significand only" |
| 2295 |
// Just hardcoding all six cases, in the absence of a better approach... |
| 2296 |
uint32_t uii; |
| 2297 |
uint32_t quotient; |
| 2298 |
uint32_t remainder; |
| 2299 |
if (dxx < 99.999949999999) { |
| 2300 |
if (dxx < 9.9999949999999) { |
| 2301 |
BankerRoundD5(dxx, kBankerRound8, "ient, &remainder); |
| 2302 |
return qrtoa_1p5(quotient, remainder, start); |
| 2303 |
} |
| 2304 |
BankerRoundD4(dxx, kBankerRound8, "ient, &remainder); |
| 2305 |
start = memcpya_k(start, &(kDigitPair[quotient]), 2); |
| 2306 |
if (!remainder) { |
| 2307 |
return start; |
| 2308 |
} |
| 2309 |
*start++ = '.'; |
| 2310 |
quotient = remainder / 100; |
| 2311 |
memcpy_k(start, &(kDigitPair[quotient]), 2); |
| 2312 |
remainder -= 100 * quotient; |
| 2313 |
if (remainder) { |
| 2314 |
start += 2; |
| 2315 |
dtoa_so6_pretail: |
| 2316 |
memcpy_k(start, &(kDigitPair[remainder]), 2); |
| 2317 |
} |
| 2318 |
dtoa_so6_tail: |
| 2319 |
if (start[1] != '0') { |
| 2320 |
return &(start[2]); |
| 2321 |
} |
| 2322 |
return &(start[1]); |
| 2323 |
} |
| 2324 |
if (dxx < 9999.9949999999) { |
| 2325 |
if (dxx < 999.99949999999) { |
| 2326 |
BankerRoundD3(dxx, kBankerRound8, &uii, &remainder); |
| 2327 |
quotient = uii / 100; |
| 2328 |
*start++ = '0' + quotient; |
| 2329 |
quotient = uii - 100 * quotient; |
| 2330 |
start = memcpya_k(start, &(kDigitPair[quotient]), 2); |
| 2331 |
if (!remainder) { |
| 2332 |
return start; |
| 2333 |
} |
| 2334 |
*start++ = '.'; |
| 2335 |
quotient = remainder / 10; |
| 2336 |
memcpy_k(start, &(kDigitPair[quotient]), 2); |
| 2337 |
remainder -= quotient * 10; |
| 2338 |
if (!remainder) { |
| 2339 |
goto dtoa_so6_tail; |
| 2340 |
} |
| 2341 |
start[2] = '0' + remainder; |
| 2342 |
return &(start[3]); |
| 2343 |
} |
| 2344 |
BankerRoundD2(dxx, kBankerRound8, &uii, &remainder); |
| 2345 |
quotient = uii / 100; |
| 2346 |
start = memcpya_k(start, &(kDigitPair[quotient]), 2); |
| 2347 |
quotient = uii - (100 * quotient); |
| 2348 |
start = memcpya_k(start, &(kDigitPair[quotient]), 2); |
| 2349 |
if (!remainder) { |
| 2350 |
return start; |
| 2351 |
} |
| 2352 |
*start++ = '.'; |
| 2353 |
goto dtoa_so6_pretail; |
| 2354 |
} |
| 2355 |
if (dxx >= 99999.949999999) { |
| 2356 |
return u32toa_z6(BankerRoundD(dxx, kBankerRound8), start); |
| 2357 |
} |
| 2358 |
BankerRoundD1(dxx, kBankerRound8, &uii, &remainder); |
| 2359 |
quotient = uii / 10000; |
| 2360 |
*start = '0' + quotient; |
| 2361 |
uii -= 10000 * quotient; |
| 2362 |
quotient = uii / 100; |
| 2363 |
start = memcpya_k(&(start[1]), &(kDigitPair[quotient]), 2); |
| 2364 |
uii = uii - 100 * quotient; |
| 2365 |
start = memcpya_k(start, &(kDigitPair[uii]), 2); |
| 2366 |
if (!remainder) { |
| 2367 |
return start; |
| 2368 |
} |
| 2369 |
*start++ = '.'; |
| 2370 |
*start = '0' + remainder; |
| 2371 |
return &(start[1]); |
| 2372 |
} |
| 2373 |
|
| 2374 |
char* dtoa_so8(double dxx, char* start) { |
| 2375 |
// 8 sig fig number, 0.99999995 <= dxx < 99999999.5 |
| 2376 |
uint32_t uii; |
| 2377 |
uint32_t quotient; |
| 2378 |
uint32_t remainder; |
| 2379 |
if (dxx < 99.999999499999) { |
| 2380 |
if (dxx < 9.9999999499999) { |
| 2381 |
BankerRoundD7(dxx, kBankerRound6, "ient, &remainder); |
| 2382 |
return qrtoa_1p7(quotient, remainder, start); |
| 2383 |
} |
| 2384 |
BankerRoundD6(dxx, kBankerRound6, "ient, &remainder); |
| 2385 |
start = memcpya_k(start, &(kDigitPair[quotient]), 2); |
| 2386 |
if (!remainder) { |
| 2387 |
return start; |
| 2388 |
} |
| 2389 |
*start++ = '.'; |
| 2390 |
quotient = remainder / 10000; |
| 2391 |
memcpy_k(start, &(kDigitPair[quotient]), 2); |
| 2392 |
remainder -= 10000 * quotient; |
| 2393 |
if (remainder) { |
| 2394 |
start += 2; |
| 2395 |
dtoa_so8_pretail4: |
| 2396 |
quotient = remainder / 100; |
| 2397 |
memcpy_k(start, &(kDigitPair[quotient]), 2); |
| 2398 |
remainder -= 100 * quotient; |
| 2399 |
if (remainder) { |
| 2400 |
start += 2; |
| 2401 |
dtoa_so8_pretail2: |
| 2402 |
memcpy_k(start, &(kDigitPair[remainder]), 2); |
| 2403 |
} |
| 2404 |
} |
| 2405 |
dtoa_so8_tail: |
| 2406 |
if (start[1] != '0') { |
| 2407 |
return &(start[2]); |
| 2408 |
} |
| 2409 |
return &(start[1]); |
| 2410 |
} |
| 2411 |
if (dxx < 9999.9999499999) { |
| 2412 |
if (dxx < 999.99999499999) { |
| 2413 |
BankerRoundD5(dxx, kBankerRound6, &uii, &remainder); |
| 2414 |
quotient = uii / 100; |
| 2415 |
*start++ = '0' + quotient; |
| 2416 |
quotient = uii - 100 * quotient; |
| 2417 |
start = memcpya_k(start, &(kDigitPair[quotient]), 2); |
| 2418 |
if (!remainder) { |
| 2419 |
return start; |
| 2420 |
} |
| 2421 |
*start++ = '.'; |
| 2422 |
quotient = remainder / 1000; |
| 2423 |
memcpy_k(start, &(kDigitPair[quotient]), 2); |
| 2424 |
remainder -= quotient * 1000; |
| 2425 |
if (!remainder) { |
| 2426 |
goto dtoa_so8_tail; |
| 2427 |
} |
| 2428 |
start += 2; |
| 2429 |
dtoa_so8_pretail3: |
| 2430 |
quotient = remainder / 10; |
| 2431 |
memcpy_k(start, &(kDigitPair[quotient]), 2); |
| 2432 |
remainder -= quotient * 10; |
| 2433 |
if (!remainder) { |
| 2434 |
goto dtoa_so8_tail; |
| 2435 |
} |
| 2436 |
start[2] = '0' + remainder; |
| 2437 |
return &(start[3]); |
| 2438 |
} |
| 2439 |
BankerRoundD4(dxx, kBankerRound6, &uii, &remainder); |
| 2440 |
quotient = uii / 100; |
| 2441 |
start = memcpya_k(start, &(kDigitPair[quotient]), 2); |
| 2442 |
quotient = uii - (100 * quotient); |
| 2443 |
start = memcpya_k(start, &(kDigitPair[quotient]), 2); |
| 2444 |
if (!remainder) { |
| 2445 |
return start; |
| 2446 |
} |
| 2447 |
*start++ = '.'; |
| 2448 |
goto dtoa_so8_pretail4; |
| 2449 |
} |
| 2450 |
if (dxx < 999999.99499999) { |
| 2451 |
if (dxx < 99999.999499999) { |
| 2452 |
BankerRoundD3(dxx, kBankerRound6, &uii, &remainder); |
| 2453 |
quotient = uii / 10000; |
| 2454 |
*start = '0' + quotient; |
| 2455 |
uii -= 10000 * quotient; |
| 2456 |
quotient = uii / 100; |
| 2457 |
start = memcpya_k(&(start[1]), &(kDigitPair[quotient]), 2); |
| 2458 |
uii -= 100 * quotient; |
| 2459 |
start = memcpya_k(start, &(kDigitPair[uii]), 2); |
| 2460 |
if (!remainder) { |
| 2461 |
return start; |
| 2462 |
} |
| 2463 |
*start++ = '.'; |
| 2464 |
goto dtoa_so8_pretail3; |
| 2465 |
} |
| 2466 |
BankerRoundD2(dxx, kBankerRound6, &uii, &remainder); |
| 2467 |
quotient = uii / 10000; |
| 2468 |
start = memcpya_k(start, &(kDigitPair[quotient]), 2); |
| 2469 |
uii -= 10000 * quotient; |
| 2470 |
quotient = uii / 100; |
| 2471 |
start = memcpya_k(start, &(kDigitPair[quotient]), 2); |
| 2472 |
uii -= 100 * quotient; |
| 2473 |
start = memcpya_k(start, &(kDigitPair[uii]), 2); |
| 2474 |
if (!remainder) { |
| 2475 |
return start; |
| 2476 |
} |
| 2477 |
*start++ = '.'; |
| 2478 |
goto dtoa_so8_pretail2; |
| 2479 |
} |
| 2480 |
if (dxx >= 9999999.9499999) { |
| 2481 |
return uitoa_z8(BankerRoundD(dxx, kBankerRound6), start); |
| 2482 |
} |
| 2483 |
BankerRoundD1(dxx, kBankerRound6, &uii, &remainder); |
| 2484 |
quotient = uii / 1000000; |
| 2485 |
*start = '0' + quotient; |
| 2486 |
uii -= 1000000 * quotient; |
| 2487 |
quotient = uii / 10000; |
| 2488 |
start = memcpya_k(&(start[1]), &(kDigitPair[quotient]), 2); |
| 2489 |
uii -= 10000 * quotient; |
| 2490 |
quotient = uii / 100; |
| 2491 |
start = memcpya_k(start, &(kDigitPair[quotient]), 2); |
| 2492 |
uii -= 100 * quotient; |
| 2493 |
start = memcpya_k(start, &(kDigitPair[uii]), 2); |
| 2494 |
if (!remainder) { |
| 2495 |
return start; |
| 2496 |
} |
| 2497 |
*start = '.'; |
| 2498 |
start[1] = '0' + remainder; |
| 2499 |
return &(start[2]); |
| 2500 |
} |
| 2501 |
|
| 2502 |
char* dtoa_g(double dxx, char* start) { |
| 2503 |
uint32_t xp10 = 0; |
| 2504 |
uint32_t quotient; |
| 2505 |
uint32_t remainder; |
| 2506 |
if (dxx != dxx) { |
| 2507 |
return strcpya_k(start, "nan"); |
| 2508 |
} |
| 2509 |
if (dxx < 0) { |
| 2510 |
*start++ = '-'; |
| 2511 |
dxx = -dxx; |
| 2512 |
} |
| 2513 |
if (dxx < 9.9999949999999e-5) { |
| 2514 |
// 6 sig fig exponential notation, small |
| 2515 |
if (dxx < 9.9999949999999e-16) { |
| 2516 |
if (dxx < 9.9999949999999e-128) { |
| 2517 |
if (dxx == 0.0) { |
| 2518 |
*start = '0'; |
| 2519 |
return &(start[1]); |
| 2520 |
} |
| 2521 |
if (dxx < 9.9999949999999e-256) { |
| 2522 |
dxx *= 1.0e256; |
| 2523 |
xp10 |= 256; |
| 2524 |
} else { |
| 2525 |
dxx *= 1.0e128; |
| 2526 |
xp10 |= 128; |
| 2527 |
} |
| 2528 |
} |
| 2529 |
if (dxx < 9.9999949999999e-64) { |
| 2530 |
dxx *= 1.0e64; |
| 2531 |
xp10 |= 64; |
| 2532 |
} |
| 2533 |
if (dxx < 9.9999949999999e-32) { |
| 2534 |
dxx *= 1.0e32; |
| 2535 |
xp10 |= 32; |
| 2536 |
} |
| 2537 |
if (dxx < 9.9999949999999e-16) { |
| 2538 |
dxx *= 1.0e16; |
| 2539 |
xp10 |= 16; |
| 2540 |
} |
| 2541 |
} |
| 2542 |
if (dxx < 9.9999949999999e-8) { |
| 2543 |
dxx *= 100000000; |
| 2544 |
xp10 |= 8; |
| 2545 |
} |
| 2546 |
if (dxx < 9.9999949999999e-4) { |
| 2547 |
dxx *= 10000; |
| 2548 |
xp10 |= 4; |
| 2549 |
} |
| 2550 |
if (dxx < 9.9999949999999e-2) { |
| 2551 |
dxx *= 100; |
| 2552 |
xp10 |= 2; |
| 2553 |
} |
| 2554 |
if (dxx < 9.9999949999999e-1) { |
| 2555 |
dxx *= 10; |
| 2556 |
++xp10; |
| 2557 |
} |
| 2558 |
BankerRoundD5(dxx, kBankerRound8, "ient, &remainder); |
| 2559 |
start = memcpya_k(qrtoa_1p5(quotient, remainder, start), "e-", 2); |
| 2560 |
if (xp10 >= 100) { |
| 2561 |
quotient = xp10 / 100; |
| 2562 |
*start++ = '0' + quotient; |
| 2563 |
xp10 -= 100 * quotient; |
| 2564 |
} |
| 2565 |
return memcpya_k(start, &(kDigitPair[xp10]), 2); |
| 2566 |
} |
| 2567 |
if (dxx >= 999999.49999999) { |
| 2568 |
// 6 sig fig exponential notation, large |
| 2569 |
if (dxx >= 9.9999949999999e15) { |
| 2570 |
if (dxx >= 9.9999949999999e127) { |
| 2571 |
if (dxx > DBL_MAX) { |
| 2572 |
return strcpya_k(start, "inf"); |
| 2573 |
} |
| 2574 |
if (dxx >= 9.9999949999999e255) { |
| 2575 |
dxx *= 1.0e-256; |
| 2576 |
xp10 |= 256; |
| 2577 |
} else { |
| 2578 |
dxx *= 1.0e-128; |
| 2579 |
xp10 |= 128; |
| 2580 |
} |
| 2581 |
} |
| 2582 |
if (dxx >= 9.9999949999999e63) { |
| 2583 |
dxx *= 1.0e-64; |
| 2584 |
xp10 |= 64; |
| 2585 |
} |
| 2586 |
if (dxx >= 9.9999949999999e31) { |
| 2587 |
dxx *= 1.0e-32; |
| 2588 |
xp10 |= 32; |
| 2589 |
} |
| 2590 |
if (dxx >= 9.9999949999999e15) { |
| 2591 |
dxx *= 1.0e-16; |
| 2592 |
xp10 |= 16; |
| 2593 |
} |
| 2594 |
} |
| 2595 |
if (dxx >= 9.9999949999999e7) { |
| 2596 |
dxx *= 1.0e-8; |
| 2597 |
xp10 |= 8; |
| 2598 |
} |
| 2599 |
if (dxx >= 9.9999949999999e3) { |
| 2600 |
dxx *= 1.0e-4; |
| 2601 |
xp10 |= 4; |
| 2602 |
} |
| 2603 |
if (dxx >= 9.9999949999999e1) { |
| 2604 |
dxx *= 1.0e-2; |
| 2605 |
xp10 |= 2; |
| 2606 |
} |
| 2607 |
if (dxx >= 9.9999949999999e0) { |
| 2608 |
dxx *= 1.0e-1; |
| 2609 |
xp10++; |
| 2610 |
} |
| 2611 |
BankerRoundD5(dxx, kBankerRound8, "ient, &remainder); |
| 2612 |
start = memcpya_k(qrtoa_1p5(quotient, remainder, start), "e+", 2); |
| 2613 |
if (xp10 >= 100) { |
| 2614 |
quotient = xp10 / 100; |
| 2615 |
*start++ = '0' + quotient; |
| 2616 |
xp10 -= 100 * quotient; |
| 2617 |
} |
| 2618 |
return memcpya_k(start, &(kDigitPair[xp10]), 2); |
| 2619 |
} |
| 2620 |
if (dxx >= 0.99999949999999) { |
| 2621 |
return dtoa_so6(dxx, start); |
| 2622 |
} |
| 2623 |
// 6 sig fig decimal, no less than ~0.0001 |
| 2624 |
start = memcpya_k(start, "0.", 2); |
| 2625 |
if (dxx < 9.9999949999999e-3) { |
| 2626 |
dxx *= 100; |
| 2627 |
start = memcpya_k(start, "00", 2); |
| 2628 |
} |
| 2629 |
if (dxx < 9.9999949999999e-2) { |
| 2630 |
dxx *= 10; |
| 2631 |
*start++ = '0'; |
| 2632 |
} |
| 2633 |
return uitoa_trunc6(BankerRoundD(dxx * 1000000, kBankerRound8), start); |
| 2634 |
} |
| 2635 |
|
| 2636 |
char* dtoa_g_p8(double dxx, char* start) { |
| 2637 |
uint32_t xp10 = 0; |
| 2638 |
char wbuf[16]; |
| 2639 |
char* wpos = wbuf; |
| 2640 |
uint32_t quotient; |
| 2641 |
uint32_t remainder; |
| 2642 |
if (dxx != dxx) { |
| 2643 |
return strcpya_k(start, "nan"); |
| 2644 |
} |
| 2645 |
if (dxx < 0) { |
| 2646 |
*wpos++ = '-'; |
| 2647 |
dxx = -dxx; |
| 2648 |
} |
| 2649 |
if (dxx < 9.9999999499999e-5) { |
| 2650 |
// 8 sig fig exponential notation, small |
| 2651 |
if (dxx < 9.9999999499999e-16) { |
| 2652 |
if (dxx < 9.9999999499999e-128) { |
| 2653 |
if (dxx == 0.0) { |
| 2654 |
*start = '0'; |
| 2655 |
return &(start[1]); |
| 2656 |
} |
| 2657 |
if (dxx < 9.9999999499999e-256) { |
| 2658 |
dxx *= 1.0e256; |
| 2659 |
xp10 |= 256; |
| 2660 |
} else { |
| 2661 |
dxx *= 1.0e128; |
| 2662 |
xp10 |= 128; |
| 2663 |
} |
| 2664 |
} |
| 2665 |
if (dxx < 9.9999999499999e-64) { |
| 2666 |
dxx *= 1.0e64; |
| 2667 |
xp10 |= 64; |
| 2668 |
} |
| 2669 |
if (dxx < 9.9999999499999e-32) { |
| 2670 |
dxx *= 1.0e32; |
| 2671 |
xp10 |= 32; |
| 2672 |
} |
| 2673 |
if (dxx < 9.9999999499999e-16) { |
| 2674 |
dxx *= 1.0e16; |
| 2675 |
xp10 |= 16; |
| 2676 |
} |
| 2677 |
} |
| 2678 |
if (dxx < 9.9999999499999e-8) { |
| 2679 |
dxx *= 100000000; |
| 2680 |
xp10 |= 8; |
| 2681 |
} |
| 2682 |
if (dxx < 9.9999999499999e-4) { |
| 2683 |
dxx *= 10000; |
| 2684 |
xp10 |= 4; |
| 2685 |
} |
| 2686 |
if (dxx < 9.9999999499999e-2) { |
| 2687 |
dxx *= 100; |
| 2688 |
xp10 |= 2; |
| 2689 |
} |
| 2690 |
if (dxx < 9.9999999499999e-1) { |
| 2691 |
dxx *= 10; |
| 2692 |
++xp10; |
| 2693 |
} |
| 2694 |
BankerRoundD7(dxx, kBankerRound6, "ient, &remainder); |
| 2695 |
wpos = qrtoa_1p7(quotient, remainder, wpos); |
| 2696 |
remainder = wpos - wbuf; |
| 2697 |
if (xp10 >= 100) { |
| 2698 |
start = memcpya(start, wbuf, remainder); |
| 2699 |
quotient = xp10 / 100; |
| 2700 |
start = memcpyax_k(start, "e-", 2, '0' + quotient); |
| 2701 |
xp10 -= 100 * quotient; |
| 2702 |
} else { |
| 2703 |
start = memcpya(start, wbuf, remainder); |
| 2704 |
start = memcpya_k(start, "e-", 2); |
| 2705 |
} |
| 2706 |
return memcpya_k(start, &(kDigitPair[xp10]), 2); |
| 2707 |
} |
| 2708 |
if (dxx >= 99999999.499999) { |
| 2709 |
// 8 sig fig exponential notation, large |
| 2710 |
if (dxx >= 9.9999999499999e15) { |
| 2711 |
if (dxx >= 9.9999999499999e127) { |
| 2712 |
if (dxx > DBL_MAX) { |
| 2713 |
if (wpos == wbuf) { |
| 2714 |
return memcpya(start, " inf", 4); |
| 2715 |
} |
| 2716 |
return memcpya(start, "-inf", 4); |
| 2717 |
} |
| 2718 |
if (dxx >= 9.9999999499999e255) { |
| 2719 |
dxx *= 1.0e-256; |
| 2720 |
xp10 |= 256; |
| 2721 |
} else { |
| 2722 |
dxx *= 1.0e-128; |
| 2723 |
xp10 |= 128; |
| 2724 |
} |
| 2725 |
} |
| 2726 |
if (dxx >= 9.9999999499999e63) { |
| 2727 |
dxx *= 1.0e-64; |
| 2728 |
xp10 |= 64; |
| 2729 |
} |
| 2730 |
if (dxx >= 9.9999999499999e31) { |
| 2731 |
dxx *= 1.0e-32; |
| 2732 |
xp10 |= 32; |
| 2733 |
} |
| 2734 |
if (dxx >= 9.9999999499999e15) { |
| 2735 |
dxx *= 1.0e-16; |
| 2736 |
xp10 |= 16; |
| 2737 |
} |
| 2738 |
} |
| 2739 |
if (dxx >= 9.9999999499999e7) { |
| 2740 |
dxx *= 1.0e-8; |
| 2741 |
xp10 |= 8; |
| 2742 |
} |
| 2743 |
if (dxx >= 9.9999999499999e3) { |
| 2744 |
dxx *= 1.0e-4; |
| 2745 |
xp10 |= 4; |
| 2746 |
} |
| 2747 |
if (dxx >= 9.9999999499999e1) { |
| 2748 |
dxx *= 1.0e-2; |
| 2749 |
xp10 |= 2; |
| 2750 |
} |
| 2751 |
if (dxx >= 9.9999999499999e0) { |
| 2752 |
dxx *= 1.0e-1; |
| 2753 |
++xp10; |
| 2754 |
} |
| 2755 |
BankerRoundD7(dxx, kBankerRound6, "ient, &remainder); |
| 2756 |
wpos = qrtoa_1p7(quotient, remainder, wpos); |
| 2757 |
remainder = wpos - wbuf; |
| 2758 |
if (xp10 >= 100) { |
| 2759 |
start = memcpya(start, wbuf, remainder); |
| 2760 |
quotient = xp10 / 100; |
| 2761 |
start = memcpyax_k(start, "e+", 2, '0' + quotient); |
| 2762 |
xp10 -= 100 * quotient; |
| 2763 |
} else { |
| 2764 |
start = memcpya(start, wbuf, remainder); |
| 2765 |
start = memcpya_k(start, "e+", 2); |
| 2766 |
} |
| 2767 |
return memcpya_k(start, &(kDigitPair[xp10]), 2); |
| 2768 |
} |
| 2769 |
if (dxx >= 0.99999999499999) { |
| 2770 |
wpos = dtoa_so8(dxx, wpos); |
| 2771 |
} else { |
| 2772 |
// 8 sig fig decimal, no less than ~0.0001 |
| 2773 |
wpos = memcpya_k(wpos, "0.", 2); |
| 2774 |
if (dxx < 9.9999999499999e-3) { |
| 2775 |
dxx *= 100; |
| 2776 |
wpos = memcpya_k(wpos, "00", 2); |
| 2777 |
} |
| 2778 |
if (dxx < 9.9999999499999e-2) { |
| 2779 |
dxx *= 10; |
| 2780 |
*wpos++ = '0'; |
| 2781 |
} |
| 2782 |
wpos = uitoa_trunc8(BankerRoundD(dxx * 100000000, kBankerRound6), wpos); |
| 2783 |
} |
| 2784 |
remainder = wpos - wbuf; |
| 2785 |
return memcpya(start, wbuf, remainder); |
| 2786 |
} |
| 2787 |
|
| 2788 |
// "prob" means that the number is guaranteed to be in [0, 1]. |
| 2789 |
// no leading space is printed. trailing zeroes (/decimal point) are erased |
| 2790 |
// iff there is equality to ~13 decimal places. |
| 2791 |
char* dtoa_f_probp6_spaced(double dxx, char* start) { |
| 2792 |
double dxx_10_6 = dxx * 1000000; |
| 2793 |
const uint32_t dec_digits = BankerRoundD(dxx_10_6, kBankerRound8); |
| 2794 |
*start++ = '0' + (dec_digits == 1000000); |
| 2795 |
*start++ = '.'; |
| 2796 |
start = u32toa_z6(dec_digits, start); |
| 2797 |
if (fabs(dxx_10_6 - u31tod(dec_digits)) >= 0.00000005) { |
| 2798 |
return start; |
| 2799 |
} |
| 2800 |
TrailingZeroesToSpaces(start); |
| 2801 |
return start; |
| 2802 |
} |
| 2803 |
|
| 2804 |
char* dtoa_f_probp6_clipped(double dxx, char* start) { |
| 2805 |
double dxx_10_6 = dxx * 1000000; |
| 2806 |
const uint32_t dec_digits = BankerRoundD(dxx_10_6, kBankerRound8); |
| 2807 |
*start++ = '0' + (dec_digits == 1000000); |
| 2808 |
*start++ = '.'; |
| 2809 |
start = u32toa_z6(dec_digits, start); |
| 2810 |
if (fabs(dxx_10_6 - u31tod(dec_digits)) >= 0.00000005) { |
| 2811 |
return start; |
| 2812 |
} |
| 2813 |
return ClipTrailingZeroes(start); |
| 2814 |
} |
| 2815 |
|
| 2816 |
/* |
| 2817 |
char* dtoa_f_p5_clipped(double dxx, char* start) { |
| 2818 |
if (dxx != dxx) { |
| 2819 |
return strcpya_k(start, "nan"); |
| 2820 |
} |
| 2821 |
if (dxx < 0.0) { |
| 2822 |
// note that "-0" will be printed for very small negative numbers; do we |
| 2823 |
// want this? |
| 2824 |
*start++ = '-'; |
| 2825 |
dxx = -dxx; |
| 2826 |
} |
| 2827 |
#ifdef __LP64__ |
| 2828 |
if (dxx < 4294967295.999994) { |
| 2829 |
// We could use different levels of banker's rounding for different-size |
| 2830 |
// quotients, but that's overkill for now; revisit after basic dosage |
| 2831 |
// support is working. |
| 2832 |
dxx *= 100000; |
| 2833 |
uint64_t remainder = (int64_t)dxx; |
| 2834 |
remainder += (int64_t)((dxx - ((int64_t)remainder)) + kBankerRound6[remainder & 1]); |
| 2835 |
uint64_t quotient = remainder / 100000; |
| 2836 |
remainder = remainder - quotient * 100000; |
| 2837 |
start = u32toa(quotient, start); |
| 2838 |
return rtoa_p5(remainder, start); |
| 2839 |
} |
| 2840 |
#else |
| 2841 |
if (dxx < 2147483647.999994) { |
| 2842 |
// avoid 64-bit integer math in 32-bit build. |
| 2843 |
// (todo: a bit of benchmarking) |
| 2844 |
const uintptr_t quotient = (intptr_t)dxx; |
| 2845 |
const double remainder_d = (dxx - ((intptr_t)quotient)) * 100000; |
| 2846 |
const uint32_t remainder_d_trunc = (int32_t)remainder_d; |
| 2847 |
const uint32_t remainder = (int32_t)(remainder_d + kBankerRound6[remainder_d_trunc & 1]); |
| 2848 |
start = u32toa(quotient, start); |
| 2849 |
return rtoa_p5(remainder, start); |
| 2850 |
} |
| 2851 |
#endif |
| 2852 |
if (dxx == INFINITY) { |
| 2853 |
return strcpya_k(start, "inf"); |
| 2854 |
} |
| 2855 |
// just punt larger numbers to glibc for now, this isn't a bottleneck |
| 2856 |
start += sprintf(start, "%.5f", dxx); |
| 2857 |
// .5f doesn't strip trailing zeroes, do that manually |
| 2858 |
for (uint32_t uii = 0; uii != 5; ++uii) { |
| 2859 |
if (start[-1] != '0') { |
| 2860 |
return start; |
| 2861 |
} |
| 2862 |
--start; |
| 2863 |
} |
| 2864 |
return &(start[-1]); // strip the decimal point |
| 2865 |
} |
| 2866 |
*/ |
| 2867 |
|
| 2868 |
// todo: benchmark the exponential-notation part of this vs. dtoa_g(); maybe |
| 2869 |
// dtoa_g() should actually call this (or at least the exponential-notation |
| 2870 |
// part, put into its own function) for the most extreme values? |
| 2871 |
char* lntoa_g(double ln_val, char* start) { |
| 2872 |
// log(999999.49999999) |
| 2873 |
if (ln_val < 13.81551005796414) { |
| 2874 |
// log(9.9999949999999e-5) |
| 2875 |
if (ln_val > -9.210340871976317) { |
| 2876 |
// No exponential notation. |
| 2877 |
|
| 2878 |
// log(0.99999949999999) |
| 2879 |
if (ln_val > -5.000001349509205e-7) { |
| 2880 |
// may as well fast-path x=1; since the most common use-case for this |
| 2881 |
// function is p-value printing, x=1 should happen a lot more than x>1. |
| 2882 |
// log(1.0000050000001) |
| 2883 |
if (ln_val < 4.999987599993995e-6) { |
| 2884 |
*start++ = '1'; |
| 2885 |
return start; |
| 2886 |
} |
| 2887 |
return dtoa_so6(exp(ln_val), start); |
| 2888 |
} |
| 2889 |
double dxx = exp(ln_val); |
| 2890 |
// 6 sig fig decimal, no less than ~0.0001 |
| 2891 |
start = memcpya_k(start, "0.", 2); |
| 2892 |
if (dxx < 9.9999949999999e-3) { |
| 2893 |
dxx *= 100; |
| 2894 |
start = memcpya_k(start, "00", 2); |
| 2895 |
} |
| 2896 |
if (dxx < 9.9999949999999e-2) { |
| 2897 |
dxx *= 10; |
| 2898 |
*start++ = '0'; |
| 2899 |
} |
| 2900 |
return uitoa_trunc6(BankerRoundD(dxx * 1000000, kBankerRound8), start); |
| 2901 |
} |
| 2902 |
// if exponent is in danger of overflowing int32, just print '0' |
| 2903 |
if (ln_val < 0x7ffffffb * (-kLn10)) { |
| 2904 |
*start++ = '0'; |
| 2905 |
return start; |
| 2906 |
} |
| 2907 |
} else { |
| 2908 |
// if exponent is in danger of overflowing int32, just print 'inf' |
| 2909 |
if (ln_val > 0x7ffffffb * kLn10) { |
| 2910 |
return strcpya_k(start, "inf"); |
| 2911 |
} |
| 2912 |
} |
| 2913 |
int32_t xp10 = S_CAST(int32_t, (5.000001349509205e-7 + ln_val) * kRecipLn10); |
| 2914 |
double mantissa = exp(xp10 * (-kLn10) + ln_val); |
| 2915 |
// mantissa will usually be in [.9999995, 9.999995], but |ln_val| can be |
| 2916 |
// larger than 2^32, and floating point errors in either direction are |
| 2917 |
// definitely possible (<20 bits of precision). |
| 2918 |
if (mantissa < 0.99999949999999) { |
| 2919 |
mantissa *= 10; |
| 2920 |
xp10 -= 1; |
| 2921 |
} else if (mantissa > 9.9999949999999) { |
| 2922 |
mantissa *= 0.1; |
| 2923 |
xp10 += 1; |
| 2924 |
} |
| 2925 |
uint32_t quotient; |
| 2926 |
uint32_t remainder; |
| 2927 |
BankerRoundD5(mantissa, kBankerRound8, "ient, &remainder); |
| 2928 |
start = qrtoa_1p5(quotient, remainder, start); |
| 2929 |
if (xp10 < 0) { |
| 2930 |
start = memcpya_k(start, "e-", 2); |
| 2931 |
if (xp10 > -10) { |
| 2932 |
*start++ = '0'; |
| 2933 |
} |
| 2934 |
return u32toa(-xp10, start); |
| 2935 |
} |
| 2936 |
start = memcpya_k(start, "e+", 2); |
| 2937 |
if (xp10 < 10) { |
| 2938 |
*start++ = '0'; |
| 2939 |
} |
| 2940 |
return u32toa(xp10, start); |
| 2941 |
} |
| 2942 |
|
| 2943 |
// Previously had specialized float-printing functions, but upon reflection it |
| 2944 |
// makes sense to just promote to double like printf does. |
| 2945 |
|
| 2946 |
|
| 2947 |
CXXCONST_CP FindSortedStrboxDuplicate(const char* sorted_ids, uintptr_t id_ct, uintptr_t max_id_blen) { |
| 2948 |
--id_ct; |
| 2949 |
for (uintptr_t id_idx = 0; id_idx != id_ct; ++id_idx) { |
| 2950 |
if (strequal_overread(&(sorted_ids[id_idx * max_id_blen]), &(sorted_ids[(id_idx + 1) * max_id_blen]))) { |
| 2951 |
return S_CAST(CXXCONST_CP, &(sorted_ids[id_idx * max_id_blen])); |
| 2952 |
} |
| 2953 |
} |
| 2954 |
return nullptr; |
| 2955 |
} |
| 2956 |
|
| 2957 |
|
| 2958 |
int32_t bsearch_strbox(const char* idbuf, const char* sorted_strbox, uintptr_t cur_id_slen, uintptr_t max_id_blen, uintptr_t end_idx) { |
| 2959 |
// does not assume null-terminated idbuf, or nonempty array. |
| 2960 |
if (cur_id_slen >= max_id_blen) { |
| 2961 |
return -1; |
| 2962 |
} |
| 2963 |
uintptr_t start_idx = 0; |
| 2964 |
while (start_idx < end_idx) { |
| 2965 |
const uintptr_t mid_idx = (start_idx + end_idx) / 2; |
| 2966 |
const int32_t ii = Memcmp(idbuf, &(sorted_strbox[mid_idx * max_id_blen]), cur_id_slen); |
| 2967 |
if (ii > 0) { |
| 2968 |
start_idx = mid_idx + 1; |
| 2969 |
} else if ((ii < 0) || sorted_strbox[mid_idx * max_id_blen + cur_id_slen]) { |
| 2970 |
end_idx = mid_idx; |
| 2971 |
} else { |
| 2972 |
return S_CAST(uint32_t, mid_idx); |
| 2973 |
} |
| 2974 |
} |
| 2975 |
return -1; |
| 2976 |
} |
| 2977 |
|
| 2978 |
int32_t bsearch_strbox_natural(const char* idbuf, const char* sorted_strbox, uintptr_t max_id_blen, uintptr_t end_idx) { |
| 2979 |
// unlike bsearch_strbox(), caller is responsible for slen >= max_id_blen |
| 2980 |
// check if appropriate here |
| 2981 |
uintptr_t start_idx = 0; |
| 2982 |
while (start_idx < end_idx) { |
| 2983 |
const uintptr_t mid_idx = (start_idx + end_idx) / 2; |
| 2984 |
const int32_t ii = strcmp_natural(idbuf, &(sorted_strbox[mid_idx * max_id_blen])); |
| 2985 |
if (ii > 0) { |
| 2986 |
start_idx = mid_idx + 1; |
| 2987 |
} else if (ii < 0) { |
| 2988 |
end_idx = mid_idx; |
| 2989 |
} else { |
| 2990 |
return S_CAST(uint32_t, mid_idx); |
| 2991 |
} |
| 2992 |
} |
| 2993 |
return -1; |
| 2994 |
} |
| 2995 |
|
| 2996 |
int32_t bsearch_strptr_overread(const char* idbuf, const char* const* sorted_strptrs, uintptr_t end_idx) { |
| 2997 |
uintptr_t start_idx = 0; |
| 2998 |
while (start_idx < end_idx) { |
| 2999 |
const uintptr_t mid_idx = (start_idx + end_idx) / 2; |
| 3000 |
const int32_t ii = strcmp_overread(idbuf, sorted_strptrs[mid_idx]); |
| 3001 |
if (ii > 0) { |
| 3002 |
start_idx = mid_idx + 1; |
| 3003 |
} else if (ii < 0) { |
| 3004 |
end_idx = mid_idx; |
| 3005 |
} else { |
| 3006 |
return S_CAST(uint32_t, mid_idx); |
| 3007 |
} |
| 3008 |
} |
| 3009 |
return -1; |
| 3010 |
} |
| 3011 |
|
| 3012 |
int32_t bsearch_strptr_natural(const char* idbuf, const char* const* sorted_strptrs, uintptr_t end_idx) { |
| 3013 |
uintptr_t start_idx = 0; |
| 3014 |
while (start_idx < end_idx) { |
| 3015 |
const uintptr_t mid_idx = (start_idx + end_idx) / 2; |
| 3016 |
const int32_t ii = strcmp_natural(idbuf, sorted_strptrs[mid_idx]); |
| 3017 |
if (ii > 0) { |
| 3018 |
start_idx = mid_idx + 1; |
| 3019 |
} else if (ii < 0) { |
| 3020 |
end_idx = mid_idx; |
| 3021 |
} else { |
| 3022 |
return S_CAST(uint32_t, mid_idx); |
| 3023 |
} |
| 3024 |
} |
| 3025 |
return -1; |
| 3026 |
} |
| 3027 |
|
| 3028 |
uintptr_t bsearch_strbox_lb(const char* idbuf, const char* sorted_strbox, uintptr_t cur_id_slen, uintptr_t max_id_blen, uintptr_t end_idx) { |
| 3029 |
// returns number of elements in sorted_strbox[] less than idbuf. |
| 3030 |
if (cur_id_slen > max_id_blen) { |
| 3031 |
cur_id_slen = max_id_blen; |
| 3032 |
} |
| 3033 |
uintptr_t start_idx = 0; |
| 3034 |
while (start_idx < end_idx) { |
| 3035 |
const uintptr_t mid_idx = (start_idx + end_idx) / 2; |
| 3036 |
if (Memcmp(idbuf, &(sorted_strbox[mid_idx * max_id_blen]), cur_id_slen) > 0) { |
| 3037 |
start_idx = mid_idx + 1; |
| 3038 |
} else { |
| 3039 |
end_idx = mid_idx; |
| 3040 |
} |
| 3041 |
} |
| 3042 |
return start_idx; |
| 3043 |
} |
| 3044 |
|
| 3045 |
uintptr_t ExpsearchStrLb(const char* idbuf, const char* sorted_strbox, uintptr_t cur_id_slen, uintptr_t max_id_blen, uintptr_t end_idx, uintptr_t cur_idx) { |
| 3046 |
uintptr_t next_incr = 1; |
| 3047 |
uintptr_t start_idx = cur_idx; |
| 3048 |
while (cur_idx < end_idx) { |
| 3049 |
if (Memcmp(idbuf, &(sorted_strbox[cur_idx * max_id_blen]), cur_id_slen) <= 0) { |
| 3050 |
end_idx = cur_idx; |
| 3051 |
break; |
| 3052 |
} |
| 3053 |
start_idx = cur_idx + 1; |
| 3054 |
cur_idx += next_incr; |
| 3055 |
next_incr *= 2; |
| 3056 |
} |
| 3057 |
while (start_idx < end_idx) { |
| 3058 |
const uintptr_t mid_idx = (start_idx + end_idx) / 2; |
| 3059 |
if (Memcmp(idbuf, &(sorted_strbox[mid_idx * max_id_blen]), cur_id_slen) > 0) { |
| 3060 |
start_idx = mid_idx + 1; |
| 3061 |
} else { |
| 3062 |
end_idx = mid_idx; |
| 3063 |
} |
| 3064 |
} |
| 3065 |
return start_idx; |
| 3066 |
} |
| 3067 |
|
| 3068 |
uintptr_t ExpsearchNsortStrLb(const char* idbuf, const char* nsorted_strbox, uintptr_t max_id_blen, uintptr_t end_idx, uintptr_t cur_idx) { |
| 3069 |
uintptr_t next_incr = 1; |
| 3070 |
uintptr_t start_idx = cur_idx; |
| 3071 |
while (cur_idx < end_idx) { |
| 3072 |
if (strcmp_natural(idbuf, &(nsorted_strbox[cur_idx * max_id_blen])) <= 0) { |
| 3073 |
end_idx = cur_idx; |
| 3074 |
break; |
| 3075 |
} |
| 3076 |
start_idx = cur_idx + 1; |
| 3077 |
cur_idx += next_incr; |
| 3078 |
next_incr *= 2; |
| 3079 |
} |
| 3080 |
while (start_idx < end_idx) { |
| 3081 |
const uintptr_t mid_idx = (start_idx + end_idx) / 2; |
| 3082 |
if (strcmp_natural(idbuf, &(nsorted_strbox[mid_idx * max_id_blen])) > 0) { |
| 3083 |
start_idx = mid_idx + 1; |
| 3084 |
} else { |
| 3085 |
end_idx = mid_idx; |
| 3086 |
} |
| 3087 |
} |
| 3088 |
return start_idx; |
| 3089 |
} |
| 3090 |
|
| 3091 |
uint32_t IsInfStr(const char* ss, uint32_t slen, uint32_t* is_neg_ptr) { |
| 3092 |
const char first_char = *ss; |
| 3093 |
if (first_char == '-') { |
| 3094 |
*is_neg_ptr = 1; |
| 3095 |
++ss; |
| 3096 |
--slen; |
| 3097 |
} else if (first_char == '+') { |
| 3098 |
++ss; |
| 3099 |
--slen; |
| 3100 |
} |
| 3101 |
if (slen == 3) { |
| 3102 |
uint32_t four_chars; |
| 3103 |
memcpy(&four_chars, ss, 4); // OVERREAD |
| 3104 |
// assumes little-endian |
| 3105 |
return ((four_chars & 0xdfdfdf) == 0x464e49); |
| 3106 |
} |
| 3107 |
if (slen != 8) { |
| 3108 |
return 0; |
| 3109 |
} |
| 3110 |
uint64_t eight_chars; |
| 3111 |
memcpy(&eight_chars, ss, 8); |
| 3112 |
return ((eight_chars & 0xdfdfdfdfdfdfdfdfLLU) == 0x5954494e49464e49LLU); |
| 3113 |
} |
| 3114 |
|
| 3115 |
|
| 3116 |
#ifdef __LP64__ |
| 3117 |
CXXCONST_CP Memrchr(const char* str_start, char needle, uintptr_t slen) { |
| 3118 |
const VecI8 vvec_all_needle = veci8_set1(needle); |
| 3119 |
const uintptr_t str_end_addr = R_CAST(uintptr_t, str_start) + slen; |
| 3120 |
const uint32_t trailing_byte_ct = str_end_addr % kBytesPerVec; |
| 3121 |
const VecI8* str_rev_viter = R_CAST(const VecI8*, RoundDownPow2(str_end_addr, kBytesPerVec)); |
| 3122 |
if (trailing_byte_ct) { |
| 3123 |
// This is a GNU vector extension parallel-equality check, which gets |
| 3124 |
// translated to e.g. _mm256_cmpeq_epi8(). |
| 3125 |
// As long as we're performing aligned reads, it's safe to read bytes |
| 3126 |
// beyond str_end as long as they're in the same vector; we only risk |
| 3127 |
// violating process read permissions if we cross a page boundary. |
| 3128 |
// (For this reason, I don't bother with AVX unaligned reads.) |
| 3129 |
const VecI8 match_vvec = (*str_rev_viter == vvec_all_needle); |
| 3130 |
# ifndef SIMDE_ARM_NEON_A32V8_NATIVE |
| 3131 |
uint32_t matching_bytes = veci8_movemask(match_vvec); |
| 3132 |
matching_bytes &= (1U << (trailing_byte_ct % kBytesPerVec)) - 1; |
| 3133 |
if (str_start > DowncastKToC(str_rev_viter)) { |
| 3134 |
const uint32_t leading_byte_ct = R_CAST(uintptr_t, str_start) % kBytesPerVec; |
| 3135 |
matching_bytes &= -(1U << leading_byte_ct); |
| 3136 |
// Special-case this, since main_loop_iter_ct below would underflow. |
| 3137 |
if (!matching_bytes) { |
| 3138 |
return nullptr; |
| 3139 |
} |
| 3140 |
} |
| 3141 |
if (matching_bytes) { |
| 3142 |
const uint32_t byte_offset_in_vec = bsru32(matching_bytes); |
| 3143 |
return &(DowncastToXC(str_rev_viter)[byte_offset_in_vec]); |
| 3144 |
} |
| 3145 |
# else |
| 3146 |
uint64_t matching_nybbles = arm_shrn4_i8(match_vvec); |
| 3147 |
matching_nybbles &= (1LLU << (4 * (trailing_byte_ct % kBytesPerVec))) - 1; |
| 3148 |
if (str_start > DowncastKToC(str_rev_viter)) { |
| 3149 |
const uint32_t leading_byte_ct = R_CAST(uintptr_t, str_start) % kBytesPerVec; |
| 3150 |
matching_nybbles &= -(1LLU << (4 * leading_byte_ct)); |
| 3151 |
// Special-case this, since main_loop_iter_ct below would underflow. |
| 3152 |
if (!matching_nybbles) { |
| 3153 |
return nullptr; |
| 3154 |
} |
| 3155 |
} |
| 3156 |
if (matching_nybbles) { |
| 3157 |
const uint32_t byte_offset_in_vec = bsrw(matching_nybbles) / 4; |
| 3158 |
return &(DowncastToXC(str_rev_viter)[byte_offset_in_vec]); |
| 3159 |
} |
| 3160 |
# endif |
| 3161 |
} |
| 3162 |
const uintptr_t main_loop_iter_ct = (R_CAST(uintptr_t, str_rev_viter) - R_CAST(uintptr_t, str_start)) / (2 * kBytesPerVec); |
| 3163 |
for (uintptr_t ulii = 0; ulii != main_loop_iter_ct; ++ulii) { |
| 3164 |
// For long lines, looping over two vectors at a time is most efficient on |
| 3165 |
// my Mac (also tried 1 and 4). |
| 3166 |
--str_rev_viter; |
| 3167 |
const VecI8 match_vvec1 = (*str_rev_viter == vvec_all_needle); |
| 3168 |
--str_rev_viter; |
| 3169 |
const VecI8 match_vvec0 = (*str_rev_viter == vvec_all_needle); |
| 3170 |
# ifndef SIMDE_ARM_NEON_A32V8_NATIVE |
| 3171 |
const uint32_t matching_bytes = veci8_movemask(match_vvec1 | match_vvec0); |
| 3172 |
if (matching_bytes) { |
| 3173 |
const uint32_t matching_bytes1 = veci8_movemask(match_vvec1); |
| 3174 |
if (matching_bytes1) { |
| 3175 |
const uint32_t byte_offset_in_vec = bsru32(matching_bytes1); |
| 3176 |
return &(DowncastToXC(&(str_rev_viter[1]))[byte_offset_in_vec]); |
| 3177 |
} |
| 3178 |
const uint32_t byte_offset_in_vec = bsru32(matching_bytes); |
| 3179 |
return &(DowncastToXC(str_rev_viter)[byte_offset_in_vec]); |
| 3180 |
} |
| 3181 |
# else |
| 3182 |
const uint64_t matching_nybbles = arm_shrn4_i8(match_vvec1 | match_vvec0); |
| 3183 |
if (matching_nybbles) { |
| 3184 |
const uint64_t matching_nybbles1 = arm_shrn4_i8(match_vvec1); |
| 3185 |
if (matching_nybbles1) { |
| 3186 |
const uint32_t byte_offset_in_vec = bsrw(matching_nybbles1) / 4; |
| 3187 |
return &(DowncastToXC(&(str_rev_viter[1]))[byte_offset_in_vec]); |
| 3188 |
} |
| 3189 |
const uint32_t byte_offset_in_vec = bsrw(matching_nybbles) / 4; |
| 3190 |
return &(DowncastToXC(str_rev_viter)[byte_offset_in_vec]); |
| 3191 |
} |
| 3192 |
# endif |
| 3193 |
} |
| 3194 |
while (1) { |
| 3195 |
uintptr_t remaining_byte_ct_underflow = R_CAST(uintptr_t, str_rev_viter) - R_CAST(uintptr_t, str_start); |
| 3196 |
if (S_CAST(intptr_t, remaining_byte_ct_underflow) <= 0) { |
| 3197 |
return nullptr; |
| 3198 |
} |
| 3199 |
--str_rev_viter; |
| 3200 |
const VecI8 match_vvec = (*str_rev_viter == vvec_all_needle); |
| 3201 |
# ifndef SIMDE_ARM_NEON_A32V8_NATIVE |
| 3202 |
const uint32_t matching_bytes = veci8_movemask(match_vvec); |
| 3203 |
if (matching_bytes) { |
| 3204 |
const uint32_t byte_offset_in_vec = bsru32(matching_bytes); |
| 3205 |
if (byte_offset_in_vec + remaining_byte_ct_underflow < kBytesPerVec) { |
| 3206 |
return nullptr; |
| 3207 |
} |
| 3208 |
return &(DowncastToXC(str_rev_viter)[byte_offset_in_vec]); |
| 3209 |
} |
| 3210 |
# else |
| 3211 |
const uint64_t matching_nybbles = arm_shrn4_i8(match_vvec); |
| 3212 |
if (matching_nybbles) { |
| 3213 |
const uint32_t byte_offset_in_vec = bsrw(matching_nybbles) / 4; |
| 3214 |
if (byte_offset_in_vec + remaining_byte_ct_underflow < kBytesPerVec) { |
| 3215 |
return nullptr; |
| 3216 |
} |
| 3217 |
return &(DowncastToXC(str_rev_viter)[byte_offset_in_vec]); |
| 3218 |
} |
| 3219 |
# endif |
| 3220 |
} |
| 3221 |
} |
| 3222 |
|
| 3223 |
CXXCONST_CP LastSpaceOrEoln(const char* str_start, uintptr_t slen) { |
| 3224 |
// See TokenLexK0(). |
| 3225 |
const VecUc vvec_all95 = vecuc_set1(95); |
| 3226 |
const uintptr_t str_end_addr = R_CAST(uintptr_t, str_start) + slen; |
| 3227 |
const uint32_t trailing_byte_ct = str_end_addr % kBytesPerVec; |
| 3228 |
const VecUc* str_rev_viter = R_CAST(const VecUc*, RoundDownPow2(str_end_addr, kBytesPerVec)); |
| 3229 |
if (trailing_byte_ct) { |
| 3230 |
// As long as we're performing aligned reads, it's safe to read bytes |
| 3231 |
// beyond str_end as long as they're in the same vector; we only risk |
| 3232 |
// violating process read permissions if we cross a page boundary. |
| 3233 |
const VecUc postspace_vvec = vecuc_adds(*str_rev_viter, vvec_all95); |
| 3234 |
# ifndef SIMDE_ARM_NEON_A32V8_NATIVE |
| 3235 |
uint32_t nontoken_bytes = S_CAST(Vec8thUint, ~vecuc_movemask(postspace_vvec)); |
| 3236 |
nontoken_bytes &= (1U << (trailing_byte_ct % kBytesPerVec)) - 1; |
| 3237 |
if (str_start > DowncastKToC(str_rev_viter)) { |
| 3238 |
const uint32_t leading_byte_ct = R_CAST(uintptr_t, str_start) % kBytesPerVec; |
| 3239 |
nontoken_bytes &= -(1U << leading_byte_ct); |
| 3240 |
// Special-case this, since main_loop_iter_ct below would underflow. |
| 3241 |
if (!nontoken_bytes) { |
| 3242 |
return nullptr; |
| 3243 |
} |
| 3244 |
} |
| 3245 |
if (nontoken_bytes) { |
| 3246 |
const uint32_t byte_offset_in_vec = bsru32(nontoken_bytes); |
| 3247 |
return &(DowncastToXC(str_rev_viter)[byte_offset_in_vec]); |
| 3248 |
} |
| 3249 |
# else |
| 3250 |
uint64_t nontoken_nybbles = ~arm_shrn4_uc(postspace_vvec); |
| 3251 |
nontoken_nybbles &= (1LLU << (4 * (trailing_byte_ct % kBytesPerVec))) - 1; |
| 3252 |
if (str_start > DowncastKToC(str_rev_viter)) { |
| 3253 |
const uint32_t leading_byte_ct = R_CAST(uintptr_t, str_start) % kBytesPerVec; |
| 3254 |
nontoken_nybbles &= -(1LLU << (4 * leading_byte_ct)); |
| 3255 |
// Special-case this, since main_loop_iter_ct below would underflow. |
| 3256 |
if (!nontoken_nybbles) { |
| 3257 |
return nullptr; |
| 3258 |
} |
| 3259 |
} |
| 3260 |
if (nontoken_nybbles) { |
| 3261 |
const uint32_t byte_offset_in_vec = bsrw(nontoken_nybbles) / 4; |
| 3262 |
return &(DowncastToXC(str_rev_viter)[byte_offset_in_vec]); |
| 3263 |
} |
| 3264 |
# endif |
| 3265 |
} |
| 3266 |
const uintptr_t main_loop_iter_ct = (R_CAST(uintptr_t, str_rev_viter) - R_CAST(uintptr_t, str_start)) / (2 * kBytesPerVec); |
| 3267 |
for (uintptr_t ulii = 0; ulii != main_loop_iter_ct; ++ulii) { |
| 3268 |
--str_rev_viter; |
| 3269 |
const VecUc postspace_vvec1 = vecuc_adds(*str_rev_viter, vvec_all95); |
| 3270 |
--str_rev_viter; |
| 3271 |
const VecUc postspace_vvec0 = vecuc_adds(*str_rev_viter, vvec_all95); |
| 3272 |
# ifndef SIMDE_ARM_NEON_A32V8_NATIVE |
| 3273 |
const uint32_t nontoken_bytes = S_CAST(Vec8thUint, ~vecuc_movemask(postspace_vvec1 & postspace_vvec0)); |
| 3274 |
if (nontoken_bytes) { |
| 3275 |
const uint32_t nontoken_bytes1 = S_CAST(Vec8thUint, ~vecuc_movemask(postspace_vvec1)); |
| 3276 |
if (nontoken_bytes1) { |
| 3277 |
const uint32_t byte_offset_in_vec = bsru32(nontoken_bytes1); |
| 3278 |
return &(DowncastToXC(&(str_rev_viter[1]))[byte_offset_in_vec]); |
| 3279 |
} |
| 3280 |
const uint32_t byte_offset_in_vec = bsru32(nontoken_bytes); |
| 3281 |
return &(DowncastToXC(str_rev_viter)[byte_offset_in_vec]); |
| 3282 |
} |
| 3283 |
# else |
| 3284 |
const uint64_t nontoken_nybbles = ~arm_shrn4_uc(postspace_vvec1 & postspace_vvec0); |
| 3285 |
if (nontoken_nybbles) { |
| 3286 |
const uint32_t nontoken_nybbles1 = ~arm_shrn4_uc(postspace_vvec1); |
| 3287 |
if (nontoken_nybbles1) { |
| 3288 |
const uint32_t byte_offset_in_vec = bsrw(nontoken_nybbles1) / 4; |
| 3289 |
return &(DowncastToXC(&(str_rev_viter[1]))[byte_offset_in_vec]); |
| 3290 |
} |
| 3291 |
const uint32_t byte_offset_in_vec = bsrw(nontoken_nybbles) / 4; |
| 3292 |
return &(DowncastToXC(str_rev_viter)[byte_offset_in_vec]); |
| 3293 |
} |
| 3294 |
# endif |
| 3295 |
} |
| 3296 |
while (1) { |
| 3297 |
uintptr_t remaining_byte_ct_underflow = R_CAST(uintptr_t, str_rev_viter) - R_CAST(uintptr_t, str_start); |
| 3298 |
if (S_CAST(intptr_t, remaining_byte_ct_underflow) <= 0) { |
| 3299 |
return nullptr; |
| 3300 |
} |
| 3301 |
--str_rev_viter; |
| 3302 |
const VecUc postspace_vvec = vecuc_adds(*str_rev_viter, vvec_all95); |
| 3303 |
# ifndef SIMDE_ARM_NEON_A32V8_NATIVE |
| 3304 |
const uint32_t nontoken_bytes = S_CAST(Vec8thUint, ~vecuc_movemask(postspace_vvec)); |
| 3305 |
if (nontoken_bytes) { |
| 3306 |
const uint32_t byte_offset_in_vec = bsru32(nontoken_bytes); |
| 3307 |
if (byte_offset_in_vec + remaining_byte_ct_underflow < kBytesPerVec) { |
| 3308 |
return nullptr; |
| 3309 |
} |
| 3310 |
return &(DowncastToXC(str_rev_viter)[byte_offset_in_vec]); |
| 3311 |
} |
| 3312 |
# else |
| 3313 |
const uint64_t nontoken_nybbles = ~arm_shrn4_uc(postspace_vvec); |
| 3314 |
if (nontoken_nybbles) { |
| 3315 |
const uint32_t byte_offset_in_vec = bsrw(nontoken_nybbles) / 4; |
| 3316 |
if (byte_offset_in_vec + remaining_byte_ct_underflow < kBytesPerVec) { |
| 3317 |
return nullptr; |
| 3318 |
} |
| 3319 |
return &(DowncastToXC(str_rev_viter)[byte_offset_in_vec]); |
| 3320 |
} |
| 3321 |
# endif |
| 3322 |
} |
| 3323 |
} |
| 3324 |
#endif // __LP64__ |
| 3325 |
|
| 3326 |
/* |
| 3327 |
void ReplaceAllInstances(char old_char, char new_char, uint32_t slen, char* dst) { |
| 3328 |
// probable todo: add SIMD version |
| 3329 |
while (1) { |
| 3330 |
char* strchr_result = memchr(dst, old_char, slen); |
| 3331 |
if (!strchr_result) { |
| 3332 |
return; |
| 3333 |
} |
| 3334 |
*strchr_result++ = new_char; |
| 3335 |
slen -= strchr_result - dst; |
| 3336 |
dst = strchr_result; |
| 3337 |
} |
| 3338 |
} |
| 3339 |
*/ |
| 3340 |
|
| 3341 |
void TabsToSpaces(char* ss_iter) { |
| 3342 |
while (1) { |
| 3343 |
ss_iter = strchr(ss_iter, '\t'); |
| 3344 |
if (!ss_iter) { |
| 3345 |
return; |
| 3346 |
} |
| 3347 |
*ss_iter++ = ' '; |
| 3348 |
} |
| 3349 |
} |
| 3350 |
|
| 3351 |
BoolErr ReplaceCharAdvChecked(char old_char, char new_char, char** str_ptr) { |
| 3352 |
char* token_end; |
| 3353 |
for (char* str_iter = *str_ptr; ; str_iter = &(token_end[1])) { |
| 3354 |
token_end = strchrnul2(str_iter, old_char, new_char); |
| 3355 |
if ((*token_end) != old_char) { |
| 3356 |
if (likely(!(*token_end))) { |
| 3357 |
*str_ptr = token_end; |
| 3358 |
return 0; |
| 3359 |
} |
| 3360 |
return 1; |
| 3361 |
} |
| 3362 |
*token_end = new_char; |
| 3363 |
} |
| 3364 |
} |
| 3365 |
|
| 3366 |
#ifdef __cplusplus |
| 3367 |
} // namespace plink2 |
| 3368 |
#endif |