Skip to content

Commit e41c832

Browse files
committed
precomputing the value
1 parent 41bce9a commit e41c832

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

quaddtype/numpy_quaddtype/src/quaddtype_main.c

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,24 @@ py_is_longdouble_128(PyObject *self, PyObject *args)
3333
}
3434
}
3535

36+
#ifndef SLEEF_QUAD_C
37+
static const union {
38+
struct {
39+
#if defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
40+
uint64_t h, l;
41+
#else
42+
uint64_t l, h;
43+
#endif
44+
} parts;
45+
Sleef_quad quad_value;
46+
long double alignment_dummy __attribute__((aligned(16)));
47+
} SMALLEST_SUBNORMAL_CONST = {
48+
.parts = {
49+
.h = 0x0000000000000000ULL, // exponent = 0 (subnormal), mantissa high = 0
50+
.l = 0x0000000000000001ULL // mantissa low = 1 (smallest possible)
51+
}};
52+
#endif
53+
3654
static PyObject *
3755
get_sleef_constant(PyObject *self, PyObject *args)
3856
{
@@ -78,23 +96,7 @@ get_sleef_constant(PyObject *self, PyObject *args)
7896
// On platforms with native __float128 support, use the correct literal
7997
result->value.sleef_value = SLEEF_QUAD_DENORM_MIN;
8098
#else
81-
pthread_mutex_lock(&constant_mutex);
82-
// On platforms without native __float128, SLEEF_QUAD_DENORM_MIN is broken
83-
// Manually constructing the smallest subnormal: 1 * 2^(-16382-112) = 2^(-16494)
84-
// This represents 0x0.0000000000000000000000000001p-16382
85-
#if defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
86-
struct {
87-
uint64_t h, l;
88-
} c;
89-
#else
90-
struct {
91-
uint64_t l, h;
92-
} c;
93-
#endif
94-
c.h = 0x0000000000000000ULL; // exponent = 0 (subnormal), mantissa high = 0
95-
c.l = 0x0000000000000001ULL; // mantissa low = 1 (smallest possible)
96-
memcpy(&result->value.sleef_value, &c, 16);
97-
pthread_mutex_unlock(&constant_mutex);
99+
result->value.sleef_value = SMALLEST_SUBNORMAL_CONST.quad_value;
98100
#endif
99101
}
100102
else if (strcmp(constant_name, "bits") == 0) {

0 commit comments

Comments
 (0)