SigProc_FIX.h 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. /***********************************************************************
  2. Copyright (c) 2006-2011, Skype Limited. All rights reserved.
  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 notice,
  7. 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. - Neither the name of Internet Society, IETF or IETF Trust, nor the
  12. names of specific contributors, may be used to endorse or promote
  13. products derived from this software without specific prior written
  14. permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  19. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  20. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  21. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. POSSIBILITY OF SUCH DAMAGE.
  26. ***********************************************************************/
  27. #ifndef SILK_SIGPROC_FIX_H
  28. #define SILK_SIGPROC_FIX_H
  29. #ifdef __cplusplus
  30. extern "C"
  31. {
  32. #endif
  33. /*#define silk_MACRO_COUNT */ /* Used to enable WMOPS counting */
  34. #define SILK_MAX_ORDER_LPC 16 /* max order of the LPC analysis in schur() and k2a() */
  35. #include <string.h> /* for memset(), memcpy(), memmove() */
  36. #include "typedef.h"
  37. #include "resampler_structs.h"
  38. #include "macros.h"
  39. #include "cpu_support.h"
  40. #if defined(OPUS_X86_MAY_HAVE_SSE4_1)
  41. #include "x86/SigProc_FIX_sse.h"
  42. #endif
  43. /********************************************************************/
  44. /* SIGNAL PROCESSING FUNCTIONS */
  45. /********************************************************************/
  46. /*!
  47. * Initialize/reset the resampler state for a given pair of input/output sampling rates
  48. */
  49. opus_int silk_resampler_init(
  50. silk_resampler_state_struct *S, /* I/O Resampler state */
  51. opus_int32 Fs_Hz_in, /* I Input sampling rate (Hz) */
  52. opus_int32 Fs_Hz_out, /* I Output sampling rate (Hz) */
  53. opus_int forEnc /* I If 1: encoder; if 0: decoder */
  54. );
  55. /*!
  56. * Resampler: convert from one sampling rate to another
  57. */
  58. opus_int silk_resampler(
  59. silk_resampler_state_struct *S, /* I/O Resampler state */
  60. opus_int16 out[], /* O Output signal */
  61. const opus_int16 in[], /* I Input signal */
  62. opus_int32 inLen /* I Number of input samples */
  63. );
  64. /*!
  65. * Downsample 2x, mediocre quality
  66. */
  67. void silk_resampler_down2(
  68. opus_int32 *S, /* I/O State vector [ 2 ] */
  69. opus_int16 *out, /* O Output signal [ len ] */
  70. const opus_int16 *in, /* I Input signal [ floor(len/2) ] */
  71. opus_int32 inLen /* I Number of input samples */
  72. );
  73. /*!
  74. * Downsample by a factor 2/3, low quality
  75. */
  76. void silk_resampler_down2_3(
  77. opus_int32 *S, /* I/O State vector [ 6 ] */
  78. opus_int16 *out, /* O Output signal [ floor(2*inLen/3) ] */
  79. const opus_int16 *in, /* I Input signal [ inLen ] */
  80. opus_int32 inLen /* I Number of input samples */
  81. );
  82. /*!
  83. * second order ARMA filter;
  84. * slower than biquad() but uses more precise coefficients
  85. * can handle (slowly) varying coefficients
  86. */
  87. void silk_biquad_alt(
  88. const opus_int16 *in, /* I input signal */
  89. const opus_int32 *B_Q28, /* I MA coefficients [3] */
  90. const opus_int32 *A_Q28, /* I AR coefficients [2] */
  91. opus_int32 *S, /* I/O State vector [2] */
  92. opus_int16 *out, /* O output signal */
  93. const opus_int32 len, /* I signal length (must be even) */
  94. opus_int stride /* I Operate on interleaved signal if > 1 */
  95. );
  96. /* Variable order MA prediction error filter. */
  97. void silk_LPC_analysis_filter(
  98. opus_int16 *out, /* O Output signal */
  99. const opus_int16 *in, /* I Input signal */
  100. const opus_int16 *B, /* I MA prediction coefficients, Q12 [order] */
  101. const opus_int32 len, /* I Signal length */
  102. const opus_int32 d, /* I Filter order */
  103. int arch /* I Run-time architecture */
  104. );
  105. /* Chirp (bandwidth expand) LP AR filter */
  106. void silk_bwexpander(
  107. opus_int16 *ar, /* I/O AR filter to be expanded (without leading 1) */
  108. const opus_int d, /* I Length of ar */
  109. opus_int32 chirp_Q16 /* I Chirp factor (typically in the range 0 to 1) */
  110. );
  111. /* Chirp (bandwidth expand) LP AR filter */
  112. void silk_bwexpander_32(
  113. opus_int32 *ar, /* I/O AR filter to be expanded (without leading 1) */
  114. const opus_int d, /* I Length of ar */
  115. opus_int32 chirp_Q16 /* I Chirp factor in Q16 */
  116. );
  117. /* Compute inverse of LPC prediction gain, and */
  118. /* test if LPC coefficients are stable (all poles within unit circle) */
  119. opus_int32 silk_LPC_inverse_pred_gain( /* O Returns inverse prediction gain in energy domain, Q30 */
  120. const opus_int16 *A_Q12, /* I Prediction coefficients, Q12 [order] */
  121. const opus_int order /* I Prediction order */
  122. );
  123. /* For input in Q24 domain */
  124. opus_int32 silk_LPC_inverse_pred_gain_Q24( /* O Returns inverse prediction gain in energy domain, Q30 */
  125. const opus_int32 *A_Q24, /* I Prediction coefficients [order] */
  126. const opus_int order /* I Prediction order */
  127. );
  128. /* Split signal in two decimated bands using first-order allpass filters */
  129. void silk_ana_filt_bank_1(
  130. const opus_int16 *in, /* I Input signal [N] */
  131. opus_int32 *S, /* I/O State vector [2] */
  132. opus_int16 *outL, /* O Low band [N/2] */
  133. opus_int16 *outH, /* O High band [N/2] */
  134. const opus_int32 N /* I Number of input samples */
  135. );
  136. /********************************************************************/
  137. /* SCALAR FUNCTIONS */
  138. /********************************************************************/
  139. /* Approximation of 128 * log2() (exact inverse of approx 2^() below) */
  140. /* Convert input to a log scale */
  141. opus_int32 silk_lin2log(
  142. const opus_int32 inLin /* I input in linear scale */
  143. );
  144. /* Approximation of a sigmoid function */
  145. opus_int silk_sigm_Q15(
  146. opus_int in_Q5 /* I */
  147. );
  148. /* Approximation of 2^() (exact inverse of approx log2() above) */
  149. /* Convert input to a linear scale */
  150. opus_int32 silk_log2lin(
  151. const opus_int32 inLog_Q7 /* I input on log scale */
  152. );
  153. /* Compute number of bits to right shift the sum of squares of a vector */
  154. /* of int16s to make it fit in an int32 */
  155. void silk_sum_sqr_shift(
  156. opus_int32 *energy, /* O Energy of x, after shifting to the right */
  157. opus_int *shift, /* O Number of bits right shift applied to energy */
  158. const opus_int16 *x, /* I Input vector */
  159. opus_int len /* I Length of input vector */
  160. );
  161. /* Calculates the reflection coefficients from the correlation sequence */
  162. /* Faster than schur64(), but much less accurate. */
  163. /* uses SMLAWB(), requiring armv5E and higher. */
  164. opus_int32 silk_schur( /* O Returns residual energy */
  165. opus_int16 *rc_Q15, /* O reflection coefficients [order] Q15 */
  166. const opus_int32 *c, /* I correlations [order+1] */
  167. const opus_int32 order /* I prediction order */
  168. );
  169. /* Calculates the reflection coefficients from the correlation sequence */
  170. /* Slower than schur(), but more accurate. */
  171. /* Uses SMULL(), available on armv4 */
  172. opus_int32 silk_schur64( /* O returns residual energy */
  173. opus_int32 rc_Q16[], /* O Reflection coefficients [order] Q16 */
  174. const opus_int32 c[], /* I Correlations [order+1] */
  175. opus_int32 order /* I Prediction order */
  176. );
  177. /* Step up function, converts reflection coefficients to prediction coefficients */
  178. void silk_k2a(
  179. opus_int32 *A_Q24, /* O Prediction coefficients [order] Q24 */
  180. const opus_int16 *rc_Q15, /* I Reflection coefficients [order] Q15 */
  181. const opus_int32 order /* I Prediction order */
  182. );
  183. /* Step up function, converts reflection coefficients to prediction coefficients */
  184. void silk_k2a_Q16(
  185. opus_int32 *A_Q24, /* O Prediction coefficients [order] Q24 */
  186. const opus_int32 *rc_Q16, /* I Reflection coefficients [order] Q16 */
  187. const opus_int32 order /* I Prediction order */
  188. );
  189. /* Apply sine window to signal vector. */
  190. /* Window types: */
  191. /* 1 -> sine window from 0 to pi/2 */
  192. /* 2 -> sine window from pi/2 to pi */
  193. /* every other sample of window is linearly interpolated, for speed */
  194. void silk_apply_sine_window(
  195. opus_int16 px_win[], /* O Pointer to windowed signal */
  196. const opus_int16 px[], /* I Pointer to input signal */
  197. const opus_int win_type, /* I Selects a window type */
  198. const opus_int length /* I Window length, multiple of 4 */
  199. );
  200. /* Compute autocorrelation */
  201. void silk_autocorr(
  202. opus_int32 *results, /* O Result (length correlationCount) */
  203. opus_int *scale, /* O Scaling of the correlation vector */
  204. const opus_int16 *inputData, /* I Input data to correlate */
  205. const opus_int inputDataSize, /* I Length of input */
  206. const opus_int correlationCount, /* I Number of correlation taps to compute */
  207. int arch /* I Run-time architecture */
  208. );
  209. void silk_decode_pitch(
  210. opus_int16 lagIndex, /* I */
  211. opus_int8 contourIndex, /* O */
  212. opus_int pitch_lags[], /* O 4 pitch values */
  213. const opus_int Fs_kHz, /* I sampling frequency (kHz) */
  214. const opus_int nb_subfr /* I number of sub frames */
  215. );
  216. opus_int silk_pitch_analysis_core( /* O Voicing estimate: 0 voiced, 1 unvoiced */
  217. const opus_int16 *frame, /* I Signal of length PE_FRAME_LENGTH_MS*Fs_kHz */
  218. opus_int *pitch_out, /* O 4 pitch lag values */
  219. opus_int16 *lagIndex, /* O Lag Index */
  220. opus_int8 *contourIndex, /* O Pitch contour Index */
  221. opus_int *LTPCorr_Q15, /* I/O Normalized correlation; input: value from previous frame */
  222. opus_int prevLag, /* I Last lag of previous frame; set to zero is unvoiced */
  223. const opus_int32 search_thres1_Q16, /* I First stage threshold for lag candidates 0 - 1 */
  224. const opus_int search_thres2_Q13, /* I Final threshold for lag candidates 0 - 1 */
  225. const opus_int Fs_kHz, /* I Sample frequency (kHz) */
  226. const opus_int complexity, /* I Complexity setting, 0-2, where 2 is highest */
  227. const opus_int nb_subfr, /* I number of 5 ms subframes */
  228. int arch /* I Run-time architecture */
  229. );
  230. /* Compute Normalized Line Spectral Frequencies (NLSFs) from whitening filter coefficients */
  231. /* If not all roots are found, the a_Q16 coefficients are bandwidth expanded until convergence. */
  232. void silk_A2NLSF(
  233. opus_int16 *NLSF, /* O Normalized Line Spectral Frequencies in Q15 (0..2^15-1) [d] */
  234. opus_int32 *a_Q16, /* I/O Monic whitening filter coefficients in Q16 [d] */
  235. const opus_int d /* I Filter order (must be even) */
  236. );
  237. /* compute whitening filter coefficients from normalized line spectral frequencies */
  238. void silk_NLSF2A(
  239. opus_int16 *a_Q12, /* O monic whitening filter coefficients in Q12, [ d ] */
  240. const opus_int16 *NLSF, /* I normalized line spectral frequencies in Q15, [ d ] */
  241. const opus_int d /* I filter order (should be even) */
  242. );
  243. void silk_insertion_sort_increasing(
  244. opus_int32 *a, /* I/O Unsorted / Sorted vector */
  245. opus_int *idx, /* O Index vector for the sorted elements */
  246. const opus_int L, /* I Vector length */
  247. const opus_int K /* I Number of correctly sorted positions */
  248. );
  249. void silk_insertion_sort_decreasing_int16(
  250. opus_int16 *a, /* I/O Unsorted / Sorted vector */
  251. opus_int *idx, /* O Index vector for the sorted elements */
  252. const opus_int L, /* I Vector length */
  253. const opus_int K /* I Number of correctly sorted positions */
  254. );
  255. void silk_insertion_sort_increasing_all_values_int16(
  256. opus_int16 *a, /* I/O Unsorted / Sorted vector */
  257. const opus_int L /* I Vector length */
  258. );
  259. /* NLSF stabilizer, for a single input data vector */
  260. void silk_NLSF_stabilize(
  261. opus_int16 *NLSF_Q15, /* I/O Unstable/stabilized normalized LSF vector in Q15 [L] */
  262. const opus_int16 *NDeltaMin_Q15, /* I Min distance vector, NDeltaMin_Q15[L] must be >= 1 [L+1] */
  263. const opus_int L /* I Number of NLSF parameters in the input vector */
  264. );
  265. /* Laroia low complexity NLSF weights */
  266. void silk_NLSF_VQ_weights_laroia(
  267. opus_int16 *pNLSFW_Q_OUT, /* O Pointer to input vector weights [D] */
  268. const opus_int16 *pNLSF_Q15, /* I Pointer to input vector [D] */
  269. const opus_int D /* I Input vector dimension (even) */
  270. );
  271. /* Compute reflection coefficients from input signal */
  272. void silk_burg_modified_c(
  273. opus_int32 *res_nrg, /* O Residual energy */
  274. opus_int *res_nrg_Q, /* O Residual energy Q value */
  275. opus_int32 A_Q16[], /* O Prediction coefficients (length order) */
  276. const opus_int16 x[], /* I Input signal, length: nb_subfr * ( D + subfr_length ) */
  277. const opus_int32 minInvGain_Q30, /* I Inverse of max prediction gain */
  278. const opus_int subfr_length, /* I Input signal subframe length (incl. D preceding samples) */
  279. const opus_int nb_subfr, /* I Number of subframes stacked in x */
  280. const opus_int D, /* I Order */
  281. int arch /* I Run-time architecture */
  282. );
  283. /* Copy and multiply a vector by a constant */
  284. void silk_scale_copy_vector16(
  285. opus_int16 *data_out,
  286. const opus_int16 *data_in,
  287. opus_int32 gain_Q16, /* I Gain in Q16 */
  288. const opus_int dataSize /* I Length */
  289. );
  290. /* Some for the LTP related function requires Q26 to work.*/
  291. void silk_scale_vector32_Q26_lshift_18(
  292. opus_int32 *data1, /* I/O Q0/Q18 */
  293. opus_int32 gain_Q26, /* I Q26 */
  294. opus_int dataSize /* I length */
  295. );
  296. /********************************************************************/
  297. /* INLINE ARM MATH */
  298. /********************************************************************/
  299. /* return sum( inVec1[i] * inVec2[i] ) */
  300. opus_int32 silk_inner_prod_aligned(
  301. const opus_int16 *const inVec1, /* I input vector 1 */
  302. const opus_int16 *const inVec2, /* I input vector 2 */
  303. const opus_int len, /* I vector lengths */
  304. int arch /* I Run-time architecture */
  305. );
  306. opus_int32 silk_inner_prod_aligned_scale(
  307. const opus_int16 *const inVec1, /* I input vector 1 */
  308. const opus_int16 *const inVec2, /* I input vector 2 */
  309. const opus_int scale, /* I number of bits to shift */
  310. const opus_int len /* I vector lengths */
  311. );
  312. opus_int64 silk_inner_prod16_aligned_64_c(
  313. const opus_int16 *inVec1, /* I input vector 1 */
  314. const opus_int16 *inVec2, /* I input vector 2 */
  315. const opus_int len /* I vector lengths */
  316. );
  317. /********************************************************************/
  318. /* MACROS */
  319. /********************************************************************/
  320. /* Rotate a32 right by 'rot' bits. Negative rot values result in rotating
  321. left. Output is 32bit int.
  322. Note: contemporary compilers recognize the C expression below and
  323. compile it into a 'ror' instruction if available. No need for OPUS_INLINE ASM! */
  324. static OPUS_INLINE opus_int32 silk_ROR32( opus_int32 a32, opus_int rot )
  325. {
  326. opus_uint32 x = (opus_uint32) a32;
  327. opus_uint32 r = (opus_uint32) rot;
  328. opus_uint32 m = (opus_uint32) -rot;
  329. if( rot == 0 ) {
  330. return a32;
  331. } else if( rot < 0 ) {
  332. return (opus_int32) ((x << m) | (x >> (32 - m)));
  333. } else {
  334. return (opus_int32) ((x << (32 - r)) | (x >> r));
  335. }
  336. }
  337. /* Allocate opus_int16 aligned to 4-byte memory address */
  338. #if EMBEDDED_ARM
  339. #define silk_DWORD_ALIGN __attribute__((aligned(4)))
  340. #else
  341. #define silk_DWORD_ALIGN
  342. #endif
  343. /* Useful Macros that can be adjusted to other platforms */
  344. #define silk_memcpy(dest, src, size) memcpy((dest), (src), (size))
  345. #define silk_memset(dest, src, size) memset((dest), (src), (size))
  346. #define silk_memmove(dest, src, size) memmove((dest), (src), (size))
  347. /* Fixed point macros */
  348. /* (a32 * b32) output have to be 32bit int */
  349. #define silk_MUL(a32, b32) ((a32) * (b32))
  350. /* (a32 * b32) output have to be 32bit uint */
  351. #define silk_MUL_uint(a32, b32) silk_MUL(a32, b32)
  352. /* a32 + (b32 * c32) output have to be 32bit int */
  353. #define silk_MLA(a32, b32, c32) silk_ADD32((a32),((b32) * (c32)))
  354. /* a32 + (b32 * c32) output have to be 32bit uint */
  355. #define silk_MLA_uint(a32, b32, c32) silk_MLA(a32, b32, c32)
  356. /* ((a32 >> 16) * (b32 >> 16)) output have to be 32bit int */
  357. #define silk_SMULTT(a32, b32) (((a32) >> 16) * ((b32) >> 16))
  358. /* a32 + ((a32 >> 16) * (b32 >> 16)) output have to be 32bit int */
  359. #define silk_SMLATT(a32, b32, c32) silk_ADD32((a32),((b32) >> 16) * ((c32) >> 16))
  360. #define silk_SMLALBB(a64, b16, c16) silk_ADD64((a64),(opus_int64)((opus_int32)(b16) * (opus_int32)(c16)))
  361. /* (a32 * b32) */
  362. #define silk_SMULL(a32, b32) ((opus_int64)(a32) * /*(opus_int64)*/(b32))
  363. /* Adds two signed 32-bit values in a way that can overflow, while not relying on undefined behaviour
  364. (just standard two's complement implementation-specific behaviour) */
  365. #define silk_ADD32_ovflw(a, b) ((opus_int32)((opus_uint32)(a) + (opus_uint32)(b)))
  366. /* Subtractss two signed 32-bit values in a way that can overflow, while not relying on undefined behaviour
  367. (just standard two's complement implementation-specific behaviour) */
  368. #define silk_SUB32_ovflw(a, b) ((opus_int32)((opus_uint32)(a) - (opus_uint32)(b)))
  369. /* Multiply-accumulate macros that allow overflow in the addition (ie, no asserts in debug mode) */
  370. #define silk_MLA_ovflw(a32, b32, c32) silk_ADD32_ovflw((a32), (opus_uint32)(b32) * (opus_uint32)(c32))
  371. #define silk_SMLABB_ovflw(a32, b32, c32) (silk_ADD32_ovflw((a32) , ((opus_int32)((opus_int16)(b32))) * (opus_int32)((opus_int16)(c32))))
  372. #define silk_DIV32_16(a32, b16) ((opus_int32)((a32) / (b16)))
  373. #define silk_DIV32(a32, b32) ((opus_int32)((a32) / (b32)))
  374. /* These macros enables checking for overflow in silk_API_Debug.h*/
  375. #define silk_ADD16(a, b) ((a) + (b))
  376. #define silk_ADD32(a, b) ((a) + (b))
  377. #define silk_ADD64(a, b) ((a) + (b))
  378. #define silk_SUB16(a, b) ((a) - (b))
  379. #define silk_SUB32(a, b) ((a) - (b))
  380. #define silk_SUB64(a, b) ((a) - (b))
  381. #define silk_SAT8(a) ((a) > silk_int8_MAX ? silk_int8_MAX : \
  382. ((a) < silk_int8_MIN ? silk_int8_MIN : (a)))
  383. #define silk_SAT16(a) ((a) > silk_int16_MAX ? silk_int16_MAX : \
  384. ((a) < silk_int16_MIN ? silk_int16_MIN : (a)))
  385. #define silk_SAT32(a) ((a) > silk_int32_MAX ? silk_int32_MAX : \
  386. ((a) < silk_int32_MIN ? silk_int32_MIN : (a)))
  387. #define silk_CHECK_FIT8(a) (a)
  388. #define silk_CHECK_FIT16(a) (a)
  389. #define silk_CHECK_FIT32(a) (a)
  390. #define silk_ADD_SAT16(a, b) (opus_int16)silk_SAT16( silk_ADD32( (opus_int32)(a), (b) ) )
  391. #define silk_ADD_SAT64(a, b) ((((a) + (b)) & 0x8000000000000000LL) == 0 ? \
  392. ((((a) & (b)) & 0x8000000000000000LL) != 0 ? silk_int64_MIN : (a)+(b)) : \
  393. ((((a) | (b)) & 0x8000000000000000LL) == 0 ? silk_int64_MAX : (a)+(b)) )
  394. #define silk_SUB_SAT16(a, b) (opus_int16)silk_SAT16( silk_SUB32( (opus_int32)(a), (b) ) )
  395. #define silk_SUB_SAT64(a, b) ((((a)-(b)) & 0x8000000000000000LL) == 0 ? \
  396. (( (a) & ((b)^0x8000000000000000LL) & 0x8000000000000000LL) ? silk_int64_MIN : (a)-(b)) : \
  397. ((((a)^0x8000000000000000LL) & (b) & 0x8000000000000000LL) ? silk_int64_MAX : (a)-(b)) )
  398. /* Saturation for positive input values */
  399. #define silk_POS_SAT32(a) ((a) > silk_int32_MAX ? silk_int32_MAX : (a))
  400. /* Add with saturation for positive input values */
  401. #define silk_ADD_POS_SAT8(a, b) ((((a)+(b)) & 0x80) ? silk_int8_MAX : ((a)+(b)))
  402. #define silk_ADD_POS_SAT16(a, b) ((((a)+(b)) & 0x8000) ? silk_int16_MAX : ((a)+(b)))
  403. #define silk_ADD_POS_SAT32(a, b) ((((a)+(b)) & 0x80000000) ? silk_int32_MAX : ((a)+(b)))
  404. #define silk_ADD_POS_SAT64(a, b) ((((a)+(b)) & 0x8000000000000000LL) ? silk_int64_MAX : ((a)+(b)))
  405. #define silk_LSHIFT8(a, shift) ((opus_int8)((opus_uint8)(a)<<(shift))) /* shift >= 0, shift < 8 */
  406. #define silk_LSHIFT16(a, shift) ((opus_int16)((opus_uint16)(a)<<(shift))) /* shift >= 0, shift < 16 */
  407. #define silk_LSHIFT32(a, shift) ((opus_int32)((opus_uint32)(a)<<(shift))) /* shift >= 0, shift < 32 */
  408. #define silk_LSHIFT64(a, shift) ((opus_int64)((opus_uint64)(a)<<(shift))) /* shift >= 0, shift < 64 */
  409. #define silk_LSHIFT(a, shift) silk_LSHIFT32(a, shift) /* shift >= 0, shift < 32 */
  410. #define silk_RSHIFT8(a, shift) ((a)>>(shift)) /* shift >= 0, shift < 8 */
  411. #define silk_RSHIFT16(a, shift) ((a)>>(shift)) /* shift >= 0, shift < 16 */
  412. #define silk_RSHIFT32(a, shift) ((a)>>(shift)) /* shift >= 0, shift < 32 */
  413. #define silk_RSHIFT64(a, shift) ((a)>>(shift)) /* shift >= 0, shift < 64 */
  414. #define silk_RSHIFT(a, shift) silk_RSHIFT32(a, shift) /* shift >= 0, shift < 32 */
  415. /* saturates before shifting */
  416. #define silk_LSHIFT_SAT32(a, shift) (silk_LSHIFT32( silk_LIMIT( (a), silk_RSHIFT32( silk_int32_MIN, (shift) ), \
  417. silk_RSHIFT32( silk_int32_MAX, (shift) ) ), (shift) ))
  418. #define silk_LSHIFT_ovflw(a, shift) ((opus_int32)((opus_uint32)(a) << (shift))) /* shift >= 0, allowed to overflow */
  419. #define silk_LSHIFT_uint(a, shift) ((a) << (shift)) /* shift >= 0 */
  420. #define silk_RSHIFT_uint(a, shift) ((a) >> (shift)) /* shift >= 0 */
  421. #define silk_ADD_LSHIFT(a, b, shift) ((a) + silk_LSHIFT((b), (shift))) /* shift >= 0 */
  422. #define silk_ADD_LSHIFT32(a, b, shift) silk_ADD32((a), silk_LSHIFT32((b), (shift))) /* shift >= 0 */
  423. #define silk_ADD_LSHIFT_uint(a, b, shift) ((a) + silk_LSHIFT_uint((b), (shift))) /* shift >= 0 */
  424. #define silk_ADD_RSHIFT(a, b, shift) ((a) + silk_RSHIFT((b), (shift))) /* shift >= 0 */
  425. #define silk_ADD_RSHIFT32(a, b, shift) silk_ADD32((a), silk_RSHIFT32((b), (shift))) /* shift >= 0 */
  426. #define silk_ADD_RSHIFT_uint(a, b, shift) ((a) + silk_RSHIFT_uint((b), (shift))) /* shift >= 0 */
  427. #define silk_SUB_LSHIFT32(a, b, shift) silk_SUB32((a), silk_LSHIFT32((b), (shift))) /* shift >= 0 */
  428. #define silk_SUB_RSHIFT32(a, b, shift) silk_SUB32((a), silk_RSHIFT32((b), (shift))) /* shift >= 0 */
  429. /* Requires that shift > 0 */
  430. #define silk_RSHIFT_ROUND(a, shift) ((shift) == 1 ? ((a) >> 1) + ((a) & 1) : (((a) >> ((shift) - 1)) + 1) >> 1)
  431. #define silk_RSHIFT_ROUND64(a, shift) ((shift) == 1 ? ((a) >> 1) + ((a) & 1) : (((a) >> ((shift) - 1)) + 1) >> 1)
  432. /* Number of rightshift required to fit the multiplication */
  433. #define silk_NSHIFT_MUL_32_32(a, b) ( -(31- (32-silk_CLZ32(silk_abs(a)) + (32-silk_CLZ32(silk_abs(b))))) )
  434. #define silk_NSHIFT_MUL_16_16(a, b) ( -(15- (16-silk_CLZ16(silk_abs(a)) + (16-silk_CLZ16(silk_abs(b))))) )
  435. #define silk_min(a, b) (((a) < (b)) ? (a) : (b))
  436. #define silk_max(a, b) (((a) > (b)) ? (a) : (b))
  437. /* Macro to convert floating-point constants to fixed-point */
  438. #define SILK_FIX_CONST( C, Q ) ((opus_int32)((C) * ((opus_int64)1 << (Q)) + 0.5))
  439. /* silk_min() versions with typecast in the function call */
  440. static OPUS_INLINE opus_int silk_min_int(opus_int a, opus_int b)
  441. {
  442. return (((a) < (b)) ? (a) : (b));
  443. }
  444. static OPUS_INLINE opus_int16 silk_min_16(opus_int16 a, opus_int16 b)
  445. {
  446. return (((a) < (b)) ? (a) : (b));
  447. }
  448. static OPUS_INLINE opus_int32 silk_min_32(opus_int32 a, opus_int32 b)
  449. {
  450. return (((a) < (b)) ? (a) : (b));
  451. }
  452. static OPUS_INLINE opus_int64 silk_min_64(opus_int64 a, opus_int64 b)
  453. {
  454. return (((a) < (b)) ? (a) : (b));
  455. }
  456. /* silk_min() versions with typecast in the function call */
  457. static OPUS_INLINE opus_int silk_max_int(opus_int a, opus_int b)
  458. {
  459. return (((a) > (b)) ? (a) : (b));
  460. }
  461. static OPUS_INLINE opus_int16 silk_max_16(opus_int16 a, opus_int16 b)
  462. {
  463. return (((a) > (b)) ? (a) : (b));
  464. }
  465. static OPUS_INLINE opus_int32 silk_max_32(opus_int32 a, opus_int32 b)
  466. {
  467. return (((a) > (b)) ? (a) : (b));
  468. }
  469. static OPUS_INLINE opus_int64 silk_max_64(opus_int64 a, opus_int64 b)
  470. {
  471. return (((a) > (b)) ? (a) : (b));
  472. }
  473. #define silk_LIMIT( a, limit1, limit2) ((limit1) > (limit2) ? ((a) > (limit1) ? (limit1) : ((a) < (limit2) ? (limit2) : (a))) \
  474. : ((a) > (limit2) ? (limit2) : ((a) < (limit1) ? (limit1) : (a))))
  475. #define silk_LIMIT_int silk_LIMIT
  476. #define silk_LIMIT_16 silk_LIMIT
  477. #define silk_LIMIT_32 silk_LIMIT
  478. #define silk_abs(a) (((a) > 0) ? (a) : -(a)) /* Be careful, silk_abs returns wrong when input equals to silk_intXX_MIN */
  479. #define silk_abs_int(a) (((a) ^ ((a) >> (8 * sizeof(a) - 1))) - ((a) >> (8 * sizeof(a) - 1)))
  480. #define silk_abs_int32(a) (((a) ^ ((a) >> 31)) - ((a) >> 31))
  481. #define silk_abs_int64(a) (((a) > 0) ? (a) : -(a))
  482. #define silk_sign(a) ((a) > 0 ? 1 : ( (a) < 0 ? -1 : 0 ))
  483. /* PSEUDO-RANDOM GENERATOR */
  484. /* Make sure to store the result as the seed for the next call (also in between */
  485. /* frames), otherwise result won't be random at all. When only using some of the */
  486. /* bits, take the most significant bits by right-shifting. */
  487. #define silk_RAND(seed) (silk_MLA_ovflw(907633515, (seed), 196314165))
  488. /* Add some multiplication functions that can be easily mapped to ARM. */
  489. /* silk_SMMUL: Signed top word multiply.
  490. ARMv6 2 instruction cycles.
  491. ARMv3M+ 3 instruction cycles. use SMULL and ignore LSB registers.(except xM)*/
  492. /*#define silk_SMMUL(a32, b32) (opus_int32)silk_RSHIFT(silk_SMLAL(silk_SMULWB((a32), (b32)), (a32), silk_RSHIFT_ROUND((b32), 16)), 16)*/
  493. /* the following seems faster on x86 */
  494. #define silk_SMMUL(a32, b32) (opus_int32)silk_RSHIFT64(silk_SMULL((a32), (b32)), 32)
  495. #if !defined(OPUS_X86_MAY_HAVE_SSE4_1)
  496. #define silk_burg_modified(res_nrg, res_nrg_Q, A_Q16, x, minInvGain_Q30, subfr_length, nb_subfr, D, arch) \
  497. ((void)(arch), silk_burg_modified_c(res_nrg, res_nrg_Q, A_Q16, x, minInvGain_Q30, subfr_length, nb_subfr, D, arch))
  498. #define silk_inner_prod16_aligned_64(inVec1, inVec2, len, arch) \
  499. ((void)(arch),silk_inner_prod16_aligned_64_c(inVec1, inVec2, len))
  500. #endif
  501. #include "Inlines.h"
  502. #include "MacroCount.h"
  503. #include "MacroDebug.h"
  504. #ifdef OPUS_ARM_INLINE_ASM
  505. #include "arm/SigProc_FIX_armv4.h"
  506. #endif
  507. #ifdef OPUS_ARM_INLINE_EDSP
  508. #include "arm/SigProc_FIX_armv5e.h"
  509. #endif
  510. #if defined(MIPSr1_ASM)
  511. #include "mips/sigproc_fix_mipsr1.h"
  512. #endif
  513. #ifdef __cplusplus
  514. }
  515. #endif
  516. #endif /* SILK_SIGPROC_FIX_H */