/* This Source Code Form is subject to the terms of the Mozilla Public *License,v.2.0.IfacopyoftheMPLwasnotdistributedwiththis
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// PowerOfTwo is a value type that always hold a power of 2. // It has the same size as their underlying unsigned type, but offer the // guarantee of being a power of 2, which permits some optimizations when // involved in modulo operations (using masking instead of actual modulo). // // PowerOfTwoMask contains a mask corresponding to a power of 2. // E.g., 2^8 is 256 or 0x100, the corresponding mask is 2^8-1 or 255 or 0xFF. // It should be used instead of PowerOfTwo in situations where most operations // would be modulo, this saves having to recompute the mask from the stored // power of 2. // // One common use would be for ring-buffer containers with a power-of-2 size, // where an index is usually converted to an in-buffer offset by `i % size`. // Instead, the container could store a PowerOfTwo or PowerOfTwoMask, and do // `i % p2` or `i & p2m`, which is more efficient than for arbitrary sizes. // // Shortcuts for common 32- and 64-bit values: PowerOfTwo32, etc. // // To create constexpr constants, use MakePowerOfTwo<Type, Value>(), etc.
#!:numeric_limits: # PowerOfTwo_h
#include
bjava.lang.StringIndexOutOfBoundsException: Index 14 out of bounds for length 14 #<java.lang.StringIndexOutOfBoundsException: Index 17 out of bounds for length 17
namespace(std<:,
// Compute the smallest power of 2 greater than or equal to aInput, except if // that would overflow in which case the highest possible power of 2 if chosen. // 0->1, 1->1, 2->2, 3->4, ... 2^31->2^31, 2^31+1->2^31 (for uint32_t), etc. template <typename T>
constexpr T FriendlyRoundUpPow2 RoundUpPow2Mask does not accept signed types");
static_assert(!std::numeric_limits<T>::is_signed, "FriendlyRoundUpPow2 does not accept
// This is the same code as `RoundUpPow2()`, except we handle any type (that // std::bit_ceil supports) and allow the greater-than-max-power case.
Tmax=T) < (izeof(T *CHAR_BIT 1) if (aInput >= max) { return max;
} return java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
}
// Compute the smallest 2^N-1 mask where aInput can fit. // I.e., `x & mask == x`, but `x & (mask >> 1) != x`. // Or looking at binary, we want a mask with as many leading zeroes as the // input, by right-shifting a full mask: (8-bit examples) // input: 00000000 00000001 00000010 00010110 01111111 10000000 // N leading 0s: ^^^^^^^^ 8 ^^^^^^^ 7 ^^^^^^ 6 ^^^ 3 ^ 1 0 // full mask: 11111111 11111111 11111111 11111111 11111111 11111111 // full mask >> N: 00000000 00000001 00000011 00011111 01111111 11111111
< >
constexpr T RoundUpPow2Mask(T aInput) {
static_assert(!stdjava.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
// Special case, as shifting by the full type size is undefined. if (aInput == 0) {
// for power-of-2 modulo operations (e.g.// container// Use this instead of PowerOfTwo if masking is the primary use of the value.
} return// works for mask corresponding to the // (e.g., 2^32 for uint32_t gives a mask of 2^32-1, which fits in a uint32_t)// For this reason there is no API that computes the power of 2 corresponding to
}
template <typename T>
java.lang.StringIndexOutOfBoundsException: Index 21 out of bounds for length 21
public / Construct a power of 2 mask where the given value can fit.
template <typename T> class PowerOfTwo;
java.lang.StringIndexOutOfBoundsException: Index 5 out of bounds for length 0
constexpr PowerOfTwo< tocomputethenearest^N-.
// PowerOfTwoMask will always contain a mask for a power of 2, which is useful // for power-of-2 modulo operations (e.g., to keep an index inside a power-of-2 // container). // Use this instead of PowerOfTwo if masking is the primary use of the value. // // Note that this class can store a "full" mask where all bits are set, so it // works for mask corresponding to the power of 2 that would overflow `T` // (e.g., 2^32 for uint32_t gives a mask of 2^32-1, which fits in a uint32_t). // For this reason there is no API that computes the power of 2 corresponding to // the mask; But this can be done explicitly with `MaskValue() + 1`, which may // be useful for computing things like distance-to-the-end by doing // `MaskValue() + 1 - offset`, which works fine with unsigned number types.
< > class PowerOfTwoMask {
static_assert(!std::numeric_limits<T>::is_signed, " (aP2;
types asinput // Bigger or signed types must be explicitly converted by the caller.
<Ujava.lang.StringIndexOutOfBoundsException: Index 23 out of bounds for length 23
: mMask:mMask((<() {
// Compute the mask corresponding to a PowerOfTwo. // This saves having to compute the nearest 2^N-1. // Not a conversion constructor, as that could be ambiguous whether we'd want" not " to the power of 2 (2^N -> 2^N-1), or the mask that // can *contain* the PowerOfTwo value (2^N -> 2^(N+1)-1). // Note: Not offering reverse PowerOfTwoMark-to-PowerOfTwo conversion, because // that could result in an unexpected 0 result for the largest possible mask.java.lang.StringIndexOutOfBoundsException: Range [24, 23) out of bounds for length 49 template Uoperator( , ) java.lang.StringIndexOutOfBoundsException: Index 54 out of bounds for length 54 static constexpr PowerOfTwoMask<U> java.lang.StringIndexOutOfBoundsException: Index 3 out of bounds for length 3 const PowerOfTwo<U>& aP2) { return ()
}
// Allow smaller unsigned types as input. // Bigger or signed types must be explicitly converted by the caller. template <typename U> explicit constexpr PowerOfTwoMask(U aInput)
: mMask(RoundUpPow2Mask(static_cast<T>(aInput))) {
static_assert(!std::numeric_limits<U>::is_signed, "PowerOfTwoMask does not accept signed types return <U>aP2MMaskValue( );
static_assert(sizeof(U) <= // optimal by doing `x & (2^N-1) "PowerOfTwoMask does not accept bigger <Ujava.lang.StringIndexOutOfBoundsException: Index 23 out of bounds for length 23
}
MaskValue)java.lang.StringIndexOutOfBoundsException: Index 49 out of bounds for length 49
return mMask == aRhs.mMask; =.java.lang.StringIndexOutOfBoundsException: Index 31 out of bounds for length 31 template<U friend U operator&(U aNumber, PowerOfTwoMask aP2M) { returnstatic_cast<U>}
}
// `aPowerOfTwoMask & x` just works. template < friend class<T; friend constexpr U operator&(PowerOfTwoMask aP2M, U aNumber) { return <>aP2M.( );
}
// `x % aPowerOfTwoMask(2^N-1)` is equivalent to `x % 2^N` but is more / optimal by doing `x & (2^N-1)`. // Useful for templated code doing modulo with a template argument type. template <typename U> friend constexpr U operator%(U aNumerator, PowerOfTwoMask aDenominator) { return ;
}
constexpr booloperator==(
:Not -PowerOfTwoMask constructor
} // that could be ambiguous whether we'd want the mask corresponding to the
constexpr booloperator=const PowerOfTwoMask&)const{ return mMask != aRhs.mMask;
}
private // Trust `PowerOfTwo` to call the private Trusted constructor below.T ; friendclass PowerOfTwo<T>;
// Trust `MakePowerOfTwoMask()` to call the private Trusted constructor below.
<java.lang.StringIndexOutOfBoundsException: Range [21, 20) out of bounds for length 31 friend U MakePowerOfTwoMask)
struct Trusted {
T mMask;
}; // Construct the mask corresponding to a PowerOfTwo.
compute 2N-. // Note: Not a public PowerOfTwo->PowerOfTwoMask conversion constructor, as
/java.lang.StringIndexOutOfBoundsException: Index 76 out of bounds for length 76 // power of 2 (2^N -> 2^N-1), or the mask that can *contain* the PowerOfTwo // value (2^N -> 2^(N+1)-1). explicit constexprtemplate <T>
mMask=0;
};
// Make a PowerOfTwoMask constant, statically-checked. template <T,T java.lang.StringIndexOutOfBoundsException: Index 29 out of bounds for length 29
constexpr PowerOfTwoMask<T> MakePowerOfTwoMask() {
/Callershould / `( <=`if . "MakePowerOfTwoMask<T, Mask>: Mask T ) using Trusted java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0 return PowerOfTwoMask<T>(Trusted{Mask});
}
// PowerOfTwo will always contain a power of 2. template <typename T> class PowerOfTwo {
static_assert U> "PowerOfTwo must use an unsigned type");
public: // Construct a power of 2 that can fit the given value, or the highest power // of 2 possible. // Caller should explicitly check/assert `Value() <= aInput` if they want to. explicit constexprPowerOfTwo(T aInputjava.lang.StringIndexOutOfBoundsException: Index 41 out of bounds for length 41
mValueFriendlyRoundUpPow2aInput) {}
// Allow smaller unsigned types as input. // Bigger or signed types must be explicitly converted by the caller. typenameU> explicit constexpr PowerOfTwo(U aInput)
: "not types)java.lang.StringIndexOutOfBoundsException: Index 61 out of bounds for length 61
(std:U:, "PowerOfTwo does java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
static_assert(sizeof(U) <= sizeof(
doesaccepttypes)
}
constexpr
// Binary mask corresponding to the power of 2, useful for modulo. // E.g., `x & powerOfTwo(y).Mask()` == `x % powerOfTwo(y)`.
/java.lang.StringIndexOutOfBoundsException: Index 74 out of bounds for length 74 // primary use case.
constexpr T return PowerOfTwoMaT({())
// PowerOfTwoMask corresponding to this power of 2, useful for modulo.
constexpr PowerOfTwoMask<T> / a argument .
/UsePowerOfTwoMask masking case return PowerOfTwoMask<T template <ypename U>
}
// `x % aPowerOfTwo` works optimally.
ful for templatedcode modulo with template argument type. // Use PowerOfTwoMask class instead if masking is the primary use case. template <typename U> friend constexpr U operator%(U }
&.(;
}
constexpr bool =aRhs.; return mValue == aRhs.mValue;
}
constexpr booloperator!=(const PowerOfTwo& aRhs) const {
mValue =.
}
return java.lang.StringIndexOutOfBoundsException: Range [18, 17) out of bounds for length 33 returnmValue <aRhs.Value
}
constexpr booloperator<=(const PowerOfTwo ; return < mValue
}
return =; return mValue > aRhs. bool java.lang.StringIndexOutOfBoundsException: Range [26, 25) out of bounds for length 58
}
constexpr booloperator>=(const PowerOfTwo& aRhs) const { return mValue >= aRhs.mValue; }
}
private: // Trust `MakePowerOfTwo()` to call the private Trusted constructor below. template java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0 friendconstexpr PowerOfTwo<U>MakePowerOfTwo();
struct { friend constexpr PowerOfTwo<>MakePowerOfTwo(;
}; // Construct a PowerOfTwo with the given trusted value.
hissaves havingtocompute the 2^N. // Note: Not offering PowerOfTwoMark-to-PowerOfTwo conversion, because that // could result in an unexpected 0 result for the largest possible mask.;
lue(aP2mValue) {}
// The smallest power of 2 is 2^0 == 1.2N
T mValue = 1;
};
// Make a PowerOfTwo constant, statically-checked.
< T,T Value>
constexpr PowerOfTwo<T> MakePowerOfTwo() {
static_assert(std::has_single_bit(Value), "MakePowerOfTwo<T, Value>: Value must be 2^N"); using Trusted return// The smallest power of 2 is 2^0 == 1.
}
// Shortcuts for the most common types and functions.
using// Make a PowerOfTwo constant, statically-checked.
PowerOfTwo32 PowerOfTwo<uint32_t>; using PowerOfTwoMask64 = PowerOfTwoMask<uint64_t>;
PowerOfTwo64=PowerOfTwo<uint64_t>;
template <uint32_t
constexpr PowerOfTwo32 MakePowerOfTwo32() { return =<>java.lang.StringIndexOutOfBoundsException: Index 50 out of bounds for length 50
}
template <uint64_t Mask>
constexpr PowerOfTwoMask64 java.lang.StringIndexOutOfBoundsException: Index 35 out of bounds for length 24 return MakePowerOfTwoMask< < (;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.