4

I'm having trouble figuring out how to type cast a long into a double. I'm trying to read in a long int and use it in calculations in the AVX Registers. However, I cannot figure out how to cast the long int into a double precision float to be used within AVX registers.

How can I take a long int and turn it into a double precision float?

Paul R
  • 208,748
  • 37
  • 389
  • 560
RNikoopour
  • 523
  • 4
  • 16
  • You can do this easily in AVX512 but not in AVX or AVX2 AFAIK. There might be a workaround though - what is the range of your long ints, and how much precision do you need ? – Paul R Sep 14 '14 at 06:25
  • 4-byte `long int` or 8-byte `long int`? – barak manos Sep 14 '14 at 06:26
  • @barakmanos: since it's x86-64 then it's almost certainly 8 bytes. – Paul R Sep 14 '14 at 06:27
  • @PaulR I'm trying to read in an int so I don't need any precision since all values after the decimal should be 0 – RNikoopour Sep 14 '14 at 06:32
  • [`_mm256_castsi256_pd`](https://software.intel.com/sites/products/documentation/studio/composer/en-us/2011Update/compiler_c/intref_cls/common/intref_avx_castsi256_pd.htm) is something that you are looking for, but you should read this [SO-QUESTION](http://stackoverflow.com/questions/16512999/avx-convert-64-bit-integer-to-64-bit-float) – DOOM Sep 14 '14 at 06:33
  • @DOOM: I think that just does a cast, there is no actual conversion, which I think is what the OP is looking for (admittedly the question is somewhat ambiguous(. – Paul R Sep 14 '14 at 06:38
  • @DhbDhb: just to clarify my earlier question: what is the *range* of your long ints (i.e. how many of the 64 bits are you actually using) and how many of these bits do you require to be accurately converted (obviously this number will be < 64). – Paul R Sep 14 '14 at 06:40
  • @DhbDhb: you should probably edit your question to say "convert" rather than "cast", as it's already caused some confusion. `_mm256_castsi256_pd` is just a cast intrinsic which doesn't actually map to an instruction (it's just there to keep the compiler happy). – Paul R Sep 14 '14 at 06:43
  • Oh sorry. I understand what you mean now. I'm only using values between 1-100 so the first 7 bits I believe – RNikoopour Sep 14 '14 at 06:43
  • @PaulR That is my bad. I've just started using x86-64 so I don't know the proper terminology. I'm trying to convert a long int into a double using only x64-64 intel. I'm not inlining in C. – RNikoopour Sep 14 '14 at 06:44
  • OK - thanks - that makes things a lot clearer (apart from why you're using 64 bit long ints to store 7 bit values, but never mind). – Paul R Sep 14 '14 at 06:45
  • If you're only using values between 1-100 then a 4-byte `long int` is sufficiently large, and you can simply use `cvtsi2sd`. – barak manos Sep 14 '14 at 06:47
  • One more clarification: are you talking about converting a single value or an AVX vector (4 values) ? – Paul R Sep 14 '14 at 07:01
  • @PaulR I'm taking a single 64 bit int and want to turn it into a 64 bit long so I can then do something along of the lines of vbroadcastsd ymm15, [rsp] ; Assuming the stack pointer is pointing to the right spot. – RNikoopour Sep 14 '14 at 16:10
  • 1
    OK - the best strategy for this sort of thing is to write the code in C with intrinsics first, then generate asm using e.g. `gcc -O3 -S`, look at the compiler output, and then if you're still determined to write raw asm then use the compiler output as a starting point. This has two advantages: (i) you know the code is correct and (ii) the compiler will often come up with something smarter than a human might. – Paul R Sep 14 '14 at 16:14
  • Omg I have no idea why I didn't think to do that!!! Thank you so much! – RNikoopour Sep 14 '14 at 16:15
  • 1
    @barakmanos: sorry - I assumed wrong about the OP's requirements - please undelete your answer as it seems to be exactly what the OP needs. – Paul R Sep 14 '14 at 16:16
  • @DhbDhb: you're welcome - let me just make one last recommendation: unless you have a *really* good reason to be writing raw asm then please consider using C/C++ and intrinsics - the compiler will then do most of the heavy lifting for you, and you'll find that the code generated by modern compilers is hard to beat unless you're a very experienced assembly programmer. – Paul R Sep 14 '14 at 16:19
  • @PaulR: Done. I'm still not sure myself that this is what OP wants, so you might want to elaborate on it (comments or whatever). – barak manos Sep 14 '14 at 16:19

1 Answers1

4

If you're only using values between 1-100, then a 4-byte int is sufficiently large:

; double x = (double)n
cvtsi2sd    xmm0,dword ptr [n]
movsd       mmword ptr [x],xmm0
barak manos
  • 29,648
  • 10
  • 62
  • 114