diff --git a/src/gfloat/round_ndarray.py b/src/gfloat/round_ndarray.py index d64372a..1f31642 100644 --- a/src/gfloat/round_ndarray.py +++ b/src/gfloat/round_ndarray.py @@ -68,9 +68,18 @@ def _frexp(v: npt.NDArray) -> Tuple[None, npt.NDArray]: ): return xp.frexp(v) - # Beware #49 - expval = xp.astype(xp.floor(xp.log2(v)), xp.int64) - return (None, expval) + exp = xp.astype(xp.floor(xp.log2(v)), xp.int64) + # log2 can round up just below a power of two. + # Check if 2**exp is greater than v, and decrement if so + # But 2**exp might overflow, so check if 2**(exp-1) > v/2 + # But halving is inexact for subnormal v, so do that shift + # only if exp > 0 (roughly the upper half of the floats) + shift = xp.astype(exp > 0, xp.int64) + two_to_e_maybe_shifted = 2.0 ** xp.astype(exp - shift, v.dtype) + # Multiplication by 1 can flush float32 subnormals to zero on ARM32. + v_maybe_shifted = v / 2.0 ** xp.astype(shift, v.dtype) + exp = exp - xp.astype(two_to_e_maybe_shifted > v_maybe_shifted, xp.int64) + return (None, exp + 1) def round_ndarray( diff --git a/test/test_array_api.py b/test/test_array_api.py index 7de8f70..9597824 100644 --- a/test/test_array_api.py +++ b/test/test_array_api.py @@ -1,5 +1,7 @@ # Copyright (c) 2024 Graphcore Ltd. All rights reserved. +from types import ModuleType + import array_api_strict as xp import numpy as np import pytest @@ -13,6 +15,7 @@ round_ndarray, ) from gfloat.formats import * +from gfloat.round_ndarray import _frexp xp.set_array_api_strict_flags(api_version="2024.12") @@ -29,3 +32,94 @@ def test_array_api(fi: FormatInfo, rnd: RoundMode, sat: bool) -> None: srbits = xp.asarray(srbits0) round_ndarray(fi, a, rnd, sat, srbits=srbits, srnumbits=srnumbits) # type: ignore + + +@pytest.mark.parametrize("namespace", (np, xp), ids=("numpy", "strict")) +@pytest.mark.parametrize("dtype_name", ("float32", "float64")) +def test_frexp_exponents(namespace: ModuleType, dtype_name: str) -> None: + dtype = np.dtype(dtype_name) + info = np.finfo(dtype) + exponents = [-126, -100, -2, 0, 1, 10, 100] + if dtype_name == "float64": + exponents += [-1022, -1000, 1000, 1023] + values = [ + float(info.smallest_subnormal), + float(info.smallest_normal), + float(info.max), + ] + for exponent in exponents: + value = np.asarray(2.0**exponent, dtype=dtype) + values += [ + float(np.nextafter(value, np.asarray(0.0, dtype=dtype))), + float(value), + float(np.nextafter(value, np.asarray(np.inf, dtype=dtype))), + ] + array = np.asarray(values, dtype=dtype).reshape(1, -1) + expected = np.frexp(array)[1] + with np.errstate(over="raise", invalid="raise"): + actual = _frexp(namespace.asarray(array))[1] + np.testing.assert_array_equal(np.asarray(actual), expected) + + +@pytest.mark.parametrize( + "fi", (format_info_ocp_e2m1, format_info_ocp_e2m3, format_info_ocp_e3m2) +) +@pytest.mark.parametrize("rnd", RoundMode) +@pytest.mark.parametrize("sat", (False, True)) +@pytest.mark.parametrize("dtype_name", ("float32", "float64")) +def test_array_api_rounding_agrees( + fi: FormatInfo, rnd: RoundMode, sat: bool, dtype_name: str +) -> None: + values = np.asarray( + [ + -5.75, + -5.0, + -3.25, + -2.75, + -2.5, + -1.75, + -1.25, + -0.75, + -0.5, + -0.25, + -0.0, + 0.0, + 0.25, + 0.5, + 0.75, + 1.25, + 1.75, + 2.5, + 2.75, + 3.25, + 5.0, + 5.75, + 2.5, + 2.5, + ], + dtype=dtype_name, + ).reshape(2, -1)[:, ::-1] + original = values.copy() + bits = np.resize(np.asarray([0, 1, 127, 128, 254, 255]), values.shape) + expected = np.asarray( + [ + round_float(fi, float(value), rnd, sat, srbits=int(bit), srnumbits=8) + for value, bit in zip(values.flat, bits.flat) + ] + ).reshape(values.shape) + native = round_ndarray(fi, values, rnd, sat, srbits=bits, srnumbits=8) + strict = round_ndarray( + fi, xp.asarray(values), rnd, sat, srbits=xp.asarray(bits), srnumbits=8 # type: ignore[arg-type] + ) + for actual in (native, np.asarray(strict)): + np.testing.assert_array_equal(actual, expected) + np.testing.assert_array_equal(np.signbit(actual), np.signbit(expected)) + np.testing.assert_array_equal(values, original) + np.testing.assert_array_equal(np.signbit(values), np.signbit(original)) + + +@pytest.mark.parametrize("dtype_name", ("float32", "float64")) +def test_array_api_empty(dtype_name: str) -> None: + values = xp.asarray(np.empty((2, 0), dtype=dtype_name)) + actual = round_ndarray(format_info_ocp_e2m1, values) # type: ignore[arg-type] + assert actual.shape == values.shape