Skip to content

Commit 0ebf6f6

Browse files
authored
Merge pull request #7 from SwayamInSync/subnormal
2 parents 1ff0191 + 378e86a commit 0ebf6f6

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

quaddtype/numpy_quaddtype/src/quaddtype_main.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,25 @@ py_is_longdouble_128(PyObject *self, PyObject *args)
3131
}
3232

3333
#ifdef SLEEF_QUAD_C
34-
// Native __float128 support
3534
static const Sleef_quad SMALLEST_SUBNORMAL_VALUE = SLEEF_QUAD_DENORM_MIN;
3635
#else
37-
// Use static union for thread-safe initialization
36+
// Use the exact same struct layout as the original buggy code
3837
static const union {
3938
struct {
39+
#if defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
40+
uint64_t h, l;
41+
#else
4042
uint64_t l, h;
43+
#endif
4144
} parts;
4245
Sleef_quad value;
43-
} smallest_subnormal_const = {.parts = {.l = 0x0000000000000001ULL, .h = 0x0000000000000000ULL}};
46+
} smallest_subnormal_const = {.parts = {
47+
#if defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
48+
.h = 0x0000000000000000ULL, .l = 0x0000000000000001ULL
49+
#else
50+
.l = 0x0000000000000001ULL, .h = 0x0000000000000000ULL
51+
#endif
52+
}};
4453
#define SMALLEST_SUBNORMAL_VALUE (smallest_subnormal_const.value)
4554
#endif
4655

quaddtype/numpy_quaddtype/src/scalar.c

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,22 @@
1515
#include "scalar_ops.h"
1616
#include "dragon4.h"
1717

18+
#ifdef Py_GIL_DISABLED
19+
static PyMutex scalar_mutex = {0};
20+
#endif
21+
1822
QuadPrecisionObject *
1923
QuadPrecision_raw_new(QuadBackendType backend)
2024
{
21-
QuadPrecisionObject *new = PyObject_New(QuadPrecisionObject, &QuadPrecision_Type);
25+
QuadPrecisionObject *new;
26+
#ifdef Py_GIL_DISABLED
27+
PyMutex_Lock(&scalar_mutex);
28+
#endif
29+
new = PyObject_New(QuadPrecisionObject, &QuadPrecision_Type);
30+
#ifdef Py_GIL_DISABLED
31+
PyMutex_Unlock(&scalar_mutex);
32+
#endif
33+
2234
if (!new)
2335
return NULL;
2436
new->backend = backend;
@@ -184,19 +196,28 @@ QuadPrecision_str(QuadPrecisionObject *self)
184196
static PyObject *
185197
QuadPrecision_repr(QuadPrecisionObject *self)
186198
{
199+
#ifdef Py_GIL_DISABLED
200+
PyMutex_Lock(&scalar_mutex);
201+
#endif
187202
PyObject *str = QuadPrecision_str(self);
188203
if (str == NULL) {
189204
return NULL;
190205
}
191206
const char *backend_str = (self->backend == BACKEND_SLEEF) ? "sleef" : "longdouble";
192207
PyObject *res = PyUnicode_FromFormat("QuadPrecision('%S', backend='%s')", str, backend_str);
193208
Py_DECREF(str);
209+
#ifdef Py_GIL_DISABLED
210+
PyMutex_Unlock(&scalar_mutex);
211+
#endif
194212
return res;
195213
}
196214

197215
static PyObject *
198216
QuadPrecision_repr_dragon4(QuadPrecisionObject *self)
199217
{
218+
#ifdef Py_GIL_DISABLED
219+
PyMutex_Lock(&scalar_mutex);
220+
#endif
200221
Dragon4_Options opt = {.scientific = 1,
201222
.digit_mode = DigitMode_Unique,
202223
.cutoff_mode = CutoffMode_TotalLength,
@@ -226,6 +247,9 @@ QuadPrecision_repr_dragon4(QuadPrecisionObject *self)
226247
const char *backend_str = (self->backend == BACKEND_SLEEF) ? "sleef" : "longdouble";
227248
PyObject *res = PyUnicode_FromFormat("QuadPrecision('%S', backend='%s')", str, backend_str);
228249
Py_DECREF(str);
250+
#ifdef Py_GIL_DISABLED
251+
PyMutex_Unlock(&scalar_mutex);
252+
#endif
229253
return res;
230254
}
231255

0 commit comments

Comments
 (0)