| 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_base.h" |
| 19 |
|
| 20 |
#ifdef __cplusplus |
| 21 |
namespace plink2 { |
| 22 |
#endif |
| 23 |
|
| 24 |
const char kErrprintfFopen[] = "Error: Failed to open %s : %s.\n"; |
| 25 |
const char kErrprintfFread[] = "Error: %s read failure: %s.\n"; |
| 26 |
const char kErrprintfRewind[] = "Error: %s could not be scanned twice. (Process-substitution/named-pipe input is not permitted in this use case.)\n"; |
| 27 |
const char kErrstrNomem[] = "Error: Out of memory. The --memory flag may be helpful.\n"; |
| 28 |
const char kErrstrWrite[] = "Error: File write failure: %s.\n"; |
| 29 |
const char kErrprintfDecompress[] = "Error: %s decompression failure: %s.\n"; |
| 30 |
|
| 31 |
uint64_t g_failed_alloc_attempt_size = 0; |
| 32 |
|
| 33 |
#if (((__GNUC__ == 4) && (__GNUC_MINOR__ < 7)) || (__GNUC__ >= 11)) && !defined(__APPLE__) |
| 34 |
BoolErr pgl_malloc(uintptr_t size, void* pp) { |
| 35 |
*S_CAST(unsigned char**, pp) = S_CAST(unsigned char*, malloc(size)); |
| 36 |
if (likely(*S_CAST(unsigned char**, pp))) { |
| 37 |
return 0; |
| 38 |
} |
| 39 |
g_failed_alloc_attempt_size = size; |
| 40 |
return 1; |
| 41 |
} |
| 42 |
#endif |
| 43 |
|
| 44 |
BoolErr fwrite_checked(const void* buf, uintptr_t len, FILE* outfile) { |
| 45 |
while (len > kMaxBytesPerIO) { |
| 46 |
// OS X fwrite() doesn't support 2GiB+ writes |
| 47 |
// typical disk block size is 4kb, so 0x7ffff000 is the largest sensible |
| 48 |
// write size |
| 49 |
// bugfix (9 Mar 2018): forgot a 'not' here... |
| 50 |
if (unlikely(!fwrite_unlocked(buf, kMaxBytesPerIO, 1, outfile))) { |
| 51 |
return 1; |
| 52 |
} |
| 53 |
buf = &(S_CAST(const unsigned char*, buf)[kMaxBytesPerIO]); |
| 54 |
len -= kMaxBytesPerIO; |
| 55 |
} |
| 56 |
uintptr_t written_byte_ct = fwrite_unlocked(buf, 1, len, outfile); |
| 57 |
// must do the right thing when len == 0 |
| 58 |
return (written_byte_ct != len); |
| 59 |
} |
| 60 |
|
| 61 |
/* |
| 62 |
IntErr fread_checked2(void* buf, uintptr_t len, FILE* infile, uintptr_t* bytes_read_ptr) { |
| 63 |
uintptr_t bytes_read = 0; |
| 64 |
while (len > kMaxBytesPerIO) { |
| 65 |
const uintptr_t cur_bytes_read = fread_unlocked(buf, 1, kMaxBytesPerIO, infile); |
| 66 |
bytes_read += cur_bytes_read; |
| 67 |
if (cur_bytes_read != kMaxBytesPerIO) { |
| 68 |
*bytes_read_ptr = bytes_read; |
| 69 |
return ferror_unlocked(infile); |
| 70 |
} |
| 71 |
buf = &(((char*)buf)[kMaxBytesPerIO]); |
| 72 |
len -= kMaxBytesPerIO; |
| 73 |
} |
| 74 |
bytes_read += fread_unlocked(buf, 1, len, infile); |
| 75 |
*bytes_read_ptr = bytes_read; |
| 76 |
// could skip ferror_unlocked call if bytes_read == original len |
| 77 |
return ferror_unlocked(infile); |
| 78 |
} |
| 79 |
*/ |
| 80 |
|
| 81 |
BoolErr fread_checked(void* buf, uintptr_t len, FILE* infile) { |
| 82 |
while (len > kMaxBytesPerIO) { |
| 83 |
const uintptr_t cur_bytes_read = fread_unlocked(buf, 1, kMaxBytesPerIO, infile); |
| 84 |
if (unlikely(cur_bytes_read != kMaxBytesPerIO)) { |
| 85 |
return 1; |
| 86 |
} |
| 87 |
buf = &(S_CAST(unsigned char*, buf)[kMaxBytesPerIO]); |
| 88 |
len -= kMaxBytesPerIO; |
| 89 |
} |
| 90 |
const uintptr_t cur_bytes_read = fread_unlocked(buf, 1, len, infile); |
| 91 |
return (cur_bytes_read != len); |
| 92 |
} |
| 93 |
|
| 94 |
#ifdef __LP64__ |
| 95 |
static inline BoolErr ScanUintCappedFinish(const char* str_iter, uint64_t cap, uint32_t* valp) { |
| 96 |
uint64_t val = *valp; |
| 97 |
while (1) { |
| 98 |
// a little bit of unrolling seems to help |
| 99 |
const uint64_t cur_digit = ctou64(*str_iter++) - 48; |
| 100 |
if (cur_digit >= 10) { |
| 101 |
break; |
| 102 |
} |
| 103 |
// val = val * 10 + cur_digit; |
| 104 |
const uint64_t cur_digit2 = ctou64(*str_iter++) - 48; |
| 105 |
if (cur_digit2 >= 10) { |
| 106 |
val = val * 10 + cur_digit; |
| 107 |
if (unlikely(val > cap)) { |
| 108 |
return 1; |
| 109 |
} |
| 110 |
break; |
| 111 |
} |
| 112 |
val = val * 100 + cur_digit * 10 + cur_digit2; |
| 113 |
if (unlikely(val > cap)) { |
| 114 |
return 1; |
| 115 |
} |
| 116 |
} |
| 117 |
*valp = val; |
| 118 |
return 0; |
| 119 |
} |
| 120 |
|
| 121 |
BoolErr ScanPosintCapped(const char* str_iter, uint64_t cap, uint32_t* valp) { |
| 122 |
// '0' has ascii code 48 |
| 123 |
assert(ctou32(str_iter[0]) > 32); |
| 124 |
*valp = ctou32(*str_iter++) - 48; |
| 125 |
if (*valp >= 10) { |
| 126 |
// permit leading '+' (ascii 43), but not '++' or '+-' |
| 127 |
// reasonable to use unlikely() here since these functions aren't used for |
| 128 |
// numeric vs. non-numeric classification anyway due to erroring out on |
| 129 |
// overflow |
| 130 |
if (unlikely(*valp != 0xfffffffbU)) { |
| 131 |
return 1; |
| 132 |
} |
| 133 |
*valp = ctou32(*str_iter++) - 48; |
| 134 |
if (unlikely(*valp >= 10)) { |
| 135 |
return 1; |
| 136 |
} |
| 137 |
} |
| 138 |
while (!(*valp)) { |
| 139 |
*valp = ctou32(*str_iter++) - 48; |
| 140 |
if (unlikely((*valp) >= 10)) { |
| 141 |
return 1; |
| 142 |
} |
| 143 |
} |
| 144 |
return ScanUintCappedFinish(str_iter, cap, valp); |
| 145 |
} |
| 146 |
|
| 147 |
// Note that NumericRangeListToBitarr() can call this in an ignore-overflow |
| 148 |
// mode. If similar logic ever goes into an inner loop, remove all unlikely() |
| 149 |
// annotations in this function and its children. |
| 150 |
BoolErr ScanUintCapped(const char* str_iter, uint64_t cap, uint32_t* valp) { |
| 151 |
// Reads an integer in [0, cap]. Assumes first character is nonspace. |
| 152 |
assert(ctou32(str_iter[0]) > 32); |
| 153 |
uint32_t val = ctou32(*str_iter++) - 48; |
| 154 |
if (val >= 10) { |
| 155 |
if (val != 0xfffffffbU) { |
| 156 |
// '-' has ascii code 45, so unsigned 45 - 48 = 0xfffffffdU |
| 157 |
if (unlikely((val != 0xfffffffdU) || (*str_iter != '0'))) { |
| 158 |
return 1; |
| 159 |
} |
| 160 |
// accept "-0", "-00", etc. |
| 161 |
while (*(++str_iter) == '0'); |
| 162 |
*valp = 0; |
| 163 |
return (ctou32(*str_iter) - 48) < 10; |
| 164 |
} |
| 165 |
// accept leading '+' |
| 166 |
val = ctou32(*str_iter++) - 48; |
| 167 |
if (unlikely(val >= 10)) { |
| 168 |
return 1; |
| 169 |
} |
| 170 |
} |
| 171 |
*valp = val; |
| 172 |
return ScanUintCappedFinish(str_iter, cap, valp); |
| 173 |
} |
| 174 |
|
| 175 |
BoolErr ScanIntAbsBounded(const char* str_iter, uint64_t bound, int32_t* valp) { |
| 176 |
// Reads an integer in [-bound, bound]. Assumes first character is nonspace. |
| 177 |
assert(ctou32(str_iter[0]) > 32); |
| 178 |
*valp = ctou32(*str_iter++) - 48; |
| 179 |
int32_t sign = 1; |
| 180 |
if (ctou32(*valp) >= 10) { |
| 181 |
if (*valp == -3) { |
| 182 |
sign = -1; |
| 183 |
} else if (unlikely(*valp != -5)) { |
| 184 |
return 1; |
| 185 |
} |
| 186 |
*valp = ctou32(*str_iter++) - 48; |
| 187 |
if (unlikely(*valp >= 10)) { |
| 188 |
return 1; |
| 189 |
} |
| 190 |
} |
| 191 |
if (unlikely(ScanUintCappedFinish(str_iter, bound, I32ToU32(valp)))) { |
| 192 |
return 1; |
| 193 |
} |
| 194 |
*valp *= sign; |
| 195 |
return 0; |
| 196 |
} |
| 197 |
#else // not __LP64__ |
| 198 |
BoolErr ScanPosintCapped32(const char* str_iter, uint32_t cap_div_10, uint32_t cap_mod_10, uint32_t* valp) { |
| 199 |
// '0' has ascii code 48 |
| 200 |
assert(ctou32(str_iter[0]) > 32); |
| 201 |
uint32_t val = ctou32(*str_iter++) - 48; |
| 202 |
if (val >= 10) { |
| 203 |
if (unlikely(val != 0xfffffffbU)) { |
| 204 |
return 1; |
| 205 |
} |
| 206 |
val = ctou32(*str_iter++) - 48; |
| 207 |
if (unlikely(val >= 10)) { |
| 208 |
return 1; |
| 209 |
} |
| 210 |
} |
| 211 |
while (!val) { |
| 212 |
val = ctou32(*str_iter++) - 48; |
| 213 |
if (unlikely(val >= 10)) { |
| 214 |
return 1; |
| 215 |
} |
| 216 |
} |
| 217 |
for (; ; ++str_iter) { |
| 218 |
const uint32_t cur_digit = ctou32(*str_iter) - 48; |
| 219 |
if (cur_digit >= 10) { |
| 220 |
*valp = val; |
| 221 |
return 0; |
| 222 |
} |
| 223 |
// avoid integer overflow in middle of computation |
| 224 |
if (unlikely((val >= cap_div_10) && ((val > cap_div_10) || (cur_digit > cap_mod_10)))) { |
| 225 |
return 1; |
| 226 |
} |
| 227 |
val = val * 10 + cur_digit; |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
BoolErr ScanUintCapped32(const char* str_iter, uint32_t cap_div_10, uint32_t cap_mod_10, uint32_t* valp) { |
| 232 |
// Reads an integer in [0, cap]. Assumes first character is nonspace. |
| 233 |
assert(ctou32(str_iter[0]) > 32); |
| 234 |
uint32_t val = ctou32(*str_iter++) - 48; |
| 235 |
if (val >= 10) { |
| 236 |
if (val != 0xfffffffbU) { |
| 237 |
if (unlikely((val != 0xfffffffdU) || (*str_iter != '0'))) { |
| 238 |
return 1; |
| 239 |
} |
| 240 |
while (*(++str_iter) == '0'); |
| 241 |
*valp = 0; |
| 242 |
return (ctou32(*str_iter) - 48) < 10; |
| 243 |
} |
| 244 |
val = ctou32(*str_iter++) - 48; |
| 245 |
if (unlikely(val >= 10)) { |
| 246 |
return 1; |
| 247 |
} |
| 248 |
} |
| 249 |
for (; ; ++str_iter) { |
| 250 |
const uint32_t cur_digit = ctou32(*str_iter) - 48; |
| 251 |
if (cur_digit >= 10) { |
| 252 |
*valp = val; |
| 253 |
return 0; |
| 254 |
} |
| 255 |
if (unlikely((val >= cap_div_10) && ((val > cap_div_10) || (cur_digit > cap_mod_10)))) { |
| 256 |
return 1; |
| 257 |
} |
| 258 |
val = val * 10 + cur_digit; |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
BoolErr ScanIntAbsBounded32(const char* str_iter, uint32_t bound_div_10, uint32_t bound_mod_10, int32_t* valp) { |
| 263 |
// Reads an integer in [-bound, bound]. Assumes first character is nonspace. |
| 264 |
assert(ctou32(str_iter[0]) > 32); |
| 265 |
uint32_t val = ctou32(*str_iter++) - 48; |
| 266 |
int32_t sign = 1; |
| 267 |
if (val >= 10) { |
| 268 |
if (val == 0xfffffffdU) { |
| 269 |
sign = -1; |
| 270 |
} else if (unlikely(val != 0xfffffffbU)) { |
| 271 |
return 1; |
| 272 |
} |
| 273 |
val = ctou32(*str_iter++) - 48; |
| 274 |
if (unlikely(val >= 10)) { |
| 275 |
return 1; |
| 276 |
} |
| 277 |
} |
| 278 |
for (; ; ++str_iter) { |
| 279 |
const uint32_t cur_digit = ctou32(*str_iter) - 48; |
| 280 |
if (cur_digit >= 10) { |
| 281 |
*valp = sign * S_CAST(int32_t, val); |
| 282 |
return 0; |
| 283 |
} |
| 284 |
if (unlikely((val >= bound_div_10) && ((val > bound_div_10) || (cur_digit > bound_mod_10)))) { |
| 285 |
return 1; |
| 286 |
} |
| 287 |
val = val * 10 + cur_digit; |
| 288 |
} |
| 289 |
} |
| 290 |
#endif |
| 291 |
|
| 292 |
BoolErr aligned_malloc(uintptr_t size, uintptr_t alignment, void* aligned_pp) { |
| 293 |
// Assumes malloc returns word-aligned addresses. |
| 294 |
assert(alignment); |
| 295 |
assert(!(alignment % kBytesPerWord)); |
| 296 |
uintptr_t malloc_addr; |
| 297 |
if (unlikely(pgl_malloc(size + alignment, &malloc_addr))) { |
| 298 |
return 1; |
| 299 |
} |
| 300 |
assert(!(malloc_addr % kBytesPerWord)); |
| 301 |
uintptr_t** casted_aligned_pp = S_CAST(uintptr_t**, aligned_pp); |
| 302 |
*casted_aligned_pp = R_CAST(uintptr_t*, RoundDownPow2(malloc_addr + alignment, alignment)); |
| 303 |
(*casted_aligned_pp)[-1] = malloc_addr; |
| 304 |
return 0; |
| 305 |
} |
| 306 |
|
| 307 |
#if defined(USE_SSE2) && !defined(NO_UNALIGNED) |
| 308 |
int32_t memequal(const void* m1, const void* m2, uintptr_t byte_ct) { |
| 309 |
const unsigned char* m1_uc = S_CAST(const unsigned char*, m1); |
| 310 |
const unsigned char* m2_uc = S_CAST(const unsigned char*, m2); |
| 311 |
if (byte_ct < 16 + (kBytesPerVec / 2)) { |
| 312 |
if (byte_ct < kBytesPerWord) { |
| 313 |
if (byte_ct < 4) { |
| 314 |
if (byte_ct < 2) { |
| 315 |
return (!byte_ct) || (m1_uc[0] == m2_uc[0]); |
| 316 |
} |
| 317 |
if ((*S_CAST(const uint16_t*, m1)) != (*S_CAST(const uint16_t*, m2))) { |
| 318 |
return 0; |
| 319 |
} |
| 320 |
if ((byte_ct == 3) && (m1_uc[2] != m2_uc[2])) { |
| 321 |
return 0; |
| 322 |
} |
| 323 |
return 1; |
| 324 |
} |
| 325 |
if ((*R_CAST(const uint32_t*, m1_uc)) != (*R_CAST(const uint32_t*, m2_uc))) { |
| 326 |
return 0; |
| 327 |
} |
| 328 |
if (byte_ct > 4) { |
| 329 |
const uintptr_t final_offset = byte_ct - 4; |
| 330 |
if ((*R_CAST(const uint32_t*, &(m1_uc[final_offset]))) != (*R_CAST(const uint32_t*, &(m2_uc[final_offset])))) { |
| 331 |
return 0; |
| 332 |
} |
| 333 |
} |
| 334 |
return 1; |
| 335 |
} |
| 336 |
const uintptr_t* m1_alias = R_CAST(const uintptr_t*, m1_uc); |
| 337 |
const uintptr_t* m2_alias = R_CAST(const uintptr_t*, m2_uc); |
| 338 |
if (m1_alias[0] != m2_alias[0]) { |
| 339 |
return 0; |
| 340 |
} |
| 341 |
if (byte_ct >= 16) { |
| 342 |
if (m1_alias[1] != m2_alias[1]) { |
| 343 |
return 0; |
| 344 |
} |
| 345 |
# ifdef USE_AVX2 |
| 346 |
if (byte_ct >= 24) { |
| 347 |
if (m1_alias[2] != m2_alias[2]) { |
| 348 |
return 0; |
| 349 |
} |
| 350 |
} |
| 351 |
# endif |
| 352 |
} |
| 353 |
if (byte_ct % kBytesPerWord) { |
| 354 |
const uintptr_t final_offset = byte_ct - kBytesPerWord; |
| 355 |
if ((*R_CAST(const uintptr_t*, &(m1_uc[final_offset]))) != (*R_CAST(const uintptr_t*, &(m2_uc[final_offset])))) { |
| 356 |
return 0; |
| 357 |
} |
| 358 |
} |
| 359 |
return 1; |
| 360 |
} |
| 361 |
// Don't use VecW since _mm_cmpeq_epi64() not defined until SSE4.1. |
| 362 |
const VecUc* m1_alias = S_CAST(const VecUc*, m1); |
| 363 |
const VecUc* m2_alias = S_CAST(const VecUc*, m2); |
| 364 |
const uintptr_t vec_ct = byte_ct / kBytesPerVec; |
| 365 |
for (uintptr_t vidx = 0; vidx != vec_ct; ++vidx) { |
| 366 |
// tried unrolling this, doesn't make a difference |
| 367 |
const VecUc v1 = vecuc_loadu(&(m1_alias[vidx])); |
| 368 |
const VecUc v2 = vecuc_loadu(&(m2_alias[vidx])); |
| 369 |
if (!vecucs_are_equal(v1, v2)) { |
| 370 |
return 0; |
| 371 |
} |
| 372 |
} |
| 373 |
if (byte_ct % kBytesPerVec) { |
| 374 |
// put this last instead of first, for better behavior when inputs are |
| 375 |
// aligned |
| 376 |
const uintptr_t final_offset = byte_ct - kBytesPerVec; |
| 377 |
const VecUc v1 = vecuc_loadu(&(m1_uc[final_offset])); |
| 378 |
const VecUc v2 = vecuc_loadu(&(m2_uc[final_offset])); |
| 379 |
if (!vecucs_are_equal(v1, v2)) { |
| 380 |
return 0; |
| 381 |
} |
| 382 |
} |
| 383 |
return 1; |
| 384 |
} |
| 385 |
|
| 386 |
// clang/gcc memcmp is not that well-optimized for the short strings we usually |
| 387 |
// compare. |
| 388 |
int32_t Memcmp(const void* m1, const void* m2, uintptr_t byte_ct) { |
| 389 |
const unsigned char* m1_uc = S_CAST(const unsigned char*, m1); |
| 390 |
const unsigned char* m2_uc = S_CAST(const unsigned char*, m2); |
| 391 |
// tried larger crossover threshold, doesn't help |
| 392 |
if (byte_ct < kBytesPerVec) { |
| 393 |
if (byte_ct < kBytesPerWord) { |
| 394 |
if (byte_ct < 4) { |
| 395 |
for (uintptr_t pos = 0; pos != byte_ct; ++pos) { |
| 396 |
const unsigned char ucc1 = m1_uc[pos]; |
| 397 |
const unsigned char ucc2 = m2_uc[pos]; |
| 398 |
if (ucc1 != ucc2) { |
| 399 |
return (ucc1 < ucc2)? -1 : 1; |
| 400 |
} |
| 401 |
} |
| 402 |
return 0; |
| 403 |
} |
| 404 |
uint32_t m1_u32 = *S_CAST(const uint32_t*, m1); |
| 405 |
uint32_t m2_u32 = *S_CAST(const uint32_t*, m2); |
| 406 |
if (m1_u32 != m2_u32) { |
| 407 |
return (__builtin_bswap32(m1_u32) < __builtin_bswap32(m2_u32))? -1 : 1; |
| 408 |
} |
| 409 |
if (byte_ct > 4) { |
| 410 |
const uintptr_t final_offset = byte_ct - 4; |
| 411 |
m1_u32 = *R_CAST(const uint32_t*, &(m1_uc[final_offset])); |
| 412 |
m2_u32 = *R_CAST(const uint32_t*, &(m2_uc[final_offset])); |
| 413 |
if (m1_u32 != m2_u32) { |
| 414 |
return (__builtin_bswap32(m1_u32) < __builtin_bswap32(m2_u32))? -1 : 1; |
| 415 |
} |
| 416 |
} |
| 417 |
return 0; |
| 418 |
} |
| 419 |
const uintptr_t* m1_alias = R_CAST(const uintptr_t*, m1_uc); |
| 420 |
const uintptr_t* m2_alias = R_CAST(const uintptr_t*, m2_uc); |
| 421 |
uintptr_t m1_word = m1_alias[0]; |
| 422 |
uintptr_t m2_word = m2_alias[0]; |
| 423 |
if (m1_word != m2_word) { |
| 424 |
return (__builtin_bswap64(m1_word) < __builtin_bswap64(m2_word))? -1 : 1; |
| 425 |
} |
| 426 |
# ifdef USE_AVX2 |
| 427 |
if (byte_ct >= 16) { |
| 428 |
m1_word = m1_alias[1]; |
| 429 |
m2_word = m2_alias[1]; |
| 430 |
if (m1_word != m2_word) { |
| 431 |
return (__builtin_bswap64(m1_word) < __builtin_bswap64(m2_word))? -1 : 1; |
| 432 |
} |
| 433 |
if (byte_ct >= 24) { |
| 434 |
m1_word = m1_alias[2]; |
| 435 |
m2_word = m2_alias[2]; |
| 436 |
if (m1_word != m2_word) { |
| 437 |
return (__builtin_bswap64(m1_word) < __builtin_bswap64(m2_word))? -1 : 1; |
| 438 |
} |
| 439 |
} |
| 440 |
} |
| 441 |
# endif |
| 442 |
if (byte_ct % kBytesPerWord) { |
| 443 |
const uintptr_t final_offset = byte_ct - kBytesPerWord; |
| 444 |
m1_word = *R_CAST(const uintptr_t*, &(m1_uc[final_offset])); |
| 445 |
m2_word = *R_CAST(const uintptr_t*, &(m2_uc[final_offset])); |
| 446 |
if (m1_word != m2_word) { |
| 447 |
return (__builtin_bswap64(m1_word) < __builtin_bswap64(m2_word))? -1 : 1; |
| 448 |
} |
| 449 |
} |
| 450 |
return 0; |
| 451 |
} |
| 452 |
const VecUc* m1_alias = S_CAST(const VecUc*, m1); |
| 453 |
const VecUc* m2_alias = S_CAST(const VecUc*, m2); |
| 454 |
const uintptr_t fullvec_ct = byte_ct / kBytesPerVec; |
| 455 |
// uh, clang/LLVM -O2 optimizes this better when comparison is != instead of |
| 456 |
// <? ugh, time to change all of the for loops... |
| 457 |
// (and yes, both -O3 configurations generate worse code here) |
| 458 |
// at least for loop is better than do-while loop even when 1 iteration is |
| 459 |
// guaranteed... |
| 460 |
for (uintptr_t vidx = 0; vidx != fullvec_ct; ++vidx) { |
| 461 |
const VecUc v1 = vecuc_loadu(&(m1_alias[vidx])); |
| 462 |
const VecUc v2 = vecuc_loadu(&(m2_alias[vidx])); |
| 463 |
# ifndef SIMDE_ARM_NEON_A32V8_NATIVE |
| 464 |
// is this even worthwhile now in non-AVX2 case? |
| 465 |
const uint32_t movemask_result = vecuc_movemask(v1 == v2); |
| 466 |
if (movemask_result != kVec8thUintMax) { |
| 467 |
// todo: check if this is faster to do outside of vector-space |
| 468 |
const uintptr_t diff_pos = vidx * kBytesPerVec + ctzu32(~movemask_result); |
| 469 |
return (m1_uc[diff_pos] < m2_uc[diff_pos])? -1 : 1; |
| 470 |
} |
| 471 |
# else |
| 472 |
const uint64_t eq_nybbles = arm_shrn4_uc(v1 == v2); |
| 473 |
if (eq_nybbles != UINT64_MAX) { |
| 474 |
const uintptr_t diff_pos = vidx * kBytesPerVec + ctzw(~eq_nybbles) / 4; |
| 475 |
return (m1_uc[diff_pos] < m2_uc[diff_pos])? -1 : 1; |
| 476 |
} |
| 477 |
# endif |
| 478 |
} |
| 479 |
if (byte_ct % kBytesPerVec) { |
| 480 |
const uintptr_t final_offset = byte_ct - kBytesPerVec; |
| 481 |
const VecUc v1 = vecuc_loadu(&(m1_uc[final_offset])); |
| 482 |
const VecUc v2 = vecuc_loadu(&(m2_uc[final_offset])); |
| 483 |
# ifndef SIMDE_ARM_NEON_A32V8_NATIVE |
| 484 |
const uint32_t movemask_result = vecuc_movemask(v1 == v2); |
| 485 |
if (movemask_result != kVec8thUintMax) { |
| 486 |
const uintptr_t diff_pos = final_offset + ctzu32(~movemask_result); |
| 487 |
return (m1_uc[diff_pos] < m2_uc[diff_pos])? -1 : 1; |
| 488 |
} |
| 489 |
# else |
| 490 |
const uint64_t eq_nybbles = arm_shrn4_uc(v1 == v2); |
| 491 |
if (eq_nybbles != UINT64_MAX) { |
| 492 |
const uintptr_t diff_pos = final_offset + ctzw(~eq_nybbles) / 4; |
| 493 |
return (m1_uc[diff_pos] < m2_uc[diff_pos])? -1 : 1; |
| 494 |
} |
| 495 |
# endif |
| 496 |
} |
| 497 |
return 0; |
| 498 |
} |
| 499 |
#endif // defined(USE_SSE2) && !defined(NO_UNALIGNED) |
| 500 |
|
| 501 |
const uint16_t kDigitPair[] = { |
| 502 |
0x3030, 0x3130, 0x3230, 0x3330, 0x3430, 0x3530, 0x3630, 0x3730, 0x3830, 0x3930, |
| 503 |
0x3031, 0x3131, 0x3231, 0x3331, 0x3431, 0x3531, 0x3631, 0x3731, 0x3831, 0x3931, |
| 504 |
0x3032, 0x3132, 0x3232, 0x3332, 0x3432, 0x3532, 0x3632, 0x3732, 0x3832, 0x3932, |
| 505 |
0x3033, 0x3133, 0x3233, 0x3333, 0x3433, 0x3533, 0x3633, 0x3733, 0x3833, 0x3933, |
| 506 |
0x3034, 0x3134, 0x3234, 0x3334, 0x3434, 0x3534, 0x3634, 0x3734, 0x3834, 0x3934, |
| 507 |
0x3035, 0x3135, 0x3235, 0x3335, 0x3435, 0x3535, 0x3635, 0x3735, 0x3835, 0x3935, |
| 508 |
0x3036, 0x3136, 0x3236, 0x3336, 0x3436, 0x3536, 0x3636, 0x3736, 0x3836, 0x3936, |
| 509 |
0x3037, 0x3137, 0x3237, 0x3337, 0x3437, 0x3537, 0x3637, 0x3737, 0x3837, 0x3937, |
| 510 |
0x3038, 0x3138, 0x3238, 0x3338, 0x3438, 0x3538, 0x3638, 0x3738, 0x3838, 0x3938, |
| 511 |
0x3039, 0x3139, 0x3239, 0x3339, 0x3439, 0x3539, 0x3639, 0x3739, 0x3839, 0x3939}; |
| 512 |
|
| 513 |
char* u32toa(uint32_t uii, char* start) { |
| 514 |
// Memory-efficient fast integer writer. (You can do a bit better sometimes |
| 515 |
// by using a larger lookup table, but on average I doubt that pays off.) |
| 516 |
// Returns a pointer to the end of the integer (not null-terminated). |
| 517 |
// |
| 518 |
// Nearly identical to 'branchlut' from |
| 519 |
// https://github.com/miloyip/itoa-benchmark , except that the hardcoded |
| 520 |
// binary search is more balanced (start by comparing 6+ digits vs. <6, |
| 521 |
// instead of 9+ digits vs. <8). This tends to be slightly better unless the |
| 522 |
// integers are almost uniformly distributed over [0, 2^32). |
| 523 |
// |
| 524 |
// Todo: compare against an_itoa in https://github.com/appnexus/acf/ . |
| 525 |
// |
| 526 |
// (Making the first comparison 7+ digits vs. <7 would seem to make sense, |
| 527 |
// but it seems to benchmark slightly worse on my Mac?) |
| 528 |
// |
| 529 |
// (Since we want to execute different code depending on the number of |
| 530 |
// digits, the UintSlen() approach doesn't pay off.) |
| 531 |
uint32_t quotient; |
| 532 |
if (uii < 100000) { |
| 533 |
if (uii < 100) { |
| 534 |
if (uii >= 10) { |
| 535 |
goto u32toa_just2; |
| 536 |
} |
| 537 |
*start++ = '0' + uii; |
| 538 |
return start; |
| 539 |
} |
| 540 |
if (uii < 10000) { |
| 541 |
if (uii >= 1000) { |
| 542 |
goto u32toa_just4; |
| 543 |
} |
| 544 |
quotient = uii / 100; |
| 545 |
*start++ = '0' + quotient; |
| 546 |
goto u32toa_2left; |
| 547 |
} |
| 548 |
quotient = uii / 10000; |
| 549 |
*start++ = '0' + quotient; |
| 550 |
goto u32toa_4left; |
| 551 |
} |
| 552 |
if (uii < 100000000) { |
| 553 |
if (uii < 1000000) { |
| 554 |
goto u32toa_just6; |
| 555 |
} |
| 556 |
if (uii >= 10000000) { |
| 557 |
goto u32toa_just8; |
| 558 |
} |
| 559 |
quotient = uii / 1000000; |
| 560 |
*start++ = '0' + quotient; |
| 561 |
goto u32toa_6left; |
| 562 |
} |
| 563 |
quotient = uii / 100000000; |
| 564 |
if (uii < 1000000000) { |
| 565 |
*start++ = '0' + quotient; |
| 566 |
} else { |
| 567 |
start = memcpya_k(start, &(kDigitPair[quotient]), 2); |
| 568 |
} |
| 569 |
uii -= quotient * 100000000; |
| 570 |
u32toa_just8: |
| 571 |
quotient = uii / 1000000; |
| 572 |
start = memcpya_k(start, &(kDigitPair[quotient]), 2); |
| 573 |
u32toa_6left: |
| 574 |
uii -= quotient * 1000000; |
| 575 |
u32toa_just6: |
| 576 |
quotient = uii / 10000; |
| 577 |
start = memcpya_k(start, &(kDigitPair[quotient]), 2); |
| 578 |
u32toa_4left: |
| 579 |
uii -= quotient * 10000; |
| 580 |
u32toa_just4: |
| 581 |
quotient = uii / 100; |
| 582 |
start = memcpya_k(start, &(kDigitPair[quotient]), 2); |
| 583 |
u32toa_2left: |
| 584 |
uii -= quotient * 100; |
| 585 |
u32toa_just2: |
| 586 |
return memcpya_k(start, &(kDigitPair[uii]), 2); |
| 587 |
} |
| 588 |
|
| 589 |
char* i64toa(int64_t llii, char* start) { |
| 590 |
uint64_t ullii = llii; |
| 591 |
uint64_t top_digits; |
| 592 |
uint32_t bottom_eight; |
| 593 |
uint32_t middle_eight; |
| 594 |
if (llii < 0) { |
| 595 |
*start++ = '-'; |
| 596 |
ullii = -ullii; |
| 597 |
} |
| 598 |
if (ullii <= 0xffffffffLLU) { |
| 599 |
return u32toa(S_CAST(uint32_t, ullii), start); |
| 600 |
} |
| 601 |
top_digits = ullii / 100000000; |
| 602 |
bottom_eight = S_CAST(uint32_t, ullii - (top_digits * 100000000)); |
| 603 |
if (top_digits <= 0xffffffffLLU) { |
| 604 |
start = u32toa(S_CAST(uint32_t, top_digits), start); |
| 605 |
return uitoa_z8(bottom_eight, start); |
| 606 |
} |
| 607 |
ullii = top_digits / 100000000; |
| 608 |
middle_eight = S_CAST(uint32_t, top_digits - (ullii * 100000000)); |
| 609 |
start = u32toa(S_CAST(uint32_t, ullii), start); |
| 610 |
start = uitoa_z8(middle_eight, start); |
| 611 |
return uitoa_z8(bottom_eight, start); |
| 612 |
} |
| 613 |
|
| 614 |
#if defined(USE_SSE2) && !defined(NO_UNALIGNED) |
| 615 |
uintptr_t FirstUnequal4(const void* arr1, const void* arr2, uintptr_t nbytes) { |
| 616 |
// Similar to memequal(). |
| 617 |
if (nbytes < kBytesPerVec) { |
| 618 |
if (nbytes < kBytesPerWord) { |
| 619 |
uint32_t xor_result = (*S_CAST(const uint32_t*, arr1)) ^ (*S_CAST(const uint32_t*, arr2)); |
| 620 |
if (xor_result) { |
| 621 |
return ctzu32(xor_result) / CHAR_BIT; |
| 622 |
} |
| 623 |
if (nbytes > 4) { |
| 624 |
const uintptr_t final_offset = nbytes - 4; |
| 625 |
const char* s1 = S_CAST(const char*, arr1); |
| 626 |
const char* s2 = S_CAST(const char*, arr2); |
| 627 |
xor_result = (*R_CAST(const uint32_t*, &(s1[final_offset]))) ^ (*R_CAST(const uint32_t*, &(s2[final_offset]))); |
| 628 |
if (xor_result) { |
| 629 |
return final_offset + ctzu32(xor_result) / CHAR_BIT; |
| 630 |
} |
| 631 |
} |
| 632 |
return nbytes; |
| 633 |
} |
| 634 |
const uintptr_t* arr1_alias = S_CAST(const uintptr_t*, arr1); |
| 635 |
const uintptr_t* arr2_alias = S_CAST(const uintptr_t*, arr2); |
| 636 |
const uintptr_t word_ct = nbytes / kBytesPerWord; |
| 637 |
for (uint32_t widx = 0; widx != word_ct; ++widx) { |
| 638 |
const uintptr_t xor_result = arr1_alias[widx] ^ arr2_alias[widx]; |
| 639 |
if (xor_result) { |
| 640 |
return widx * kBytesPerWord + ctzw(xor_result) / CHAR_BIT; |
| 641 |
} |
| 642 |
} |
| 643 |
if (nbytes % kBytesPerWord) { |
| 644 |
const uintptr_t final_offset = nbytes - kBytesPerWord; |
| 645 |
const char* s1 = S_CAST(const char*, arr1); |
| 646 |
const char* s2 = S_CAST(const char*, arr2); |
| 647 |
const uintptr_t xor_result = (*R_CAST(const uintptr_t*, &(s1[final_offset]))) ^ (*R_CAST(const uintptr_t*, &(s2[final_offset]))); |
| 648 |
if (xor_result) { |
| 649 |
return final_offset + ctzw(xor_result) / CHAR_BIT; |
| 650 |
} |
| 651 |
} |
| 652 |
return nbytes; |
| 653 |
} |
| 654 |
const VecUc* arr1_alias = S_CAST(const VecUc*, arr1); |
| 655 |
const VecUc* arr2_alias = S_CAST(const VecUc*, arr2); |
| 656 |
const uintptr_t vec_ct = nbytes / kBytesPerVec; |
| 657 |
for (uintptr_t vidx = 0; vidx != vec_ct; ++vidx) { |
| 658 |
const VecUc v1 = vecuc_loadu(&(arr1_alias[vidx])); |
| 659 |
const VecUc v2 = vecuc_loadu(&(arr2_alias[vidx])); |
| 660 |
# ifndef SIMDE_ARM_NEON_A32V8_NATIVE |
| 661 |
const uint32_t eq_result = vecw_movemask(v1 == v2); |
| 662 |
if (eq_result != kVec8thUintMax) { |
| 663 |
return vidx * kBytesPerVec + ctzu32(~eq_result); |
| 664 |
} |
| 665 |
# else |
| 666 |
const uint64_t eq_nybbles = arm_shrn4_uc(v1 == v2); |
| 667 |
if (eq_nybbles != UINT64_MAX) { |
| 668 |
return vidx * kBytesPerVec + ctzw(~eq_nybbles) / 4; |
| 669 |
} |
| 670 |
# endif |
| 671 |
} |
| 672 |
if (nbytes % kBytesPerVec) { |
| 673 |
const uintptr_t final_offset = nbytes - kBytesPerVec; |
| 674 |
const char* s1 = S_CAST(const char*, arr1); |
| 675 |
const char* s2 = S_CAST(const char*, arr2); |
| 676 |
const VecW v1 = vecw_loadu(&(s1[final_offset])); |
| 677 |
const VecW v2 = vecw_loadu(&(s2[final_offset])); |
| 678 |
# ifndef SIMDE_ARM_NEON_A32V8_NATIVE |
| 679 |
const uint32_t eq_result = vecw_movemask(v1 == v2); |
| 680 |
if (eq_result != kVec8thUintMax) { |
| 681 |
return final_offset + ctzu32(~eq_result); |
| 682 |
} |
| 683 |
# else |
| 684 |
const uint64_t eq_nybbles = arm_shrn4_uc(v1 == v2); |
| 685 |
if (eq_nybbles != UINT64_MAX) { |
| 686 |
return final_offset + ctzw(~eq_nybbles) / 4; |
| 687 |
} |
| 688 |
# endif |
| 689 |
} |
| 690 |
return nbytes; |
| 691 |
} |
| 692 |
#else // !(defined(USE_SSE2) && !defined(NO_UNALIGNED)) |
| 693 |
uintptr_t FirstUnequalW(const void* arr1, const void* arr2, uintptr_t nbytes) { |
| 694 |
const unsigned char* arr1b = S_CAST(const unsigned char*, arr1); |
| 695 |
const unsigned char* arr2b = S_CAST(const unsigned char*, arr2); |
| 696 |
const uintptr_t word_ct = nbytes / kBytesPerWord; |
| 697 |
for (uintptr_t widx = 0; widx != word_ct; ++widx) { |
| 698 |
uintptr_t arr1_word; |
| 699 |
uintptr_t arr2_word; |
| 700 |
CopyFromUnalignedOffsetW(&arr1_word, arr1b, widx); |
| 701 |
CopyFromUnalignedOffsetW(&arr2_word, arr2b, widx); |
| 702 |
const uintptr_t xor_result = arr1_word ^ arr2_word; |
| 703 |
if (xor_result) { |
| 704 |
return widx * kBytesPerWord + ctzw(xor_result) / CHAR_BIT; |
| 705 |
} |
| 706 |
} |
| 707 |
if (nbytes % kBytesPerWord) { |
| 708 |
const uintptr_t final_offset = nbytes - kBytesPerWord; |
| 709 |
uintptr_t arr1_word; |
| 710 |
uintptr_t arr2_word; |
| 711 |
CopyFromUnalignedW(&arr1_word, &(arr1b[final_offset])); |
| 712 |
CopyFromUnalignedW(&arr2_word, &(arr2b[final_offset])); |
| 713 |
const uintptr_t xor_result = arr1_word ^ arr2_word; |
| 714 |
if (xor_result) { |
| 715 |
return final_offset + ctzw(xor_result) / CHAR_BIT; |
| 716 |
} |
| 717 |
} |
| 718 |
return nbytes; |
| 719 |
} |
| 720 |
#endif |
| 721 |
|
| 722 |
#ifdef __LP64__ |
| 723 |
uintptr_t CountVintsNonempty(const unsigned char* buf, const unsigned char* buf_end) { |
| 724 |
const uintptr_t starting_addr = R_CAST(uintptr_t, buf); |
| 725 |
const VecUc* buf_viter = R_CAST(const VecUc*, RoundDownPow2(starting_addr, kBytesPerVec)); |
| 726 |
const uintptr_t ending_addr = R_CAST(uintptr_t, buf_end); |
| 727 |
const VecUc* buf_vlast = R_CAST(const VecUc*, RoundDownPow2(ending_addr - 1, kBytesPerVec)); |
| 728 |
const uint32_t leading_byte_ct = starting_addr - R_CAST(uintptr_t, buf_viter); |
| 729 |
// todo: better ARM implementation |
| 730 |
Vec8thUint vint_ends = (UINT32_MAX << leading_byte_ct) & (~vecuc_movemask(*buf_viter)); |
| 731 |
uintptr_t total = 0; |
| 732 |
while (buf_viter != buf_vlast) { |
| 733 |
total += PopcountVec8thUint(vint_ends); |
| 734 |
++buf_viter; |
| 735 |
vint_ends = ~vecuc_movemask(*buf_viter); |
| 736 |
} |
| 737 |
const uint32_t trailing_byte_ct = ending_addr - R_CAST(uintptr_t, buf_vlast); |
| 738 |
vint_ends &= (k1LU << trailing_byte_ct) - 1; |
| 739 |
total += PopcountVec8thUint(vint_ends); |
| 740 |
return total; |
| 741 |
} |
| 742 |
#else |
| 743 |
uintptr_t CountVints(const unsigned char* buf, const unsigned char* buf_end) { |
| 744 |
// Could check one word at a time. |
| 745 |
const uintptr_t len = buf_end - buf; |
| 746 |
uintptr_t inv_result = 0; |
| 747 |
for (uintptr_t ulii = 0; ulii != len; ++ulii) { |
| 748 |
inv_result += buf[ulii] >> 7; |
| 749 |
} |
| 750 |
return len - inv_result; |
| 751 |
} |
| 752 |
#endif |
| 753 |
|
| 754 |
#ifdef __cplusplus |
| 755 |
} // namespace plink2 |
| 756 |
#endif |