@@ -27,10 +27,7 @@ module FixedPointDecimals
27
27
28
28
export FixedDecimal, RoundThrows
29
29
30
- using Base: decompose
31
-
32
- const BitInteger = Union{Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64,
33
- UInt64, Int128, UInt128}
30
+ using Base: decompose, BitInteger
34
31
35
32
# floats that support fma and are roughly IEEE-like
36
33
const FMAFloat = Union{Float16, Float32, Float64, BigFloat}
472
469
473
470
The highest value of `x` which does not result in an overflow when evaluating `T(10)^x`. For
474
471
types of `T` that do not overflow -1 will be returned.
472
+
473
+ NOTE: This function is expensive, since it contains a while-loop, but it is actually
474
+ computing a constant value for types, so it really only needs to be run once per type.
475
+ We achieve this by `@eval`ing new methods in a loop, below. Users can do this
476
+ themselves to add more "frozen" methods for custom Integer types:
477
+ ```julia
478
+ @eval FixedPointDecimals.max_exp10(::Type{CustomIntType}) = \$ (max_exp10(CustomIntType))
479
+ ```
480
+ This function does not have or depend on any side-effects.
475
481
"""
476
- Base . @pure function max_exp10 (:: Type{T} ) where {T <: Integer }
482
+ function max_exp10 (:: Type{T} ) where {T <: Integer }
477
483
# This function is marked as `Base.@pure`. Even though it does call some generic
478
484
# functions, they are all simple methods that should be able to be evaluated as
479
485
# constants. This function does not have or depend on any side-effects.
@@ -494,9 +500,12 @@ Base.@pure function max_exp10(::Type{T}) where {T <: Integer}
494
500
end
495
501
496
502
max_exp10 (:: Type{BigInt} ) = - 1
497
- # Freeze the evaluation for Int128, since max_exp10(Int128) is too compilicated to get
498
- # optimized away by the compiler during const-folding.
499
- @eval max_exp10 (:: Type{Int128} ) = $ (max_exp10 (Int128))
503
+ # Freeze the evaluation for BitInteger types, since max_exp10() is too compilicated to get
504
+ # optimized away by the compiler during const-folding. (We can't freeze for user-defined
505
+ # types because we don't know what they are yet.)
506
+ for T in Base. BitInteger_types
507
+ @eval max_exp10 (:: Type{$T} ) = $ (max_exp10 (T))
508
+ end
500
509
501
510
# coefficient is marked pure. This is needed to ensure that the result is always available
502
511
# at compile time, and can therefore be used when optimizing mathematical operations.
0 commit comments