From 83e522af9b2609dc486cba15c4518c74361553de Mon Sep 17 00:00:00 2001 From: kimikage Date: Wed, 23 Sep 2020 17:54:43 +0900 Subject: [PATCH 1/2] Improve generation of typealiases --- src/FixedPointNumbers.jl | 20 +++++++++++++------- src/deprecations.jl | 8 ++++++++ src/fixed.jl | 8 ++++---- src/normed.jl | 8 ++++---- test/fixed.jl | 2 ++ test/normed.jl | 2 ++ 6 files changed, 33 insertions(+), 15 deletions(-) diff --git a/src/FixedPointNumbers.jl b/src/FixedPointNumbers.jl index d899f2ff..e88c52ae 100644 --- a/src/FixedPointNumbers.jl +++ b/src/FixedPointNumbers.jl @@ -460,17 +460,23 @@ end hasalias(::Type) = false hasalias(::Type{X}) where {T<:NotBiggerThanInt64, f, X<:FixedPoint{T,f}} = f isa Int -# Printing. These are used to generate type-symbols, so we need them -# before we include "src/fixed.jl" / "src/normed.jl". +# `alias_symbol` is used to define type aliases, so we need this before we +# include "src/fixed.jl" / "src/normed.jl". +function alias_symbol(@nospecialize(X)) + Symbol(type_prefix(X), nbitsint(X), 'f', nbitsfrac(X)) +end + @inline function showtype(io::IO, ::Type{X}) where {X <: FixedPoint} if hasalias(X) + # low-level code equivalent to `write(io, alias_symbol(X))` + # This is faster than dynamic string `print`ing, but still slow. f = nbitsfrac(X) m = nbitsint(X) - write(io, typechar(X)) - m > 9 && write(io, Char(m ÷ 10 + 0x30)) - write(io, Char(m % 10 + 0x30), 'f') - f > 9 && write(io, Char(f ÷ 10 + 0x30)) - write(io, Char(f % 10 + 0x30)) + write(io, type_prefix(X)) + m > 9 && write(io, (m ÷ 10) % UInt8 + 0x30) + write(io, (m % 10) % UInt8 + 0x30, 0x66) + f > 9 && write(io, (f ÷ 10) % UInt8 + 0x30) + write(io, (f % 10) % UInt8 + 0x30) else print(io, X) end diff --git a/src/deprecations.jl b/src/deprecations.jl index 30d586a8..2441efd3 100644 --- a/src/deprecations.jl +++ b/src/deprecations.jl @@ -7,3 +7,11 @@ function floattype(::Type{T}) where {T <: Real} """, :floattype) return T end + +function typechar(::Type{X}) where {X} + Base.depwarn(""" + `typechar` was deprecated since the prefix may not be a single character in the future. + We recommend not using private functions, but if you need to, use `type_prefix` instead. + """, :typechar) + Char(string(type_prefix(X))[1]) +end diff --git a/src/fixed.jl b/src/fixed.jl index 33db0123..1abd7ff4 100644 --- a/src/fixed.jl +++ b/src/fixed.jl @@ -27,14 +27,14 @@ end # TODO: remove this hasalias(::Type{F}) where {F <: Union{Fixed{Int8,8},Fixed{Int16,16},Fixed{Int32,32},Fixed{Int64,64}}} = false -typechar(::Type{X}) where {X <: Fixed} = 'Q' +type_prefix(::Type{F}) where {F <: Fixed{<:Signed}} = :Q for T in (Int8, Int16, Int32, Int64) - io = IOBuffer() for f in 0:bitwidth(T)-1 - sym = Symbol(String(take!(showtype(io, Fixed{T,f})))) + F = Fixed{T,f} + sym = alias_symbol(F) @eval begin - const $sym = Fixed{$T,$f} + const $sym = $F export $sym end end diff --git a/src/normed.jl b/src/normed.jl index 625055a2..a8fae420 100644 --- a/src/normed.jl +++ b/src/normed.jl @@ -19,14 +19,14 @@ struct Normed{T <: Unsigned, f} <: FixedPoint{T, f} end end -typechar(::Type{X}) where {X <: Normed} = 'N' +type_prefix(::Type{N}) where {N <: Normed{<:Unsigned}} = :N for T in (UInt8, UInt16, UInt32, UInt64) - io = IOBuffer() for f in 1:bitwidth(T) - sym = Symbol(String(take!(showtype(io, Normed{T,f})))) + N = Normed{T,f} + sym = alias_symbol(N) @eval begin - const $sym = Normed{$T,$f} + const $sym = $N export $sym end end diff --git a/test/fixed.jl b/test/fixed.jl index e8b5c75d..c66a9495 100644 --- a/test/fixed.jl +++ b/test/fixed.jl @@ -718,6 +718,8 @@ end end @testset "show" begin + @test (@test_deprecated FixedPointNumbers.typechar(Q0f7)) === 'Q' + iob = IOBuffer() q0f7 = reinterpret(Q0f7, signed(0xaa)) show(iob, q0f7) diff --git a/test/normed.jl b/test/normed.jl index e03bff36..c5328299 100644 --- a/test/normed.jl +++ b/test/normed.jl @@ -690,6 +690,8 @@ end end @testset "show" begin + @test (@test_deprecated FixedPointNumbers.typechar(N0f8)) === 'N' + iob = IOBuffer() n0f8 = reinterpret(N0f8, 0xaa) show(iob, n0f8) From 953001e6600d50320fe9e6b61092a8ea39e8831a Mon Sep 17 00:00:00 2001 From: kimikage Date: Sun, 27 Sep 2020 18:39:02 +0900 Subject: [PATCH 2/2] Use generated function for `showtype` --- src/FixedPointNumbers.jl | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/FixedPointNumbers.jl b/src/FixedPointNumbers.jl index e88c52ae..67b38160 100644 --- a/src/FixedPointNumbers.jl +++ b/src/FixedPointNumbers.jl @@ -466,17 +466,18 @@ function alias_symbol(@nospecialize(X)) Symbol(type_prefix(X), nbitsint(X), 'f', nbitsfrac(X)) end +function _alias_symbol(::Type{X}) where {X <: FixedPoint} + if @generated + sym = string(alias_symbol(X)) + return :(Symbol($sym)) + else + return alias_symbol(X) + end +end + @inline function showtype(io::IO, ::Type{X}) where {X <: FixedPoint} if hasalias(X) - # low-level code equivalent to `write(io, alias_symbol(X))` - # This is faster than dynamic string `print`ing, but still slow. - f = nbitsfrac(X) - m = nbitsint(X) - write(io, type_prefix(X)) - m > 9 && write(io, (m ÷ 10) % UInt8 + 0x30) - write(io, (m % 10) % UInt8 + 0x30, 0x66) - f > 9 && write(io, (f ÷ 10) % UInt8 + 0x30) - write(io, (f % 10) % UInt8 + 0x30) + write(io, _alias_symbol(X)) else print(io, X) end