fork(4) download
  1. #include <iostream>
  2. #include <emmintrin.h>
  3.  
  4. using namespace std;
  5.  
  6. short input[8] = {0, -1, 2048, -2048, 0x7fff, -0x8000, short(2048*3.1415), short(2048*2.7182)};
  7.  
  8. float output[8];
  9.  
  10.  
  11.  
  12. int main() {
  13. // get input:
  14. __m128i val = _mm_loadu_si128((__m128i*)input);
  15. // add 0x8000 to wrap to unsigned short domain:
  16. val = _mm_add_epi16(val, _mm_set1_epi16(0x8000));
  17. // interleave with upper part of float(1<<23)/2048.f:
  18. __m128i lo = _mm_unpacklo_epi16(val, _mm_set1_epi16(0x4580));
  19. __m128i hi = _mm_unpackhi_epi16(val, _mm_set1_epi16(0x4580));
  20. // interpret as float and subtract float((1<<23) + (0x8000))/2048.f
  21. __m128 lo_f = _mm_sub_ps(_mm_castsi128_ps(lo), _mm_set_ps1(float((1<<23) + (1<<15))/2048.f));
  22. __m128 hi_f = _mm_sub_ps(_mm_castsi128_ps(hi), _mm_set_ps1(float((1<<23) + (1<<15))/2048.f));
  23. // store:
  24. _mm_storeu_ps(output, lo_f);
  25. _mm_storeu_ps(output+4, hi_f);
  26.  
  27. for(int i=0; i<8; ++i)
  28. std::cout << output[i] << " ";
  29. std::cout << "\n";
  30. }
Success #stdin #stdout 0s 4352KB
stdin
Standard input is empty
stdout
0 -0.000488281 1 -1 15.9995 -16 3.14111 2.71777