Skip to content
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
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,15 @@ The signedness of data and storage types are always identical.

When both packages are loaded, a package extension enables checked conversion
from emulated integers to BitIntegers types, including user-defined types from
`@define_integers`.
`@define_integers`. Modular conversion with `x % T` wraps to the destination
range instead of throwing for out-of-range values.

```jldoctest usage
julia> Int256(UInt3(7))
7

julia> UInt3(7) % Int256
7
```

If you want to `@emulate` multiple types, you can simply provide multiple arguments to the macro with regular macro syntax:
Expand Down
2 changes: 2 additions & 0 deletions ext/BitIntegersExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ using BitIntegers: AbstractBitSigned, AbstractBitUnsigned

(::Type{Target})(value::EmulatedInteger) where {Target<:Union{AbstractBitSigned, AbstractBitUnsigned}} = Target(value[])

Base.rem(value::EmulatedInteger, ::Type{Target}) where {Target<:Union{AbstractBitSigned, AbstractBitUnsigned}} = value[] % Target

end
4 changes: 2 additions & 2 deletions src/methods.jl
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,7 @@ Base.count_zeros(x::T) where T<:EmulatedInteger = count_zeros(x[] | (~storagetyp
# Shift the wasted high bits out, then count leading ones in the storage type. Works for both signednesses because after the shift, the bits that count are the same as the logical high bits.
Base.leading_ones(x::T) where T<:EmulatedInteger = leading_ones(x[] << wastedbits(T)) % Int

# `x[] % UInt8` matches Base's two's-complement reinterpretation convention and lowers to a single `reinterpret`/truncation for any storage type.
Base.rem(x::EmulatedInteger, ::Type{UInt8}) = x[] % UInt8
Base.rem(x::EmulatedInteger, Target::Base.BitIntegerType) = x[] % Target

Base.:/(x::T, y::T) where T<:EmulatedInteger = x[] / y[] # Should result in Float64

Expand All @@ -155,6 +154,7 @@ Base.trailing_zeros(x::T) where T<:EmulatedInteger = trailing_zeros(x[] | (one(s
Base.trailing_ones(x::EmulatedInteger) = x |> zext |> trailing_ones

Base.AbstractFloat(x::EmulatedInteger) = x[] |> AbstractFloat
Base.BigInt(x::EmulatedInteger) = BigInt(x[])

# Promote to the regular primitive types resulting in InexactErrors if values are not representable.
for B in Union{Base.BitInteger, Base.IEEEFloat} |> Base.uniontypes .|> Symbol
Expand Down
8 changes: 7 additions & 1 deletion test/bitintegers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
@emulate Int3 UInt3 Int3_128 UInt3_128 Int129 UInt129

for Source in (Int3, UInt3, Int3_128, UInt3_128, Int129, UInt129)
for Target in (Int256, UInt256, Int512, UInt512, Int1024, UInt1024, ConversionInt24, ConversionUInt24)
for value in (typemin(Source), zero(Source), one(Source), typemax(Source))
@test BigInt(value) == BigInt(value[])
end
for Target in (Base.BitInteger_types..., Int256, UInt256, Int512, UInt512, Int1024, UInt1024, ConversionInt24, ConversionUInt24)
for value in (typemin(Source), zero(Source), one(Source), typemax(Source))
@test (@inferred value % Target) === BigInt(value[]) % Target
if typemin(Target) <= BigInt(value[]) <= typemax(Target)
expected = Target(value[])
@test (@inferred Target(value)) === expected
Expand All @@ -19,6 +23,8 @@
end

@test Int256(Int3(1)) === Int256(1)
@test Int3(-1) % UInt256 === typemax(UInt256)
@test_throws InexactError UInt256(Int3(-1))
extension = Base.get_extension(EmulatedBitIntegers, :BitIntegersExt)
@test isempty(Test.detect_ambiguities(EmulatedBitIntegers, BitIntegers, extension))
end