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

Implement checked operations on Bool #15294

Merged
merged 2 commits into from
Mar 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion base/checked.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import Core.Intrinsics: box, unbox,
checked_srem_int,
checked_uadd_int, checked_usub_int, checked_umul_int, checked_udiv_int,
checked_urem_int
import Base: no_op_err
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do you need this import? It seems unused.

Copy link
Contributor

Choose a reason for hiding this comment

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

julia/base/checked.jl

Lines 29 to 38 in 58bdac3

checked_neg{T<:Integer}(x::T) = no_op_err("checked_neg", T)
checked_abs{T<:Integer}(x::T) = no_op_err("checked_abs", T)
checked_add{T<:Integer}(x::T, y::T) = no_op_err("checked_add", T)
checked_sub{T<:Integer}(x::T, y::T) = no_op_err("checked_sub", T)
checked_mul{T<:Integer}(x::T, y::T) = no_op_err("checked_mul", T)
checked_div{T<:Integer}(x::T, y::T) = no_op_err("checked_div", T)
checked_rem{T<:Integer}(x::T, y::T) = no_op_err("checked_rem", T)
checked_fld{T<:Integer}(x::T, y::T) = no_op_err("checked_fld", T)
checked_mod{T<:Integer}(x::T, y::T) = no_op_err("checked_mod", T)
checked_cld{T<:Integer}(x::T, y::T) = no_op_err("checked_cld", T)
?


# define promotion behavior for checked operations
checked_add(x::Integer, y::Integer) = checked_add(promote(x,y)...)
Expand All @@ -37,7 +38,7 @@ checked_mod{T<:Integer}(x::T, y::T) = no_op_err("checked_mod", T)
checked_cld{T<:Integer}(x::T, y::T) = no_op_err("checked_cld", T)

typealias SignedInt Union{Int8,Int16,Int32,Int64,Int128}
typealias UnsignedInt Union{UInt8,UInt16,UInt32,UInt64,UInt128}
typealias UnsignedInt Union{UInt8,UInt16,UInt32,UInt64,UInt128,Bool}

# LLVM has several code generation bugs for checked integer arithmetic (see e.g.
# #4905). We thus distinguish between operations that can be implemented via
Expand Down Expand Up @@ -165,6 +166,8 @@ function checked_add{T<:BrokenUnsignedInt}(x::T, y::T)
x + y
end
end
checked_add(x::Bool, y::Bool) = x + y
checked_add(x::Bool) = +x

# Handle multiple arguments
checked_add(x) = x
Expand Down Expand Up @@ -211,6 +214,8 @@ function checked_sub{T<:BrokenUnsignedInt}(x::T, y::T)
x - y
end
end
checked_sub(x::Bool, y::Bool) = x - y
checked_sub(x::Bool) = -x

"""
Base.checked_mul(x, y)
Expand Down
44 changes: 44 additions & 0 deletions test/checked.jl
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,50 @@ for T in (UInt8, UInt16, UInt32, UInt64, UInt128)
@test_throws DivideError checked_cld(typemax(T), T(0))
end

# Boolean
@test checked_add(false) === 0
@test checked_add(true) === 1
@test checked_sub(false) === 0
@test checked_sub(true) === -1
@test checked_neg(false) === 0
@test checked_neg(true) === -1
@test checked_abs(true) === true
@test checked_abs(false) === false
@test checked_mul(false) === false
@test checked_mul(true) === true

@test checked_add(true, true) === 2
@test checked_add(true, false) === 1
@test checked_add(false, false) === 0
@test checked_add(false, true) === 1

@test checked_sub(true, true) === 0
@test checked_sub(true, false) === 1
@test checked_sub(false, false) === 0
@test checked_sub(false, true) === -1

@test checked_mul(true, false) === false
@test checked_mul(false, false) === false
@test checked_mul(true, true) === true
@test checked_mul(false, true) === false

@test checked_div(true, true) === true
@test checked_div(false, true) === false
@test_throws DivideError checked_div(true, false)
@test checked_rem(true, true) === false
@test checked_rem(false, true) === false
@test_throws DivideError checked_rem(true, false)
@test checked_fld(true, true) === true
@test checked_fld(false, true) === false
@test_throws DivideError checked_fld(true, false)
@test checked_mod(true, true) === false
@test checked_mod(false, true) === false
@test_throws DivideError checked_mod(true, false)
@test checked_cld(true, true) === true
@test checked_cld(false, true) === false
@test_throws DivideError checked_cld(true, false)

# BigInt
@test checked_abs(BigInt(-1)) == BigInt(1)
@test checked_abs(BigInt(1)) == BigInt(1)
@test checked_neg(BigInt(-1)) == BigInt(1)
Expand Down