Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
19 changes: 19 additions & 0 deletions quaddtype/numpy_quaddtype/src/quaddtype_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,26 @@ get_sleef_constant(PyObject *self, PyObject *args)
result->value.sleef_value = SLEEF_QUAD_MIN;
}
else if (strcmp(constant_name, "smallest_subnormal") == 0) {
#ifdef SLEEF_QUAD_C
// On platforms with native __float128 support, use the correct literal
result->value.sleef_value = SLEEF_QUAD_DENORM_MIN;
#else
// On platforms without native __float128, SLEEF_QUAD_DENORM_MIN is broken
// Manually constructing the smallest subnormal: 1 * 2^(-16382-112) = 2^(-16494)
// This represents 0x0.0000000000000000000000000001p-16382
#if defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
struct {
uint64_t h, l;
} c;
#else
struct {
uint64_t l, h;
} c;
#endif
c.h = 0x0000000000000000ULL; // exponent = 0 (subnormal), mantissa high = 0
c.l = 0x0000000000000001ULL; // mantissa low = 1 (smallest possible)
memcpy(&result->value.sleef_value, &c, 16);
#endif
}
else if (strcmp(constant_name, "bits") == 0) {
Py_DECREF(result);
Expand Down
10 changes: 10 additions & 0 deletions quaddtype/tests/test_quaddtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ def test_finfo_constant(name):
assert isinstance(getattr(numpy_quaddtype, name), QuadPrecision)


def test_smallest_subnormal_value():
"""Test that smallest_subnormal has the correct value across all platforms."""
smallest_sub = numpy_quaddtype.smallest_subnormal
repr_str = repr(smallest_sub)

# The repr should show QuadPrecision('6.0e-4966', backend='sleef')
assert "6.0e-4966" in repr_str, f"Expected '6.0e-4966' in repr, got {repr_str}"

assert smallest_sub > 0, "smallest_subnormal should be positive"

@pytest.mark.parametrize("name,value", [("bits", 128), ("precision", 33)])
def test_finfo_int_constant(name, value):
assert getattr(numpy_quaddtype, name) == value
Expand Down
Loading