float_cast.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /* Copyright (C) 2001 Erik de Castro Lopo <erikd AT mega-nerd DOT com> */
  2. /*
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions
  5. are met:
  6. - Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. - Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  12. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  13. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  14. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  15. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  16. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  17. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  18. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  19. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  20. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  21. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  22. */
  23. /* Version 1.1 */
  24. #ifndef FLOAT_CAST_H
  25. #define FLOAT_CAST_H
  26. #include "arch.h"
  27. /*============================================================================
  28. ** On Intel Pentium processors (especially PIII and probably P4), converting
  29. ** from float to int is very slow. To meet the C specs, the code produced by
  30. ** most C compilers targeting Pentium needs to change the FPU rounding mode
  31. ** before the float to int conversion is performed.
  32. **
  33. ** Changing the FPU rounding mode causes the FPU pipeline to be flushed. It
  34. ** is this flushing of the pipeline which is so slow.
  35. **
  36. ** Fortunately the ISO C99 specifications define the functions lrint, lrintf,
  37. ** llrint and llrintf which fix this problem as a side effect.
  38. **
  39. ** On Unix-like systems, the configure process should have detected the
  40. ** presence of these functions. If they weren't found we have to replace them
  41. ** here with a standard C cast.
  42. */
  43. /*
  44. ** The C99 prototypes for lrint and lrintf are as follows:
  45. **
  46. ** long int lrintf (float x) ;
  47. ** long int lrint (double x) ;
  48. */
  49. /* The presence of the required functions are detected during the configure
  50. ** process and the values HAVE_LRINT and HAVE_LRINTF are set accordingly in
  51. ** the config.h file.
  52. */
  53. /* With GCC, when SSE is available, the fastest conversion is cvtss2si. */
  54. #if defined(__GNUC__) && defined(__SSE__)
  55. #include <xmmintrin.h>
  56. static OPUS_INLINE opus_int32 float2int(float x) {return _mm_cvt_ss2si(_mm_set_ss(x));}
  57. #elif (defined(_MSC_VER) && _MSC_VER >= 1400) && (defined(_M_X64) || (defined(_M_IX86_FP) && _M_IX86_FP >= 1))
  58. #include <xmmintrin.h>
  59. static OPUS_INLINE opus_int32 float2int(float value)
  60. {
  61. /* _mm_load_ss will generate same code as _mm_set_ss
  62. ** in _MSC_VER >= 1914 /02 so keep __mm_load__ss
  63. ** for backward compatibility.
  64. */
  65. return _mm_cvtss_si32(_mm_load_ss(&value));
  66. }
  67. #elif (defined(_MSC_VER) && _MSC_VER >= 1400) && defined (_M_IX86)
  68. #include <math.h>
  69. /* Win32 doesn't seem to have these functions.
  70. ** Therefore implement OPUS_INLINE versions of these functions here.
  71. */
  72. static OPUS_INLINE opus_int32
  73. float2int (float flt)
  74. { int intgr;
  75. _asm
  76. { fld flt
  77. fistp intgr
  78. } ;
  79. return intgr ;
  80. }
  81. #elif defined(HAVE_LRINTF)
  82. /* These defines enable functionality introduced with the 1999 ISO C
  83. ** standard. They must be defined before the inclusion of math.h to
  84. ** engage them. If optimisation is enabled, these functions will be
  85. ** inlined. With optimisation switched off, you have to link in the
  86. ** maths library using -lm.
  87. */
  88. #define _ISOC9X_SOURCE 1
  89. #define _ISOC99_SOURCE 1
  90. #define __USE_ISOC9X 1
  91. #define __USE_ISOC99 1
  92. #include <math.h>
  93. #define float2int(x) lrintf(x)
  94. #elif (defined(HAVE_LRINT))
  95. #define _ISOC9X_SOURCE 1
  96. #define _ISOC99_SOURCE 1
  97. #define __USE_ISOC9X 1
  98. #define __USE_ISOC99 1
  99. #include <math.h>
  100. #define float2int(x) lrint(x)
  101. #else
  102. #if (defined(__GNUC__) && defined(__STDC__) && __STDC__ && __STDC_VERSION__ >= 199901L)
  103. /* supported by gcc in C99 mode, but not by all other compilers */
  104. #warning "Don't have the functions lrint() and lrintf ()."
  105. #warning "Replacing these functions with a standard C cast."
  106. #endif /* __STDC_VERSION__ >= 199901L */
  107. #include <math.h>
  108. #define float2int(flt) ((int)(floor(.5+flt)))
  109. #endif
  110. #ifndef DISABLE_FLOAT_API
  111. static OPUS_INLINE opus_int16 FLOAT2INT16(float x)
  112. {
  113. x = x*CELT_SIG_SCALE;
  114. x = MAX32(x, -32768);
  115. x = MIN32(x, 32767);
  116. return (opus_int16)float2int(x);
  117. }
  118. #endif /* DISABLE_FLOAT_API */
  119. #endif /* FLOAT_CAST_H */