Skip to content

Commit

Permalink
Merge pull request #7 from JuliaOpt/gp/add-examples
Browse files Browse the repository at this point in the history
Add example implementing the API for BigInt
  • Loading branch information
blegat committed Oct 2, 2019
2 parents 3578a3f + 283b49e commit 8de298c
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions examples/bigint.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# This example contains a full implementation of the MutableArithmetics API for the BigInt datatype.

using MutableArithmetics
const MA = MutableArithmetics

# zero!
MA.mutability(::Type{BigInt}, ::typeof(MA.zero!)) = MA.IsMutable()
MA.zero_impl!(x::BigInt) = Base.GMP.MPZ.set_si!(x, 0)

# one!
MA.mutability(::Type{BigInt}, ::typeof(MA.one!)) = MA.IsMutable()
MA.one_impl!(x::BigInt) = Base.GMP.MPZ.set_si!(x, 1)

# add_to! / add!
MA.mutability(::Type{BigInt}, ::typeof(MA.add_to!), ::Type{BigInt}, ::Type{BigInt}) = MA.IsMutable()
MA.add_to_impl!(x::BigInt, a::BigInt, b::BigInt) = Base.GMP.MPZ.add!(x, a, b)
MA.add_impl!(a::BigInt, b::BigInt) = Base.GMP.MPZ.add!(a, a, b)

# mul_to! / mul!
MA.mutability(::Type{BigInt}, ::typeof(MA.mul_to!), ::Type{BigInt}, ::Type{BigInt}) = MA.IsMutable()
MA.mul_to_impl!(x::BigInt, a::BigInt, b::BigInt) = Base.GMP.MPZ.mul!(x, a, b)
MA.mul_impl!(a::BigInt, b::BigInt) = Base.GMP.MPZ.mul!(a, a, b)

# muladd_to! / muladd! / muladd_buf!
MA.muladd_to_impl!(dest::BigInt, b::BigInt, c::BigInt, d::BigInt) = Base.GMP.MPZ.add!(dest, Base.GMP.MPZ.mul!(BigInt(), c, d), b)
MA.muladd_impl!(a::BigInt, b::BigInt, c::BigInt) = Base.GMP.MPZ.add!(a, a, Base.GMP.MPZ.mul!(BigInt(), b, c))
MA.muladd_buf_impl!(buf::BigInt, a::BigInt, b::BigInt, c::BigInt) = Base.GMP.MPZ.add!(a, Base.GMP.MPZ.mul!(buf, b, c))

0 comments on commit 8de298c

Please sign in to comment.