fixed_debug.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  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. /* result fits in 32 bits */
  385. static OPUS_INLINE int MULT32_32_32(opus_int64 a, opus_int64 b)
  386. {
  387. opus_int64 res;
  388. if (!VERIFY_INT(a) || !VERIFY_INT(b))
  389. {
  390. fprintf (stderr, "MULT32_32_32: inputs are not int: %d %d\n", a, b);
  391. #ifdef FIXED_DEBUG_ASSERT
  392. celt_assert(0);
  393. #endif
  394. }
  395. res = a*b;
  396. if (!VERIFY_INT(res))
  397. {
  398. fprintf (stderr, "MULT32_32_32: output is not int: %d\n", res);
  399. #ifdef FIXED_DEBUG_ASSERT
  400. celt_assert(0);
  401. #endif
  402. }
  403. celt_mips+=5;
  404. return res;
  405. }
  406. static OPUS_INLINE int MULT32_32_Q16(opus_int64 a, opus_int64 b)
  407. {
  408. opus_int64 res;
  409. if (!VERIFY_INT(a) || !VERIFY_INT(b))
  410. {
  411. fprintf (stderr, "MULT32_32_Q16: inputs are not int: %d %d\n", a, b);
  412. #ifdef FIXED_DEBUG_ASSERT
  413. celt_assert(0);
  414. #endif
  415. }
  416. res = ((opus_int64)(a)*(opus_int64)(b)) >> 16;
  417. if (!VERIFY_INT(res))
  418. {
  419. fprintf (stderr, "MULT32_32_Q16: output is not int: %d*%d=%d\n", a, b, (int)res);
  420. #ifdef FIXED_DEBUG_ASSERT
  421. celt_assert(0);
  422. #endif
  423. }
  424. celt_mips+=5;
  425. return res;
  426. }
  427. #define MULT16_16(a, b) MULT16_16_(a, b, __FILE__, __LINE__)
  428. static OPUS_INLINE int MULT16_16_(int a, int b, char *file, int line)
  429. {
  430. opus_int64 res;
  431. if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
  432. {
  433. fprintf (stderr, "MULT16_16: inputs are not short: %d %d in %s: line %d\n", a, b, file, line);
  434. #ifdef FIXED_DEBUG_ASSERT
  435. celt_assert(0);
  436. #endif
  437. }
  438. res = ((opus_int64)a)*b;
  439. if (!VERIFY_INT(res))
  440. {
  441. fprintf (stderr, "MULT16_16: output is not int: %d in %s: line %d\n", (int)res, file, line);
  442. #ifdef FIXED_DEBUG_ASSERT
  443. celt_assert(0);
  444. #endif
  445. }
  446. celt_mips++;
  447. return res;
  448. }
  449. #define MAC16_16(c,a,b) (celt_mips-=2,ADD32((c),MULT16_16((a),(b))))
  450. #define MULT16_32_QX(a, b, Q) MULT16_32_QX_(a, b, Q, __FILE__, __LINE__)
  451. static OPUS_INLINE int MULT16_32_QX_(int a, opus_int64 b, int Q, char *file, int line)
  452. {
  453. opus_int64 res;
  454. if (!VERIFY_SHORT(a) || !VERIFY_INT(b))
  455. {
  456. 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);
  457. #ifdef FIXED_DEBUG_ASSERT
  458. celt_assert(0);
  459. #endif
  460. }
  461. if (ABS32(b)>=((opus_val32)(1)<<(15+Q)))
  462. {
  463. fprintf (stderr, "MULT16_32_Q%d: second operand too large: %d %d in %s: line %d\n", Q, (int)a, (int)b, file, line);
  464. #ifdef FIXED_DEBUG_ASSERT
  465. celt_assert(0);
  466. #endif
  467. }
  468. res = (((opus_int64)a)*(opus_int64)b) >> Q;
  469. if (!VERIFY_INT(res))
  470. {
  471. 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);
  472. #ifdef FIXED_DEBUG_ASSERT
  473. celt_assert(0);
  474. #endif
  475. }
  476. if (Q==15)
  477. celt_mips+=3;
  478. else
  479. celt_mips+=4;
  480. return res;
  481. }
  482. #define MULT16_32_PX(a, b, Q) MULT16_32_PX_(a, b, Q, __FILE__, __LINE__)
  483. static OPUS_INLINE int MULT16_32_PX_(int a, opus_int64 b, int Q, char *file, int line)
  484. {
  485. opus_int64 res;
  486. if (!VERIFY_SHORT(a) || !VERIFY_INT(b))
  487. {
  488. 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);
  489. #ifdef FIXED_DEBUG_ASSERT
  490. celt_assert(0);
  491. #endif
  492. }
  493. if (ABS32(b)>=((opus_int64)(1)<<(15+Q)))
  494. {
  495. 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);
  496. #ifdef FIXED_DEBUG_ASSERT
  497. celt_assert(0);
  498. #endif
  499. }
  500. res = ((((opus_int64)a)*(opus_int64)b) + (((opus_val32)(1)<<Q)>>1))>> Q;
  501. if (!VERIFY_INT(res))
  502. {
  503. 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);
  504. #ifdef FIXED_DEBUG_ASSERT
  505. celt_assert(0);
  506. #endif
  507. }
  508. if (Q==15)
  509. celt_mips+=4;
  510. else
  511. celt_mips+=5;
  512. return res;
  513. }
  514. #define MULT16_32_Q15(a,b) MULT16_32_QX(a,b,15)
  515. #define MAC16_32_Q15(c,a,b) (celt_mips-=2,ADD32((c),MULT16_32_Q15((a),(b))))
  516. #define MAC16_32_Q16(c,a,b) (celt_mips-=2,ADD32((c),MULT16_32_Q16((a),(b))))
  517. static OPUS_INLINE int SATURATE(int a, int b)
  518. {
  519. if (a>b)
  520. a=b;
  521. if (a<-b)
  522. a = -b;
  523. celt_mips+=3;
  524. return a;
  525. }
  526. static OPUS_INLINE opus_int16 SATURATE16(opus_int32 a)
  527. {
  528. celt_mips+=3;
  529. if (a>32767)
  530. return 32767;
  531. else if (a<-32768)
  532. return -32768;
  533. else return a;
  534. }
  535. static OPUS_INLINE int MULT16_16_Q11_32(int a, int b)
  536. {
  537. opus_int64 res;
  538. if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
  539. {
  540. fprintf (stderr, "MULT16_16_Q11: inputs are not short: %d %d\n", a, b);
  541. #ifdef FIXED_DEBUG_ASSERT
  542. celt_assert(0);
  543. #endif
  544. }
  545. res = ((opus_int64)a)*b;
  546. res >>= 11;
  547. if (!VERIFY_INT(res))
  548. {
  549. fprintf (stderr, "MULT16_16_Q11: output is not short: %d*%d=%d\n", (int)a, (int)b, (int)res);
  550. #ifdef FIXED_DEBUG_ASSERT
  551. celt_assert(0);
  552. #endif
  553. }
  554. celt_mips+=3;
  555. return res;
  556. }
  557. static OPUS_INLINE short MULT16_16_Q13(int a, int b)
  558. {
  559. opus_int64 res;
  560. if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
  561. {
  562. fprintf (stderr, "MULT16_16_Q13: inputs are not short: %d %d\n", a, b);
  563. #ifdef FIXED_DEBUG_ASSERT
  564. celt_assert(0);
  565. #endif
  566. }
  567. res = ((opus_int64)a)*b;
  568. res >>= 13;
  569. if (!VERIFY_SHORT(res))
  570. {
  571. fprintf (stderr, "MULT16_16_Q13: output is not short: %d*%d=%d\n", a, b, (int)res);
  572. #ifdef FIXED_DEBUG_ASSERT
  573. celt_assert(0);
  574. #endif
  575. }
  576. celt_mips+=3;
  577. return res;
  578. }
  579. static OPUS_INLINE short MULT16_16_Q14(int a, int b)
  580. {
  581. opus_int64 res;
  582. if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
  583. {
  584. fprintf (stderr, "MULT16_16_Q14: inputs are not short: %d %d\n", a, b);
  585. #ifdef FIXED_DEBUG_ASSERT
  586. celt_assert(0);
  587. #endif
  588. }
  589. res = ((opus_int64)a)*b;
  590. res >>= 14;
  591. if (!VERIFY_SHORT(res))
  592. {
  593. fprintf (stderr, "MULT16_16_Q14: output is not short: %d\n", (int)res);
  594. #ifdef FIXED_DEBUG_ASSERT
  595. celt_assert(0);
  596. #endif
  597. }
  598. celt_mips+=3;
  599. return res;
  600. }
  601. #define MULT16_16_Q15(a, b) MULT16_16_Q15_(a, b, __FILE__, __LINE__)
  602. static OPUS_INLINE short MULT16_16_Q15_(int a, int b, char *file, int line)
  603. {
  604. opus_int64 res;
  605. if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
  606. {
  607. fprintf (stderr, "MULT16_16_Q15: inputs are not short: %d %d in %s: line %d\n", a, b, file, line);
  608. #ifdef FIXED_DEBUG_ASSERT
  609. celt_assert(0);
  610. #endif
  611. }
  612. res = ((opus_int64)a)*b;
  613. res >>= 15;
  614. if (!VERIFY_SHORT(res))
  615. {
  616. fprintf (stderr, "MULT16_16_Q15: output is not short: %d in %s: line %d\n", (int)res, file, line);
  617. #ifdef FIXED_DEBUG_ASSERT
  618. celt_assert(0);
  619. #endif
  620. }
  621. celt_mips+=1;
  622. return res;
  623. }
  624. static OPUS_INLINE short MULT16_16_P13(int a, int b)
  625. {
  626. opus_int64 res;
  627. if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
  628. {
  629. fprintf (stderr, "MULT16_16_P13: inputs are not short: %d %d\n", a, b);
  630. #ifdef FIXED_DEBUG_ASSERT
  631. celt_assert(0);
  632. #endif
  633. }
  634. res = ((opus_int64)a)*b;
  635. res += 4096;
  636. if (!VERIFY_INT(res))
  637. {
  638. fprintf (stderr, "MULT16_16_P13: overflow: %d*%d=%d\n", a, b, (int)res);
  639. #ifdef FIXED_DEBUG_ASSERT
  640. celt_assert(0);
  641. #endif
  642. }
  643. res >>= 13;
  644. if (!VERIFY_SHORT(res))
  645. {
  646. fprintf (stderr, "MULT16_16_P13: output is not short: %d*%d=%d\n", a, b, (int)res);
  647. #ifdef FIXED_DEBUG_ASSERT
  648. celt_assert(0);
  649. #endif
  650. }
  651. celt_mips+=4;
  652. return res;
  653. }
  654. static OPUS_INLINE short MULT16_16_P14(int a, int b)
  655. {
  656. opus_int64 res;
  657. if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
  658. {
  659. fprintf (stderr, "MULT16_16_P14: inputs are not short: %d %d\n", a, b);
  660. #ifdef FIXED_DEBUG_ASSERT
  661. celt_assert(0);
  662. #endif
  663. }
  664. res = ((opus_int64)a)*b;
  665. res += 8192;
  666. if (!VERIFY_INT(res))
  667. {
  668. fprintf (stderr, "MULT16_16_P14: overflow: %d*%d=%d\n", a, b, (int)res);
  669. #ifdef FIXED_DEBUG_ASSERT
  670. celt_assert(0);
  671. #endif
  672. }
  673. res >>= 14;
  674. if (!VERIFY_SHORT(res))
  675. {
  676. fprintf (stderr, "MULT16_16_P14: output is not short: %d*%d=%d\n", a, b, (int)res);
  677. #ifdef FIXED_DEBUG_ASSERT
  678. celt_assert(0);
  679. #endif
  680. }
  681. celt_mips+=4;
  682. return res;
  683. }
  684. static OPUS_INLINE short MULT16_16_P15(int a, int b)
  685. {
  686. opus_int64 res;
  687. if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
  688. {
  689. fprintf (stderr, "MULT16_16_P15: inputs are not short: %d %d\n", a, b);
  690. #ifdef FIXED_DEBUG_ASSERT
  691. celt_assert(0);
  692. #endif
  693. }
  694. res = ((opus_int64)a)*b;
  695. res += 16384;
  696. if (!VERIFY_INT(res))
  697. {
  698. fprintf (stderr, "MULT16_16_P15: overflow: %d*%d=%d\n", a, b, (int)res);
  699. #ifdef FIXED_DEBUG_ASSERT
  700. celt_assert(0);
  701. #endif
  702. }
  703. res >>= 15;
  704. if (!VERIFY_SHORT(res))
  705. {
  706. fprintf (stderr, "MULT16_16_P15: output is not short: %d*%d=%d\n", a, b, (int)res);
  707. #ifdef FIXED_DEBUG_ASSERT
  708. celt_assert(0);
  709. #endif
  710. }
  711. celt_mips+=2;
  712. return res;
  713. }
  714. #define DIV32_16(a, b) DIV32_16_(a, b, __FILE__, __LINE__)
  715. static OPUS_INLINE int DIV32_16_(opus_int64 a, opus_int64 b, char *file, int line)
  716. {
  717. opus_int64 res;
  718. if (b==0)
  719. {
  720. fprintf(stderr, "DIV32_16: divide by zero: %d/%d in %s: line %d\n", (int)a, (int)b, file, line);
  721. #ifdef FIXED_DEBUG_ASSERT
  722. celt_assert(0);
  723. #endif
  724. return 0;
  725. }
  726. if (!VERIFY_INT(a) || !VERIFY_SHORT(b))
  727. {
  728. fprintf (stderr, "DIV32_16: inputs are not int/short: %d %d in %s: line %d\n", (int)a, (int)b, file, line);
  729. #ifdef FIXED_DEBUG_ASSERT
  730. celt_assert(0);
  731. #endif
  732. }
  733. res = a/b;
  734. if (!VERIFY_SHORT(res))
  735. {
  736. fprintf (stderr, "DIV32_16: output is not short: %d / %d = %d in %s: line %d\n", (int)a,(int)b,(int)res, file, line);
  737. if (res>32767)
  738. res = 32767;
  739. if (res<-32768)
  740. res = -32768;
  741. #ifdef FIXED_DEBUG_ASSERT
  742. celt_assert(0);
  743. #endif
  744. }
  745. celt_mips+=35;
  746. return res;
  747. }
  748. #define DIV32(a, b) DIV32_(a, b, __FILE__, __LINE__)
  749. static OPUS_INLINE int DIV32_(opus_int64 a, opus_int64 b, char *file, int line)
  750. {
  751. opus_int64 res;
  752. if (b==0)
  753. {
  754. fprintf(stderr, "DIV32: divide by zero: %d/%d in %s: line %d\n", (int)a, (int)b, file, line);
  755. #ifdef FIXED_DEBUG_ASSERT
  756. celt_assert(0);
  757. #endif
  758. return 0;
  759. }
  760. if (!VERIFY_INT(a) || !VERIFY_INT(b))
  761. {
  762. fprintf (stderr, "DIV32: inputs are not int/short: %d %d in %s: line %d\n", (int)a, (int)b, file, line);
  763. #ifdef FIXED_DEBUG_ASSERT
  764. celt_assert(0);
  765. #endif
  766. }
  767. res = a/b;
  768. if (!VERIFY_INT(res))
  769. {
  770. fprintf (stderr, "DIV32: output is not int: %d in %s: line %d\n", (int)res, file, line);
  771. #ifdef FIXED_DEBUG_ASSERT
  772. celt_assert(0);
  773. #endif
  774. }
  775. celt_mips+=70;
  776. return res;
  777. }
  778. static OPUS_INLINE opus_val16 SIG2WORD16_generic(celt_sig x)
  779. {
  780. x = PSHR32(x, SIG_SHIFT);
  781. x = MAX32(x, -32768);
  782. x = MIN32(x, 32767);
  783. return EXTRACT16(x);
  784. }
  785. #define SIG2WORD16(x) (SIG2WORD16_generic(x))
  786. #undef PRINT_MIPS
  787. #define PRINT_MIPS(file) do {fprintf (file, "total complexity = %llu MIPS\n", celt_mips);} while (0);
  788. #endif