MacroDebug.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. /***********************************************************************
  2. Copyright (c) 2006-2012 IETF Trust and Skype Limited. All rights reserved.
  3. This file is extracted from RFC6716. Please see that RFC for additional
  4. information.
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions
  7. are met:
  8. - Redistributions of source code must retain the above copyright notice,
  9. this list of conditions and the following disclaimer.
  10. - Redistributions in binary form must reproduce the above copyright
  11. notice, this list of conditions and the following disclaimer in the
  12. documentation and/or other materials provided with the distribution.
  13. - Neither the name of Internet Society, IETF or IETF Trust, nor the
  14. names of specific contributors, may be used to endorse or promote
  15. products derived from this software without specific prior written
  16. permission.
  17. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS”
  18. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  21. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  25. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. POSSIBILITY OF SUCH DAMAGE.
  28. ***********************************************************************/
  29. #ifndef MACRO_DEBUG_H
  30. #define MACRO_DEBUG_H
  31. /* Redefine macro functions with extensive assertion in DEBUG mode.
  32. As functions can't be undefined, this file can't work with SigProcFIX_MacroCount.h */
  33. #if 0 && defined (_DEBUG) && !defined (silk_MACRO_COUNT)
  34. #undef silk_ADD16
  35. static inline opus_int16 silk_ADD16(opus_int16 a, opus_int16 b){
  36. opus_int16 ret;
  37. ret = a + b;
  38. silk_assert( ret == silk_ADD_SAT16( a, b ));
  39. return ret;
  40. }
  41. #undef silk_ADD32
  42. static inline opus_int32 silk_ADD32(opus_int32 a, opus_int32 b){
  43. opus_int32 ret;
  44. ret = a + b;
  45. silk_assert( ret == silk_ADD_SAT32( a, b ));
  46. return ret;
  47. }
  48. #undef silk_ADD64
  49. static inline opus_int64 silk_ADD64(opus_int64 a, opus_int64 b){
  50. opus_int64 ret;
  51. ret = a + b;
  52. silk_assert( ret == silk_ADD_SAT64( a, b ));
  53. return ret;
  54. }
  55. #undef silk_SUB16
  56. static inline opus_int16 silk_SUB16(opus_int16 a, opus_int16 b){
  57. opus_int16 ret;
  58. ret = a - b;
  59. silk_assert( ret == silk_SUB_SAT16( a, b ));
  60. return ret;
  61. }
  62. #undef silk_SUB32
  63. static inline opus_int32 silk_SUB32(opus_int32 a, opus_int32 b){
  64. opus_int32 ret;
  65. ret = a - b;
  66. silk_assert( ret == silk_SUB_SAT32( a, b ));
  67. return ret;
  68. }
  69. #undef silk_SUB64
  70. static inline opus_int64 silk_SUB64(opus_int64 a, opus_int64 b){
  71. opus_int64 ret;
  72. ret = a - b;
  73. silk_assert( ret == silk_SUB_SAT64( a, b ));
  74. return ret;
  75. }
  76. #undef silk_ADD_SAT16
  77. static inline opus_int16 silk_ADD_SAT16( opus_int16 a16, opus_int16 b16 ) {
  78. opus_int16 res;
  79. res = (opus_int16)silk_SAT16( silk_ADD32( (opus_int32)(a16), (b16) ) );
  80. silk_assert( res == silk_SAT16( (opus_int32)a16 + (opus_int32)b16 ) );
  81. return res;
  82. }
  83. #undef silk_ADD_SAT32
  84. static inline opus_int32 silk_ADD_SAT32(opus_int32 a32, opus_int32 b32){
  85. opus_int32 res;
  86. res = ((((a32) + (b32)) & 0x80000000) == 0 ? \
  87. ((((a32) & (b32)) & 0x80000000) != 0 ? silk_int32_MIN : (a32)+(b32)) : \
  88. ((((a32) | (b32)) & 0x80000000) == 0 ? silk_int32_MAX : (a32)+(b32)) );
  89. silk_assert( res == silk_SAT32( (opus_int64)a32 + (opus_int64)b32 ) );
  90. return res;
  91. }
  92. #undef silk_ADD_SAT64
  93. static inline opus_int64 silk_ADD_SAT64( opus_int64 a64, opus_int64 b64 ) {
  94. opus_int64 res;
  95. res = ((((a64) + (b64)) & 0x8000000000000000LL) == 0 ? \
  96. ((((a64) & (b64)) & 0x8000000000000000LL) != 0 ? silk_int64_MIN : (a64)+(b64)) : \
  97. ((((a64) | (b64)) & 0x8000000000000000LL) == 0 ? silk_int64_MAX : (a64)+(b64)) );
  98. if( res != a64 + b64 ) {
  99. /* Check that we saturated to the correct extreme value */
  100. silk_assert( ( res == silk_int64_MAX && ( ( a64 >> 1 ) + ( b64 >> 1 ) > ( silk_int64_MAX >> 3 ) ) ) ||
  101. ( res == silk_int64_MIN && ( ( a64 >> 1 ) + ( b64 >> 1 ) < ( silk_int64_MIN >> 3 ) ) ) );
  102. } else {
  103. /* Saturation not necessary */
  104. silk_assert( res == a64 + b64 );
  105. }
  106. return res;
  107. }
  108. #undef silk_SUB_SAT16
  109. static inline opus_int16 silk_SUB_SAT16( opus_int16 a16, opus_int16 b16 ) {
  110. opus_int16 res;
  111. res = (opus_int16)silk_SAT16( silk_SUB32( (opus_int32)(a16), (b16) ) );
  112. silk_assert( res == silk_SAT16( (opus_int32)a16 - (opus_int32)b16 ) );
  113. return res;
  114. }
  115. #undef silk_SUB_SAT32
  116. static inline opus_int32 silk_SUB_SAT32( opus_int32 a32, opus_int32 b32 ) {
  117. opus_int32 res;
  118. res = ((((a32)-(b32)) & 0x80000000) == 0 ? \
  119. (( (a32) & ((b32)^0x80000000) & 0x80000000) ? silk_int32_MIN : (a32)-(b32)) : \
  120. ((((a32)^0x80000000) & (b32) & 0x80000000) ? silk_int32_MAX : (a32)-(b32)) );
  121. silk_assert( res == silk_SAT32( (opus_int64)a32 - (opus_int64)b32 ) );
  122. return res;
  123. }
  124. #undef silk_SUB_SAT64
  125. static inline opus_int64 silk_SUB_SAT64( opus_int64 a64, opus_int64 b64 ) {
  126. opus_int64 res;
  127. res = ((((a64)-(b64)) & 0x8000000000000000LL) == 0 ? \
  128. (( (a64) & ((b64)^0x8000000000000000LL) & 0x8000000000000000LL) ? silk_int64_MIN : (a64)-(b64)) : \
  129. ((((a64)^0x8000000000000000LL) & (b64) & 0x8000000000000000LL) ? silk_int64_MAX : (a64)-(b64)) );
  130. if( res != a64 - b64 ) {
  131. /* Check that we saturated to the correct extreme value */
  132. silk_assert( ( res == silk_int64_MAX && ( ( a64 >> 1 ) + ( b64 >> 1 ) > ( silk_int64_MAX >> 3 ) ) ) ||
  133. ( res == silk_int64_MIN && ( ( a64 >> 1 ) + ( b64 >> 1 ) < ( silk_int64_MIN >> 3 ) ) ) );
  134. } else {
  135. /* Saturation not necessary */
  136. silk_assert( res == a64 - b64 );
  137. }
  138. return res;
  139. }
  140. #undef silk_MUL
  141. static inline opus_int32 silk_MUL(opus_int32 a32, opus_int32 b32){
  142. opus_int32 ret;
  143. opus_int64 ret64;
  144. ret = a32 * b32;
  145. ret64 = (opus_int64)a32 * (opus_int64)b32;
  146. silk_assert((opus_int64)ret == ret64 ); /* Check output overflow */
  147. return ret;
  148. }
  149. #undef silk_MUL_uint
  150. static inline opus_uint32 silk_MUL_uint(opus_uint32 a32, opus_uint32 b32){
  151. opus_uint32 ret;
  152. ret = a32 * b32;
  153. silk_assert((opus_uint64)ret == (opus_uint64)a32 * (opus_uint64)b32); /* Check output overflow */
  154. return ret;
  155. }
  156. #undef silk_MLA
  157. static inline opus_int32 silk_MLA(opus_int32 a32, opus_int32 b32, opus_int32 c32){
  158. opus_int32 ret;
  159. ret = a32 + b32 * c32;
  160. silk_assert((opus_int64)ret == (opus_int64)a32 + (opus_int64)b32 * (opus_int64)c32); /* Check output overflow */
  161. return ret;
  162. }
  163. #undef silk_MLA_uint
  164. static inline opus_int32 silk_MLA_uint(opus_uint32 a32, opus_uint32 b32, opus_uint32 c32){
  165. opus_uint32 ret;
  166. ret = a32 + b32 * c32;
  167. silk_assert((opus_int64)ret == (opus_int64)a32 + (opus_int64)b32 * (opus_int64)c32); /* Check output overflow */
  168. return ret;
  169. }
  170. #undef silk_SMULWB
  171. static inline opus_int32 silk_SMULWB(opus_int32 a32, opus_int32 b32){
  172. opus_int32 ret;
  173. ret = (a32 >> 16) * (opus_int32)((opus_int16)b32) + (((a32 & 0x0000FFFF) * (opus_int32)((opus_int16)b32)) >> 16);
  174. silk_assert((opus_int64)ret == ((opus_int64)a32 * (opus_int16)b32) >> 16);
  175. return ret;
  176. }
  177. #undef silk_SMLAWB
  178. static inline opus_int32 silk_SMLAWB(opus_int32 a32, opus_int32 b32, opus_int32 c32){
  179. opus_int32 ret;
  180. ret = silk_ADD32( a32, silk_SMULWB( b32, c32 ) );
  181. silk_assert(silk_ADD32( a32, silk_SMULWB( b32, c32 ) ) == silk_ADD_SAT32( a32, silk_SMULWB( b32, c32 ) ));
  182. return ret;
  183. }
  184. #undef silk_SMULWT
  185. static inline opus_int32 silk_SMULWT(opus_int32 a32, opus_int32 b32){
  186. opus_int32 ret;
  187. ret = (a32 >> 16) * (b32 >> 16) + (((a32 & 0x0000FFFF) * (b32 >> 16)) >> 16);
  188. silk_assert((opus_int64)ret == ((opus_int64)a32 * (b32 >> 16)) >> 16);
  189. return ret;
  190. }
  191. #undef silk_SMLAWT
  192. static inline opus_int32 silk_SMLAWT(opus_int32 a32, opus_int32 b32, opus_int32 c32){
  193. opus_int32 ret;
  194. ret = a32 + ((b32 >> 16) * (c32 >> 16)) + (((b32 & 0x0000FFFF) * ((c32 >> 16)) >> 16));
  195. silk_assert((opus_int64)ret == (opus_int64)a32 + (((opus_int64)b32 * (c32 >> 16)) >> 16));
  196. return ret;
  197. }
  198. #undef silk_SMULL
  199. static inline opus_int64 silk_SMULL(opus_int64 a64, opus_int64 b64){
  200. opus_int64 ret64;
  201. ret64 = a64 * b64;
  202. if( b64 != 0 ) {
  203. silk_assert( a64 == (ret64 / b64) );
  204. } else if( a64 != 0 ) {
  205. silk_assert( b64 == (ret64 / a64) );
  206. }
  207. return ret64;
  208. }
  209. /* no checking needed for silk_SMULBB */
  210. #undef silk_SMLABB
  211. static inline opus_int32 silk_SMLABB(opus_int32 a32, opus_int32 b32, opus_int32 c32){
  212. opus_int32 ret;
  213. ret = a32 + (opus_int32)((opus_int16)b32) * (opus_int32)((opus_int16)c32);
  214. silk_assert((opus_int64)ret == (opus_int64)a32 + (opus_int64)b32 * (opus_int16)c32);
  215. return ret;
  216. }
  217. /* no checking needed for silk_SMULBT */
  218. #undef silk_SMLABT
  219. static inline opus_int32 silk_SMLABT(opus_int32 a32, opus_int32 b32, opus_int32 c32){
  220. opus_int32 ret;
  221. ret = a32 + ((opus_int32)((opus_int16)b32)) * (c32 >> 16);
  222. silk_assert((opus_int64)ret == (opus_int64)a32 + (opus_int64)b32 * (c32 >> 16));
  223. return ret;
  224. }
  225. /* no checking needed for silk_SMULTT */
  226. #undef silk_SMLATT
  227. static inline opus_int32 silk_SMLATT(opus_int32 a32, opus_int32 b32, opus_int32 c32){
  228. opus_int32 ret;
  229. ret = a32 + (b32 >> 16) * (c32 >> 16);
  230. silk_assert((opus_int64)ret == (opus_int64)a32 + (b32 >> 16) * (c32 >> 16));
  231. return ret;
  232. }
  233. #undef silk_SMULWW
  234. static inline opus_int32 silk_SMULWW(opus_int32 a32, opus_int32 b32){
  235. opus_int32 ret, tmp1, tmp2;
  236. opus_int64 ret64;
  237. ret = silk_SMULWB( a32, b32 );
  238. tmp1 = silk_RSHIFT_ROUND( b32, 16 );
  239. tmp2 = silk_MUL( a32, tmp1 );
  240. silk_assert( (opus_int64)tmp2 == (opus_int64) a32 * (opus_int64) tmp1 );
  241. tmp1 = ret;
  242. ret = silk_ADD32( tmp1, tmp2 );
  243. silk_assert( silk_ADD32( tmp1, tmp2 ) == silk_ADD_SAT32( tmp1, tmp2 ) );
  244. ret64 = silk_RSHIFT64( silk_SMULL( a32, b32 ), 16 );
  245. silk_assert( (opus_int64)ret == ret64 );
  246. return ret;
  247. }
  248. #undef silk_SMLAWW
  249. static inline opus_int32 silk_SMLAWW(opus_int32 a32, opus_int32 b32, opus_int32 c32){
  250. opus_int32 ret, tmp;
  251. tmp = silk_SMULWW( b32, c32 );
  252. ret = silk_ADD32( a32, tmp );
  253. silk_assert( ret == silk_ADD_SAT32( a32, tmp ) );
  254. return ret;
  255. }
  256. /* Multiply-accumulate macros that allow overflow in the addition (ie, no asserts in debug mode) */
  257. #undef silk_MLA_ovflw
  258. #define silk_MLA_ovflw(a32, b32, c32) ((a32) + ((b32) * (c32)))
  259. #undef silk_SMLABB_ovflw
  260. #define silk_SMLABB_ovflw(a32, b32, c32) ((a32) + ((opus_int32)((opus_int16)(b32))) * (opus_int32)((opus_int16)(c32)))
  261. /* no checking needed for silk_SMULL
  262. no checking needed for silk_SMLAL
  263. no checking needed for silk_SMLALBB
  264. no checking needed for SigProcFIX_CLZ16
  265. no checking needed for SigProcFIX_CLZ32*/
  266. #undef silk_DIV32
  267. static inline opus_int32 silk_DIV32(opus_int32 a32, opus_int32 b32){
  268. silk_assert( b32 != 0 );
  269. return a32 / b32;
  270. }
  271. #undef silk_DIV32_16
  272. static inline opus_int32 silk_DIV32_16(opus_int32 a32, opus_int32 b32){
  273. silk_assert( b32 != 0 );
  274. silk_assert( b32 <= silk_int16_MAX );
  275. silk_assert( b32 >= silk_int16_MIN );
  276. return a32 / b32;
  277. }
  278. /* no checking needed for silk_SAT8
  279. no checking needed for silk_SAT16
  280. no checking needed for silk_SAT32
  281. no checking needed for silk_POS_SAT32
  282. no checking needed for silk_ADD_POS_SAT8
  283. no checking needed for silk_ADD_POS_SAT16
  284. no checking needed for silk_ADD_POS_SAT32
  285. no checking needed for silk_ADD_POS_SAT64 */
  286. #undef silk_LSHIFT8
  287. static inline opus_int8 silk_LSHIFT8(opus_int8 a, opus_int32 shift){
  288. opus_int8 ret;
  289. ret = a << shift;
  290. silk_assert(shift >= 0);
  291. silk_assert(shift < 8);
  292. silk_assert((opus_int64)ret == ((opus_int64)a) << shift);
  293. return ret;
  294. }
  295. #undef silk_LSHIFT16
  296. static inline opus_int16 silk_LSHIFT16(opus_int16 a, opus_int32 shift){
  297. opus_int16 ret;
  298. ret = a << shift;
  299. silk_assert(shift >= 0);
  300. silk_assert(shift < 16);
  301. silk_assert((opus_int64)ret == ((opus_int64)a) << shift);
  302. return ret;
  303. }
  304. #undef silk_LSHIFT32
  305. static inline opus_int32 silk_LSHIFT32(opus_int32 a, opus_int32 shift){
  306. opus_int32 ret;
  307. ret = a << shift;
  308. silk_assert(shift >= 0);
  309. silk_assert(shift < 32);
  310. silk_assert((opus_int64)ret == ((opus_int64)a) << shift);
  311. return ret;
  312. }
  313. #undef silk_LSHIFT64
  314. static inline opus_int64 silk_LSHIFT64(opus_int64 a, opus_int shift){
  315. silk_assert(shift >= 0);
  316. silk_assert(shift < 64);
  317. return a << shift;
  318. }
  319. #undef silk_LSHIFT_ovflw
  320. static inline opus_int32 silk_LSHIFT_ovflw(opus_int32 a, opus_int32 shift){
  321. silk_assert(shift >= 0); /* no check for overflow */
  322. return a << shift;
  323. }
  324. #undef silk_LSHIFT_uint
  325. static inline opus_uint32 silk_LSHIFT_uint(opus_uint32 a, opus_int32 shift){
  326. opus_uint32 ret;
  327. ret = a << shift;
  328. silk_assert(shift >= 0);
  329. silk_assert((opus_int64)ret == ((opus_int64)a) << shift);
  330. return ret;
  331. }
  332. #undef silk_RSHIFT8
  333. static inline opus_int8 silk_RSHIFT8(opus_int8 a, opus_int32 shift){
  334. silk_assert(shift >= 0);
  335. silk_assert(shift < 8);
  336. return a >> shift;
  337. }
  338. #undef silk_RSHIFT16
  339. static inline opus_int16 silk_RSHIFT16(opus_int16 a, opus_int32 shift){
  340. silk_assert(shift >= 0);
  341. silk_assert(shift < 16);
  342. return a >> shift;
  343. }
  344. #undef silk_RSHIFT32
  345. static inline opus_int32 silk_RSHIFT32(opus_int32 a, opus_int32 shift){
  346. silk_assert(shift >= 0);
  347. silk_assert(shift < 32);
  348. return a >> shift;
  349. }
  350. #undef silk_RSHIFT64
  351. static inline opus_int64 silk_RSHIFT64(opus_int64 a, opus_int64 shift){
  352. silk_assert(shift >= 0);
  353. silk_assert(shift <= 63);
  354. return a >> shift;
  355. }
  356. #undef silk_RSHIFT_uint
  357. static inline opus_uint32 silk_RSHIFT_uint(opus_uint32 a, opus_int32 shift){
  358. silk_assert(shift >= 0);
  359. silk_assert(shift <= 32);
  360. return a >> shift;
  361. }
  362. #undef silk_ADD_LSHIFT
  363. static inline opus_int32 silk_ADD_LSHIFT(opus_int32 a, opus_int32 b, opus_int32 shift){
  364. opus_int32 ret;
  365. silk_assert(shift >= 0);
  366. silk_assert(shift <= 31);
  367. ret = a + (b << shift);
  368. silk_assert((opus_int64)ret == (opus_int64)a + (((opus_int64)b) << shift));
  369. return ret; /* shift >= 0 */
  370. }
  371. #undef silk_ADD_LSHIFT32
  372. static inline opus_int32 silk_ADD_LSHIFT32(opus_int32 a, opus_int32 b, opus_int32 shift){
  373. opus_int32 ret;
  374. silk_assert(shift >= 0);
  375. silk_assert(shift <= 31);
  376. ret = a + (b << shift);
  377. silk_assert((opus_int64)ret == (opus_int64)a + (((opus_int64)b) << shift));
  378. return ret; /* shift >= 0 */
  379. }
  380. #undef silk_ADD_LSHIFT_uint
  381. static inline opus_uint32 silk_ADD_LSHIFT_uint(opus_uint32 a, opus_uint32 b, opus_int32 shift){
  382. opus_uint32 ret;
  383. silk_assert(shift >= 0);
  384. silk_assert(shift <= 32);
  385. ret = a + (b << shift);
  386. silk_assert((opus_int64)ret == (opus_int64)a + (((opus_int64)b) << shift));
  387. return ret; /* shift >= 0 */
  388. }
  389. #undef silk_ADD_RSHIFT
  390. static inline opus_int32 silk_ADD_RSHIFT(opus_int32 a, opus_int32 b, opus_int32 shift){
  391. opus_int32 ret;
  392. silk_assert(shift >= 0);
  393. silk_assert(shift <= 31);
  394. ret = a + (b >> shift);
  395. silk_assert((opus_int64)ret == (opus_int64)a + (((opus_int64)b) >> shift));
  396. return ret; /* shift > 0 */
  397. }
  398. #undef silk_ADD_RSHIFT32
  399. static inline opus_int32 silk_ADD_RSHIFT32(opus_int32 a, opus_int32 b, opus_int32 shift){
  400. opus_int32 ret;
  401. silk_assert(shift >= 0);
  402. silk_assert(shift <= 31);
  403. ret = a + (b >> shift);
  404. silk_assert((opus_int64)ret == (opus_int64)a + (((opus_int64)b) >> shift));
  405. return ret; /* shift > 0 */
  406. }
  407. #undef silk_ADD_RSHIFT_uint
  408. static inline opus_uint32 silk_ADD_RSHIFT_uint(opus_uint32 a, opus_uint32 b, opus_int32 shift){
  409. opus_uint32 ret;
  410. silk_assert(shift >= 0);
  411. silk_assert(shift <= 32);
  412. ret = a + (b >> shift);
  413. silk_assert((opus_int64)ret == (opus_int64)a + (((opus_int64)b) >> shift));
  414. return ret; /* shift > 0 */
  415. }
  416. #undef silk_SUB_LSHIFT32
  417. static inline opus_int32 silk_SUB_LSHIFT32(opus_int32 a, opus_int32 b, opus_int32 shift){
  418. opus_int32 ret;
  419. silk_assert(shift >= 0);
  420. silk_assert(shift <= 31);
  421. ret = a - (b << shift);
  422. silk_assert((opus_int64)ret == (opus_int64)a - (((opus_int64)b) << shift));
  423. return ret; /* shift >= 0 */
  424. }
  425. #undef silk_SUB_RSHIFT32
  426. static inline opus_int32 silk_SUB_RSHIFT32(opus_int32 a, opus_int32 b, opus_int32 shift){
  427. opus_int32 ret;
  428. silk_assert(shift >= 0);
  429. silk_assert(shift <= 31);
  430. ret = a - (b >> shift);
  431. silk_assert((opus_int64)ret == (opus_int64)a - (((opus_int64)b) >> shift));
  432. return ret; /* shift > 0 */
  433. }
  434. #undef silk_RSHIFT_ROUND
  435. static inline opus_int32 silk_RSHIFT_ROUND(opus_int32 a, opus_int32 shift){
  436. opus_int32 ret;
  437. silk_assert(shift > 0); /* the marco definition can't handle a shift of zero */
  438. silk_assert(shift < 32);
  439. ret = shift == 1 ? (a >> 1) + (a & 1) : ((a >> (shift - 1)) + 1) >> 1;
  440. silk_assert((opus_int64)ret == ((opus_int64)a + ((opus_int64)1 << (shift - 1))) >> shift);
  441. return ret;
  442. }
  443. #undef silk_RSHIFT_ROUND64
  444. static inline opus_int64 silk_RSHIFT_ROUND64(opus_int64 a, opus_int32 shift){
  445. opus_int64 ret;
  446. silk_assert(shift > 0); /* the marco definition can't handle a shift of zero */
  447. silk_assert(shift < 64);
  448. ret = shift == 1 ? (a >> 1) + (a & 1) : ((a >> (shift - 1)) + 1) >> 1;
  449. return ret;
  450. }
  451. /* silk_abs is used on floats also, so doesn't work... */
  452. /*#undef silk_abs
  453. static inline opus_int32 silk_abs(opus_int32 a){
  454. silk_assert(a != 0x80000000);
  455. return (((a) > 0) ? (a) : -(a)); // Be careful, silk_abs returns wrong when input equals to silk_intXX_MIN
  456. }*/
  457. #undef silk_abs_int64
  458. static inline opus_int64 silk_abs_int64(opus_int64 a){
  459. silk_assert(a != 0x8000000000000000);
  460. return (((a) > 0) ? (a) : -(a)); /* Be careful, silk_abs returns wrong when input equals to silk_intXX_MIN */
  461. }
  462. #undef silk_abs_int32
  463. static inline opus_int32 silk_abs_int32(opus_int32 a){
  464. silk_assert(a != 0x80000000);
  465. return abs(a);
  466. }
  467. #undef silk_CHECK_FIT8
  468. static inline opus_int8 silk_CHECK_FIT8( opus_int64 a ){
  469. opus_int8 ret;
  470. ret = (opus_int8)a;
  471. silk_assert( (opus_int64)ret == a );
  472. return( ret );
  473. }
  474. #undef silk_CHECK_FIT16
  475. static inline opus_int16 silk_CHECK_FIT16( opus_int64 a ){
  476. opus_int16 ret;
  477. ret = (opus_int16)a;
  478. silk_assert( (opus_int64)ret == a );
  479. return( ret );
  480. }
  481. #undef silk_CHECK_FIT32
  482. static inline opus_int32 silk_CHECK_FIT32( opus_int64 a ){
  483. opus_int32 ret;
  484. ret = (opus_int32)a;
  485. silk_assert( (opus_int64)ret == a );
  486. return( ret );
  487. }
  488. /* no checking for silk_NSHIFT_MUL_32_32
  489. no checking for silk_NSHIFT_MUL_16_16
  490. no checking needed for silk_min
  491. no checking needed for silk_max
  492. no checking needed for silk_sign
  493. */
  494. #endif
  495. #endif /* MACRO_DEBUG_H */