Skip to content

Commit

Permalink
Merge pull request #84 from JuliaMath/nhd-128-reduce-BigInts
Browse files Browse the repository at this point in the history
Eliminate BigInt allocs for 128-bit FixedDecimal conversions.
  • Loading branch information
NHDaly authored Nov 20, 2023
2 parents 0e04214 + 958fe07 commit b923f29
Show file tree
Hide file tree
Showing 4 changed files with 1,132 additions and 953 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "FixedPointDecimals"
uuid = "fb4d412d-6eee-574d-9565-ede6634db7b0"
authors = ["Fengyang Wang <fengyang.wang.0@gmail.com>", "Curtis Vogt <curtis.vogt@gmail.com>"]
version = "0.4.3"
version = "0.4.4"

[deps]
Parsers = "69de0a69-1ddd-5017-9359-2bf0b02dc9f0"
Expand Down
35 changes: 33 additions & 2 deletions src/FixedPointDecimals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,35 @@ end
Base.convert(::Type{FD{T, f}}, x::FD{T, f}) where {T, f} = x # Converting an FD to itself is a no-op

function Base.convert(::Type{FD{T, f}}, x::Integer) where {T, f}
reinterpret(FD{T, f}, T(widemul(x, coefficient(FD{T, f}))))
C = coefficient(FD{T, f})
throw_inexact() = throw(InexactError(:convert, FD{T, f}, x))
typemin(T) <= x <= typemax(T) || throw_inexact()
xT = convert(T, x) # This won't throw, since we already checked, above.
# Perform x * C, and check for overflow. This is cheaper than a widemul, especially for
# 128-bit T, since those widen into a BigInt.
v = _mul_checked_overflow(throw_inexact, xT, C)
return reinterpret(FD{T, f}, v)
end
function Base.convert(::Type{FD{BigInt, f}}, x::Integer) where {f}
# We specialize on f==0, since julia can't eliminate BigInt multiplication.
if f == 0
# If x is already a BigInt, this is a no-op, otherwise we alloc a new BigInt.
return reinterpret(FD{BigInt, f}, BigInt(x))
end
# For the normal case, we multiply by C, which produces a BigInt value.
C = coefficient(FD{BigInt, f})
# This can't throw since BigInt and BigFloat can hold any number.
v = x * C
return reinterpret(FD{BigInt, f}, v)
end

# x * y - if overflow, report an InexactError(FDT, )
function _mul_checked_overflow(overflow_callback, x, y)
v, overflow = Base.mul_with_overflow(x, y)
if overflow
overflow_callback()
end
return v
end

Base.convert(::Type{T}, x::AbstractFloat) where {T <: FD} = round(T, x)
Expand All @@ -310,7 +338,10 @@ function Base.convert(::Type{FD{T, f}}, x::FD{U, g}) where {T, f, U, g}
if f g
# Compute `10^(f - g)` without overflow
powt = div(coefficient(FD{T, f}), coefficient(FD{U, g}))
reinterpret(FD{T, f}, T(widemul(x.i, powt)))
v = _mul_checked_overflow(promote(x.i, powt)...) do
throw(InexactError(:convert, FD{T, f}, x))
end
reinterpret(FD{T, f}, T(v))
else
# Compute `10^(g - f)` without overflow
powt = div(coefficient(FD{U, g}), coefficient(FD{T, f}))
Expand Down
Loading

2 comments on commit b923f29

@NHDaly
Copy link
Member Author

@NHDaly NHDaly commented on b923f29 Nov 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register()

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/95654

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.4.4 -m "<description of version>" b923f299775131c8f2ed59dc45778efe046dbc8d
git push origin v0.4.4

Please sign in to comment.