1
0

conversions.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #include "main.h"
  2. #include <math.h>
  3. #pragma intrinsic(fabs)
  4. /*
  5. #ifndef _WIN64
  6. __inline static int lrint(double flt)
  7. {
  8. int intgr;
  9. _asm
  10. {
  11. fld flt
  12. fistp intgr
  13. }
  14. return intgr;
  15. }
  16. #else
  17. __inline static int lrint(double flt)
  18. {
  19. return (int)flt;
  20. }
  21. #endif
  22. */
  23. #define PA_CLIP_( val, min, max )\
  24. { val = ((val) < (min)) ? (min) : (((val) > (max)) ? (max) : (val)); }
  25. void Float32_To_Int16_Clip(
  26. void *destinationBuffer, signed int destinationStride,
  27. void *sourceBuffer, signed int sourceStride,
  28. unsigned int count)
  29. {
  30. float *src = (float*)sourceBuffer;
  31. signed short *dest = (signed short*)destinationBuffer;
  32. while( count-- )
  33. {
  34. long samp = lrint((*src * (32768.0)));
  35. PA_CLIP_( samp, -0x8000, 0x7FFF );
  36. *dest = (signed short) samp;
  37. src += sourceStride;
  38. dest += destinationStride;
  39. }
  40. }
  41. inline static double clip(double x, double a, double b)
  42. {
  43. double x1 = fabs (x-a);
  44. double x2 = fabs (x-b);
  45. x = x1 + (a+b);
  46. x -= x2;
  47. x *= 0.5;
  48. return x;
  49. }
  50. /*
  51. benski> this might be faster than what the compiler spits out for the above function,
  52. but we should benchmark
  53. inline static double clip(double x, double a, double b)
  54. {
  55. const double zero_point_five = 0.5;
  56. __asm
  57. {
  58. fld x
  59. fld a
  60. fld b
  61. fld st(2)
  62. fsub st(0),st(2) // x-b
  63. fabs
  64. fadd st(0),st(2)
  65. fadd st(0),st(1)
  66. fld st(3)
  67. fsub st(0), st(2)
  68. fabs
  69. fsubp st(1), st(0)
  70. fmul zero_point_five
  71. ffree st(4)
  72. ffree st(3)
  73. ffree st(2)
  74. ffree st(1)
  75. }
  76. }
  77. */
  78. void Float32_To_Int24_Clip(
  79. void *destinationBuffer, signed int destinationStride,
  80. void *sourceBuffer, signed int sourceStride,
  81. unsigned int count)
  82. {
  83. float *src = (float*)sourceBuffer;
  84. unsigned char *dest = (unsigned char*)destinationBuffer;
  85. while( count-- )
  86. {
  87. /* convert to 32 bit and drop the low 8 bits */
  88. double scaled = *src * 0x7FFFFFFF;
  89. scaled=clip( scaled, -2147483648., 2147483647. );
  90. signed long temp = (signed long) scaled;
  91. dest[0] = (unsigned char)(temp >> 8);
  92. dest[1] = (unsigned char)(temp >> 16);
  93. dest[2] = (unsigned char)(temp >> 24);
  94. src += sourceStride;
  95. dest += destinationStride * 3;
  96. }
  97. }