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
25 changes: 16 additions & 9 deletions src/FixedPointNumbers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -460,17 +460,24 @@ 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

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)
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, _alias_symbol(X))
else
print(io, X)
end
Expand Down
8 changes: 8 additions & 0 deletions src/deprecations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 4 additions & 4 deletions src/fixed.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions src/normed.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions test/fixed.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions test/normed.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down