fixed_generic.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /* Copyright (C) 2007-2009 Xiph.Org Foundation
  2. Copyright (C) 2003-2008 Jean-Marc Valin
  3. Copyright (C) 2007-2008 CSIRO */
  4. /**
  5. @file fixed_generic.h
  6. @brief Generic fixed-point operations
  7. */
  8. /*
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions
  11. are met:
  12. - Redistributions of source code must retain the above copyright
  13. notice, this list of conditions and the following disclaimer.
  14. - Redistributions in binary form must reproduce the above copyright
  15. notice, this list of conditions and the following disclaimer in the
  16. documentation and/or other materials provided with the distribution.
  17. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  20. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  21. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  22. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  23. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  24. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  25. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  26. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #ifndef FIXED_GENERIC_H
  30. #define FIXED_GENERIC_H
  31. /** Multiply a 16-bit signed value by a 16-bit unsigned value. The result is a 32-bit signed value */
  32. #define MULT16_16SU(a,b) ((opus_val32)(opus_val16)(a)*(opus_val32)(opus_uint16)(b))
  33. /** 16x32 multiplication, followed by a 16-bit shift right. Results fits in 32 bits */
  34. #if OPUS_FAST_INT64
  35. #define MULT16_32_Q16(a,b) ((opus_val32)SHR((opus_int64)((opus_val16)(a))*(b),16))
  36. #else
  37. #define MULT16_32_Q16(a,b) ADD32(MULT16_16((a),SHR((b),16)), SHR(MULT16_16SU((a),((b)&0x0000ffff)),16))
  38. #endif
  39. /** 16x32 multiplication, followed by a 16-bit shift right (round-to-nearest). Results fits in 32 bits */
  40. #if OPUS_FAST_INT64
  41. #define MULT16_32_P16(a,b) ((opus_val32)PSHR((opus_int64)((opus_val16)(a))*(b),16))
  42. #else
  43. #define MULT16_32_P16(a,b) ADD32(MULT16_16((a),SHR((b),16)), PSHR(MULT16_16SU((a),((b)&0x0000ffff)),16))
  44. #endif
  45. /** 16x32 multiplication, followed by a 15-bit shift right. Results fits in 32 bits */
  46. #if OPUS_FAST_INT64
  47. #define MULT16_32_Q15(a,b) ((opus_val32)SHR((opus_int64)((opus_val16)(a))*(b),15))
  48. #else
  49. #define MULT16_32_Q15(a,b) ADD32(SHL(MULT16_16((a),SHR((b),16)),1), SHR(MULT16_16SU((a),((b)&0x0000ffff)),15))
  50. #endif
  51. /** 32x32 multiplication, followed by a 31-bit shift right. Results fits in 32 bits */
  52. #if OPUS_FAST_INT64
  53. #define MULT32_32_Q31(a,b) ((opus_val32)SHR((opus_int64)(a)*(opus_int64)(b),31))
  54. #else
  55. #define MULT32_32_Q31(a,b) ADD32(ADD32(SHL(MULT16_16(SHR((a),16),SHR((b),16)),1), SHR(MULT16_16SU(SHR((a),16),((b)&0x0000ffff)),15)), SHR(MULT16_16SU(SHR((b),16),((a)&0x0000ffff)),15))
  56. #endif
  57. /** Compile-time conversion of float constant to 16-bit value */
  58. #define QCONST16(x,bits) ((opus_val16)(.5+(x)*(((opus_val32)1)<<(bits))))
  59. /** Compile-time conversion of float constant to 32-bit value */
  60. #define QCONST32(x,bits) ((opus_val32)(.5+(x)*(((opus_val32)1)<<(bits))))
  61. /** Negate a 16-bit value */
  62. #define NEG16(x) (-(x))
  63. /** Negate a 32-bit value */
  64. #define NEG32(x) (-(x))
  65. /** Change a 32-bit value into a 16-bit value. The value is assumed to fit in 16-bit, otherwise the result is undefined */
  66. #define EXTRACT16(x) ((opus_val16)(x))
  67. /** Change a 16-bit value into a 32-bit value */
  68. #define EXTEND32(x) ((opus_val32)(x))
  69. /** Arithmetic shift-right of a 16-bit value */
  70. #define SHR16(a,shift) ((a) >> (shift))
  71. /** Arithmetic shift-left of a 16-bit value */
  72. #define SHL16(a,shift) ((opus_int16)((opus_uint16)(a)<<(shift)))
  73. /** Arithmetic shift-right of a 32-bit value */
  74. #define SHR32(a,shift) ((a) >> (shift))
  75. /** Arithmetic shift-left of a 32-bit value */
  76. #define SHL32(a,shift) ((opus_int32)((opus_uint32)(a)<<(shift)))
  77. /** 32-bit arithmetic shift right with rounding-to-nearest instead of rounding down */
  78. #define PSHR32(a,shift) (SHR32((a)+((EXTEND32(1)<<((shift))>>1)),shift))
  79. /** 32-bit arithmetic shift right where the argument can be negative */
  80. #define VSHR32(a, shift) (((shift)>0) ? SHR32(a, shift) : SHL32(a, -(shift)))
  81. /** "RAW" macros, should not be used outside of this header file */
  82. #define SHR(a,shift) ((a) >> (shift))
  83. #define SHL(a,shift) SHL32(a,shift)
  84. #define PSHR(a,shift) (SHR((a)+((EXTEND32(1)<<((shift))>>1)),shift))
  85. #define SATURATE(x,a) (((x)>(a) ? (a) : (x)<-(a) ? -(a) : (x)))
  86. #define SATURATE16(x) (EXTRACT16((x)>32767 ? 32767 : (x)<-32768 ? -32768 : (x)))
  87. /** Shift by a and round-to-neareast 32-bit value. Result is a 16-bit value */
  88. #define ROUND16(x,a) (EXTRACT16(PSHR32((x),(a))))
  89. /** Shift by a and round-to-neareast 32-bit value. Result is a saturated 16-bit value */
  90. #define SROUND16(x,a) EXTRACT16(SATURATE(PSHR32(x,a), 32767));
  91. /** Divide by two */
  92. #define HALF16(x) (SHR16(x,1))
  93. #define HALF32(x) (SHR32(x,1))
  94. /** Add two 16-bit values */
  95. #define ADD16(a,b) ((opus_val16)((opus_val16)(a)+(opus_val16)(b)))
  96. /** Subtract two 16-bit values */
  97. #define SUB16(a,b) ((opus_val16)(a)-(opus_val16)(b))
  98. /** Add two 32-bit values */
  99. #define ADD32(a,b) ((opus_val32)(a)+(opus_val32)(b))
  100. /** Subtract two 32-bit values */
  101. #define SUB32(a,b) ((opus_val32)(a)-(opus_val32)(b))
  102. /** Add two 32-bit values, ignore any overflows */
  103. #define ADD32_ovflw(a,b) ((opus_val32)((opus_uint32)(a)+(opus_uint32)(b)))
  104. /** Subtract two 32-bit values, ignore any overflows */
  105. #define SUB32_ovflw(a,b) ((opus_val32)((opus_uint32)(a)-(opus_uint32)(b)))
  106. /* Avoid MSVC warning C4146: unary minus operator applied to unsigned type */
  107. /** Negate 32-bit value, ignore any overflows */
  108. #define NEG32_ovflw(a) ((opus_val32)(0-(opus_uint32)(a)))
  109. /** 16x16 multiplication where the result fits in 16 bits */
  110. #define MULT16_16_16(a,b) ((((opus_val16)(a))*((opus_val16)(b))))
  111. /* (opus_val32)(opus_val16) gives TI compiler a hint that it's 16x16->32 multiply */
  112. /** 16x16 multiplication where the result fits in 32 bits */
  113. #define MULT16_16(a,b) (((opus_val32)(opus_val16)(a))*((opus_val32)(opus_val16)(b)))
  114. /** 16x16 multiply-add where the result fits in 32 bits */
  115. #define MAC16_16(c,a,b) (ADD32((c),MULT16_16((a),(b))))
  116. /** 16x32 multiply, followed by a 15-bit shift right and 32-bit add.
  117. b must fit in 31 bits.
  118. Result fits in 32 bits. */
  119. #define MAC16_32_Q15(c,a,b) ADD32((c),ADD32(MULT16_16((a),SHR((b),15)), SHR(MULT16_16((a),((b)&0x00007fff)),15)))
  120. /** 16x32 multiplication, followed by a 16-bit shift right and 32-bit add.
  121. Results fits in 32 bits */
  122. #define MAC16_32_Q16(c,a,b) ADD32((c),ADD32(MULT16_16((a),SHR((b),16)), SHR(MULT16_16SU((a),((b)&0x0000ffff)),16)))
  123. #define MULT16_16_Q11_32(a,b) (SHR(MULT16_16((a),(b)),11))
  124. #define MULT16_16_Q11(a,b) (SHR(MULT16_16((a),(b)),11))
  125. #define MULT16_16_Q13(a,b) (SHR(MULT16_16((a),(b)),13))
  126. #define MULT16_16_Q14(a,b) (SHR(MULT16_16((a),(b)),14))
  127. #define MULT16_16_Q15(a,b) (SHR(MULT16_16((a),(b)),15))
  128. #define MULT16_16_P13(a,b) (SHR(ADD32(4096,MULT16_16((a),(b))),13))
  129. #define MULT16_16_P14(a,b) (SHR(ADD32(8192,MULT16_16((a),(b))),14))
  130. #define MULT16_16_P15(a,b) (SHR(ADD32(16384,MULT16_16((a),(b))),15))
  131. /** Divide a 32-bit value by a 16-bit value. Result fits in 16 bits */
  132. #define DIV32_16(a,b) ((opus_val16)(((opus_val32)(a))/((opus_val16)(b))))
  133. /** Divide a 32-bit value by a 32-bit value. Result fits in 32 bits */
  134. #define DIV32(a,b) (((opus_val32)(a))/((opus_val32)(b)))
  135. #if defined(MIPSr1_ASM)
  136. #include "mips/fixed_generic_mipsr1.h"
  137. #endif
  138. static OPUS_INLINE opus_val16 SIG2WORD16_generic(celt_sig x)
  139. {
  140. x = PSHR32(x, SIG_SHIFT);
  141. x = MAX32(x, -32768);
  142. x = MIN32(x, 32767);
  143. return EXTRACT16(x);
  144. }
  145. #define SIG2WORD16(x) (SIG2WORD16_generic(x))
  146. #endif