From 26b23231db94c716ae8609234fc666014740df0a Mon Sep 17 00:00:00 2001 From: Swudu Susuwu <2002luvabbaluvu@gmail.com> Date: Fri, 16 May 2025 10:21:53 -0700 Subject: [PATCH 1/2] `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`, which extends `NextPowerOfTwo` (was limited to integer sizes, now is less code plus is general use). --- ml_dtypes/include/float8.h | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/ml_dtypes/include/float8.h b/ml_dtypes/include/float8.h index 7696c059..3a97d3e0 100644 --- a/ml_dtypes/include/float8.h +++ b/ml_dtypes/include/float8.h @@ -1194,26 +1194,19 @@ template bool constexpr IsPowerOfTwo(T x) { return (x != 0) && ((x & (x - 1)) == 0); } +template +constexpr unsigned int MostSignificantBit() { + unsigned int result = 0; + unsigned int 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 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(); }; // Helper for getting a bit representation provided a byte size. From 6291f38463653ecd709454aba4fb197d35fc2351 Mon Sep 17 00:00:00 2001 From: Swudu Susuwu <2002luvabbaluvu@gmail.com> Date: Fri, 16 May 2025 10:33:02 -0700 Subject: [PATCH 2/2] `NextPowerOfTwo`; `s/unsigned int/long long/` `MostSignificantBit`; `s/unsigned int/long long/` `NextPowerOfTwo`; `s/int/long long/` Ensures that those functions allowed the most possible amount of values. --- ml_dtypes/include/float8.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ml_dtypes/include/float8.h b/ml_dtypes/include/float8.h index 3a97d3e0..db9acdb8 100644 --- a/ml_dtypes/include/float8.h +++ b/ml_dtypes/include/float8.h @@ -1194,17 +1194,17 @@ template bool constexpr IsPowerOfTwo(T x) { return (x != 0) && ((x & (x - 1)) == 0); } -template +template constexpr unsigned int MostSignificantBit() { unsigned int result = 0; - unsigned int x = N; + 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 +template struct NextPowerOfTwo { static constexpr int value = IsPowerOfTwo(Size) ? Size : 2 << MostSignificantBit(); };