diff --git a/README.md b/README.md index 028c2db..5fa7ce7 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/ext/BitIntegersExt.jl b/ext/BitIntegersExt.jl index cc01cf1..f176959 100644 --- a/ext/BitIntegersExt.jl +++ b/ext/BitIntegersExt.jl @@ -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 \ No newline at end of file diff --git a/src/methods.jl b/src/methods.jl index 26f59f8..6935557 100644 --- a/src/methods.jl +++ b/src/methods.jl @@ -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 @@ -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 diff --git a/test/bitintegers.jl b/test/bitintegers.jl index 86c64f6..c542611 100644 --- a/test/bitintegers.jl +++ b/test/bitintegers.jl @@ -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 @@ -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 \ No newline at end of file