@@ -7,8 +7,6 @@ import Printf, Random, SpecialFunctions
7
7
8
8
export Dec32, Dec64, Dec128, @d_str , @d32_str , @d64_str , @d128_str , exponent10, ldexp10, sigexp
9
9
10
- const _buffer = Vector {Vector{UInt8}} ()
11
-
12
10
import Base. promote_rule
13
11
14
12
# Check exception flags in mask & throw, otherwise returning x;
@@ -60,13 +58,9 @@ function Base.convert(::Type{RoundingMode}, r::DecFPRoundingMode)
60
58
end
61
59
end
62
60
63
- # global vectors must be initialized at runtime (via __init__)
64
- function __init__ ()
65
- resize! (_buffer, Threads. nthreads ())
66
- for i = 1 : Threads. nthreads ()
67
- _buffer[i] = fill (0x00 , 1024 )
68
- end
69
- end
61
+ # internal task-local buffer for I/O and string conversions
62
+ const _STRINGBUFFER_KEY = :DecFP_stringbuffer_abb78e082af23329 # unique key
63
+ _stringbuffer () = get! (() -> fill (0x00 , 1024 ), task_local_storage (), _STRINGBUFFER_KEY):: Vector{UInt8}
70
64
71
65
# status flags from bid_functions.h:
72
66
const INVALID = 0x01
@@ -344,16 +338,16 @@ for w in (32,64,128)
344
338
end
345
339
346
340
function tostring (x:: $BID )
347
- # fills global _buffer
348
- ccall (($ (bidsym (w," to_string" )), libbid), Cvoid, (Ptr{UInt8},$ BID,Ref{Cuint}), _buffer[Threads . threadid ()] , x, zero (Cuint))
341
+ # fills global _stringbuffer
342
+ ccall (($ (bidsym (w," to_string" )), libbid), Cvoid, (Ptr{UInt8},$ BID,Ref{Cuint}), _stringbuffer () , x, zero (Cuint))
349
343
end
350
344
351
345
function Base. show (io:: IO , x:: $BID )
352
346
isnan (x) && (print (io, " NaN" ); return )
353
347
isinf (x) && (print (io, signbit (x) ? " -Inf" : " Inf" ); return )
354
348
x == 0 && (print (io, signbit (x) ? " -0.0" : " 0.0" ); return )
355
349
tostring (x)
356
- buffer = _buffer[Threads . threadid ()]
350
+ buffer = _stringbuffer ()
357
351
if buffer[1 ] == UInt8 (' -' )
358
352
print (io, ' -' )
359
353
end
@@ -415,7 +409,7 @@ for w in (32,64,128)
415
409
return Int32 (1 ), Int32 (1 ), signbit (x)
416
410
end
417
411
tostring (rounded)
418
- buffer = _buffer[Threads . threadid ()]
412
+ buffer = _stringbuffer ()
419
413
trailing_zeros = 0
420
414
i = 2
421
415
while buffer[i] != UInt8 (' E' )
@@ -461,7 +455,7 @@ for w in (32,64,128)
461
455
rounded = round (ldexp10 (x, n - 1 - normalized_exponent), RoundNearestTiesAway)
462
456
rounded_exponent = exponent10 (rounded)
463
457
tostring (rounded)
464
- buffer = _buffer[Threads . threadid ()]
458
+ buffer = _stringbuffer ()
465
459
i = 2
466
460
while buffer[i] != UInt8 (' E' )
467
461
digits[i - 1 ] = buffer[i]
0 commit comments