Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 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
28 changes: 28 additions & 0 deletions quaddtype/numpy_quaddtype/src/quaddtype_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,29 @@ py_is_longdouble_128(PyObject *self, PyObject *args)
}
}

#ifdef SLEEF_QUAD_C
static const Sleef_quad SMALLEST_SUBNORMAL_VALUE = SLEEF_QUAD_DENORM_MIN;
#else
// Use the exact same struct layout as the original buggy code
static const union {
struct {
#if defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
uint64_t h, l;
#else
uint64_t l, h;
#endif
} parts;
Sleef_quad value;
} smallest_subnormal_const = {.parts = {
#if defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
.h = 0x0000000000000000ULL, .l = 0x0000000000000001ULL
#else
.l = 0x0000000000000001ULL, .h = 0x0000000000000000ULL
#endif
}};
#define SMALLEST_SUBNORMAL_VALUE (smallest_subnormal_const.value)
#endif

static PyObject *
get_sleef_constant(PyObject *self, PyObject *args)
{
Expand Down Expand Up @@ -71,7 +94,12 @@ 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
result->value.sleef_value = SMALLEST_SUBNORMAL_VALUE;
#endif
}
else if (strcmp(constant_name, "bits") == 0) {
Py_DECREF(result);
Expand Down
26 changes: 25 additions & 1 deletion quaddtype/numpy_quaddtype/src/scalar.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,22 @@
#include "scalar_ops.h"
#include "dragon4.h"

#ifdef Py_GIL_DISABLED
static PyMutex scalar_mutex = {0};
#endif

QuadPrecisionObject *
QuadPrecision_raw_new(QuadBackendType backend)
{
QuadPrecisionObject *new = PyObject_New(QuadPrecisionObject, &QuadPrecision_Type);
QuadPrecisionObject *new;
#ifdef Py_GIL_DISABLED
PyMutex_Lock(&scalar_mutex);
#endif
new = PyObject_New(QuadPrecisionObject, &QuadPrecision_Type);
#ifdef Py_GIL_DISABLED
PyMutex_Unlock(&scalar_mutex);
#endif

if (!new)
return NULL;
new->backend = backend;
Expand Down Expand Up @@ -184,19 +196,28 @@ QuadPrecision_str(QuadPrecisionObject *self)
static PyObject *
QuadPrecision_repr(QuadPrecisionObject *self)
{
#ifdef Py_GIL_DISABLED
PyMutex_Lock(&scalar_mutex);
#endif
PyObject *str = QuadPrecision_str(self);
if (str == NULL) {
return NULL;
}
const char *backend_str = (self->backend == BACKEND_SLEEF) ? "sleef" : "longdouble";
PyObject *res = PyUnicode_FromFormat("QuadPrecision('%S', backend='%s')", str, backend_str);
Py_DECREF(str);
#ifdef Py_GIL_DISABLED
PyMutex_Unlock(&scalar_mutex);
#endif
return res;
}

static PyObject *
QuadPrecision_repr_dragon4(QuadPrecisionObject *self)
{
#ifdef Py_GIL_DISABLED
PyMutex_Lock(&scalar_mutex);
#endif
Dragon4_Options opt = {.scientific = 1,
.digit_mode = DigitMode_Unique,
.cutoff_mode = CutoffMode_TotalLength,
Expand Down Expand Up @@ -226,6 +247,9 @@ QuadPrecision_repr_dragon4(QuadPrecisionObject *self)
const char *backend_str = (self->backend == BACKEND_SLEEF) ? "sleef" : "longdouble";
PyObject *res = PyUnicode_FromFormat("QuadPrecision('%S', backend='%s')", str, backend_str);
Py_DECREF(str);
#ifdef Py_GIL_DISABLED
PyMutex_Unlock(&scalar_mutex);
#endif
return res;
}

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