Skip to content
Merged
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
15 changes: 12 additions & 3 deletions quaddtype/numpy_quaddtype/src/quaddtype_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,25 @@ py_is_longdouble_128(PyObject *self, PyObject *args)
}

#ifdef SLEEF_QUAD_C
// Native __float128 support
static const Sleef_quad SMALLEST_SUBNORMAL_VALUE = SLEEF_QUAD_DENORM_MIN;
#else
// Use static union for thread-safe initialization
// 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 = {.l = 0x0000000000000001ULL, .h = 0x0000000000000000ULL}};
} 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

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
Loading