[upstream_utils] Upgrade to LLVM 19.1.6 (#7101)

This commit is contained in:
Tyler Veness
2024-12-24 17:40:31 -08:00
committed by GitHub
parent b670a59b5b
commit 939a9ceee1
70 changed files with 2127 additions and 799 deletions

View File

@@ -95,7 +95,8 @@ void wpi::report_fatal_error(std::string_view Reason, bool GenCrashDiag) {
void wpi::install_bad_alloc_error_handler(fatal_error_handler_t handler,
void *user_data) {
std::scoped_lock Lock(BadAllocErrorHandlerMutex);
assert(!ErrorHandler && "Bad alloc error handler already registered!\n");
assert(!BadAllocErrorHandler &&
"Bad alloc error handler already registered!\n");
BadAllocErrorHandler = handler;
BadAllocErrorHandlerUserData = user_data;
}
@@ -174,8 +175,20 @@ void wpi::wpi_unreachable_internal(const char *msg, const char *file,
#ifdef _WIN32
#define WIN32_NO_STATUS
#include "Windows/WindowsSupport.h"
#undef WIN32_NO_STATUS
#include <ntstatus.h>
#include <winerror.h>
// This function obtains the last error code and maps it. It may call
// RtlGetLastNtStatus, which is a lower level API that can return a
// more specific error code than GetLastError.
std::error_code wpi::mapLastWindowsError() {
unsigned EV = ::GetLastError();
return mapWindowsError(EV);
}
// I'd rather not double the line count of the following.
#define MAP_ERR_TO_COND(x, y) \
case x: \

View File

@@ -1,28 +0,0 @@
//===-------------- lib/Support/Hashing.cpp -------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file provides implementation bits for the LLVM common hashing
// infrastructure. Documentation and most of the other information is in the
// header file.
//
//===----------------------------------------------------------------------===//
#include "wpi/Hashing.h"
using namespace wpi;
// Provide a definition and static initializer for the fixed seed. This
// initializer should always be zero to ensure its value can never appear to be
// non-zero, even during dynamic initialization.
uint64_t wpi::hashing::detail::fixed_seed_override = 0;
// Implement the function for forced setting of the fixed seed.
// FIXME: Use atomic operations here so that there is no data race.
void wpi::set_fixed_execution_hash_seed(uint64_t fixed_value) {
hashing::detail::fixed_seed_override = fixed_value;
}

View File

@@ -172,7 +172,7 @@ void raw_ostream::flush_nonempty() {
assert(OutBufCur > OutBufStart && "Invalid call to flush_nonempty.");
size_t Length = OutBufCur - OutBufStart;
OutBufCur = OutBufStart;
flush_tied_then_write(OutBufStart, Length);
write_impl(OutBufStart, Length);
}
raw_ostream &raw_ostream::write(unsigned char C) {
@@ -180,7 +180,7 @@ raw_ostream &raw_ostream::write(unsigned char C) {
if (LLVM_UNLIKELY(OutBufCur >= OutBufEnd)) {
if (LLVM_UNLIKELY(!OutBufStart)) {
if (BufferMode == BufferKind::Unbuffered) {
flush_tied_then_write(reinterpret_cast<char *>(&C), 1);
write_impl(reinterpret_cast<char *>(&C), 1);
return *this;
}
// Set up a buffer and start over.
@@ -200,7 +200,7 @@ raw_ostream &raw_ostream::write(const char *Ptr, size_t Size) {
if (LLVM_UNLIKELY(size_t(OutBufEnd - OutBufCur) < Size)) {
if (LLVM_UNLIKELY(!OutBufStart)) {
if (BufferMode == BufferKind::Unbuffered) {
flush_tied_then_write(Ptr, Size);
write_impl(Ptr, Size);
return *this;
}
// Set up a buffer and start over.
@@ -216,7 +216,7 @@ raw_ostream &raw_ostream::write(const char *Ptr, size_t Size) {
if (LLVM_UNLIKELY(OutBufCur == OutBufStart)) {
assert(NumBytes != 0 && "undefined behavior");
size_t BytesToWrite = Size - (Size % NumBytes);
flush_tied_then_write(Ptr, BytesToWrite);
write_impl(Ptr, BytesToWrite);
size_t BytesRemaining = Size - BytesToWrite;
if (BytesRemaining > size_t(OutBufEnd - OutBufCur)) {
// Too much left over to copy into our buffer.
@@ -257,12 +257,6 @@ void raw_ostream::copy_to_buffer(const char *Ptr, size_t Size) {
OutBufCur += Size;
}
void raw_ostream::flush_tied_then_write(const char *Ptr, size_t Size) {
if (TiedStream)
TiedStream->flush();
write_impl(Ptr, Size);
}
template <char C>
static raw_ostream &write_padding(raw_ostream &OS, unsigned NumChars) {
static const char Chars[] = {C, C, C, C, C, C, C, C, C, C, C, C, C, C, C, C,
@@ -471,6 +465,9 @@ static bool write_console_impl(int FD, std::string_view Data) {
#endif
void raw_fd_ostream::write_impl(const char *Ptr, size_t Size) {
if (TiedStream)
TiedStream->flush();
assert(FD >= 0 && "File already closed.");
pos += Size;
@@ -663,6 +660,10 @@ void raw_svector_ostream::pwrite_impl(const char *Ptr, size_t Size,
memcpy(OS.data() + Offset, Ptr, Size);
}
bool raw_svector_ostream::classof(const raw_ostream *OS) {
return OS->get_kind() == OStreamKind::OK_SVecStream;
}
//===----------------------------------------------------------------------===//
// raw_vector_ostream
//===----------------------------------------------------------------------===//

View File

@@ -1,36 +1,36 @@
/*
* xxHash - Fast Hash algorithm
* Copyright (C) 2012-2021, Yann Collet
*
* BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* You can contact the author at :
* - xxHash homepage: http://www.xxhash.com
* - xxHash source repository : https://github.com/Cyan4973/xxHash
*/
* xxHash - Extremely Fast Hash algorithm
* Copyright (C) 2012-2023, Yann Collet
*
* BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* You can contact the author at :
* - xxHash homepage: http://www.xxhash.com
* - xxHash source repository : https://github.com/Cyan4973/xxHash
*/
// xxhash64 is based on commit d2df04efcbef7d7f6886d345861e5dfda4edacc1. Removed
// everything but a simple interface for computing xxh64.
@@ -38,12 +38,28 @@
// xxh3_64bits is based on commit d5891596637d21366b9b1dcf2c0007a3edb26a9e (July
// 2023).
// xxh3_128bits is based on commit b0adcc54188c3130b1793e7b19c62eb1e669f7df
// (June 2024).
#include "wpi/xxhash.h"
#include "wpi/Compiler.h"
#include "wpi/Endian.h"
#include <stdlib.h>
#if !defined(LLVM_XXH_USE_NEON)
#if (defined(__aarch64__) || defined(_M_ARM64) || defined(_M_ARM64EC)) && \
!defined(__ARM_BIG_ENDIAN)
#define LLVM_XXH_USE_NEON 1
#else
#define LLVM_XXH_USE_NEON 0
#endif
#endif
#if LLVM_XXH_USE_NEON
#include <arm_neon.h>
#endif
using namespace wpi;
using namespace support;
@@ -297,12 +313,12 @@ static uint64_t XXH3_len_17to128_64b(const uint8_t *input, size_t len,
}
constexpr size_t XXH3_MIDSIZE_MAX = 240;
constexpr size_t XXH3_MIDSIZE_STARTOFFSET = 3;
constexpr size_t XXH3_MIDSIZE_LASTOFFSET = 17;
LLVM_ATTRIBUTE_NOINLINE
static uint64_t XXH3_len_129to240_64b(const uint8_t *input, size_t len,
const uint8_t *secret, uint64_t seed) {
constexpr size_t XXH3_MIDSIZE_STARTOFFSET = 3;
constexpr size_t XXH3_MIDSIZE_LASTOFFSET = 17;
uint64_t acc = (uint64_t)len * PRIME64_1;
const unsigned nbRounds = len / 16;
for (unsigned i = 0; i < 8; ++i)
@@ -320,6 +336,144 @@ static uint64_t XXH3_len_129to240_64b(const uint8_t *input, size_t len,
return XXH3_avalanche(acc);
}
#if LLVM_XXH_USE_NEON
#define XXH3_accumulate_512 XXH3_accumulate_512_neon
#define XXH3_scrambleAcc XXH3_scrambleAcc_neon
// NEON implementation based on commit a57f6cce2698049863af8c25787084ae0489d849
// (July 2024), with the following removed:
// - workaround for suboptimal codegen on older GCC
// - compiler barriers against instruction reordering
// - WebAssembly SIMD support
// - configurable split between NEON and scalar lanes (benchmarking shows no
// penalty when fully doing SIMD on the Apple M1)
#if defined(__GNUC__) || defined(__clang__)
#define XXH_ALIASING __attribute__((__may_alias__))
#else
#define XXH_ALIASING /* nothing */
#endif
typedef uint64x2_t xxh_aliasing_uint64x2_t XXH_ALIASING;
LLVM_ATTRIBUTE_ALWAYS_INLINE static uint64x2_t XXH_vld1q_u64(void const *ptr) {
return vreinterpretq_u64_u8(vld1q_u8((uint8_t const *)ptr));
}
LLVM_ATTRIBUTE_ALWAYS_INLINE
static void XXH3_accumulate_512_neon(uint64_t *acc, const uint8_t *input,
const uint8_t *secret) {
xxh_aliasing_uint64x2_t *const xacc = (xxh_aliasing_uint64x2_t *)acc;
#ifdef __clang__
#pragma clang loop unroll(full)
#endif
for (size_t i = 0; i < XXH_ACC_NB / 2; i += 2) {
/* data_vec = input[i]; */
uint64x2_t data_vec_1 = XXH_vld1q_u64(input + (i * 16));
uint64x2_t data_vec_2 = XXH_vld1q_u64(input + ((i + 1) * 16));
/* key_vec = secret[i]; */
uint64x2_t key_vec_1 = XXH_vld1q_u64(secret + (i * 16));
uint64x2_t key_vec_2 = XXH_vld1q_u64(secret + ((i + 1) * 16));
/* data_swap = swap(data_vec) */
uint64x2_t data_swap_1 = vextq_u64(data_vec_1, data_vec_1, 1);
uint64x2_t data_swap_2 = vextq_u64(data_vec_2, data_vec_2, 1);
/* data_key = data_vec ^ key_vec; */
uint64x2_t data_key_1 = veorq_u64(data_vec_1, key_vec_1);
uint64x2_t data_key_2 = veorq_u64(data_vec_2, key_vec_2);
/*
* If we reinterpret the 64x2 vectors as 32x4 vectors, we can use a
* de-interleave operation for 4 lanes in 1 step with `vuzpq_u32` to
* get one vector with the low 32 bits of each lane, and one vector
* with the high 32 bits of each lane.
*
* The intrinsic returns a double vector because the original ARMv7-a
* instruction modified both arguments in place. AArch64 and SIMD128 emit
* two instructions from this intrinsic.
*
* [ dk11L | dk11H | dk12L | dk12H ] -> [ dk11L | dk12L | dk21L | dk22L ]
* [ dk21L | dk21H | dk22L | dk22H ] -> [ dk11H | dk12H | dk21H | dk22H ]
*/
uint32x4x2_t unzipped = vuzpq_u32(vreinterpretq_u32_u64(data_key_1),
vreinterpretq_u32_u64(data_key_2));
/* data_key_lo = data_key & 0xFFFFFFFF */
uint32x4_t data_key_lo = unzipped.val[0];
/* data_key_hi = data_key >> 32 */
uint32x4_t data_key_hi = unzipped.val[1];
/*
* Then, we can split the vectors horizontally and multiply which, as for
* most widening intrinsics, have a variant that works on both high half
* vectors for free on AArch64. A similar instruction is available on
* SIMD128.
*
* sum = data_swap + (u64x2) data_key_lo * (u64x2) data_key_hi
*/
uint64x2_t sum_1 = vmlal_u32(data_swap_1, vget_low_u32(data_key_lo),
vget_low_u32(data_key_hi));
uint64x2_t sum_2 = vmlal_u32(data_swap_2, vget_high_u32(data_key_lo),
vget_high_u32(data_key_hi));
/* xacc[i] = acc_vec + sum; */
xacc[i] = vaddq_u64(xacc[i], sum_1);
xacc[i + 1] = vaddq_u64(xacc[i + 1], sum_2);
}
}
LLVM_ATTRIBUTE_ALWAYS_INLINE
static void XXH3_scrambleAcc_neon(uint64_t *acc, const uint8_t *secret) {
xxh_aliasing_uint64x2_t *const xacc = (xxh_aliasing_uint64x2_t *)acc;
/* { prime32_1, prime32_1 } */
uint32x2_t const kPrimeLo = vdup_n_u32(PRIME32_1);
/* { 0, prime32_1, 0, prime32_1 } */
uint32x4_t const kPrimeHi =
vreinterpretq_u32_u64(vdupq_n_u64((uint64_t)PRIME32_1 << 32));
for (size_t i = 0; i < XXH_ACC_NB / 2; ++i) {
/* xacc[i] ^= (xacc[i] >> 47); */
uint64x2_t acc_vec = XXH_vld1q_u64(acc + (2 * i));
uint64x2_t shifted = vshrq_n_u64(acc_vec, 47);
uint64x2_t data_vec = veorq_u64(acc_vec, shifted);
/* xacc[i] ^= secret[i]; */
uint64x2_t key_vec = XXH_vld1q_u64(secret + (i * 16));
uint64x2_t data_key = veorq_u64(data_vec, key_vec);
/*
* xacc[i] *= XXH_PRIME32_1
*
* Expanded version with portable NEON intrinsics
*
* lo(x) * lo(y) + (hi(x) * lo(y) << 32)
*
* prod_hi = hi(data_key) * lo(prime) << 32
*
* Since we only need 32 bits of this multiply a trick can be used,
* reinterpreting the vector as a uint32x4_t and multiplying by
* { 0, prime, 0, prime } to cancel out the unwanted bits and avoid the
* shift.
*/
uint32x4_t prod_hi = vmulq_u32(vreinterpretq_u32_u64(data_key), kPrimeHi);
/* Extract low bits for vmlal_u32 */
uint32x2_t data_key_lo = vmovn_u64(data_key);
/* xacc[i] = prod_hi + lo(data_key) * XXH_PRIME32_1; */
xacc[i] = vmlal_u32(vreinterpretq_u64_u32(prod_hi), data_key_lo, kPrimeLo);
}
}
#else
#define XXH3_accumulate_512 XXH3_accumulate_512_scalar
#define XXH3_scrambleAcc XXH3_scrambleAcc_scalar
LLVM_ATTRIBUTE_ALWAYS_INLINE
static void XXH3_accumulate_512_scalar(uint64_t *acc, const uint8_t *input,
const uint8_t *secret) {
@@ -332,20 +486,23 @@ static void XXH3_accumulate_512_scalar(uint64_t *acc, const uint8_t *input,
}
LLVM_ATTRIBUTE_ALWAYS_INLINE
static void XXH3_accumulate_scalar(uint64_t *acc, const uint8_t *input,
const uint8_t *secret, size_t nbStripes) {
for (size_t n = 0; n < nbStripes; ++n)
XXH3_accumulate_512_scalar(acc, input + n * XXH_STRIPE_LEN,
secret + n * XXH_SECRET_CONSUME_RATE);
}
static void XXH3_scrambleAcc(uint64_t *acc, const uint8_t *secret) {
static void XXH3_scrambleAcc_scalar(uint64_t *acc, const uint8_t *secret) {
for (size_t i = 0; i < XXH_ACC_NB; ++i) {
acc[i] ^= acc[i] >> 47;
acc[i] ^= endian::read64le(secret + 8 * i);
acc[i] *= PRIME32_1;
}
}
#endif
LLVM_ATTRIBUTE_ALWAYS_INLINE
static void XXH3_accumulate(uint64_t *acc, const uint8_t *input,
const uint8_t *secret, size_t nbStripes) {
for (size_t n = 0; n < nbStripes; ++n) {
XXH3_accumulate_512(acc, input + n * XXH_STRIPE_LEN,
secret + n * XXH_SECRET_CONSUME_RATE);
}
}
static uint64_t XXH3_mix2Accs(const uint64_t *acc, const uint8_t *secret) {
return XXH3_mul128_fold64(acc[0] ^ endian::read64le(secret),
@@ -372,21 +529,20 @@ static uint64_t XXH3_hashLong_64b(const uint8_t *input, size_t len,
PRIME64_4, PRIME32_2, PRIME64_5, PRIME32_1,
};
for (size_t n = 0; n < nb_blocks; ++n) {
XXH3_accumulate_scalar(acc, input + n * block_len, secret,
nbStripesPerBlock);
XXH3_accumulate(acc, input + n * block_len, secret, nbStripesPerBlock);
XXH3_scrambleAcc(acc, secret + secretSize - XXH_STRIPE_LEN);
}
/* last partial block */
const size_t nbStripes = (len - 1 - (block_len * nb_blocks)) / XXH_STRIPE_LEN;
assert(nbStripes <= secretSize / XXH_SECRET_CONSUME_RATE);
XXH3_accumulate_scalar(acc, input + nb_blocks * block_len, secret, nbStripes);
XXH3_accumulate(acc, input + nb_blocks * block_len, secret, nbStripes);
/* last stripe */
constexpr size_t XXH_SECRET_LASTACC_START = 7;
XXH3_accumulate_512_scalar(acc, input + len - XXH_STRIPE_LEN,
secret + secretSize - XXH_STRIPE_LEN -
XXH_SECRET_LASTACC_START);
XXH3_accumulate_512(acc, input + len - XXH_STRIPE_LEN,
secret + secretSize - XXH_STRIPE_LEN -
XXH_SECRET_LASTACC_START);
/* converge into final hash */
constexpr size_t XXH_SECRET_MERGEACCS_START = 11;
@@ -405,3 +561,482 @@ uint64_t wpi::xxh3_64bits(std::span<const uint8_t> data) {
return XXH3_len_129to240_64b(in, len, kSecret, 0);
return XXH3_hashLong_64b(in, len, kSecret, sizeof(kSecret));
}
/* ==========================================
* XXH3 128 bits (a.k.a XXH128)
* ==========================================
* XXH3's 128-bit variant has better mixing and strength than the 64-bit
* variant, even without counting the significantly larger output size.
*
* For example, extra steps are taken to avoid the seed-dependent collisions
* in 17-240 byte inputs (See XXH3_mix16B and XXH128_mix32B).
*
* This strength naturally comes at the cost of some speed, especially on short
* lengths. Note that longer hashes are about as fast as the 64-bit version
* due to it using only a slight modification of the 64-bit loop.
*
* XXH128 is also more oriented towards 64-bit machines. It is still extremely
* fast for a _128-bit_ hash on 32-bit (it usually clears XXH64).
*/
/*!
* @internal
* @def XXH_rotl32(x,r)
* @brief 32-bit rotate left.
*
* @param x The 32-bit integer to be rotated.
* @param r The number of bits to rotate.
* @pre
* @p r > 0 && @p r < 32
* @note
* @p x and @p r may be evaluated multiple times.
* @return The rotated result.
*/
#if __has_builtin(__builtin_rotateleft32) && \
__has_builtin(__builtin_rotateleft64)
#define XXH_rotl32 __builtin_rotateleft32
#define XXH_rotl64 __builtin_rotateleft64
/* Note: although _rotl exists for minGW (GCC under windows), performance seems
* poor */
#elif defined(_MSC_VER)
#define XXH_rotl32(x, r) _rotl(x, r)
#define XXH_rotl64(x, r) _rotl64(x, r)
#else
#define XXH_rotl32(x, r) (((x) << (r)) | ((x) >> (32 - (r))))
#define XXH_rotl64(x, r) (((x) << (r)) | ((x) >> (64 - (r))))
#endif
#define XXH_mult32to64(x, y) ((uint64_t)(uint32_t)(x) * (uint64_t)(uint32_t)(y))
/*!
* @brief Calculates a 64->128-bit long multiply.
*
* Uses `__uint128_t` and `_umul128` if available, otherwise uses a scalar
* version.
*
* @param lhs , rhs The 64-bit integers to be multiplied
* @return The 128-bit result represented in an @ref XXH128_hash_t.
*/
static XXH128_hash_t XXH_mult64to128(uint64_t lhs, uint64_t rhs) {
/*
* GCC/Clang __uint128_t method.
*
* On most 64-bit targets, GCC and Clang define a __uint128_t type.
* This is usually the best way as it usually uses a native long 64-bit
* multiply, such as MULQ on x86_64 or MUL + UMULH on aarch64.
*
* Usually.
*
* Despite being a 32-bit platform, Clang (and emscripten) define this type
* despite not having the arithmetic for it. This results in a laggy
* compiler builtin call which calculates a full 128-bit multiply.
* In that case it is best to use the portable one.
* https://github.com/Cyan4973/xxHash/issues/211#issuecomment-515575677
*/
#if (defined(__GNUC__) || defined(__clang__)) && !defined(__wasm__) && \
defined(__SIZEOF_INT128__) || \
(defined(_INTEGRAL_MAX_BITS) && _INTEGRAL_MAX_BITS >= 128)
__uint128_t const product = (__uint128_t)lhs * (__uint128_t)rhs;
XXH128_hash_t r128;
r128.low64 = (uint64_t)(product);
r128.high64 = (uint64_t)(product >> 64);
return r128;
/*
* MSVC for x64's _umul128 method.
*
* uint64_t _umul128(uint64_t Multiplier, uint64_t Multiplicand, uint64_t
* *HighProduct);
*
* This compiles to single operand MUL on x64.
*/
#elif (defined(_M_X64) || defined(_M_IA64)) && !defined(_M_ARM64EC)
#ifndef _MSC_VER
#pragma intrinsic(_umul128)
#endif
uint64_t product_high;
uint64_t const product_low = _umul128(lhs, rhs, &product_high);
XXH128_hash_t r128;
r128.low64 = product_low;
r128.high64 = product_high;
return r128;
/*
* MSVC for ARM64's __umulh method.
*
* This compiles to the same MUL + UMULH as GCC/Clang's __uint128_t method.
*/
#elif defined(_M_ARM64) || defined(_M_ARM64EC)
#ifndef _MSC_VER
#pragma intrinsic(__umulh)
#endif
XXH128_hash_t r128;
r128.low64 = lhs * rhs;
r128.high64 = __umulh(lhs, rhs);
return r128;
#else
/*
* Portable scalar method. Optimized for 32-bit and 64-bit ALUs.
*
* This is a fast and simple grade school multiply, which is shown below
* with base 10 arithmetic instead of base 0x100000000.
*
* 9 3 // D2 lhs = 93
* x 7 5 // D2 rhs = 75
* ----------
* 1 5 // D2 lo_lo = (93 % 10) * (75 % 10) = 15
* 4 5 | // D2 hi_lo = (93 / 10) * (75 % 10) = 45
* 2 1 | // D2 lo_hi = (93 % 10) * (75 / 10) = 21
* + 6 3 | | // D2 hi_hi = (93 / 10) * (75 / 10) = 63
* ---------
* 2 7 | // D2 cross = (15 / 10) + (45 % 10) + 21 = 27
* + 6 7 | | // D2 upper = (27 / 10) + (45 / 10) + 63 = 67
* ---------
* 6 9 7 5 // D4 res = (27 * 10) + (15 % 10) + (67 * 100) = 6975
*
* The reasons for adding the products like this are:
* 1. It avoids manual carry tracking. Just like how
* (9 * 9) + 9 + 9 = 99, the same applies with this for UINT64_MAX.
* This avoids a lot of complexity.
*
* 2. It hints for, and on Clang, compiles to, the powerful UMAAL
* instruction available in ARM's Digital Signal Processing extension
* in 32-bit ARMv6 and later, which is shown below:
*
* void UMAAL(xxh_u32 *RdLo, xxh_u32 *RdHi, xxh_u32 Rn, xxh_u32 Rm)
* {
* uint64_t product = (uint64_t)*RdLo * (uint64_t)*RdHi + Rn + Rm;
* *RdLo = (xxh_u32)(product & 0xFFFFFFFF);
* *RdHi = (xxh_u32)(product >> 32);
* }
*
* This instruction was designed for efficient long multiplication, and
* allows this to be calculated in only 4 instructions at speeds
* comparable to some 64-bit ALUs.
*
* 3. It isn't terrible on other platforms. Usually this will be a couple
* of 32-bit ADD/ADCs.
*/
/* First calculate all of the cross products. */
uint64_t const lo_lo = XXH_mult32to64(lhs & 0xFFFFFFFF, rhs & 0xFFFFFFFF);
uint64_t const hi_lo = XXH_mult32to64(lhs >> 32, rhs & 0xFFFFFFFF);
uint64_t const lo_hi = XXH_mult32to64(lhs & 0xFFFFFFFF, rhs >> 32);
uint64_t const hi_hi = XXH_mult32to64(lhs >> 32, rhs >> 32);
/* Now add the products together. These will never overflow. */
uint64_t const cross = (lo_lo >> 32) + (hi_lo & 0xFFFFFFFF) + lo_hi;
uint64_t const upper = (hi_lo >> 32) + (cross >> 32) + hi_hi;
uint64_t const lower = (cross << 32) | (lo_lo & 0xFFFFFFFF);
XXH128_hash_t r128;
r128.low64 = lower;
r128.high64 = upper;
return r128;
#endif
}
/*! Seems to produce slightly better code on GCC for some reason. */
LLVM_ATTRIBUTE_ALWAYS_INLINE constexpr uint64_t XXH_xorshift64(uint64_t v64,
int shift) {
return v64 ^ (v64 >> shift);
}
LLVM_ATTRIBUTE_ALWAYS_INLINE static XXH128_hash_t
XXH3_len_1to3_128b(const uint8_t *input, size_t len, const uint8_t *secret,
uint64_t seed) {
/* A doubled version of 1to3_64b with different constants. */
/*
* len = 1: combinedl = { input[0], 0x01, input[0], input[0] }
* len = 2: combinedl = { input[1], 0x02, input[0], input[1] }
* len = 3: combinedl = { input[2], 0x03, input[0], input[1] }
*/
uint8_t const c1 = input[0];
uint8_t const c2 = input[len >> 1];
uint8_t const c3 = input[len - 1];
uint32_t const combinedl = ((uint32_t)c1 << 16) | ((uint32_t)c2 << 24) |
((uint32_t)c3 << 0) | ((uint32_t)len << 8);
uint32_t const combinedh = XXH_rotl32(byteswap(combinedl), 13);
uint64_t const bitflipl =
(endian::read32le(secret) ^ endian::read32le(secret + 4)) + seed;
uint64_t const bitfliph =
(endian::read32le(secret + 8) ^ endian::read32le(secret + 12)) - seed;
uint64_t const keyed_lo = (uint64_t)combinedl ^ bitflipl;
uint64_t const keyed_hi = (uint64_t)combinedh ^ bitfliph;
XXH128_hash_t h128;
h128.low64 = XXH64_avalanche(keyed_lo);
h128.high64 = XXH64_avalanche(keyed_hi);
return h128;
}
LLVM_ATTRIBUTE_ALWAYS_INLINE static XXH128_hash_t
XXH3_len_4to8_128b(const uint8_t *input, size_t len, const uint8_t *secret,
uint64_t seed) {
seed ^= (uint64_t)byteswap((uint32_t)seed) << 32;
uint32_t const input_lo = endian::read32le(input);
uint32_t const input_hi = endian::read32le(input + len - 4);
uint64_t const input_64 = input_lo + ((uint64_t)input_hi << 32);
uint64_t const bitflip =
(endian::read64le(secret + 16) ^ endian::read64le(secret + 24)) + seed;
uint64_t const keyed = input_64 ^ bitflip;
/* Shift len to the left to ensure it is even, this avoids even multiplies.
*/
XXH128_hash_t m128 = XXH_mult64to128(keyed, PRIME64_1 + (len << 2));
m128.high64 += (m128.low64 << 1);
m128.low64 ^= (m128.high64 >> 3);
m128.low64 = XXH_xorshift64(m128.low64, 35);
m128.low64 *= PRIME_MX2;
m128.low64 = XXH_xorshift64(m128.low64, 28);
m128.high64 = XXH3_avalanche(m128.high64);
return m128;
}
LLVM_ATTRIBUTE_ALWAYS_INLINE static XXH128_hash_t
XXH3_len_9to16_128b(const uint8_t *input, size_t len, const uint8_t *secret,
uint64_t seed) {
uint64_t const bitflipl =
(endian::read64le(secret + 32) ^ endian::read64le(secret + 40)) - seed;
uint64_t const bitfliph =
(endian::read64le(secret + 48) ^ endian::read64le(secret + 56)) + seed;
uint64_t const input_lo = endian::read64le(input);
uint64_t input_hi = endian::read64le(input + len - 8);
XXH128_hash_t m128 =
XXH_mult64to128(input_lo ^ input_hi ^ bitflipl, PRIME64_1);
/*
* Put len in the middle of m128 to ensure that the length gets mixed to
* both the low and high bits in the 128x64 multiply below.
*/
m128.low64 += (uint64_t)(len - 1) << 54;
input_hi ^= bitfliph;
/*
* Add the high 32 bits of input_hi to the high 32 bits of m128, then
* add the long product of the low 32 bits of input_hi and PRIME32_2 to
* the high 64 bits of m128.
*
* The best approach to this operation is different on 32-bit and 64-bit.
*/
if (sizeof(void *) < sizeof(uint64_t)) { /* 32-bit */
/*
* 32-bit optimized version, which is more readable.
*
* On 32-bit, it removes an ADC and delays a dependency between the two
* halves of m128.high64, but it generates an extra mask on 64-bit.
*/
m128.high64 += (input_hi & 0xFFFFFFFF00000000ULL) +
XXH_mult32to64((uint32_t)input_hi, PRIME32_2);
} else {
/*
* 64-bit optimized (albeit more confusing) version.
*
* Uses some properties of addition and multiplication to remove the mask:
*
* Let:
* a = input_hi.lo = (input_hi & 0x00000000FFFFFFFF)
* b = input_hi.hi = (input_hi & 0xFFFFFFFF00000000)
* c = PRIME32_2
*
* a + (b * c)
* Inverse Property: x + y - x == y
* a + (b * (1 + c - 1))
* Distributive Property: x * (y + z) == (x * y) + (x * z)
* a + (b * 1) + (b * (c - 1))
* Identity Property: x * 1 == x
* a + b + (b * (c - 1))
*
* Substitute a, b, and c:
* input_hi.hi + input_hi.lo + ((uint64_t)input_hi.lo * (PRIME32_2
* - 1))
*
* Since input_hi.hi + input_hi.lo == input_hi, we get this:
* input_hi + ((uint64_t)input_hi.lo * (PRIME32_2 - 1))
*/
m128.high64 += input_hi + XXH_mult32to64((uint32_t)input_hi, PRIME32_2 - 1);
}
/* m128 ^= XXH_swap64(m128 >> 64); */
m128.low64 ^= byteswap(m128.high64);
/* 128x64 multiply: h128 = m128 * PRIME64_2; */
XXH128_hash_t h128 = XXH_mult64to128(m128.low64, PRIME64_2);
h128.high64 += m128.high64 * PRIME64_2;
h128.low64 = XXH3_avalanche(h128.low64);
h128.high64 = XXH3_avalanche(h128.high64);
return h128;
}
/*
* Assumption: `secret` size is >= XXH3_SECRET_SIZE_MIN
*/
LLVM_ATTRIBUTE_ALWAYS_INLINE static XXH128_hash_t
XXH3_len_0to16_128b(const uint8_t *input, size_t len, const uint8_t *secret,
uint64_t seed) {
if (len > 8)
return XXH3_len_9to16_128b(input, len, secret, seed);
if (len >= 4)
return XXH3_len_4to8_128b(input, len, secret, seed);
if (len)
return XXH3_len_1to3_128b(input, len, secret, seed);
XXH128_hash_t h128;
uint64_t const bitflipl =
endian::read64le(secret + 64) ^ endian::read64le(secret + 72);
uint64_t const bitfliph =
endian::read64le(secret + 80) ^ endian::read64le(secret + 88);
h128.low64 = XXH64_avalanche(seed ^ bitflipl);
h128.high64 = XXH64_avalanche(seed ^ bitfliph);
return h128;
}
/*
* A bit slower than XXH3_mix16B, but handles multiply by zero better.
*/
LLVM_ATTRIBUTE_ALWAYS_INLINE static XXH128_hash_t
XXH128_mix32B(XXH128_hash_t acc, const uint8_t *input_1, const uint8_t *input_2,
const uint8_t *secret, uint64_t seed) {
acc.low64 += XXH3_mix16B(input_1, secret + 0, seed);
acc.low64 ^= endian::read64le(input_2) + endian::read64le(input_2 + 8);
acc.high64 += XXH3_mix16B(input_2, secret + 16, seed);
acc.high64 ^= endian::read64le(input_1) + endian::read64le(input_1 + 8);
return acc;
}
LLVM_ATTRIBUTE_ALWAYS_INLINE static XXH128_hash_t
XXH3_len_17to128_128b(const uint8_t *input, size_t len, const uint8_t *secret,
size_t secretSize, uint64_t seed) {
(void)secretSize;
XXH128_hash_t acc;
acc.low64 = len * PRIME64_1;
acc.high64 = 0;
if (len > 32) {
if (len > 64) {
if (len > 96) {
acc =
XXH128_mix32B(acc, input + 48, input + len - 64, secret + 96, seed);
}
acc = XXH128_mix32B(acc, input + 32, input + len - 48, secret + 64, seed);
}
acc = XXH128_mix32B(acc, input + 16, input + len - 32, secret + 32, seed);
}
acc = XXH128_mix32B(acc, input, input + len - 16, secret, seed);
XXH128_hash_t h128;
h128.low64 = acc.low64 + acc.high64;
h128.high64 = (acc.low64 * PRIME64_1) + (acc.high64 * PRIME64_4) +
((len - seed) * PRIME64_2);
h128.low64 = XXH3_avalanche(h128.low64);
h128.high64 = (uint64_t)0 - XXH3_avalanche(h128.high64);
return h128;
}
LLVM_ATTRIBUTE_NOINLINE static XXH128_hash_t
XXH3_len_129to240_128b(const uint8_t *input, size_t len, const uint8_t *secret,
size_t secretSize, uint64_t seed) {
(void)secretSize;
XXH128_hash_t acc;
unsigned i;
acc.low64 = len * PRIME64_1;
acc.high64 = 0;
/*
* We set as `i` as offset + 32. We do this so that unchanged
* `len` can be used as upper bound. This reaches a sweet spot
* where both x86 and aarch64 get simple agen and good codegen
* for the loop.
*/
for (i = 32; i < 160; i += 32) {
acc = XXH128_mix32B(acc, input + i - 32, input + i - 16, secret + i - 32,
seed);
}
acc.low64 = XXH3_avalanche(acc.low64);
acc.high64 = XXH3_avalanche(acc.high64);
/*
* NB: `i <= len` will duplicate the last 32-bytes if
* len % 32 was zero. This is an unfortunate necessity to keep
* the hash result stable.
*/
for (i = 160; i <= len; i += 32) {
acc = XXH128_mix32B(acc, input + i - 32, input + i - 16,
secret + XXH3_MIDSIZE_STARTOFFSET + i - 160, seed);
}
/* last bytes */
acc =
XXH128_mix32B(acc, input + len - 16, input + len - 32,
secret + XXH3_SECRETSIZE_MIN - XXH3_MIDSIZE_LASTOFFSET - 16,
(uint64_t)0 - seed);
XXH128_hash_t h128;
h128.low64 = acc.low64 + acc.high64;
h128.high64 = (acc.low64 * PRIME64_1) + (acc.high64 * PRIME64_4) +
((len - seed) * PRIME64_2);
h128.low64 = XXH3_avalanche(h128.low64);
h128.high64 = (uint64_t)0 - XXH3_avalanche(h128.high64);
return h128;
}
LLVM_ATTRIBUTE_ALWAYS_INLINE XXH128_hash_t
XXH3_hashLong_128b(const uint8_t *input, size_t len, const uint8_t *secret,
size_t secretSize) {
const size_t nbStripesPerBlock =
(secretSize - XXH_STRIPE_LEN) / XXH_SECRET_CONSUME_RATE;
const size_t block_len = XXH_STRIPE_LEN * nbStripesPerBlock;
const size_t nb_blocks = (len - 1) / block_len;
alignas(16) uint64_t acc[XXH_ACC_NB] = {
PRIME32_3, PRIME64_1, PRIME64_2, PRIME64_3,
PRIME64_4, PRIME32_2, PRIME64_5, PRIME32_1,
};
for (size_t n = 0; n < nb_blocks; ++n) {
XXH3_accumulate(acc, input + n * block_len, secret, nbStripesPerBlock);
XXH3_scrambleAcc(acc, secret + secretSize - XXH_STRIPE_LEN);
}
/* last partial block */
const size_t nbStripes = (len - 1 - (block_len * nb_blocks)) / XXH_STRIPE_LEN;
assert(nbStripes <= secretSize / XXH_SECRET_CONSUME_RATE);
XXH3_accumulate(acc, input + nb_blocks * block_len, secret, nbStripes);
/* last stripe */
constexpr size_t XXH_SECRET_LASTACC_START = 7;
XXH3_accumulate_512(acc, input + len - XXH_STRIPE_LEN,
secret + secretSize - XXH_STRIPE_LEN -
XXH_SECRET_LASTACC_START);
/* converge into final hash */
static_assert(sizeof(acc) == 64);
XXH128_hash_t h128;
constexpr size_t XXH_SECRET_MERGEACCS_START = 11;
h128.low64 = XXH3_mergeAccs(acc, secret + XXH_SECRET_MERGEACCS_START,
(uint64_t)len * PRIME64_1);
h128.high64 = XXH3_mergeAccs(
acc, secret + secretSize - sizeof(acc) - XXH_SECRET_MERGEACCS_START,
~((uint64_t)len * PRIME64_2));
return h128;
}
wpi::XXH128_hash_t wpi::xxh3_128bits(std::span<const uint8_t> data) {
size_t len = data.size();
const uint8_t *input = data.data();
/*
* If an action is to be taken if `secret` conditions are not respected,
* it should be done here.
* For now, it's a contract pre-condition.
* Adding a check and a branch here would cost performance at every hash.
*/
if (len <= 16)
return XXH3_len_0to16_128b(input, len, kSecret, /*seed64=*/0);
if (len <= 128)
return XXH3_len_17to128_128b(input, len, kSecret, sizeof(kSecret),
/*seed64=*/0);
if (len <= XXH3_MIDSIZE_MAX)
return XXH3_len_129to240_128b(input, len, kSecret, sizeof(kSecret),
/*seed64=*/0);
return XXH3_hashLong_128b(input, len, kSecret, sizeof(kSecret));
}

View File

@@ -37,6 +37,22 @@ constexpr auto end_impl(RangeT &&range)
return end(std::forward<RangeT>(range));
}
using std::rbegin;
template <typename RangeT>
constexpr auto rbegin_impl(RangeT &&range)
-> decltype(rbegin(std::forward<RangeT>(range))) {
return rbegin(std::forward<RangeT>(range));
}
using std::rend;
template <typename RangeT>
constexpr auto rend_impl(RangeT &&range)
-> decltype(rend(std::forward<RangeT>(range))) {
return rend(std::forward<RangeT>(range));
}
using std::swap;
template <typename T>
@@ -72,6 +88,22 @@ constexpr auto adl_end(RangeT &&range)
return adl_detail::end_impl(std::forward<RangeT>(range));
}
/// Returns the reverse-begin iterator to \p range using `std::rbegin` and
/// function found through Argument-Dependent Lookup (ADL).
template <typename RangeT>
constexpr auto adl_rbegin(RangeT &&range)
-> decltype(adl_detail::rbegin_impl(std::forward<RangeT>(range))) {
return adl_detail::rbegin_impl(std::forward<RangeT>(range));
}
/// Returns the reverse-end iterator to \p range using `std::rend` and
/// functions found through Argument-Dependent Lookup (ADL).
template <typename RangeT>
constexpr auto adl_rend(RangeT &&range)
-> decltype(adl_detail::rend_impl(std::forward<RangeT>(range))) {
return adl_detail::rend_impl(std::forward<RangeT>(range));
}
/// Swaps \p lhs with \p rhs using `std::swap` and functions found through
/// Argument-Dependent Lookup (ADL).
template <typename T>

View File

@@ -801,6 +801,52 @@ template <class X, class Y>
return unique_dyn_cast_or_null<X, Y>(Val);
}
//===----------------------------------------------------------------------===//
// Isa Predicates
//===----------------------------------------------------------------------===//
/// These are wrappers over isa* function that allow them to be used in generic
/// algorithms such as `wpi:all_of`, `wpi::none_of`, etc. This is accomplished
/// by exposing the isa* functions through function objects with a generic
/// function call operator.
namespace detail {
template <typename... Types> struct IsaCheckPredicate {
template <typename T> [[nodiscard]] bool operator()(const T &Val) const {
return isa<Types...>(Val);
}
};
template <typename... Types> struct IsaAndPresentCheckPredicate {
template <typename T> [[nodiscard]] bool operator()(const T &Val) const {
return isa_and_present<Types...>(Val);
}
};
} // namespace detail
/// Function object wrapper for the `wpi::isa` type check. The function call
/// operator returns true when the value can be cast to any type in `Types`.
/// Example:
/// ```
/// SmallVector<Type> myTypes = ...;
/// if (wpi::all_of(myTypes, wpi::IsaPred<VectorType>))
/// ...
/// ```
template <typename... Types>
inline constexpr detail::IsaCheckPredicate<Types...> IsaPred{};
/// Function object wrapper for the `wpi::isa_and_present` type check. The
/// function call operator returns true when the value can be cast to any type
/// in `Types`, or if the value is not present (e.g., nullptr). Example:
/// ```
/// SmallVector<Type> myTypes = ...;
/// if (wpi::all_of(myTypes, wpi::IsaAndPresentPred<VectorType>))
/// ...
/// ```
template <typename... Types>
inline constexpr detail::IsaAndPresentCheckPredicate<Types...>
IsaAndPresentPred{};
} // end namespace wpi
#endif // WPIUTIL_WPI_CASTING_H

View File

@@ -297,6 +297,14 @@
#endif
#endif
/// LLVM_ATTRIBUTE_RESTRICT - Annotates a pointer to tell the compiler that
/// it is not aliased in the current scope.
#if defined(__clang__) || defined(__GNUC__) || defined(_MSC_VER)
#define LLVM_ATTRIBUTE_RESTRICT __restrict
#else
#define LLVM_ATTRIBUTE_RESTRICT
#endif
/// \macro LLVM_ATTRIBUTE_RETURNS_NOALIAS Used to mark a function as returning a
/// pointer that does not alias any other valid pointer.
#ifndef LLVM_ATTRIBUTE_RETURNS_NOALIAS

View File

@@ -313,6 +313,22 @@ public:
insert(*I);
}
template <typename V>
std::pair<iterator, bool> insert_or_assign(const KeyT &Key, V &&Val) {
auto Ret = try_emplace(Key, std::forward<V>(Val));
if (!Ret.second)
Ret.first->second = std::forward<V>(Val);
return Ret;
}
template <typename V>
std::pair<iterator, bool> insert_or_assign(KeyT &&Key, V &&Val) {
auto Ret = try_emplace(std::move(Key), std::forward<V>(Val));
if (!Ret.second)
Ret.first->second = std::forward<V>(Val);
return Ret;
}
/// Returns the value associated to the key in the map if it exists. If it
/// does not exist, emplace a default value for the key and returns a
/// reference to the newly created value.

View File

@@ -23,20 +23,22 @@
namespace wpi {
namespace densemap::detail {
// A bit mixer with very low latency using one multiplications and one
// xor-shift. The constant is from splitmix64.
inline uint64_t mix(uint64_t x) {
x *= 0xbf58476d1ce4e5b9u;
x ^= x >> 31;
return x;
}
} // namespace densemap::detail
namespace detail {
/// Simplistic combination of 32-bit hash values into 32-bit hash values.
static inline unsigned combineHashValue(unsigned a, unsigned b) {
uint64_t key = (uint64_t)a << 32 | (uint64_t)b;
key += ~(key << 32);
key ^= (key >> 22);
key += ~(key << 13);
key ^= (key >> 8);
key += (key << 3);
key ^= (key >> 15);
key += ~(key << 27);
key ^= (key >> 31);
return (unsigned)key;
inline unsigned combineHashValue(unsigned a, unsigned b) {
uint64_t x = (uint64_t)a << 32 | (uint64_t)b;
return (unsigned)densemap::detail::mix(x);
}
} // end namespace detail
@@ -137,7 +139,10 @@ template<> struct DenseMapInfo<unsigned long> {
static inline unsigned long getTombstoneKey() { return ~0UL - 1L; }
static unsigned getHashValue(const unsigned long& Val) {
return (unsigned)(Val * 37UL);
if constexpr (sizeof(Val) == 4)
return DenseMapInfo<unsigned>::getHashValue(Val);
else
return densemap::detail::mix(Val);
}
static bool isEqual(const unsigned long& LHS, const unsigned long& RHS) {
@@ -151,7 +156,7 @@ template<> struct DenseMapInfo<unsigned long long> {
static inline unsigned long long getTombstoneKey() { return ~0ULL - 1ULL; }
static unsigned getHashValue(const unsigned long long& Val) {
return (unsigned)(Val * 37ULL);
return densemap::detail::mix(Val);
}
static bool isEqual(const unsigned long long& LHS,
@@ -297,6 +302,24 @@ template <typename... Ts> struct DenseMapInfo<std::tuple<Ts...>> {
}
};
// Provide DenseMapInfo for enum classes.
template <typename Enum>
struct DenseMapInfo<Enum, std::enable_if_t<std::is_enum_v<Enum>>> {
using UnderlyingType = std::underlying_type_t<Enum>;
using Info = DenseMapInfo<UnderlyingType>;
static Enum getEmptyKey() { return static_cast<Enum>(Info::getEmptyKey()); }
static Enum getTombstoneKey() {
return static_cast<Enum>(Info::getTombstoneKey());
}
static unsigned getHashValue(const Enum &Val) {
return Info::getHashValue(static_cast<UnderlyingType>(Val));
}
static bool isEqual(const Enum &LHS, const Enum &RHS) { return LHS == RHS; }
};
} // end namespace wpi
#endif // WPIUTIL_WPI_DENSEMAPINFO_H

View File

@@ -74,7 +74,8 @@ template <typename value_type, endianness endian, std::size_t alignment>
/// Read a value of a particular endianness from a buffer, and increment the
/// buffer past that value.
template <typename value_type, std::size_t alignment, typename CharT>
template <typename value_type, std::size_t alignment = unaligned,
typename CharT>
[[nodiscard]] inline value_type readNext(const CharT *&memory,
endianness endian) {
value_type ret = read<value_type, alignment>(memory, endian);
@@ -82,8 +83,8 @@ template <typename value_type, std::size_t alignment, typename CharT>
return ret;
}
template <typename value_type, endianness endian, std::size_t alignment,
typename CharT>
template <typename value_type, endianness endian,
std::size_t alignment = unaligned, typename CharT>
[[nodiscard]] inline value_type readNext(const CharT *&memory) {
return readNext<value_type, alignment, CharT>(memory, endian);
}
@@ -104,6 +105,21 @@ inline void write(void *memory, value_type value) {
write<value_type, alignment>(memory, value, endian);
}
/// Write a value of a particular endianness, and increment the buffer past that
/// value.
template <typename value_type, std::size_t alignment = unaligned,
typename CharT>
inline void writeNext(CharT *&memory, value_type value, endianness endian) {
write(memory, value, endian);
memory += sizeof(value_type);
}
template <typename value_type, endianness endian,
std::size_t alignment = unaligned, typename CharT>
inline void writeNext(CharT *&memory, value_type value) {
writeNext<value_type, alignment, CharT>(memory, value, endian);
}
template <typename value_type>
using make_unsigned_t = std::make_unsigned_t<value_type>;

View File

@@ -38,6 +38,10 @@ enum class errc {
bad_address = int(std::errc::bad_address),
bad_file_descriptor = int(std::errc::bad_file_descriptor),
broken_pipe = int(std::errc::broken_pipe),
// There is no delete_pending in std::errc; this error code is negative to
// avoid conflicts. This error roughly corresponds with Windows'
// STATUS_DELETE_PENDING 0xC0000056.
delete_pending = -56,
device_or_resource_busy = int(std::errc::device_or_resource_busy),
directory_not_empty = int(std::errc::directory_not_empty),
executable_format_error = int(std::errc::executable_format_error),

View File

@@ -38,7 +38,6 @@
#include "wpi/Compiler.h"
#include "wpi/MemAlloc.h"
#include "wpi/type_traits.h"
#include <cstddef>
#include <cstring>
#include <memory>
#include <type_traits>
@@ -169,7 +168,7 @@ protected:
// provide four pointers worth of storage here.
// This is mutable as an inlined `const unique_function<void() const>` may
// still modify its own mutable members.
alignas(void*) mutable std::byte InlineStorage[InlineStorageSize];
alignas(void *) mutable std::byte InlineStorage[InlineStorageSize];
} StorageUnion;
// A compressed pointer to either our dispatching callback or our table of

View File

@@ -131,23 +131,6 @@ hash_code hash_value(const std::basic_string<T> &arg);
/// Compute a hash_code for a standard string.
template <typename T> hash_code hash_value(const std::optional<T> &arg);
/// Override the execution seed with a fixed value.
///
/// This hashing library uses a per-execution seed designed to change on each
/// run with high probability in order to ensure that the hash codes are not
/// attackable and to ensure that output which is intended to be stable does
/// not rely on the particulars of the hash codes produced.
///
/// That said, there are use cases where it is important to be able to
/// reproduce *exactly* a specific behavior. To that end, we provide a function
/// which will forcibly set the seed to a fixed value. This must be done at the
/// start of the program, before any hashes are computed. Also, it cannot be
/// undone. This makes it thread-hostile and very hard to use outside of
/// immediately on start of a simple program designed for reproducible
/// behavior.
void set_fixed_execution_hash_seed(uint64_t fixed_value);
// All of the implementation details of actually computing the various hash
// code values are held within this namespace. These routines are included in
// the header file mainly to allow inlining and constant propagation.
@@ -327,24 +310,20 @@ struct hash_state {
}
};
/// A global, fixed seed-override variable.
///
/// This variable can be set using the \see wpi::set_fixed_execution_seed
/// function. See that function for details. Do not, under any circumstances,
/// set or read this variable.
extern uint64_t fixed_seed_override;
/// In LLVM_ENABLE_ABI_BREAKING_CHECKS builds, the seed is non-deterministic
/// per process (address of a function in LLVMSupport) to prevent having users
/// depend on the particular hash values. On platforms without ASLR, this is
/// still likely non-deterministic per build.
inline uint64_t get_execution_seed() {
// FIXME: This needs to be a per-execution seed. This is just a placeholder
// implementation. Switching to a per-execution seed is likely to flush out
// instability bugs and so will happen as its own commit.
//
// However, if there is a fixed seed override set the first time this is
// called, return that instead of the per-execution seed.
const uint64_t seed_prime = 0xff51afd7ed558ccdULL;
static uint64_t seed = fixed_seed_override ? fixed_seed_override : seed_prime;
return seed;
// Work around x86-64 negative offset folding for old Clang -fno-pic
// https://reviews.llvm.org/D93931
#if LLVM_ENABLE_ABI_BREAKING_CHECKS && \
(!defined(__clang__) || __clang_major__ > 11)
return static_cast<uint64_t>(
reinterpret_cast<uintptr_t>(&install_fatal_error_handler));
#else
return 0xff51afd7ed558ccdULL;
#endif
}

View File

@@ -24,6 +24,22 @@
#include <type_traits>
namespace wpi {
/// Some template parameter helpers to optimize for bitwidth, for functions that
/// take multiple arguments.
// We can't verify signedness, since callers rely on implicit coercions to
// signed/unsigned.
template <typename T, typename U>
using enableif_int =
std::enable_if_t<std::is_integral_v<T> && std::is_integral_v<U>>;
// Use std::common_type_t to widen only up to the widest argument.
template <typename T, typename U, typename = enableif_int<T, U>>
using common_uint =
std::common_type_t<std::make_unsigned_t<T>, std::make_unsigned_t<U>>;
template <typename T, typename U, typename = enableif_int<T, U>>
using common_sint =
std::common_type_t<std::make_signed_t<T>, std::make_signed_t<U>>;
/// Create a bitmask with the N right-most bits set to 1, and all other
/// bits set to 0. Only unsigned types are allowed.
@@ -31,7 +47,9 @@ template <typename T> T maskTrailingOnes(unsigned N) {
static_assert(std::is_unsigned_v<T>, "Invalid type!");
const unsigned Bits = CHAR_BIT * sizeof(T);
assert(N <= Bits && "Invalid bit index");
return N == 0 ? 0 : (T(-1) >> (Bits - N));
if (N == 0)
return 0;
return T(-1) >> (Bits - N);
}
/// Create a bitmask with the N left-most bits set to 1, and all other
@@ -98,22 +116,24 @@ template <typename T> T reverseBits(T Val) {
// ambiguity.
/// Return the high 32 bits of a 64 bit value.
constexpr inline uint32_t Hi_32(uint64_t Value) {
constexpr uint32_t Hi_32(uint64_t Value) {
return static_cast<uint32_t>(Value >> 32);
}
/// Return the low 32 bits of a 64 bit value.
constexpr inline uint32_t Lo_32(uint64_t Value) {
constexpr uint32_t Lo_32(uint64_t Value) {
return static_cast<uint32_t>(Value);
}
/// Make a 64-bit integer from a high / low pair of 32-bit integers.
constexpr inline uint64_t Make_64(uint32_t High, uint32_t Low) {
constexpr uint64_t Make_64(uint32_t High, uint32_t Low) {
return ((uint64_t)High << 32) | (uint64_t)Low;
}
/// Checks if an integer fits into the given bit width.
template <unsigned N> constexpr inline bool isInt(int64_t x) {
template <unsigned N> constexpr bool isInt(int64_t x) {
if constexpr (N == 0)
return 0 == x;
if constexpr (N == 8)
return static_cast<int8_t>(x) == x;
if constexpr (N == 16)
@@ -128,16 +148,16 @@ template <unsigned N> constexpr inline bool isInt(int64_t x) {
/// Checks if a signed integer is an N bit number shifted left by S.
template <unsigned N, unsigned S>
constexpr inline bool isShiftedInt(int64_t x) {
static_assert(
N > 0, "isShiftedInt<0> doesn't make sense (refers to a 0-bit number.");
constexpr bool isShiftedInt(int64_t x) {
static_assert(S < 64, "isShiftedInt<N, S> with S >= 64 is too much.");
static_assert(N + S <= 64, "isShiftedInt<N, S> with N + S > 64 is too wide.");
return isInt<N + S>(x) && (x % (UINT64_C(1) << S) == 0);
}
/// Checks if an unsigned integer fits into the given bit width.
template <unsigned N> constexpr inline bool isUInt(uint64_t x) {
static_assert(N > 0, "isUInt<0> doesn't make sense");
template <unsigned N> constexpr bool isUInt(uint64_t x) {
if constexpr (N == 0)
return 0 == x;
if constexpr (N == 8)
return static_cast<uint8_t>(x) == x;
if constexpr (N == 16)
@@ -152,24 +172,27 @@ template <unsigned N> constexpr inline bool isUInt(uint64_t x) {
/// Checks if a unsigned integer is an N bit number shifted left by S.
template <unsigned N, unsigned S>
constexpr inline bool isShiftedUInt(uint64_t x) {
static_assert(
N > 0, "isShiftedUInt<0> doesn't make sense (refers to a 0-bit number)");
constexpr bool isShiftedUInt(uint64_t x) {
static_assert(S < 64, "isShiftedUInt<N, S> with S >= 64 is too much.");
static_assert(N + S <= 64,
"isShiftedUInt<N, S> with N + S > 64 is too wide.");
// Per the two static_asserts above, S must be strictly less than 64. So
// 1 << S is not undefined behavior.
// S must be strictly less than 64. So 1 << S is not undefined behavior.
return isUInt<N + S>(x) && (x % (UINT64_C(1) << S) == 0);
}
/// Gets the maximum value for a N-bit unsigned integer.
inline uint64_t maxUIntN(uint64_t N) {
assert(N > 0 && N <= 64 && "integer width out of range");
assert(N <= 64 && "integer width out of range");
// uint64_t(1) << 64 is undefined behavior, so we can't do
// (uint64_t(1) << N) - 1
// without checking first that N != 64. But this works and doesn't have a
// branch.
// branch for N != 0.
// Unfortunately, shifting a uint64_t right by 64 bit is undefined
// behavior, so the condition on N == 0 is necessary. Fortunately, most
// optimizers do not emit branches for this check.
if (N == 0)
return 0;
return UINT64_MAX >> (64 - N);
}
@@ -180,8 +203,10 @@ inline uint64_t maxUIntN(uint64_t N) {
/// Gets the minimum value for a N-bit signed integer.
inline int64_t minIntN(int64_t N) {
assert(N > 0 && N <= 64 && "integer width out of range");
assert(N >= 0 && N <= 64 && "integer width out of range");
if (N == 0)
return 0;
return UINT64_C(1) + ~(UINT64_C(1) << (N - 1));
}
@@ -191,10 +216,12 @@ inline int64_t minIntN(int64_t N) {
/// Gets the maximum value for a N-bit signed integer.
inline int64_t maxIntN(int64_t N) {
assert(N > 0 && N <= 64 && "integer width out of range");
assert(N >= 0 && N <= 64 && "integer width out of range");
// This relies on two's complement wraparound when N == 64, so we convert to
// int64_t only at the very end to avoid UB.
if (N == 0)
return 0;
return (UINT64_C(1) << (N - 1)) - 1;
}
@@ -211,36 +238,36 @@ inline bool isIntN(unsigned N, int64_t x) {
/// Return true if the argument is a non-empty sequence of ones starting at the
/// least significant bit with the remainder zero (32 bit version).
/// Ex. isMask_32(0x0000FFFFU) == true.
constexpr inline bool isMask_32(uint32_t Value) {
constexpr bool isMask_32(uint32_t Value) {
return Value && ((Value + 1) & Value) == 0;
}
/// Return true if the argument is a non-empty sequence of ones starting at the
/// least significant bit with the remainder zero (64 bit version).
constexpr inline bool isMask_64(uint64_t Value) {
constexpr bool isMask_64(uint64_t Value) {
return Value && ((Value + 1) & Value) == 0;
}
/// Return true if the argument contains a non-empty sequence of ones with the
/// remainder zero (32 bit version.) Ex. isShiftedMask_32(0x0000FF00U) == true.
constexpr inline bool isShiftedMask_32(uint32_t Value) {
constexpr bool isShiftedMask_32(uint32_t Value) {
return Value && isMask_32((Value - 1) | Value);
}
/// Return true if the argument contains a non-empty sequence of ones with the
/// remainder zero (64 bit version.)
constexpr inline bool isShiftedMask_64(uint64_t Value) {
constexpr bool isShiftedMask_64(uint64_t Value) {
return Value && isMask_64((Value - 1) | Value);
}
/// Return true if the argument is a power of two > 0.
/// Ex. isPowerOf2_32(0x00100000U) == true (32 bit edition.)
constexpr inline bool isPowerOf2_32(uint32_t Value) {
constexpr bool isPowerOf2_32(uint32_t Value) {
return std::has_single_bit(Value);
}
/// Return true if the argument is a power of two > 0 (64 bit edition.)
constexpr inline bool isPowerOf2_64(uint64_t Value) {
constexpr bool isPowerOf2_64(uint64_t Value) {
return std::has_single_bit(Value);
}
@@ -273,13 +300,13 @@ inline bool isShiftedMask_64(uint64_t Value, unsigned &MaskIdx,
/// Compile time Log2.
/// Valid only for positive powers of two.
template <size_t kValue> constexpr inline size_t CTLog2() {
template <size_t kValue> constexpr size_t CTLog2() {
static_assert(kValue > 0 && wpi::isPowerOf2_64(kValue),
"Value is not a valid power of 2");
return 1 + CTLog2<kValue / 2>();
}
template <> constexpr inline size_t CTLog2<1>() { return 0; }
template <> constexpr size_t CTLog2<1>() { return 0; }
/// Return the floor log base 2 of the specified value, -1 if the value is zero.
/// (32 bit edition.)
@@ -309,7 +336,8 @@ inline unsigned Log2_64_Ceil(uint64_t Value) {
/// A and B are either alignments or offsets. Return the minimum alignment that
/// may be assumed after adding the two together.
constexpr inline uint64_t MinAlign(uint64_t A, uint64_t B) {
template <typename U, typename V, typename T = common_uint<U, V>>
constexpr T MinAlign(U A, V B) {
// The largest power of 2 that divides both A and B.
//
// Replace "-Value" by "1+~Value" in the following commented code to avoid
@@ -318,9 +346,14 @@ constexpr inline uint64_t MinAlign(uint64_t A, uint64_t B) {
return (A | B) & (1 + ~(A | B));
}
/// Fallback when arguments aren't integral.
constexpr uint64_t MinAlign(uint64_t A, uint64_t B) {
return (A | B) & (1 + ~(A | B));
}
/// Returns the next power of two (in 64-bits) that is strictly greater than A.
/// Returns zero on overflow.
constexpr inline uint64_t NextPowerOf2(uint64_t A) {
constexpr uint64_t NextPowerOf2(uint64_t A) {
A |= (A >> 1);
A |= (A >> 2);
A |= (A >> 4);
@@ -338,7 +371,81 @@ inline uint64_t PowerOf2Ceil(uint64_t A) {
return UINT64_C(1) << Log2_64_Ceil(A);
}
/// Returns the next integer (mod 2**64) that is greater than or equal to
/// Returns the integer ceil(Numerator / Denominator). Unsigned version.
/// Guaranteed to never overflow.
template <typename U, typename V, typename T = common_uint<U, V>>
constexpr T divideCeil(U Numerator, V Denominator) {
assert(Denominator && "Division by zero");
T Bias = (Numerator != 0);
return (Numerator - Bias) / Denominator + Bias;
}
/// Fallback when arguments aren't integral.
constexpr uint64_t divideCeil(uint64_t Numerator, uint64_t Denominator) {
assert(Denominator && "Division by zero");
uint64_t Bias = (Numerator != 0);
return (Numerator - Bias) / Denominator + Bias;
}
// Check whether divideCeilSigned or divideFloorSigned would overflow. This
// happens only when Numerator = INT_MIN and Denominator = -1.
template <typename U, typename V>
constexpr bool divideSignedWouldOverflow(U Numerator, V Denominator) {
return Numerator == (std::numeric_limits<U>::min)() && Denominator == -1;
}
/// Returns the integer ceil(Numerator / Denominator). Signed version.
/// Overflow is explicitly forbidden with an assert.
template <typename U, typename V, typename T = common_sint<U, V>>
constexpr T divideCeilSigned(U Numerator, V Denominator) {
assert(Denominator && "Division by zero");
assert(!divideSignedWouldOverflow(Numerator, Denominator) &&
"Divide would overflow");
if (!Numerator)
return 0;
// C's integer division rounds towards 0.
T Bias = Denominator >= 0 ? 1 : -1;
bool SameSign = (Numerator >= 0) == (Denominator >= 0);
return SameSign ? (Numerator - Bias) / Denominator + 1
: Numerator / Denominator;
}
/// Returns the integer floor(Numerator / Denominator). Signed version.
/// Overflow is explicitly forbidden with an assert.
template <typename U, typename V, typename T = common_sint<U, V>>
constexpr T divideFloorSigned(U Numerator, V Denominator) {
assert(Denominator && "Division by zero");
assert(!divideSignedWouldOverflow(Numerator, Denominator) &&
"Divide would overflow");
if (!Numerator)
return 0;
// C's integer division rounds towards 0.
T Bias = Denominator >= 0 ? -1 : 1;
bool SameSign = (Numerator >= 0) == (Denominator >= 0);
return SameSign ? Numerator / Denominator
: (Numerator - Bias) / Denominator - 1;
}
/// Returns the remainder of the Euclidean division of LHS by RHS. Result is
/// always non-negative.
template <typename U, typename V, typename T = common_sint<U, V>>
constexpr T mod(U Numerator, V Denominator) {
assert(Denominator >= 1 && "Mod by non-positive number");
T Mod = Numerator % Denominator;
return Mod < 0 ? Mod + Denominator : Mod;
}
/// Returns (Numerator / Denominator) rounded by round-half-up. Guaranteed to
/// never overflow.
template <typename U, typename V, typename T = common_uint<U, V>>
constexpr T divideNearest(U Numerator, V Denominator) {
assert(Denominator && "Division by zero");
T Mod = Numerator % Denominator;
return (Numerator / Denominator) +
(Mod > (static_cast<T>(Denominator) - 1) / 2);
}
/// Returns the next integer (mod 2**nbits) that is greater than or equal to
/// \p Value and is a multiple of \p Align. \p Align must be non-zero.
///
/// Examples:
@@ -348,18 +455,29 @@ inline uint64_t PowerOf2Ceil(uint64_t A) {
/// alignTo(~0LL, 8) = 0
/// alignTo(321, 255) = 510
/// \endcode
inline uint64_t alignTo(uint64_t Value, uint64_t Align) {
///
/// Will overflow only if result is not representable in T.
template <typename U, typename V, typename T = common_uint<U, V>>
constexpr T alignTo(U Value, V Align) {
assert(Align != 0u && "Align can't be 0.");
return (Value + Align - 1) / Align * Align;
T CeilDiv = divideCeil(Value, Align);
return CeilDiv * Align;
}
inline uint64_t alignToPowerOf2(uint64_t Value, uint64_t Align) {
/// Fallback when arguments aren't integral.
constexpr uint64_t alignTo(uint64_t Value, uint64_t Align) {
assert(Align != 0u && "Align can't be 0.");
uint64_t CeilDiv = divideCeil(Value, Align);
return CeilDiv * Align;
}
constexpr uint64_t alignToPowerOf2(uint64_t Value, uint64_t Align) {
assert(Align != 0 && (Align & (Align - 1)) == 0 &&
"Align must be a power of 2");
// Replace unary minus to avoid compilation error on Windows:
// "unary minus operator applied to unsigned type, result still unsigned"
uint64_t negAlign = (~Align) + 1;
return (Value + Align - 1) & negAlign;
uint64_t NegAlign = (~Align) + 1;
return (Value + Align - 1) & NegAlign;
}
/// If non-zero \p Skew is specified, the return value will be a minimal integer
@@ -374,73 +492,78 @@ inline uint64_t alignToPowerOf2(uint64_t Value, uint64_t Align) {
/// alignTo(~0LL, 8, 3) = 3
/// alignTo(321, 255, 42) = 552
/// \endcode
inline uint64_t alignTo(uint64_t Value, uint64_t Align, uint64_t Skew) {
///
/// May overflow.
template <typename U, typename V, typename W,
typename T = common_uint<common_uint<U, V>, W>>
constexpr T alignTo(U Value, V Align, W Skew) {
assert(Align != 0u && "Align can't be 0.");
Skew %= Align;
return alignTo(Value - Skew, Align) + Skew;
}
/// Returns the next integer (mod 2**64) that is greater than or equal to
/// Returns the next integer (mod 2**nbits) that is greater than or equal to
/// \p Value and is a multiple of \c Align. \c Align must be non-zero.
template <uint64_t Align> constexpr inline uint64_t alignTo(uint64_t Value) {
///
/// Will overflow only if result is not representable in T.
template <auto Align, typename V, typename T = common_uint<decltype(Align), V>>
constexpr T alignTo(V Value) {
static_assert(Align != 0u, "Align must be non-zero");
return (Value + Align - 1) / Align * Align;
T CeilDiv = divideCeil(Value, Align);
return CeilDiv * Align;
}
/// Returns the integer ceil(Numerator / Denominator).
inline uint64_t divideCeil(uint64_t Numerator, uint64_t Denominator) {
return alignTo(Numerator, Denominator) / Denominator;
}
/// Returns the integer nearest(Numerator / Denominator).
inline uint64_t divideNearest(uint64_t Numerator, uint64_t Denominator) {
return (Numerator + (Denominator / 2)) / Denominator;
}
/// Returns the largest uint64_t less than or equal to \p Value and is
/// \p Skew mod \p Align. \p Align must be non-zero
inline uint64_t alignDown(uint64_t Value, uint64_t Align, uint64_t Skew = 0) {
/// Returns the largest unsigned integer less than or equal to \p Value and is
/// \p Skew mod \p Align. \p Align must be non-zero. Guaranteed to never
/// overflow.
template <typename U, typename V, typename W = uint8_t,
typename T = common_uint<common_uint<U, V>, W>>
constexpr T alignDown(U Value, V Align, W Skew = 0) {
assert(Align != 0u && "Align can't be 0.");
Skew %= Align;
return (Value - Skew) / Align * Align + Skew;
}
/// Sign-extend the number in the bottom B bits of X to a 32-bit integer.
/// Requires 0 < B <= 32.
template <unsigned B> constexpr inline int32_t SignExtend32(uint32_t X) {
static_assert(B > 0, "Bit width can't be 0.");
/// Requires B <= 32.
template <unsigned B> constexpr int32_t SignExtend32(uint32_t X) {
static_assert(B <= 32, "Bit width out of range.");
if constexpr (B == 0)
return 0;
return int32_t(X << (32 - B)) >> (32 - B);
}
/// Sign-extend the number in the bottom B bits of X to a 32-bit integer.
/// Requires 0 < B <= 32.
/// Requires B <= 32.
inline int32_t SignExtend32(uint32_t X, unsigned B) {
assert(B > 0 && "Bit width can't be 0.");
assert(B <= 32 && "Bit width out of range.");
if (B == 0)
return 0;
return int32_t(X << (32 - B)) >> (32 - B);
}
/// Sign-extend the number in the bottom B bits of X to a 64-bit integer.
/// Requires 0 < B <= 64.
template <unsigned B> constexpr inline int64_t SignExtend64(uint64_t x) {
static_assert(B > 0, "Bit width can't be 0.");
/// Requires B <= 64.
template <unsigned B> constexpr int64_t SignExtend64(uint64_t x) {
static_assert(B <= 64, "Bit width out of range.");
if constexpr (B == 0)
return 0;
return int64_t(x << (64 - B)) >> (64 - B);
}
/// Sign-extend the number in the bottom B bits of X to a 64-bit integer.
/// Requires 0 < B <= 64.
/// Requires B <= 64.
inline int64_t SignExtend64(uint64_t X, unsigned B) {
assert(B > 0 && "Bit width can't be 0.");
assert(B <= 64 && "Bit width out of range.");
if (B == 0)
return 0;
return int64_t(X << (64 - B)) >> (64 - B);
}
/// Subtract two unsigned integers, X and Y, of type T and return the absolute
/// value of the result.
template <typename T>
std::enable_if_t<std::is_unsigned_v<T>, T> AbsoluteDifference(T X, T Y) {
template <typename U, typename V, typename T = common_uint<U, V>>
constexpr T AbsoluteDifference(U X, V Y) {
return X > Y ? (X - Y) : (Y - X);
}
@@ -538,7 +661,6 @@ SaturatingMultiplyAdd(T X, T Y, T A, bool *ResultOverflowed = nullptr) {
/// Use this rather than HUGE_VALF; the latter causes warnings on MSVC.
extern const float huge_valf;
/// Add two signed integers, computing the two's complement truncated result,
/// returning true if overflow occurred.
template <typename T>
@@ -595,6 +717,9 @@ std::enable_if_t<std::is_signed_v<T>, T> SubOverflow(T X, T Y, T &Result) {
/// result, returning true if an overflow ocurred.
template <typename T>
std::enable_if_t<std::is_signed_v<T>, T> MulOverflow(T X, T Y, T &Result) {
#if __has_builtin(__builtin_mul_overflow)
return __builtin_mul_overflow(X, Y, &Result);
#else
// Perform the unsigned multiplication on absolute values.
using U = std::make_unsigned_t<T>;
const U UX = X < 0 ? (0 - static_cast<U>(X)) : static_cast<U>(X);
@@ -616,8 +741,17 @@ std::enable_if_t<std::is_signed_v<T>, T> MulOverflow(T X, T Y, T &Result) {
return UX > (static_cast<U>((std::numeric_limits<T>::max)()) + U(1)) / UY;
else
return UX > (static_cast<U>((std::numeric_limits<T>::max)())) / UY;
#endif
}
/// Type to force float point values onto the stack, so that x86 doesn't add
/// hidden precision, avoiding rounding differences on various platforms.
#if defined(__i386__) || defined(_M_IX86)
using stack_float_t = volatile float;
#else
using stack_float_t = float;
#endif
// Typesafe implementation of the signum function.
// Returns -1 if negative, 1 if positive, 0 if 0.
template <typename T>
@@ -638,6 +772,7 @@ template <typename T>
constexpr T Lerp(const T& startValue, const T& endValue, double t) {
return startValue + (endValue - startValue) * t;
}
} // End wpi namespace
} // namespace wpi
#endif

View File

@@ -25,6 +25,7 @@
#include <cstring>
#include <initializer_list>
#include <iterator>
#include <limits>
#include <utility>
namespace wpi {
@@ -126,22 +127,11 @@ protected:
std::pair<const void *const *, bool> insert_imp(const void *Ptr) {
if (isSmall()) {
// Check to see if it is already in the set.
const void **LastTombstone = nullptr;
for (const void **APtr = SmallArray, **E = SmallArray + NumNonEmpty;
APtr != E; ++APtr) {
const void *Value = *APtr;
if (Value == Ptr)
return std::make_pair(APtr, false);
if (Value == getTombstoneMarker())
LastTombstone = APtr;
}
// Did we find any tombstone marker?
if (LastTombstone != nullptr) {
*LastTombstone = Ptr;
--NumTombstones;
incrementEpoch();
return std::make_pair(LastTombstone, true);
}
// Nope, there isn't. If we stay small, just 'pushback' now.
@@ -160,14 +150,27 @@ protected:
/// that the derived class can check that the right type of pointer is passed
/// in.
bool erase_imp(const void * Ptr) {
const void *const *P = find_imp(Ptr);
if (P == EndPointer())
if (isSmall()) {
for (const void **APtr = SmallArray, **E = SmallArray + NumNonEmpty;
APtr != E; ++APtr) {
if (*APtr == Ptr) {
*APtr = SmallArray[--NumNonEmpty];
incrementEpoch();
return true;
}
}
return false;
}
auto *Bucket = FindBucketFor(Ptr);
if (*Bucket != Ptr)
return false;
const void **Loc = const_cast<const void **>(P);
assert(*Loc == Ptr && "broken find!");
*Loc = getTombstoneMarker();
*const_cast<const void **>(Bucket) = getTombstoneMarker();
NumTombstones++;
// Treat this consistently from an API perspective, even if we don't
// actually invalidate iterators here.
incrementEpoch();
return true;
}
@@ -191,9 +194,9 @@ protected:
return EndPointer();
}
private:
bool isSmall() const { return CurArray == SmallArray; }
private:
std::pair<const void *const *, bool> insert_imp_big(const void *Ptr);
const void * const *FindBucketFor(const void *Ptr) const;
@@ -311,31 +314,6 @@ public:
}
};
/// RoundUpToPowerOfTwo - This is a helper template that rounds N up to the next
/// power of two (which means N itself if N is already a power of two).
template<unsigned N>
struct RoundUpToPowerOfTwo;
/// RoundUpToPowerOfTwoH - If N is not a power of two, increase it. This is a
/// helper template used to implement RoundUpToPowerOfTwo.
template<unsigned N, bool isPowerTwo>
struct RoundUpToPowerOfTwoH {
enum { Val = N };
};
template<unsigned N>
struct RoundUpToPowerOfTwoH<N, false> {
enum {
// We could just use NextVal = N+1, but this converges faster. N|(N-1) sets
// the right-most zero bits to one all at once, e.g. 0b0011000 -> 0b0011111.
Val = RoundUpToPowerOfTwo<(N|(N-1)) + 1>::Val
};
};
template<unsigned N>
struct RoundUpToPowerOfTwo {
enum { Val = RoundUpToPowerOfTwoH<N, (N&(N-1)) == 0>::Val };
};
/// A templated base class for \c SmallPtrSet which provides the
/// typesafe interface that is common across all small sizes.
///
@@ -375,11 +353,61 @@ public:
return insert(Ptr).first;
}
/// erase - If the set contains the specified pointer, remove it and return
/// true, otherwise return false.
/// Remove pointer from the set.
///
/// Returns whether the pointer was in the set. Invalidates iterators if
/// true is returned. To remove elements while iterating over the set, use
/// remove_if() instead.
bool erase(PtrType Ptr) {
return erase_imp(PtrTraits::getAsVoidPointer(Ptr));
}
/// Remove elements that match the given predicate.
///
/// This method is a safe replacement for the following pattern, which is not
/// valid, because the erase() calls would invalidate the iterator:
///
/// for (PtrType *Ptr : Set)
/// if (Pred(P))
/// Set.erase(P);
///
/// Returns whether anything was removed. It is safe to read the set inside
/// the predicate function. However, the predicate must not modify the set
/// itself, only indicate a removal by returning true.
template <typename UnaryPredicate>
bool remove_if(UnaryPredicate P) {
bool Removed = false;
if (isSmall()) {
const void **APtr = SmallArray, **E = SmallArray + NumNonEmpty;
while (APtr != E) {
PtrType Ptr = PtrTraits::getFromVoidPointer(const_cast<void *>(*APtr));
if (P(Ptr)) {
*APtr = *--E;
--NumNonEmpty;
incrementEpoch();
Removed = true;
} else {
++APtr;
}
}
return Removed;
}
for (const void **APtr = CurArray, **E = EndPointer(); APtr != E; ++APtr) {
const void *Value = *APtr;
if (Value == getTombstoneMarker() || Value == getEmptyMarker())
continue;
PtrType Ptr = PtrTraits::getFromVoidPointer(const_cast<void *>(Value));
if (P(Ptr)) {
*APtr = getTombstoneMarker();
++NumTombstones;
incrementEpoch();
Removed = true;
}
}
return Removed;
}
/// count - Return 1 if the specified pointer is in the set, 0 otherwise.
size_type count(ConstPtrType Ptr) const {
return find_imp(ConstPtrTraits::getAsVoidPointer(Ptr)) != EndPointer();
@@ -456,8 +484,18 @@ class SmallPtrSet : public SmallPtrSetImpl<PtrType> {
using BaseT = SmallPtrSetImpl<PtrType>;
// A constexpr version of wpi::bit_ceil.
// TODO: Replace this with std::bit_ceil once C++20 is available.
static constexpr size_t RoundUpToPowerOfTwo(size_t X) {
size_t C = 1;
size_t CMax = C << (std::numeric_limits<size_t>::digits - 1);
while (C < X && C < CMax)
C <<= 1;
return C;
}
// Make sure that SmallSize is a power of two, round up if not.
enum { SmallSizePowTwo = RoundUpToPowerOfTwo<SmallSize>::Val };
static constexpr size_t SmallSizePowTwo = RoundUpToPowerOfTwo(SmallSize);
/// SmallStorage - Fixed size storage used in 'small mode'.
const void *SmallStorage[SmallSizePowTwo];

View File

@@ -27,6 +27,7 @@
#include <algorithm>
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <functional>

View File

@@ -12,6 +12,7 @@
#include <system_error>
namespace wpi {
std::error_code mapLastWindowsError();
std::error_code mapWindowsError(unsigned EV);
}

View File

@@ -48,9 +48,10 @@ public:
// See https://github.com/llvm/llvm-project/issues/63843
template <typename Container>
#else
template <typename Container,
std::enable_if_t<explicitly_convertible<
detail::IterOfRange<Container>, IteratorT>::value> * = nullptr>
template <
typename Container,
std::enable_if_t<explicitly_convertible<
wpi::detail::IterOfRange<Container>, IteratorT>::value> * = nullptr>
#endif
iterator_range(Container &&c)
: begin_iterator(adl_begin(c)), end_iterator(adl_end(c)) {
@@ -65,7 +66,8 @@ public:
};
template <typename Container>
iterator_range(Container &&) -> iterator_range<detail::IterOfRange<Container>>;
iterator_range(Container &&)
-> iterator_range<wpi::detail::IterOfRange<Container>>;
/// Convenience function for iterating over sub-ranges.
///

View File

@@ -46,6 +46,7 @@ public:
enum class OStreamKind {
OK_OStream,
OK_FDStream,
OK_SVecStream,
};
private:
@@ -71,10 +72,6 @@ private:
/// this buffer.
char *OutBufStart, *OutBufEnd, *OutBufCur;
/// Optional stream this stream is tied to. If this stream is written to, the
/// tied-to stream will be flushed first.
raw_ostream *TiedStream = nullptr;
enum class BufferKind {
Unbuffered = 0,
InternalBuffer,
@@ -92,6 +89,14 @@ public:
MAGENTA,
CYAN,
WHITE,
BRIGHT_BLACK,
BRIGHT_RED,
BRIGHT_GREEN,
BRIGHT_YELLOW,
BRIGHT_BLUE,
BRIGHT_MAGENTA,
BRIGHT_CYAN,
BRIGHT_WHITE,
SAVEDCOLOR,
RESET,
};
@@ -104,6 +109,14 @@ public:
static constexpr Colors MAGENTA = Colors::MAGENTA;
static constexpr Colors CYAN = Colors::CYAN;
static constexpr Colors WHITE = Colors::WHITE;
static constexpr Colors BRIGHT_BLACK = Colors::BRIGHT_BLACK;
static constexpr Colors BRIGHT_RED = Colors::BRIGHT_RED;
static constexpr Colors BRIGHT_GREEN = Colors::BRIGHT_GREEN;
static constexpr Colors BRIGHT_YELLOW = Colors::BRIGHT_YELLOW;
static constexpr Colors BRIGHT_BLUE = Colors::BRIGHT_BLUE;
static constexpr Colors BRIGHT_MAGENTA = Colors::BRIGHT_MAGENTA;
static constexpr Colors BRIGHT_CYAN = Colors::BRIGHT_CYAN;
static constexpr Colors BRIGHT_WHITE = Colors::BRIGHT_WHITE;
static constexpr Colors SAVEDCOLOR = Colors::SAVEDCOLOR;
static constexpr Colors RESET = Colors::RESET;
@@ -320,10 +333,6 @@ public:
bool colors_enabled() const { return false; }
/// Tie this stream to the specified stream. Replaces any existing tied-to
/// stream. Specifying a nullptr unties the stream.
void tie(raw_ostream *TieTo) { TiedStream = TieTo; }
//===--------------------------------------------------------------------===//
// Subclass Interface
//===--------------------------------------------------------------------===//
@@ -383,9 +392,6 @@ private:
/// unused bytes in the buffer.
void copy_to_buffer(const char *Ptr, size_t Size);
/// Flush the tied-to stream (if present) and then write the required data.
void flush_tied_then_write(const char *Ptr, size_t Size);
virtual void anchor();
};
@@ -435,6 +441,10 @@ class raw_fd_ostream : public raw_pwrite_stream {
bool SupportsSeeking = false;
bool IsRegularFile = false;
/// Optional stream this stream is tied to. If this stream is written to, the
/// tied-to stream will be flushed first.
raw_ostream *TiedStream = nullptr;
#ifdef _WIN32
/// True if this fd refers to a Windows console device. Mintty and other
/// terminal emulators are TTYs, but they are not consoles.
@@ -509,6 +519,13 @@ public:
/// to the offset specified from the beginning of the file.
uint64_t seek(uint64_t off);
/// Tie this stream to the specified stream. Replaces any existing tied-to
/// stream. Specifying a nullptr unties the stream. This is intended for to
/// tie errs() to outs(), so that outs() is flushed whenever something is
/// written to errs(), preventing weird and hard-to-test output when stderr
/// is redirected to stdout.
void tie(raw_ostream *TieTo) { TiedStream = TieTo; }
std::error_code error() const { return EC; }
/// Return the value of the flag in this raw_fd_ostream indicating whether an
@@ -615,7 +632,11 @@ public:
///
/// \param O The vector to write to; this should generally have at least 128
/// bytes free to avoid any extraneous memory overhead.
explicit raw_svector_ostream(SmallVectorImpl<char> &O) : OS(O) {
explicit raw_svector_ostream(SmallVectorImpl<char> &O)
: raw_pwrite_stream(false, raw_ostream::OStreamKind::OK_SVecStream),
OS(O) {
// FIXME: here and in a few other places, set directly to unbuffered in the
// ctor.
SetUnbuffered();
}
@@ -625,10 +646,13 @@ public:
/// Return a std::string_view for the vector contents.
std::string_view str() const { return std::string_view(OS.data(), OS.size()); }
SmallVectorImpl<char> &buffer() { return OS; }
void reserveExtraSpace(uint64_t ExtraSize) override {
OS.reserve(tell() + ExtraSize);
}
static bool classof(const raw_ostream *OS);
};
/// A raw_ostream that writes to a vector. This is a

View File

@@ -44,6 +44,7 @@
#include <string_view>
namespace wpi {
uint64_t xxHash64(std::string_view Data);
uint64_t xxHash64(std::span<const uint8_t> Data);
@@ -51,6 +52,30 @@ uint64_t xxh3_64bits(std::span<const uint8_t> data);
inline uint64_t xxh3_64bits(std::string_view data) {
return xxh3_64bits(std::span(reinterpret_cast<const uint8_t*>(data.data()), data.size()));
}
}
/*-**********************************************************************
* XXH3 128-bit variant
************************************************************************/
/*!
* @brief The return value from 128-bit hashes.
*
* Stored in little endian order, although the fields themselves are in native
* endianness.
*/
struct XXH128_hash_t {
uint64_t low64; /*!< `value & 0xFFFFFFFFFFFFFFFF` */
uint64_t high64; /*!< `value >> 64` */
/// Convenience equality check operator.
bool operator==(const XXH128_hash_t rhs) const {
return low64 == rhs.low64 && high64 == rhs.high64;
}
};
/// XXH3's 128-bit variant.
XXH128_hash_t xxh3_128bits(std::span<const uint8_t> data);
} // namespace wpi
#endif