@@ -163,28 +163,7 @@ typedef struct {
163
163
char repr [16384 ];
164
164
} Dragon4_Scratch ;
165
165
166
- static int _bigint_static_in_use = 0 ;
167
- static Dragon4_Scratch _bigint_static ;
168
-
169
- static Dragon4_Scratch *
170
- get_dragon4_bigint_scratch (void ) {
171
- /* this test+set is not threadsafe, but no matter because we have GIL */
172
- if (_bigint_static_in_use ) {
173
- PyErr_SetString (PyExc_RuntimeError ,
174
- "numpy float printing code is not re-entrant. "
175
- "Ping the devs to fix it." );
176
- return NULL ;
177
- }
178
- _bigint_static_in_use = 1 ;
179
-
180
- /* in this dummy implementation we only return the static allocation */
181
- return & _bigint_static ;
182
- }
183
-
184
- static void
185
- free_dragon4_bigint_scratch (Dragon4_Scratch * mem ){
186
- _bigint_static_in_use = 0 ;
187
- }
166
+ static NPY_TLS Dragon4_Scratch _bigint_static ;
188
167
189
168
/* Copy integer */
190
169
static void
@@ -2210,11 +2189,11 @@ Format_floatbits(char *buffer, npy_uint32 bufferSize, BigInt *mantissa,
2210
2189
*/
2211
2190
static npy_uint32
2212
2191
Dragon4_PrintFloat_IEEE_binary16 (
2213
- Dragon4_Scratch * scratch , npy_half * value , Dragon4_Options * opt )
2192
+ npy_half * value , Dragon4_Options * opt )
2214
2193
{
2215
- char * buffer = scratch -> repr ;
2216
- const npy_uint32 bufferSize = sizeof (scratch -> repr );
2217
- 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 ;
2218
2197
2219
2198
npy_uint16 val = * value ;
2220
2199
npy_uint32 floatExponent , floatMantissa , floatSign ;
@@ -2297,12 +2276,12 @@ Dragon4_PrintFloat_IEEE_binary16(
2297
2276
*/
2298
2277
static npy_uint32
2299
2278
Dragon4_PrintFloat_IEEE_binary32 (
2300
- Dragon4_Scratch * scratch , npy_float32 * value ,
2279
+ npy_float32 * value ,
2301
2280
Dragon4_Options * opt )
2302
2281
{
2303
- char * buffer = scratch -> repr ;
2304
- const npy_uint32 bufferSize = sizeof (scratch -> repr );
2305
- 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 ;
2306
2285
2307
2286
union
2308
2287
{
@@ -2390,11 +2369,11 @@ Dragon4_PrintFloat_IEEE_binary32(
2390
2369
*/
2391
2370
static npy_uint32
2392
2371
Dragon4_PrintFloat_IEEE_binary64 (
2393
- Dragon4_Scratch * scratch , npy_float64 * value , Dragon4_Options * opt )
2372
+ npy_float64 * value , Dragon4_Options * opt )
2394
2373
{
2395
- char * buffer = scratch -> repr ;
2396
- const npy_uint32 bufferSize = sizeof (scratch -> repr );
2397
- 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 ;
2398
2377
2399
2378
union
2400
2379
{
@@ -2505,11 +2484,11 @@ typedef struct FloatVal128 {
2505
2484
*/
2506
2485
static npy_uint32
2507
2486
Dragon4_PrintFloat_Intel_extended (
2508
- Dragon4_Scratch * scratch , FloatVal128 value , Dragon4_Options * opt )
2487
+ FloatVal128 value , Dragon4_Options * opt )
2509
2488
{
2510
- char * buffer = scratch -> repr ;
2511
- const npy_uint32 bufferSize = sizeof (scratch -> repr );
2512
- 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 ;
2513
2492
2514
2493
npy_uint32 floatExponent , floatSign ;
2515
2494
npy_uint64 floatMantissa ;
@@ -2603,7 +2582,7 @@ Dragon4_PrintFloat_Intel_extended(
2603
2582
*/
2604
2583
static npy_uint32
2605
2584
Dragon4_PrintFloat_Intel_extended80 (
2606
- Dragon4_Scratch * scratch , npy_float80 * value , Dragon4_Options * opt )
2585
+ npy_float80 * value , Dragon4_Options * opt )
2607
2586
{
2608
2587
FloatVal128 val128 ;
2609
2588
union {
@@ -2619,15 +2598,15 @@ Dragon4_PrintFloat_Intel_extended80(
2619
2598
val128 .lo = buf80 .integer .a ;
2620
2599
val128 .hi = buf80 .integer .b ;
2621
2600
2622
- return Dragon4_PrintFloat_Intel_extended (scratch , val128 , opt );
2601
+ return Dragon4_PrintFloat_Intel_extended (val128 , opt );
2623
2602
}
2624
2603
#endif /* HAVE_LDOUBLE_INTEL_EXTENDED_10_BYTES_LE */
2625
2604
2626
2605
#ifdef HAVE_LDOUBLE_INTEL_EXTENDED_12_BYTES_LE
2627
2606
/* Intel's 80-bit IEEE extended precision format, 96-bit storage */
2628
2607
static npy_uint32
2629
2608
Dragon4_PrintFloat_Intel_extended96 (
2630
- Dragon4_Scratch * scratch , npy_float96 * value , Dragon4_Options * opt )
2609
+ npy_float96 * value , Dragon4_Options * opt )
2631
2610
{
2632
2611
FloatVal128 val128 ;
2633
2612
union {
@@ -2643,15 +2622,15 @@ Dragon4_PrintFloat_Intel_extended96(
2643
2622
val128 .lo = buf96 .integer .a ;
2644
2623
val128 .hi = buf96 .integer .b ;
2645
2624
2646
- return Dragon4_PrintFloat_Intel_extended (scratch , val128 , opt );
2625
+ return Dragon4_PrintFloat_Intel_extended (val128 , opt );
2647
2626
}
2648
2627
#endif /* HAVE_LDOUBLE_INTEL_EXTENDED_12_BYTES_LE */
2649
2628
2650
2629
#ifdef HAVE_LDOUBLE_MOTOROLA_EXTENDED_12_BYTES_BE
2651
2630
/* Motorola Big-endian equivalent of the Intel-extended 96 fp format */
2652
2631
static npy_uint32
2653
2632
Dragon4_PrintFloat_Motorola_extended96 (
2654
- Dragon4_Scratch * scratch , npy_float96 * value , Dragon4_Options * opt )
2633
+ npy_float96 * value , Dragon4_Options * opt )
2655
2634
{
2656
2635
FloatVal128 val128 ;
2657
2636
union {
@@ -2668,7 +2647,7 @@ Dragon4_PrintFloat_Motorola_extended96(
2668
2647
val128 .hi = buf96 .integer .a >> 16 ;
2669
2648
/* once again we assume the int has same endianness as the float */
2670
2649
2671
- return Dragon4_PrintFloat_Intel_extended (scratch , val128 , opt );
2650
+ return Dragon4_PrintFloat_Intel_extended (val128 , opt );
2672
2651
}
2673
2652
#endif /* HAVE_LDOUBLE_MOTOROLA_EXTENDED_12_BYTES_BE */
2674
2653
@@ -2688,7 +2667,7 @@ typedef union FloatUnion128
2688
2667
/* Intel's 80-bit IEEE extended precision format, 128-bit storage */
2689
2668
static npy_uint32
2690
2669
Dragon4_PrintFloat_Intel_extended128 (
2691
- Dragon4_Scratch * scratch , npy_float128 * value , Dragon4_Options * opt )
2670
+ npy_float128 * value , Dragon4_Options * opt )
2692
2671
{
2693
2672
FloatVal128 val128 ;
2694
2673
FloatUnion128 buf128 ;
@@ -2698,7 +2677,7 @@ Dragon4_PrintFloat_Intel_extended128(
2698
2677
val128 .lo = buf128 .integer .a ;
2699
2678
val128 .hi = buf128 .integer .b ;
2700
2679
2701
- return Dragon4_PrintFloat_Intel_extended (scratch , val128 , opt );
2680
+ return Dragon4_PrintFloat_Intel_extended (val128 , opt );
2702
2681
}
2703
2682
#endif /* HAVE_LDOUBLE_INTEL_EXTENDED_16_BYTES_LE */
2704
2683
@@ -2717,11 +2696,11 @@ Dragon4_PrintFloat_Intel_extended128(
2717
2696
*/
2718
2697
static npy_uint32
2719
2698
Dragon4_PrintFloat_IEEE_binary128 (
2720
- Dragon4_Scratch * scratch , FloatVal128 val128 , Dragon4_Options * opt )
2699
+ FloatVal128 val128 , Dragon4_Options * opt )
2721
2700
{
2722
- char * buffer = scratch -> repr ;
2723
- const npy_uint32 bufferSize = sizeof (scratch -> repr );
2724
- 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 ;
2725
2704
2726
2705
npy_uint32 floatExponent , floatSign ;
2727
2706
@@ -2802,7 +2781,7 @@ Dragon4_PrintFloat_IEEE_binary128(
2802
2781
#if defined(HAVE_LDOUBLE_IEEE_QUAD_LE )
2803
2782
static npy_uint32
2804
2783
Dragon4_PrintFloat_IEEE_binary128_le (
2805
- Dragon4_Scratch * scratch , npy_float128 * value , Dragon4_Options * opt )
2784
+ npy_float128 * value , Dragon4_Options * opt )
2806
2785
{
2807
2786
FloatVal128 val128 ;
2808
2787
FloatUnion128 buf128 ;
@@ -2811,7 +2790,7 @@ Dragon4_PrintFloat_IEEE_binary128_le(
2811
2790
val128 .lo = buf128 .integer .a ;
2812
2791
val128 .hi = buf128 .integer .b ;
2813
2792
2814
- return Dragon4_PrintFloat_IEEE_binary128 (scratch , val128 , opt );
2793
+ return Dragon4_PrintFloat_IEEE_binary128 (val128 , opt );
2815
2794
}
2816
2795
#endif /* HAVE_LDOUBLE_IEEE_QUAD_LE */
2817
2796
@@ -2822,7 +2801,7 @@ Dragon4_PrintFloat_IEEE_binary128_le(
2822
2801
*/
2823
2802
static npy_uint32
2824
2803
Dragon4_PrintFloat_IEEE_binary128_be (
2825
- Dragon4_Scratch * scratch , npy_float128 * value , Dragon4_Options * opt )
2804
+ npy_float128 * value , Dragon4_Options * opt )
2826
2805
{
2827
2806
FloatVal128 val128 ;
2828
2807
FloatUnion128 buf128 ;
@@ -2831,7 +2810,7 @@ Dragon4_PrintFloat_IEEE_binary128_be(
2831
2810
val128 .lo = buf128 .integer .b ;
2832
2811
val128 .hi = buf128 .integer .a ;
2833
2812
2834
- return Dragon4_PrintFloat_IEEE_binary128 (scratch , val128 , opt );
2813
+ return Dragon4_PrintFloat_IEEE_binary128 (val128 , opt );
2835
2814
}
2836
2815
#endif /* HAVE_LDOUBLE_IEEE_QUAD_BE */
2837
2816
@@ -2877,11 +2856,11 @@ Dragon4_PrintFloat_IEEE_binary128_be(
2877
2856
*/
2878
2857
static npy_uint32
2879
2858
Dragon4_PrintFloat_IBM_double_double (
2880
- Dragon4_Scratch * scratch , npy_float128 * value , Dragon4_Options * opt )
2859
+ npy_float128 * value , Dragon4_Options * opt )
2881
2860
{
2882
- char * buffer = scratch -> repr ;
2883
- const npy_uint32 bufferSize = sizeof (scratch -> repr );
2884
- 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 ;
2885
2864
2886
2865
FloatVal128 val128 ;
2887
2866
FloatUnion128 buf128 ;
@@ -3068,16 +3047,10 @@ PyObject *\
3068
3047
Dragon4_Positional_##Type##_opt(npy_type *val, Dragon4_Options *opt)\
3069
3048
{\
3070
3049
PyObject *ret;\
3071
- Dragon4_Scratch *scratch = get_dragon4_bigint_scratch();\
3072
- if (scratch == NULL) {\
3073
- return NULL;\
3074
- }\
3075
- if (Dragon4_PrintFloat_##format(scratch, val, opt) < 0) {\
3076
- free_dragon4_bigint_scratch(scratch);\
3050
+ if (Dragon4_PrintFloat_##format(val, opt) < 0) {\
3077
3051
return NULL;\
3078
3052
}\
3079
- ret = PyUnicode_FromString(scratch->repr);\
3080
- free_dragon4_bigint_scratch(scratch);\
3053
+ ret = PyUnicode_FromString(_bigint_static.repr);\
3081
3054
return ret;\
3082
3055
}\
3083
3056
\
@@ -3106,16 +3079,10 @@ PyObject *\
3106
3079
Dragon4_Scientific_##Type##_opt(npy_type *val, Dragon4_Options *opt)\
3107
3080
{\
3108
3081
PyObject *ret;\
3109
- Dragon4_Scratch *scratch = get_dragon4_bigint_scratch();\
3110
- if (scratch == NULL) {\
3111
- return NULL;\
3112
- }\
3113
- if (Dragon4_PrintFloat_##format(scratch, val, opt) < 0) {\
3114
- free_dragon4_bigint_scratch(scratch);\
3082
+ if (Dragon4_PrintFloat_##format(val, opt) < 0) { \
3115
3083
return NULL;\
3116
3084
}\
3117
- ret = PyUnicode_FromString(scratch->repr);\
3118
- free_dragon4_bigint_scratch(scratch);\
3085
+ ret = PyUnicode_FromString(_bigint_static.repr);\
3119
3086
return ret;\
3120
3087
}\
3121
3088
PyObject *\
0 commit comments