Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor with operate functions #9

Merged
merged 6 commits into from
Oct 16, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions src/MutableArithmetics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
module MutableArithmetics

include("interface.jl")
include("shortcuts.jl")

# Test that can be used to test an implementation of the interface
include("Test/Test.jl")
Expand Down
37 changes: 17 additions & 20 deletions src/Test/int.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
function int_add_test(::Type{T}) where T
@testset "add_to! / add!" begin
@test MA.mutability(T, MA.add_to!, T, T) isa MA.IsMutable
@test MA.mutability(T, MA.add!, T) isa MA.IsMutable
@test MA.mutability(T, +, T, T) isa MA.IsMutable

t(n) = convert(T, n)
a = t(5)
Expand All @@ -21,8 +20,7 @@ function int_add_test(::Type{T}) where T
end
function int_mul_test(::Type{T}) where T
@testset "mul_to! / mul!" begin
@test MA.mutability(T, MA.mul_to!, T, T) isa MA.IsMutable
@test MA.mutability(T, MA.mul!, T) isa MA.IsMutable
@test MA.mutability(T, *, T, T) isa MA.IsMutable

t(n) = convert(T, n)
a = t(5)
Expand All @@ -39,12 +37,11 @@ function int_mul_test(::Type{T}) where T
@test a == t(420)
end
end
function int_muladd_test(::Type{T}) where T
@testset "muladd_to! / muladd! / muladd_buf_to! /muladd_buf!" begin
@test MA.mutability(T, MA.muladd_to!, T, T, T) isa MA.IsMutable
@test MA.mutability(T, MA.muladd!, T, T) isa MA.IsMutable
@test MA.mutability(T, MA.muladd_buf_to!, T, T, T, T) isa MA.IsMutable
@test MA.mutability(T, MA.muladd_buf!, T, T, T) isa MA.IsMutable
function int_add_mul_test(::Type{T}) where T
@testset "add_mul_to! / add_mul! / add_mul_buf_to! /add_mul_buf!" begin
blegat marked this conversation as resolved.
Show resolved Hide resolved
@test MA.mutability(T, MA.add_mul, T, T) isa MA.IsMutable
@test MA.mutability(T, MA.add_mul, T, T, T) isa MA.IsMutable
@test MA.mutability(T, MA.add_mul, T, T, T, T) isa MA.IsMutable

t(n) = convert(T, n)
a = t(5)
Expand All @@ -53,36 +50,36 @@ function int_muladd_test(::Type{T}) where T
d = t(20)
buf = t(24)

@test MA.muladd_to!(a, b, c, d) == t(69)
@test MA.add_mul_to!(a, b, c, d) == t(69)
@test a == t(69)
a = t(5)
@test MA.muladd!(b, c, d) == t(69)
@test MA.add_mul!(b, c, d) == t(69)
@test b == t(69)
b = t(9)

@test MA.muladd_buf_to!(buf, a, b, c, d) == t(69)
@test MA.add_mul_buf_to!(buf, a, b, c, d) == t(69)
@test a == t(69)
@test MA.muladd_buf!(buf, b, c, d) == t(69)
@test MA.add_mul_buf!(buf, b, c, d) == t(69)
@test b == t(69)

a = t(148)
b = t(16)
c = t(17)
d = t(42)
buf = t(56)
@test MA.muladd!(a, b, c) == t(420)
@test MA.add_mul!(a, b, c) == t(420)
@test a == t(420)
a = t(148)
@test MA.muladd_buf_to!(buf, d, a, b, c) == t(420)
@test MA.add_mul_buf_to!(buf, d, a, b, c) == t(420)
@test d == t(420)
@test MA.muladd_buf!(buf, a, b, c) == t(420)
@test MA.add_mul_buf!(buf, a, b, c) == t(420)
@test a == t(420)
end
end

function int_zero_test(::Type{T}) where T
@testset "zero!" begin
@test MA.mutability(T, MA.zero!) isa MA.IsMutable
@test MA.mutability(T, zero, T) isa MA.IsMutable

t(n) = convert(T, n)
a = t(5)
Expand All @@ -94,7 +91,7 @@ end

function int_one_test(::Type{T}) where T
@testset "one!" begin
@test MA.mutability(T, MA.one!) isa MA.IsMutable
@test MA.mutability(T, one, T) isa MA.IsMutable

t(n) = convert(T, n)
a = t(5)
Expand All @@ -107,7 +104,7 @@ end
const int_tests = Dict(
"int_add" => int_add_test,
"int_mul" => int_mul_test,
"int_muladd" => int_muladd_test,
"int_add_mul" => int_add_mul_test,
"int_zero" => int_zero_test,
"int_one" => int_one_test
)
Expand Down
48 changes: 26 additions & 22 deletions src/bigint.jl
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
# This example contains a full implementation of the MutableArithmetics API for the BigInt datatype.
mutability(::Type{BigInt}) = IsMutable()

using MutableArithmetics
const MA = MutableArithmetics
# zero
promote_operation(::typeof(zero), ::Type{BigInt}) = BigInt
mutable_operate!(::typeof(zero), x::BigInt) = Base.GMP.MPZ.set_si!(x, 0)

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

# one!
MA.mutability(::Type{BigInt}, ::typeof(MA.one!)) = MA.IsMutable()
MA.one_impl!(x::BigInt) = Base.GMP.MPZ.set_si!(x, 1)
# +
promote_operation(::typeof(+), ::Type{BigInt}...) = BigInt
function mutable_operate_to!(output::BigInt, ::typeof(+), a::BigInt, b::BigInt)
return Base.GMP.MPZ.add!(output, a, b)
end

# 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)
# *
promote_operation(::typeof(*), ::Type{BigInt}...) = BigInt
function mutable_operate_to!(output::BigInt, ::typeof(*), a::BigInt, b::BigInt)
return Base.GMP.MPZ.mul!(output, a, b)
end

# 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))
# add_mul
function mutable_operate_to!(output::BigInt, ::typeof(add_mul), args::BigInt...)
return mutable_buffered_operate_to!(BigInt(), output, add_mul, args...)
end
function mutable_buffered_operate_to!(buffer::BigInt, output::BigInt, ::typeof(add_mul),
a::BigInt, args::BigInt...)
mutable_operate_to!(buffer, *, args...)
return mutable_operate_to!(output, +, a, buffer)
end