-
Notifications
You must be signed in to change notification settings - Fork 33
Support for non-standard integer types? #247
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
Comments
I would like to relax as much as possible the limitations which come from the assumptions of the built-in types. For example, in the future, I would like to support a 14-bit However, padding (or alignment) is a troublesome issue. Personally, I don't think it's a good idea for FixedPointNumbers to interfere with Julia's memory layout policy. In other words, the following should be an invariant property. julia> Core.sizeof(Fixed{Int24,0}) # not `Base` but `Core`
4 If we need a type of exactly 3 bytes, for example, I think it should have fields "in bytes". julia> struct UInt24P <: Unsigned
data::NTuple{3, UInt8}
end
julia> sizeof(Normed{UInt24P, 2})
3 Whether julia> a = reinterpret(Fixed{Int24, 0}, UInt8.(1:3))
1-element reinterpret(Fixed{Int24, 0}, ::Vector{UInt8}):
197121.0Q23f0
julia> a[1] = 0
ERROR: Padding of type Fixed{Int24, 0} is not compatible with type UInt8. I would start by surveying past discussions on the JuliaLang/julia and Discourse. |
Ugh, I should have realized its not that simple. I guess the padding happens because julia> struct Misaligned
a::Int8
b::Int16
end
julia> sizeof(Misaligned)
4 It was a bit surprising that I did not encounter the issue when testing my application, but it seems like things work if the reinterpreted array is collected: julia> aa = reinterpret(Fixed{Int24, 1}, UInt8.(1:3)) |> collect
1-element Array{Q22f1,1} with eltype Fixed{Int24, 1}:
98560.5Q22f1
julia> aa[1] = 1
1
julia> aa
1-element Array{Q22f1,1} with eltype Fixed{Int24, 1}:
1.0Q22f1
julia> aa .+= 3
1-element Array{Q22f1,1} with eltype Fixed{Int24, 1}:
4.0Q22f1 I have not put it under any further scrutinity than this though and realizing the padding/alignment issue does make me a bit nervous that things will fail unpredictably. The tuple approach is quite interesting, but won't one end up in the painful situation of having to define almost every operator for julia> aa = reinterpret(Normed{UInt24P, 1}, UInt8.(1:3))
1-element reinterpret(Normed{UInt24P, 1}, ::Vector{UInt8}):
Error showing value of type Base.ReinterpretArray{Normed{UInt24P, 1}, 1, UInt8, Vector{UInt8}, false}:
ERROR: MethodError: no method matching typemax(::Type{UInt24P})
julia> aa .+ 1
ERROR: MethodError: no method matching typemax(::Type{UInt24P}) |
I don't have a definite solution for this issue, but I am thinking of including padding for the I think the first step to address this issue is to define |
Sorry if this is naive, but would it be possible to support fixed point representations of non-standard integer sizes? It looks like it would be a simple thing to fix:
Example:
The text was updated successfully, but these errors were encountered: