fixed_debug.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  1. /* Copyright (C) 2003-2008 Jean-Marc Valin
  2. Copyright (C) 2007-2012 Xiph.Org Foundation */
  3. /**
  4. @file fixed_debug.h
  5. @brief Fixed-point operations with debugging
  6. */
  7. /*
  8. Redistribution and use in source and binary forms, with or without
  9. modification, are permitted provided that the following conditions
  10. are met:
  11. - Redistributions of source code must retain the above copyright
  12. notice, this list of conditions and the following disclaimer.
  13. - Redistributions in binary form must reproduce the above copyright
  14. notice, this list of conditions and the following disclaimer in the
  15. documentation and/or other materials provided with the distribution.
  16. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  19. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  20. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  21. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  22. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  23. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  24. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  25. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #ifndef FIXED_DEBUG_H
  29. #define FIXED_DEBUG_H
  30. #include <stdio.h>
  31. #include "opus_defines.h"
  32. #ifdef CELT_C
  33. OPUS_EXPORT opus_int64 celt_mips=0;
  34. #else
  35. extern opus_int64 celt_mips;
  36. #endif
  37. #define MULT16_16SU(a,b) ((opus_val32)(opus_val16)(a)*(opus_val32)(opus_uint16)(b))
  38. #define MULT32_32_Q31(a,b) ADD32(ADD32(SHL32(MULT16_16(SHR32((a),16),SHR((b),16)),1), SHR32(MULT16_16SU(SHR32((a),16),((b)&0x0000ffff)),15)), SHR32(MULT16_16SU(SHR32((b),16),((a)&0x0000ffff)),15))
  39. /** 16x32 multiplication, followed by a 16-bit shift right. Results fits in 32 bits */
  40. #define MULT16_32_Q16(a,b) ADD32(MULT16_16((a),SHR32((b),16)), SHR32(MULT16_16SU((a),((b)&0x0000ffff)),16))
  41. #define MULT16_32_P16(a,b) MULT16_32_PX(a,b,16)
  42. #define QCONST16(x,bits) ((opus_val16)(.5+(x)*(((opus_val32)1)<<(bits))))
  43. #define QCONST32(x,bits) ((opus_val32)(.5+(x)*(((opus_val32)1)<<(bits))))
  44. #define VERIFY_SHORT(x) ((x)<=32767&&(x)>=-32768)
  45. #define VERIFY_INT(x) ((x)<=2147483647LL&&(x)>=-2147483648LL)
  46. #define VERIFY_UINT(x) ((x)<=(2147483647LLU<<1))
  47. #define SHR(a,b) SHR32(a,b)
  48. #define PSHR(a,b) PSHR32(a,b)
  49. /** Add two 32-bit values, ignore any overflows */
  50. #define ADD32_ovflw(a,b) (celt_mips+=2,(opus_val32)((opus_uint32)(a)+(opus_uint32)(b)))
  51. /** Subtract two 32-bit values, ignore any overflows */
  52. #define SUB32_ovflw(a,b) (celt_mips+=2,(opus_val32)((opus_uint32)(a)-(opus_uint32)(b)))
  53. /* Avoid MSVC warning C4146: unary minus operator applied to unsigned type */
  54. /** Negate 32-bit value, ignore any overflows */
  55. #define NEG32_ovflw(a) (celt_mips+=2,(opus_val32)(0-(opus_uint32)(a)))
  56. static OPUS_INLINE short NEG16(int x)
  57. {
  58. int res;
  59. if (!VERIFY_SHORT(x))
  60. {
  61. fprintf (stderr, "NEG16: input is not short: %d\n", (int)x);
  62. #ifdef FIXED_DEBUG_ASSERT
  63. celt_assert(0);
  64. #endif
  65. }
  66. res = -x;
  67. if (!VERIFY_SHORT(res))
  68. {
  69. fprintf (stderr, "NEG16: output is not short: %d\n", (int)res);
  70. #ifdef FIXED_DEBUG_ASSERT
  71. celt_assert(0);
  72. #endif
  73. }
  74. celt_mips++;
  75. return res;
  76. }
  77. static OPUS_INLINE int NEG32(opus_int64 x)
  78. {
  79. opus_int64 res;
  80. if (!VERIFY_INT(x))
  81. {
  82. fprintf (stderr, "NEG16: input is not int: %d\n", (int)x);
  83. #ifdef FIXED_DEBUG_ASSERT
  84. celt_assert(0);
  85. #endif
  86. }
  87. res = -x;
  88. if (!VERIFY_INT(res))
  89. {
  90. fprintf (stderr, "NEG16: output is not int: %d\n", (int)res);
  91. #ifdef FIXED_DEBUG_ASSERT
  92. celt_assert(0);
  93. #endif
  94. }
  95. celt_mips+=2;
  96. return res;
  97. }
  98. #define EXTRACT16(x) EXTRACT16_(x, __FILE__, __LINE__)
  99. static OPUS_INLINE short EXTRACT16_(int x, char *file, int line)
  100. {
  101. int res;
  102. if (!VERIFY_SHORT(x))
  103. {
  104. fprintf (stderr, "EXTRACT16: input is not short: %d in %s: line %d\n", x, file, line);
  105. #ifdef FIXED_DEBUG_ASSERT
  106. celt_assert(0);
  107. #endif
  108. }
  109. res = x;
  110. celt_mips++;
  111. return res;
  112. }
  113. #define EXTEND32(x) EXTEND32_(x, __FILE__, __LINE__)
  114. static OPUS_INLINE int EXTEND32_(int x, char *file, int line)
  115. {
  116. int res;
  117. if (!VERIFY_SHORT(x))
  118. {
  119. fprintf (stderr, "EXTEND32: input is not short: %d in %s: line %d\n", x, file, line);
  120. #ifdef FIXED_DEBUG_ASSERT
  121. celt_assert(0);
  122. #endif
  123. }
  124. res = x;
  125. celt_mips++;
  126. return res;
  127. }
  128. #define SHR16(a, shift) SHR16_(a, shift, __FILE__, __LINE__)
  129. static OPUS_INLINE short SHR16_(int a, int shift, char *file, int line)
  130. {
  131. int res;
  132. if (!VERIFY_SHORT(a) || !VERIFY_SHORT(shift))
  133. {
  134. fprintf (stderr, "SHR16: inputs are not short: %d >> %d in %s: line %d\n", a, shift, file, line);
  135. #ifdef FIXED_DEBUG_ASSERT
  136. celt_assert(0);
  137. #endif
  138. }
  139. res = a>>shift;
  140. if (!VERIFY_SHORT(res))
  141. {
  142. fprintf (stderr, "SHR16: output is not short: %d in %s: line %d\n", res, file, line);
  143. #ifdef FIXED_DEBUG_ASSERT
  144. celt_assert(0);
  145. #endif
  146. }
  147. celt_mips++;
  148. return res;
  149. }
  150. #define SHL16(a, shift) SHL16_(a, shift, __FILE__, __LINE__)
  151. static OPUS_INLINE short SHL16_(int a, int shift, char *file, int line)
  152. {
  153. int res;
  154. if (!VERIFY_SHORT(a) || !VERIFY_SHORT(shift))
  155. {
  156. fprintf (stderr, "SHL16: inputs are not short: %d %d in %s: line %d\n", a, shift, file, line);
  157. #ifdef FIXED_DEBUG_ASSERT
  158. celt_assert(0);
  159. #endif
  160. }
  161. res = a<<shift;
  162. if (!VERIFY_SHORT(res))
  163. {
  164. fprintf (stderr, "SHL16: output is not short: %d in %s: line %d\n", res, file, line);
  165. #ifdef FIXED_DEBUG_ASSERT
  166. celt_assert(0);
  167. #endif
  168. }
  169. celt_mips++;
  170. return res;
  171. }
  172. static OPUS_INLINE int SHR32(opus_int64 a, int shift)
  173. {
  174. opus_int64 res;
  175. if (!VERIFY_INT(a) || !VERIFY_SHORT(shift))
  176. {
  177. fprintf (stderr, "SHR32: inputs are not int: %d %d\n", (int)a, shift);
  178. #ifdef FIXED_DEBUG_ASSERT
  179. celt_assert(0);
  180. #endif
  181. }
  182. res = a>>shift;
  183. if (!VERIFY_INT(res))
  184. {
  185. fprintf (stderr, "SHR32: output is not int: %d\n", (int)res);
  186. #ifdef FIXED_DEBUG_ASSERT
  187. celt_assert(0);
  188. #endif
  189. }
  190. celt_mips+=2;
  191. return res;
  192. }
  193. #define SHL32(a, shift) SHL32_(a, shift, __FILE__, __LINE__)
  194. static OPUS_INLINE int SHL32_(opus_int64 a, int shift, char *file, int line)
  195. {
  196. opus_int64 res;
  197. if (!VERIFY_INT(a) || !VERIFY_SHORT(shift))
  198. {
  199. fprintf (stderr, "SHL32: inputs are not int: %lld %d in %s: line %d\n", a, shift, file, line);
  200. #ifdef FIXED_DEBUG_ASSERT
  201. celt_assert(0);
  202. #endif
  203. }
  204. res = a<<shift;
  205. if (!VERIFY_INT(res))
  206. {
  207. fprintf (stderr, "SHL32: output is not int: %lld<<%d = %lld in %s: line %d\n", a, shift, res, file, line);
  208. #ifdef FIXED_DEBUG_ASSERT
  209. celt_assert(0);
  210. #endif
  211. }
  212. celt_mips+=2;
  213. return res;
  214. }
  215. #define PSHR32(a,shift) (celt_mips--,SHR32(ADD32((a),(((opus_val32)(1)<<((shift))>>1))),shift))
  216. #define VSHR32(a, shift) (((shift)>0) ? SHR32(a, shift) : SHL32(a, -(shift)))
  217. #define ROUND16(x,a) (celt_mips--,EXTRACT16(PSHR32((x),(a))))
  218. #define SROUND16(x,a) (celt_mips--,EXTRACT16(SATURATE(PSHR32(x,a), 32767)));
  219. #define HALF16(x) (SHR16(x,1))
  220. #define HALF32(x) (SHR32(x,1))
  221. #define ADD16(a, b) ADD16_(a, b, __FILE__, __LINE__)
  222. static OPUS_INLINE short ADD16_(int a, int b, char *file, int line)
  223. {
  224. int res;
  225. if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
  226. {
  227. fprintf (stderr, "ADD16: inputs are not short: %d %d in %s: line %d\n", a, b, file, line);
  228. #ifdef FIXED_DEBUG_ASSERT
  229. celt_assert(0);
  230. #endif
  231. }
  232. res = a+b;
  233. if (!VERIFY_SHORT(res))
  234. {
  235. fprintf (stderr, "ADD16: output is not short: %d+%d=%d in %s: line %d\n", a,b,res, file, line);
  236. #ifdef FIXED_DEBUG_ASSERT
  237. celt_assert(0);
  238. #endif
  239. }
  240. celt_mips++;
  241. return res;
  242. }
  243. #define SUB16(a, b) SUB16_(a, b, __FILE__, __LINE__)
  244. static OPUS_INLINE short SUB16_(int a, int b, char *file, int line)
  245. {
  246. int res;
  247. if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
  248. {
  249. fprintf (stderr, "SUB16: inputs are not short: %d %d in %s: line %d\n", a, b, file, line);
  250. #ifdef FIXED_DEBUG_ASSERT
  251. celt_assert(0);
  252. #endif
  253. }
  254. res = a-b;
  255. if (!VERIFY_SHORT(res))
  256. {
  257. fprintf (stderr, "SUB16: output is not short: %d in %s: line %d\n", res, file, line);
  258. #ifdef FIXED_DEBUG_ASSERT
  259. celt_assert(0);
  260. #endif
  261. }
  262. celt_mips++;
  263. return res;
  264. }
  265. #define ADD32(a, b) ADD32_(a, b, __FILE__, __LINE__)
  266. static OPUS_INLINE int ADD32_(opus_int64 a, opus_int64 b, char *file, int line)
  267. {
  268. opus_int64 res;
  269. if (!VERIFY_INT(a) || !VERIFY_INT(b))
  270. {
  271. fprintf (stderr, "ADD32: inputs are not int: %d %d in %s: line %d\n", (int)a, (int)b, file, line);
  272. #ifdef FIXED_DEBUG_ASSERT
  273. celt_assert(0);
  274. #endif
  275. }
  276. res = a+b;
  277. if (!VERIFY_INT(res))
  278. {
  279. fprintf (stderr, "ADD32: output is not int: %d in %s: line %d\n", (int)res, file, line);
  280. #ifdef FIXED_DEBUG_ASSERT
  281. celt_assert(0);
  282. #endif
  283. }
  284. celt_mips+=2;
  285. return res;
  286. }
  287. #define SUB32(a, b) SUB32_(a, b, __FILE__, __LINE__)
  288. static OPUS_INLINE int SUB32_(opus_int64 a, opus_int64 b, char *file, int line)
  289. {
  290. opus_int64 res;
  291. if (!VERIFY_INT(a) || !VERIFY_INT(b))
  292. {
  293. fprintf (stderr, "SUB32: inputs are not int: %d %d in %s: line %d\n", (int)a, (int)b, file, line);
  294. #ifdef FIXED_DEBUG_ASSERT
  295. celt_assert(0);
  296. #endif
  297. }
  298. res = a-b;
  299. if (!VERIFY_INT(res))
  300. {
  301. fprintf (stderr, "SUB32: output is not int: %d in %s: line %d\n", (int)res, file, line);
  302. #ifdef FIXED_DEBUG_ASSERT
  303. celt_assert(0);
  304. #endif
  305. }
  306. celt_mips+=2;
  307. return res;
  308. }
  309. #undef UADD32
  310. #define UADD32(a, b) UADD32_(a, b, __FILE__, __LINE__)
  311. static OPUS_INLINE unsigned int UADD32_(opus_uint64 a, opus_uint64 b, char *file, int line)
  312. {
  313. opus_uint64 res;
  314. if (!VERIFY_UINT(a) || !VERIFY_UINT(b))
  315. {
  316. fprintf (stderr, "UADD32: inputs are not uint32: %llu %llu in %s: line %d\n", a, b, file, line);
  317. #ifdef FIXED_DEBUG_ASSERT
  318. celt_assert(0);
  319. #endif
  320. }
  321. res = a+b;
  322. if (!VERIFY_UINT(res))
  323. {
  324. fprintf (stderr, "UADD32: output is not uint32: %llu in %s: line %d\n", res, file, line);
  325. #ifdef FIXED_DEBUG_ASSERT
  326. celt_assert(0);
  327. #endif
  328. }
  329. celt_mips+=2;
  330. return res;
  331. }
  332. #undef USUB32
  333. #define USUB32(a, b) USUB32_(a, b, __FILE__, __LINE__)
  334. static OPUS_INLINE unsigned int USUB32_(opus_uint64 a, opus_uint64 b, char *file, int line)
  335. {
  336. opus_uint64 res;
  337. if (!VERIFY_UINT(a) || !VERIFY_UINT(b))
  338. {
  339. fprintf (stderr, "USUB32: inputs are not uint32: %llu %llu in %s: line %d\n", a, b, file, line);
  340. #ifdef FIXED_DEBUG_ASSERT
  341. celt_assert(0);
  342. #endif
  343. }
  344. if (a<b)
  345. {
  346. fprintf (stderr, "USUB32: inputs underflow: %llu < %llu in %s: line %d\n", a, b, file, line);
  347. #ifdef FIXED_DEBUG_ASSERT
  348. celt_assert(0);
  349. #endif
  350. }
  351. res = a-b;
  352. if (!VERIFY_UINT(res))
  353. {
  354. fprintf (stderr, "USUB32: output is not uint32: %llu - %llu = %llu in %s: line %d\n", a, b, res, file, line);
  355. #ifdef FIXED_DEBUG_ASSERT
  356. celt_assert(0);
  357. #endif
  358. }
  359. celt_mips+=2;
  360. return res;
  361. }
  362. /* result fits in 16 bits */
  363. static OPUS_INLINE short MULT16_16_16(int a, int b)
  364. {
  365. int res;
  366. if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
  367. {
  368. fprintf (stderr, "MULT16_16_16: inputs are not short: %d %d\n", a, b);
  369. #ifdef FIXED_DEBUG_ASSERT
  370. celt_assert(0);
  371. #endif
  372. }
  373. res = a*b;
  374. if (!VERIFY_SHORT(res))
  375. {
  376. fprintf (stderr, "MULT16_16_16: output is not short: %d\n", res);
  377. #ifdef FIXED_DEBUG_ASSERT
  378. celt_assert(0);
  379. #endif
  380. }
  381. celt_mips++;
  382. return res;
  383. }
  384. #define MULT16_16(a, b) MULT16_16_(a, b, __FILE__, __LINE__)
  385. static OPUS_INLINE int MULT16_16_(int a, int b, char *file, int line)
  386. {
  387. opus_int64 res;
  388. if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
  389. {
  390. fprintf (stderr, "MULT16_16: inputs are not short: %d %d in %s: line %d\n", a, b, file, line);
  391. #ifdef FIXED_DEBUG_ASSERT
  392. celt_assert(0);
  393. #endif
  394. }
  395. res = ((opus_int64)a)*b;
  396. if (!VERIFY_INT(res))
  397. {
  398. fprintf (stderr, "MULT16_16: output is not int: %d in %s: line %d\n", (int)res, file, line);
  399. #ifdef FIXED_DEBUG_ASSERT
  400. celt_assert(0);
  401. #endif
  402. }
  403. celt_mips++;
  404. return res;
  405. }
  406. #define MAC16_16(c,a,b) (celt_mips-=2,ADD32((c),MULT16_16((a),(b))))
  407. #define MULT16_32_QX(a, b, Q) MULT16_32_QX_(a, b, Q, __FILE__, __LINE__)
  408. static OPUS_INLINE int MULT16_32_QX_(int a, opus_int64 b, int Q, char *file, int line)
  409. {
  410. opus_int64 res;
  411. if (!VERIFY_SHORT(a) || !VERIFY_INT(b))
  412. {
  413. fprintf (stderr, "MULT16_32_Q%d: inputs are not short+int: %d %d in %s: line %d\n", Q, (int)a, (int)b, file, line);
  414. #ifdef FIXED_DEBUG_ASSERT
  415. celt_assert(0);
  416. #endif
  417. }
  418. if (ABS32(b)>=((opus_val32)(1)<<(15+Q)))
  419. {
  420. fprintf (stderr, "MULT16_32_Q%d: second operand too large: %d %d in %s: line %d\n", Q, (int)a, (int)b, file, line);
  421. #ifdef FIXED_DEBUG_ASSERT
  422. celt_assert(0);
  423. #endif
  424. }
  425. res = (((opus_int64)a)*(opus_int64)b) >> Q;
  426. if (!VERIFY_INT(res))
  427. {
  428. fprintf (stderr, "MULT16_32_Q%d: output is not int: %d*%d=%d in %s: line %d\n", Q, (int)a, (int)b,(int)res, file, line);
  429. #ifdef FIXED_DEBUG_ASSERT
  430. celt_assert(0);
  431. #endif
  432. }
  433. if (Q==15)
  434. celt_mips+=3;
  435. else
  436. celt_mips+=4;
  437. return res;
  438. }
  439. #define MULT16_32_PX(a, b, Q) MULT16_32_PX_(a, b, Q, __FILE__, __LINE__)
  440. static OPUS_INLINE int MULT16_32_PX_(int a, opus_int64 b, int Q, char *file, int line)
  441. {
  442. opus_int64 res;
  443. if (!VERIFY_SHORT(a) || !VERIFY_INT(b))
  444. {
  445. fprintf (stderr, "MULT16_32_P%d: inputs are not short+int: %d %d in %s: line %d\n\n", Q, (int)a, (int)b, file, line);
  446. #ifdef FIXED_DEBUG_ASSERT
  447. celt_assert(0);
  448. #endif
  449. }
  450. if (ABS32(b)>=((opus_int64)(1)<<(15+Q)))
  451. {
  452. fprintf (stderr, "MULT16_32_Q%d: second operand too large: %d %d in %s: line %d\n\n", Q, (int)a, (int)b,file, line);
  453. #ifdef FIXED_DEBUG_ASSERT
  454. celt_assert(0);
  455. #endif
  456. }
  457. res = ((((opus_int64)a)*(opus_int64)b) + (((opus_val32)(1)<<Q)>>1))>> Q;
  458. if (!VERIFY_INT(res))
  459. {
  460. fprintf (stderr, "MULT16_32_P%d: output is not int: %d*%d=%d in %s: line %d\n\n", Q, (int)a, (int)b,(int)res, file, line);
  461. #ifdef FIXED_DEBUG_ASSERT
  462. celt_assert(0);
  463. #endif
  464. }
  465. if (Q==15)
  466. celt_mips+=4;
  467. else
  468. celt_mips+=5;
  469. return res;
  470. }
  471. #define MULT16_32_Q15(a,b) MULT16_32_QX(a,b,15)
  472. #define MAC16_32_Q15(c,a,b) (celt_mips-=2,ADD32((c),MULT16_32_Q15((a),(b))))
  473. #define MAC16_32_Q16(c,a,b) (celt_mips-=2,ADD32((c),MULT16_32_Q16((a),(b))))
  474. static OPUS_INLINE int SATURATE(int a, int b)
  475. {
  476. if (a>b)
  477. a=b;
  478. if (a<-b)
  479. a = -b;
  480. celt_mips+=3;
  481. return a;
  482. }
  483. static OPUS_INLINE opus_int16 SATURATE16(opus_int32 a)
  484. {
  485. celt_mips+=3;
  486. if (a>32767)
  487. return 32767;
  488. else if (a<-32768)
  489. return -32768;
  490. else return a;
  491. }
  492. static OPUS_INLINE int MULT16_16_Q11_32(int a, int b)
  493. {
  494. opus_int64 res;
  495. if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
  496. {
  497. fprintf (stderr, "MULT16_16_Q11: inputs are not short: %d %d\n", a, b);
  498. #ifdef FIXED_DEBUG_ASSERT
  499. celt_assert(0);
  500. #endif
  501. }
  502. res = ((opus_int64)a)*b;
  503. res >>= 11;
  504. if (!VERIFY_INT(res))
  505. {
  506. fprintf (stderr, "MULT16_16_Q11: output is not short: %d*%d=%d\n", (int)a, (int)b, (int)res);
  507. #ifdef FIXED_DEBUG_ASSERT
  508. celt_assert(0);
  509. #endif
  510. }
  511. celt_mips+=3;
  512. return res;
  513. }
  514. static OPUS_INLINE short MULT16_16_Q13(int a, int b)
  515. {
  516. opus_int64 res;
  517. if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
  518. {
  519. fprintf (stderr, "MULT16_16_Q13: inputs are not short: %d %d\n", a, b);
  520. #ifdef FIXED_DEBUG_ASSERT
  521. celt_assert(0);
  522. #endif
  523. }
  524. res = ((opus_int64)a)*b;
  525. res >>= 13;
  526. if (!VERIFY_SHORT(res))
  527. {
  528. fprintf (stderr, "MULT16_16_Q13: output is not short: %d*%d=%d\n", a, b, (int)res);
  529. #ifdef FIXED_DEBUG_ASSERT
  530. celt_assert(0);
  531. #endif
  532. }
  533. celt_mips+=3;
  534. return res;
  535. }
  536. static OPUS_INLINE short MULT16_16_Q14(int a, int b)
  537. {
  538. opus_int64 res;
  539. if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
  540. {
  541. fprintf (stderr, "MULT16_16_Q14: inputs are not short: %d %d\n", a, b);
  542. #ifdef FIXED_DEBUG_ASSERT
  543. celt_assert(0);
  544. #endif
  545. }
  546. res = ((opus_int64)a)*b;
  547. res >>= 14;
  548. if (!VERIFY_SHORT(res))
  549. {
  550. fprintf (stderr, "MULT16_16_Q14: output is not short: %d\n", (int)res);
  551. #ifdef FIXED_DEBUG_ASSERT
  552. celt_assert(0);
  553. #endif
  554. }
  555. celt_mips+=3;
  556. return res;
  557. }
  558. #define MULT16_16_Q15(a, b) MULT16_16_Q15_(a, b, __FILE__, __LINE__)
  559. static OPUS_INLINE short MULT16_16_Q15_(int a, int b, char *file, int line)
  560. {
  561. opus_int64 res;
  562. if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
  563. {
  564. fprintf (stderr, "MULT16_16_Q15: inputs are not short: %d %d in %s: line %d\n", a, b, file, line);
  565. #ifdef FIXED_DEBUG_ASSERT
  566. celt_assert(0);
  567. #endif
  568. }
  569. res = ((opus_int64)a)*b;
  570. res >>= 15;
  571. if (!VERIFY_SHORT(res))
  572. {
  573. fprintf (stderr, "MULT16_16_Q15: output is not short: %d in %s: line %d\n", (int)res, file, line);
  574. #ifdef FIXED_DEBUG_ASSERT
  575. celt_assert(0);
  576. #endif
  577. }
  578. celt_mips+=1;
  579. return res;
  580. }
  581. static OPUS_INLINE short MULT16_16_P13(int a, int b)
  582. {
  583. opus_int64 res;
  584. if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
  585. {
  586. fprintf (stderr, "MULT16_16_P13: inputs are not short: %d %d\n", a, b);
  587. #ifdef FIXED_DEBUG_ASSERT
  588. celt_assert(0);
  589. #endif
  590. }
  591. res = ((opus_int64)a)*b;
  592. res += 4096;
  593. if (!VERIFY_INT(res))
  594. {
  595. fprintf (stderr, "MULT16_16_P13: overflow: %d*%d=%d\n", a, b, (int)res);
  596. #ifdef FIXED_DEBUG_ASSERT
  597. celt_assert(0);
  598. #endif
  599. }
  600. res >>= 13;
  601. if (!VERIFY_SHORT(res))
  602. {
  603. fprintf (stderr, "MULT16_16_P13: output is not short: %d*%d=%d\n", a, b, (int)res);
  604. #ifdef FIXED_DEBUG_ASSERT
  605. celt_assert(0);
  606. #endif
  607. }
  608. celt_mips+=4;
  609. return res;
  610. }
  611. static OPUS_INLINE short MULT16_16_P14(int a, int b)
  612. {
  613. opus_int64 res;
  614. if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
  615. {
  616. fprintf (stderr, "MULT16_16_P14: inputs are not short: %d %d\n", a, b);
  617. #ifdef FIXED_DEBUG_ASSERT
  618. celt_assert(0);
  619. #endif
  620. }
  621. res = ((opus_int64)a)*b;
  622. res += 8192;
  623. if (!VERIFY_INT(res))
  624. {
  625. fprintf (stderr, "MULT16_16_P14: overflow: %d*%d=%d\n", a, b, (int)res);
  626. #ifdef FIXED_DEBUG_ASSERT
  627. celt_assert(0);
  628. #endif
  629. }
  630. res >>= 14;
  631. if (!VERIFY_SHORT(res))
  632. {
  633. fprintf (stderr, "MULT16_16_P14: output is not short: %d*%d=%d\n", a, b, (int)res);
  634. #ifdef FIXED_DEBUG_ASSERT
  635. celt_assert(0);
  636. #endif
  637. }
  638. celt_mips+=4;
  639. return res;
  640. }
  641. static OPUS_INLINE short MULT16_16_P15(int a, int b)
  642. {
  643. opus_int64 res;
  644. if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
  645. {
  646. fprintf (stderr, "MULT16_16_P15: inputs are not short: %d %d\n", a, b);
  647. #ifdef FIXED_DEBUG_ASSERT
  648. celt_assert(0);
  649. #endif
  650. }
  651. res = ((opus_int64)a)*b;
  652. res += 16384;
  653. if (!VERIFY_INT(res))
  654. {
  655. fprintf (stderr, "MULT16_16_P15: overflow: %d*%d=%d\n", a, b, (int)res);
  656. #ifdef FIXED_DEBUG_ASSERT
  657. celt_assert(0);
  658. #endif
  659. }
  660. res >>= 15;
  661. if (!VERIFY_SHORT(res))
  662. {
  663. fprintf (stderr, "MULT16_16_P15: output is not short: %d*%d=%d\n", a, b, (int)res);
  664. #ifdef FIXED_DEBUG_ASSERT
  665. celt_assert(0);
  666. #endif
  667. }
  668. celt_mips+=2;
  669. return res;
  670. }
  671. #define DIV32_16(a, b) DIV32_16_(a, b, __FILE__, __LINE__)
  672. static OPUS_INLINE int DIV32_16_(opus_int64 a, opus_int64 b, char *file, int line)
  673. {
  674. opus_int64 res;
  675. if (b==0)
  676. {
  677. fprintf(stderr, "DIV32_16: divide by zero: %d/%d in %s: line %d\n", (int)a, (int)b, file, line);
  678. #ifdef FIXED_DEBUG_ASSERT
  679. celt_assert(0);
  680. #endif
  681. return 0;
  682. }
  683. if (!VERIFY_INT(a) || !VERIFY_SHORT(b))
  684. {
  685. fprintf (stderr, "DIV32_16: inputs are not int/short: %d %d in %s: line %d\n", (int)a, (int)b, file, line);
  686. #ifdef FIXED_DEBUG_ASSERT
  687. celt_assert(0);
  688. #endif
  689. }
  690. res = a/b;
  691. if (!VERIFY_SHORT(res))
  692. {
  693. fprintf (stderr, "DIV32_16: output is not short: %d / %d = %d in %s: line %d\n", (int)a,(int)b,(int)res, file, line);
  694. if (res>32767)
  695. res = 32767;
  696. if (res<-32768)
  697. res = -32768;
  698. #ifdef FIXED_DEBUG_ASSERT
  699. celt_assert(0);
  700. #endif
  701. }
  702. celt_mips+=35;
  703. return res;
  704. }
  705. #define DIV32(a, b) DIV32_(a, b, __FILE__, __LINE__)
  706. static OPUS_INLINE int DIV32_(opus_int64 a, opus_int64 b, char *file, int line)
  707. {
  708. opus_int64 res;
  709. if (b==0)
  710. {
  711. fprintf(stderr, "DIV32: divide by zero: %d/%d in %s: line %d\n", (int)a, (int)b, file, line);
  712. #ifdef FIXED_DEBUG_ASSERT
  713. celt_assert(0);
  714. #endif
  715. return 0;
  716. }
  717. if (!VERIFY_INT(a) || !VERIFY_INT(b))
  718. {
  719. fprintf (stderr, "DIV32: inputs are not int/short: %d %d in %s: line %d\n", (int)a, (int)b, file, line);
  720. #ifdef FIXED_DEBUG_ASSERT
  721. celt_assert(0);
  722. #endif
  723. }
  724. res = a/b;
  725. if (!VERIFY_INT(res))
  726. {
  727. fprintf (stderr, "DIV32: output is not int: %d in %s: line %d\n", (int)res, file, line);
  728. #ifdef FIXED_DEBUG_ASSERT
  729. celt_assert(0);
  730. #endif
  731. }
  732. celt_mips+=70;
  733. return res;
  734. }
  735. static OPUS_INLINE opus_val16 SIG2WORD16_generic(celt_sig x)
  736. {
  737. x = PSHR32(x, SIG_SHIFT);
  738. x = MAX32(x, -32768);
  739. x = MIN32(x, 32767);
  740. return EXTRACT16(x);
  741. }
  742. #define SIG2WORD16(x) (SIG2WORD16_generic(x))
  743. #undef PRINT_MIPS
  744. #define PRINT_MIPS(file) do {fprintf (file, "total complexity = %llu MIPS\n", celt_mips);} while (0);
  745. #endif