Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 11 additions & 18 deletions ml_dtypes/include/float8.h
Original file line number Diff line number Diff line change
Expand Up @@ -1194,26 +1194,19 @@ template <typename T>
bool constexpr IsPowerOfTwo(T x) {
return (x != 0) && ((x & (x - 1)) == 0);
}
template <long long N>
constexpr unsigned int MostSignificantBit() {
unsigned int result = 0;
long long x = N;
while (x >>= 1) {
++result;
}
return result; // return N == 0 ? 0 : (sizeof(long long) * 8 - 1 - __builtin_clz(N));
}
// Helper for getting a bytes size which is a power of two.
template <int Size>
template <long long Size>
struct NextPowerOfTwo {
static constexpr int value = Size;
};
template <>
struct NextPowerOfTwo<3> {
static constexpr int value = 4;
};
template <>
struct NextPowerOfTwo<5> {
static constexpr int value = 8;
};
template <>
struct NextPowerOfTwo<6> {
static constexpr int value = 8;
};
template <>
struct NextPowerOfTwo<7> {
static constexpr int value = 8;
static constexpr int value = IsPowerOfTwo(Size) ? Size : 2 << MostSignificantBit<Size>();
};

// Helper for getting a bit representation provided a byte size.
Expand Down