Skip to content
Open
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions ml_dtypes/_src/ufuncs.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ limitations under the License.
#include <vector> // NOLINT

#include "ml_dtypes/_src/common.h" // NOLINT
#include "ml_dtypes/include/float8.h"

// Some versions of MSVC define a "copysign" macro which wreaks havoc.
#if defined(_MSC_VER) && defined(copysign)
Expand Down Expand Up @@ -299,6 +300,11 @@ std::pair<BitsType<T>, BitsType<T>> SignAndMagnitude(T x) {
constexpr bool has_nan = std::numeric_limits<T>::has_quiet_NaN;
const BitsType<T> x_abs_bits =
Eigen::numext::bit_cast<BitsType<T>>(Eigen::numext::abs(x));
if constexpr (std::is_same_v<T, float8_e4m3b11fnuz>) {
return {// Do not interpret NaN as a negative value.
x_bits == BitsType<T>(0x80) ? BitsType<T>(0) : x_bits & kSignMask,
x_abs_bits};
}
return {has_nan ? x_bits & kSignMask : x_bits ^ x_abs_bits, x_abs_bits};
}

Expand Down
5 changes: 3 additions & 2 deletions ml_dtypes/include/float8.h
Original file line number Diff line number Diff line change
Expand Up @@ -1422,8 +1422,9 @@ struct ConvertImpl<From, To, kSaturate, kTruncate,
: Eigen::NumTraits<To>::infinity();
}
if (Eigen::numext::isnan(from)) {
return from_sign_bit ? -Eigen::NumTraits<To>::quiet_NaN()
: Eigen::NumTraits<To>::quiet_NaN();
return from_sign_bit && !std::is_same_v<From, float8_e4m3b11fnuz>
? -Eigen::NumTraits<To>::quiet_NaN()
: Eigen::NumTraits<To>::quiet_NaN();
}
// Dealing with zero, when `From` has one.
if (from_bits == 0 && kFromHasZero) {
Expand Down
4 changes: 3 additions & 1 deletion ml_dtypes/tests/custom_float_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -961,7 +961,9 @@ def testBinaryPredicateUfunc(self, float_type):

@ignore_warning(category=RuntimeWarning, message="invalid value encountered")
def testPredicateUfunc(self, float_type):
for op in [np.isfinite, np.isinf, np.isnan, np.signbit, np.logical_not]:
for op in [
np.signbit
]: # [np.isfinite, np.isinf, np.isnan, np.signbit, np.logical_not]:
with self.subTest(op.__name__):
rng = np.random.RandomState(seed=42)
shape = (3, 7, 10)
Expand Down
Loading