Convert to and from Int24

I am want to try an Int24 type, purely for storage. It is easy to declare one with

primitive type Int24 <: Signed 24 end

but I am not sure how to access its value.

I only need conversion to and from Int32, but I don’t think I can use reinterpret because they don’t have the same number of bits. How can I convert to and from Int32?

(BTW, I am aware that I destroy alignment and it will degrade performance; it is an experiment)

1 Like

Once you find out, it would be great to update the docs with an example like this (or a simpler one).

2 Likes

There is an example in test/intrinsics.jl:

primitive type Int24 24 end
Int24(x::Int) = Core.Intrinsics.trunc_int(Int24, x)
Int(x::Int24) = Core.Intrinsics.zext_int(Int, x)

which actually solves my problem, but it may not be general enough to add to the manual.

2 Likes

It would be interesting to see how well LLVM supports this these days. Once upon a time, “strange” bit sizes were very poorly supported but it may have gotten better.