# Is there a way to reinterpret a Float64 to 2 Int32?

**URL:** <https://discourse.julialang.org/t/is-there-a-way-to-reinterpret-a-float64-to-2-int32/43596>\
**Category:** Performance\
**Created:** [July 23, 2020, 11:33pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-reinterpret-a-float64-to-2-int32/43596 "2020-07-23T23:33:56Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [July 24, 2020, 11:24pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-reinterpret-a-float64-to-2-int32/43596/4 "2020-07-24T23:24:46Z")

</div>

> [@dpsanders](#):
>
> `julia> reinterpret(Int32, [x])`

This is A way, but I find it doubtful it’s the best way, and possibly it’s buggy. I extended Elrod’s code to (his SIMD code doesn’t have the shift issue, but it (currently) ties you to x86, my code should work everywere):

```julia
other_half(x) = (reinterpret(Int, x) >> 32) % Int32

julia> @code_native other_half(x) # ok, I'm doubt the shift can be avoided.

This might be alternative code to consider:
julia> f(x) = (reinterpret(UInt64, x) & typemax(UInt32) << 32) % Int32

```

I have two concerns with your code. a) `[x]` assumes x is in memory? I guess the compiler is careful either way, but if your value is in a register it would force it stored in memory? b) It seems it depends on endianness, and the order you get is for little-endian. It could be reversed, in some theoretical (not yet supported) big-endian platform?

> **[IBM Developer](https://developer.ibm.com/articles/l-power-little-endian-faq-trs/)**
>
> IBM Developer is your one-stop location for getting hands-on training and learning in-demand skills on relevant technologies such as generative AI, data science, AI, and open source.

> [@Packing and unpacking binary data](https://discourse.julialang.org/t/packing-and-unpacking-binary-data/1638/7):
>
> Converting the UInt8 array to a string is done by String(). I tried string() before but that was not the right one. So now I am playing around with reinterpret but I need a way to deal with different endianness. So I guess I have to reverse the array if needed confused julia\> a[9:12] 4-element Array{UInt8,1}: 0x00 0x00 0x00 0x04 julia\> reinterpret(Int32, a[9:12]) 1-element Array{Int32,1}: 67108864 julia\> reinterpret(Int32, reverse(a[9:12])) 1-element Array{Int32,1}: 4

I used `@code_native` on your code, and it seems longer than I would want, and `@code_lowered` is way longer.

Similar for splitting Float32:

```julia
julia> both_half(x::Float32) = ((reinterpret(UInt32, x) >> 16), reinterpret(UInt32, x) % Int16)

```

Julia still does a shift, here by 16, and it should be avoidable, as x86 assembly has access to lower 16 bits, and higher 16 bits of the same register (but not higher 32-bits of 64 bit register without shifting), so Julia’s code generator could in theory do something slightly better (doesn’t not even on `-O3`). [I don’t see a way around it otherwise, except maybe with injecting LLVM code.] I’m assuming x86 register set and not SIMD, I just don’t recall, such likely might avoid generating a shift on x86 (in case Julia’s code generator tried to exploit such SIMD feature), even for 32-bit, and probably even ARM, without ARM would need a shift.

---

_[View the full topic](https://discourse.julialang.org/t/is-there-a-way-to-reinterpret-a-float64-to-2-int32/43596)._
