Skip to content

Commit f3337a3

Browse files
committed
MAINT: remove indirection and use thread-local _bigint_static directly
1 parent 3662311 commit f3337a3

File tree

1 file changed

+40
-72
lines changed

1 file changed

+40
-72
lines changed

numpy/_core/src/multiarray/dragon4.c

Lines changed: 40 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -163,28 +163,8 @@ typedef struct {
163163
char repr[16384];
164164
} Dragon4_Scratch;
165165

166-
static NPY_TLS int _bigint_static_in_use = 0;
167166
static NPY_TLS Dragon4_Scratch _bigint_static;
168167

169-
static Dragon4_Scratch*
170-
get_dragon4_bigint_scratch(void) {
171-
if (_bigint_static_in_use) {
172-
PyErr_SetString(PyExc_RuntimeError,
173-
"numpy float printing code is not re-entrant. "
174-
"Ping the devs to fix it.");
175-
return NULL;
176-
}
177-
_bigint_static_in_use = 1;
178-
179-
/* in this dummy implementation we only return the static allocation */
180-
return &_bigint_static;
181-
}
182-
183-
static void
184-
free_dragon4_bigint_scratch(Dragon4_Scratch *mem){
185-
_bigint_static_in_use = 0;
186-
}
187-
188168
/* Copy integer */
189169
static void
190170
BigInt_Copy(BigInt *dst, const BigInt *src)
@@ -2209,11 +2189,11 @@ Format_floatbits(char *buffer, npy_uint32 bufferSize, BigInt *mantissa,
22092189
*/
22102190
static npy_uint32
22112191
Dragon4_PrintFloat_IEEE_binary16(
2212-
Dragon4_Scratch *scratch, npy_half *value, Dragon4_Options *opt)
2192+
npy_half *value, Dragon4_Options *opt)
22132193
{
2214-
char *buffer = scratch->repr;
2215-
const npy_uint32 bufferSize = sizeof(scratch->repr);
2216-
BigInt *bigints = scratch->bigints;
2194+
char *buffer = _bigint_static.repr;
2195+
const npy_uint32 bufferSize = sizeof(_bigint_static.repr);
2196+
BigInt *bigints = _bigint_static.bigints;
22172197

22182198
npy_uint16 val = *value;
22192199
npy_uint32 floatExponent, floatMantissa, floatSign;
@@ -2296,12 +2276,12 @@ Dragon4_PrintFloat_IEEE_binary16(
22962276
*/
22972277
static npy_uint32
22982278
Dragon4_PrintFloat_IEEE_binary32(
2299-
Dragon4_Scratch *scratch, npy_float32 *value,
2279+
npy_float32 *value,
23002280
Dragon4_Options *opt)
23012281
{
2302-
char *buffer = scratch->repr;
2303-
const npy_uint32 bufferSize = sizeof(scratch->repr);
2304-
BigInt *bigints = scratch->bigints;
2282+
char *buffer = _bigint_static.repr;
2283+
const npy_uint32 bufferSize = sizeof(_bigint_static.repr);
2284+
BigInt *bigints = _bigint_static.bigints;
23052285

23062286
union
23072287
{
@@ -2389,11 +2369,11 @@ Dragon4_PrintFloat_IEEE_binary32(
23892369
*/
23902370
static npy_uint32
23912371
Dragon4_PrintFloat_IEEE_binary64(
2392-
Dragon4_Scratch *scratch, npy_float64 *value, Dragon4_Options *opt)
2372+
npy_float64 *value, Dragon4_Options *opt)
23932373
{
2394-
char *buffer = scratch->repr;
2395-
const npy_uint32 bufferSize = sizeof(scratch->repr);
2396-
BigInt *bigints = scratch->bigints;
2374+
char *buffer = _bigint_static.repr;
2375+
const npy_uint32 bufferSize = sizeof(_bigint_static.repr);
2376+
BigInt *bigints = _bigint_static.bigints;
23972377

23982378
union
23992379
{
@@ -2504,11 +2484,11 @@ typedef struct FloatVal128 {
25042484
*/
25052485
static npy_uint32
25062486
Dragon4_PrintFloat_Intel_extended(
2507-
Dragon4_Scratch *scratch, FloatVal128 value, Dragon4_Options *opt)
2487+
FloatVal128 value, Dragon4_Options *opt)
25082488
{
2509-
char *buffer = scratch->repr;
2510-
const npy_uint32 bufferSize = sizeof(scratch->repr);
2511-
BigInt *bigints = scratch->bigints;
2489+
char *buffer = _bigint_static.repr;
2490+
const npy_uint32 bufferSize = sizeof(_bigint_static.repr);
2491+
BigInt *bigints = _bigint_static.bigints;
25122492

25132493
npy_uint32 floatExponent, floatSign;
25142494
npy_uint64 floatMantissa;
@@ -2602,7 +2582,7 @@ Dragon4_PrintFloat_Intel_extended(
26022582
*/
26032583
static npy_uint32
26042584
Dragon4_PrintFloat_Intel_extended80(
2605-
Dragon4_Scratch *scratch, npy_float80 *value, Dragon4_Options *opt)
2585+
npy_float80 *value, Dragon4_Options *opt)
26062586
{
26072587
FloatVal128 val128;
26082588
union {
@@ -2618,15 +2598,15 @@ Dragon4_PrintFloat_Intel_extended80(
26182598
val128.lo = buf80.integer.a;
26192599
val128.hi = buf80.integer.b;
26202600

2621-
return Dragon4_PrintFloat_Intel_extended(scratch, val128, opt);
2601+
return Dragon4_PrintFloat_Intel_extended(val128, opt);
26222602
}
26232603
#endif /* HAVE_LDOUBLE_INTEL_EXTENDED_10_BYTES_LE */
26242604

26252605
#ifdef HAVE_LDOUBLE_INTEL_EXTENDED_12_BYTES_LE
26262606
/* Intel's 80-bit IEEE extended precision format, 96-bit storage */
26272607
static npy_uint32
26282608
Dragon4_PrintFloat_Intel_extended96(
2629-
Dragon4_Scratch *scratch, npy_float96 *value, Dragon4_Options *opt)
2609+
npy_float96 *value, Dragon4_Options *opt)
26302610
{
26312611
FloatVal128 val128;
26322612
union {
@@ -2642,15 +2622,15 @@ Dragon4_PrintFloat_Intel_extended96(
26422622
val128.lo = buf96.integer.a;
26432623
val128.hi = buf96.integer.b;
26442624

2645-
return Dragon4_PrintFloat_Intel_extended(scratch, val128, opt);
2625+
return Dragon4_PrintFloat_Intel_extended(val128, opt);
26462626
}
26472627
#endif /* HAVE_LDOUBLE_INTEL_EXTENDED_12_BYTES_LE */
26482628

26492629
#ifdef HAVE_LDOUBLE_MOTOROLA_EXTENDED_12_BYTES_BE
26502630
/* Motorola Big-endian equivalent of the Intel-extended 96 fp format */
26512631
static npy_uint32
26522632
Dragon4_PrintFloat_Motorola_extended96(
2653-
Dragon4_Scratch *scratch, npy_float96 *value, Dragon4_Options *opt)
2633+
npy_float96 *value, Dragon4_Options *opt)
26542634
{
26552635
FloatVal128 val128;
26562636
union {
@@ -2667,7 +2647,7 @@ Dragon4_PrintFloat_Motorola_extended96(
26672647
val128.hi = buf96.integer.a >> 16;
26682648
/* once again we assume the int has same endianness as the float */
26692649

2670-
return Dragon4_PrintFloat_Intel_extended(scratch, val128, opt);
2650+
return Dragon4_PrintFloat_Intel_extended(val128, opt);
26712651
}
26722652
#endif /* HAVE_LDOUBLE_MOTOROLA_EXTENDED_12_BYTES_BE */
26732653

@@ -2687,7 +2667,7 @@ typedef union FloatUnion128
26872667
/* Intel's 80-bit IEEE extended precision format, 128-bit storage */
26882668
static npy_uint32
26892669
Dragon4_PrintFloat_Intel_extended128(
2690-
Dragon4_Scratch *scratch, npy_float128 *value, Dragon4_Options *opt)
2670+
npy_float128 *value, Dragon4_Options *opt)
26912671
{
26922672
FloatVal128 val128;
26932673
FloatUnion128 buf128;
@@ -2697,7 +2677,7 @@ Dragon4_PrintFloat_Intel_extended128(
26972677
val128.lo = buf128.integer.a;
26982678
val128.hi = buf128.integer.b;
26992679

2700-
return Dragon4_PrintFloat_Intel_extended(scratch, val128, opt);
2680+
return Dragon4_PrintFloat_Intel_extended(val128, opt);
27012681
}
27022682
#endif /* HAVE_LDOUBLE_INTEL_EXTENDED_16_BYTES_LE */
27032683

@@ -2716,11 +2696,11 @@ Dragon4_PrintFloat_Intel_extended128(
27162696
*/
27172697
static npy_uint32
27182698
Dragon4_PrintFloat_IEEE_binary128(
2719-
Dragon4_Scratch *scratch, FloatVal128 val128, Dragon4_Options *opt)
2699+
FloatVal128 val128, Dragon4_Options *opt)
27202700
{
2721-
char *buffer = scratch->repr;
2722-
const npy_uint32 bufferSize = sizeof(scratch->repr);
2723-
BigInt *bigints = scratch->bigints;
2701+
char *buffer = _bigint_static.repr;
2702+
const npy_uint32 bufferSize = sizeof(_bigint_static.repr);
2703+
BigInt *bigints = _bigint_static.bigints;
27242704

27252705
npy_uint32 floatExponent, floatSign;
27262706

@@ -2801,7 +2781,7 @@ Dragon4_PrintFloat_IEEE_binary128(
28012781
#if defined(HAVE_LDOUBLE_IEEE_QUAD_LE)
28022782
static npy_uint32
28032783
Dragon4_PrintFloat_IEEE_binary128_le(
2804-
Dragon4_Scratch *scratch, npy_float128 *value, Dragon4_Options *opt)
2784+
npy_float128 *value, Dragon4_Options *opt)
28052785
{
28062786
FloatVal128 val128;
28072787
FloatUnion128 buf128;
@@ -2810,7 +2790,7 @@ Dragon4_PrintFloat_IEEE_binary128_le(
28102790
val128.lo = buf128.integer.a;
28112791
val128.hi = buf128.integer.b;
28122792

2813-
return Dragon4_PrintFloat_IEEE_binary128(scratch, val128, opt);
2793+
return Dragon4_PrintFloat_IEEE_binary128(val128, opt);
28142794
}
28152795
#endif /* HAVE_LDOUBLE_IEEE_QUAD_LE */
28162796

@@ -2821,7 +2801,7 @@ Dragon4_PrintFloat_IEEE_binary128_le(
28212801
*/
28222802
static npy_uint32
28232803
Dragon4_PrintFloat_IEEE_binary128_be(
2824-
Dragon4_Scratch *scratch, npy_float128 *value, Dragon4_Options *opt)
2804+
npy_float128 *value, Dragon4_Options *opt)
28252805
{
28262806
FloatVal128 val128;
28272807
FloatUnion128 buf128;
@@ -2830,7 +2810,7 @@ Dragon4_PrintFloat_IEEE_binary128_be(
28302810
val128.lo = buf128.integer.b;
28312811
val128.hi = buf128.integer.a;
28322812

2833-
return Dragon4_PrintFloat_IEEE_binary128(scratch, val128, opt);
2813+
return Dragon4_PrintFloat_IEEE_binary128(val128, opt);
28342814
}
28352815
#endif /* HAVE_LDOUBLE_IEEE_QUAD_BE */
28362816

@@ -2876,11 +2856,11 @@ Dragon4_PrintFloat_IEEE_binary128_be(
28762856
*/
28772857
static npy_uint32
28782858
Dragon4_PrintFloat_IBM_double_double(
2879-
Dragon4_Scratch *scratch, npy_float128 *value, Dragon4_Options *opt)
2859+
npy_float128 *value, Dragon4_Options *opt)
28802860
{
2881-
char *buffer = scratch->repr;
2882-
const npy_uint32 bufferSize = sizeof(scratch->repr);
2883-
BigInt *bigints = scratch->bigints;
2861+
char *buffer = _bigint_static.repr;
2862+
const npy_uint32 bufferSize = sizeof(_bigint_static.repr);
2863+
BigInt *bigints = _bigint_static.bigints;
28842864

28852865
FloatVal128 val128;
28862866
FloatUnion128 buf128;
@@ -3067,16 +3047,10 @@ PyObject *\
30673047
Dragon4_Positional_##Type##_opt(npy_type *val, Dragon4_Options *opt)\
30683048
{\
30693049
PyObject *ret;\
3070-
Dragon4_Scratch *scratch = get_dragon4_bigint_scratch();\
3071-
if (scratch == NULL) {\
3072-
return NULL;\
3073-
}\
3074-
if (Dragon4_PrintFloat_##format(scratch, val, opt) < 0) {\
3075-
free_dragon4_bigint_scratch(scratch);\
3050+
if (Dragon4_PrintFloat_##format(val, opt) < 0) {\
30763051
return NULL;\
30773052
}\
3078-
ret = PyUnicode_FromString(scratch->repr);\
3079-
free_dragon4_bigint_scratch(scratch);\
3053+
ret = PyUnicode_FromString(_bigint_static.repr);\
30803054
return ret;\
30813055
}\
30823056
\
@@ -3105,16 +3079,10 @@ PyObject *\
31053079
Dragon4_Scientific_##Type##_opt(npy_type *val, Dragon4_Options *opt)\
31063080
{\
31073081
PyObject *ret;\
3108-
Dragon4_Scratch *scratch = get_dragon4_bigint_scratch();\
3109-
if (scratch == NULL) {\
3110-
return NULL;\
3111-
}\
3112-
if (Dragon4_PrintFloat_##format(scratch, val, opt) < 0) {\
3113-
free_dragon4_bigint_scratch(scratch);\
3082+
if (Dragon4_PrintFloat_##format(val, opt) < 0) { \
31143083
return NULL;\
31153084
}\
3116-
ret = PyUnicode_FromString(scratch->repr);\
3117-
free_dragon4_bigint_scratch(scratch);\
3085+
ret = PyUnicode_FromString(_bigint_static.repr);\
31183086
return ret;\
31193087
}\
31203088
PyObject *\

0 commit comments

Comments
 (0)