Skip to content

Commit 26b2323

Browse files
committed
include/float8.h:NextPowerOfTwo; infinite values
+`ml_dtypes/include/float8.h:MostSignificantBit`; `constexpr` version of `clz` opcode. *`ml_dtypes/include/float8.h:NextPowerOfTwo`; replace long list of hardcoded values with `2 << MostSignificantBit<Size>`, which extends `NextPowerOfTwo` (was limited to integer sizes, now is less code plus is general use).
1 parent 8f0b222 commit 26b2323

File tree

1 file changed

+10
-17
lines changed

1 file changed

+10
-17
lines changed

ml_dtypes/include/float8.h

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1194,26 +1194,19 @@ template <typename T>
11941194
bool constexpr IsPowerOfTwo(T x) {
11951195
return (x != 0) && ((x & (x - 1)) == 0);
11961196
}
1197+
template <unsigned int N>
1198+
constexpr unsigned int MostSignificantBit() {
1199+
unsigned int result = 0;
1200+
unsigned int x = N;
1201+
while (x >>= 1) {
1202+
++result;
1203+
}
1204+
return result; // return N == 0 ? 0 : (sizeof(long long) * 8 - 1 - __builtin_clz(N));
1205+
}
11971206
// Helper for getting a bytes size which is a power of two.
11981207
template <int Size>
11991208
struct NextPowerOfTwo {
1200-
static constexpr int value = Size;
1201-
};
1202-
template <>
1203-
struct NextPowerOfTwo<3> {
1204-
static constexpr int value = 4;
1205-
};
1206-
template <>
1207-
struct NextPowerOfTwo<5> {
1208-
static constexpr int value = 8;
1209-
};
1210-
template <>
1211-
struct NextPowerOfTwo<6> {
1212-
static constexpr int value = 8;
1213-
};
1214-
template <>
1215-
struct NextPowerOfTwo<7> {
1216-
static constexpr int value = 8;
1209+
static constexpr int value = IsPowerOfTwo(Size) ? Size : 2 << MostSignificantBit<Size>();
12171210
};
12181211

12191212
// Helper for getting a bit representation provided a byte size.

0 commit comments

Comments
 (0)