basic_op40.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  1. /**********************************************************************
  2. Each of the companies; Lucent, Motorola, Nokia, and Qualcomm (hereinafter
  3. referred to individually as "Source" or collectively as "Sources") do
  4. hereby state:
  5. To the extent to which the Source(s) may legally and freely do so, the
  6. Source(s), upon submission of a Contribution, grant(s) a free,
  7. irrevocable, non-exclusive, license to the Third Generation Partnership
  8. Project 2 (3GPP2) and its Organizational Partners: ARIB, CCSA, TIA, TTA,
  9. and TTC, under the Source's copyright or copyright license rights in the
  10. Contribution, to, in whole or in part, copy, make derivative works,
  11. perform, display and distribute the Contribution and derivative works
  12. thereof consistent with 3GPP2's and each Organizational Partner's
  13. policies and procedures, with the right to (i) sublicense the foregoing
  14. rights consistent with 3GPP2's and each Organizational Partner's policies
  15. and procedures and (ii) copyright and sell, if applicable) in 3GPP2's name
  16. or each Organizational Partner's name any 3GPP2 or transposed Publication
  17. even though this Publication may contain the Contribution or a derivative
  18. work thereof. The Contribution shall disclose any known limitations on
  19. the Source's rights to license as herein provided.
  20. When a Contribution is submitted by the Source(s) to assist the
  21. formulating groups of 3GPP2 or any of its Organizational Partners, it
  22. is proposed to the Committee as a basis for discussion and is not to
  23. be construed as a binding proposal on the Source(s). The Source(s)
  24. specifically reserve(s) the right to amend or modify the material
  25. contained in the Contribution. Nothing contained in the Contribution
  26. shall, except as herein expressly provided, be construed as conferring
  27. by implication, estoppel or otherwise, any license or right under (i)
  28. any existing or later issuing patent, whether or not the use of
  29. information in the document necessarily employs an invention of any
  30. existing or later issued patent, (ii) any copyright, (iii) any
  31. trademark, or (iv) any other intellectual property right.
  32. With respect to the Software necessary for the practice of any or
  33. all Normative portions of the Enhanced Variable Rate Codec (EVRC) as
  34. it exists on the date of submittal of this form, should the EVRC be
  35. approved as a Specification or Report by 3GPP2, or as a transposed
  36. Standard by any of the 3GPP2's Organizational Partners, the Source(s)
  37. state(s) that a worldwide license to reproduce, use and distribute the
  38. Software, the license rights to which are held by the Source(s), will
  39. be made available to applicants under terms and conditions that are
  40. reasonable and non-discriminatory, which may include monetary compensation,
  41. and only to the extent necessary for the practice of any or all of the
  42. Normative portions of the EVRC or the field of use of practice of the
  43. EVRC Specification, Report, or Standard. The statement contained above
  44. is irrevocable and shall be binding upon the Source(s). In the event
  45. the rights of the Source(s) in and to copyright or copyright license
  46. rights subject to such commitment are assigned or transferred, the
  47. Source(s) shall notify the assignee or transferee of the existence of
  48. such commitments.
  49. *******************************************************************/
  50. /*============================================================================*/
  51. /*===================================================================*/
  52. /* */
  53. /* Conexant Systems, Inc., Qualcomm Inc., Nokia Inc,. Nortel */
  54. /* Networks, Lucent Technologies, and Motorola Inc. grants a free, */
  55. /* irrevocable license to 3GPP2 and its Organizational Partners to */
  56. /* incorporate text or other copyrightable material contained in the */
  57. /* contribution and any modifications thereof in the creation of */
  58. /* 3GPP2 publications; to copyright and sell in Organizational */
  59. /* Partner's name any Organizational Partner's standards publication */
  60. /* even though it may include portions of the contribution; and at */
  61. /* the Organizational Partner's sole discretion to permit others to */
  62. /* reproduce in whole or in part such contributions or the resulting */
  63. /* Organizational Partner's standards publication. The contributor */
  64. /* must also be willing to grant licenses under such contributor */
  65. /* copyrights to third parties on reasonable, non-discriminatory */
  66. /* terms and conditions, as appropriate. */
  67. /* */
  68. /*-------------------------------------------------------------------*/
  69. /* */
  70. /* Copyright 2001 Conexant Systems, Inc. This document is provided */
  71. /* as a discussion instrument only and is not binding on Conexant */
  72. /* Systems, Inc. The contributor reserves the right to add to, amend */
  73. /* or otherwise modify the contents thereof. */
  74. /* */
  75. /* Conexant Systems, Inc. grants a free, irrevocable license to 3GPP2*/
  76. /* and its Organization Partners to incorporate, for any legitimate */
  77. /* 3GPP2 purpose, any copyrightable material contained in this */
  78. /* contribution and any revisions thereof, in publications of 3GPP2. */
  79. /* The contributor may hold one or more patents or applications for */
  80. /* letters patent that cover the information contained in this */
  81. /* contribution. Noting contained in this copyright statement shall */
  82. /* be construed as conferring by implication or estoppel, or */
  83. /* otherwise a license or any other right under any existing or */
  84. /* later issuing patent, whether or not the use of information */
  85. /* herein necessarily employs an invention of any such patent or */
  86. /* later issuing patent. */
  87. /* */
  88. /*-------------------------------------------------------------------*/
  89. /* */
  90. /* Copyright QUALCOMM Incorporated. All rights reserved. */
  91. /* The information contained in this contribution is provided for */
  92. /* the sole purpose of promoting discussion within the 3GPP2 and its */
  93. /* Organization Partners and is not binding on the contributor. */
  94. /* The contributor reserves the right to add to, amend, or withdraw */
  95. /* the statements contained herein. */
  96. /* QUALCOMM Incorporated grants a free, irrevocable license to 3GPP2 */
  97. /* and its Organization Partners to incorporate text or other */
  98. /* copyrightable material contained in the contribution and any */
  99. /* modifications thereof in the creation of 3GPP2 publications; */
  100. /* to copyright and sell in Organizational Partner's name any */
  101. /* Organizational Partner's standards publication even though it may */
  102. /* include portions of the contribution; and at the Organization */
  103. /* Partner's sole discretion to permit others to reproduce in whole */
  104. /* or in part such contributions or the resulting Organizational */
  105. /* Partner's standards publication. The contributor may hold one or */
  106. /* more patents or copyrights that cover information contained in */
  107. /* this contribution. A license will be made available to applicants */
  108. /* under reasonable terms and conditions that are demonstrably free */
  109. /* of any unfair discrimination. */
  110. /* Nothing contained herein shall be construed as conferring by */
  111. /* implication, estoppel, or otherwise any license or right under */
  112. /* any patent, whether or not the use of information herein */
  113. /* necessarily employs an invention of any existing or later issued */
  114. /* patent, or copyright. The contributor reserves the right to use */
  115. /* all material submitted in this contribution for his own purposes, */
  116. /* including republication and distribution to others. */
  117. /* */
  118. /*-------------------------------------------------------------------*/
  119. /* */
  120. /* The proposals in this submission have been formulated by Nokia, */
  121. /* Inc. (Nokia), to assist the 3GPP2 Standards Committee. This */
  122. /* document is offered to the committee as a basis for discussion */
  123. /* and is not binding on Nokia. The requirements are subject to */
  124. /* change in form and in numerical values after more study. Nokia */
  125. /* specifically reserves the right to add to, or amend, the */
  126. /* quantitative statements made herein. Nothing contained herein */
  127. /* shall be construed as conferring by implication, estoppel, or */
  128. /* otherwise any license or right under any patent, whether or not */
  129. /* the use of information herein necessarily employs an invention of */
  130. /* any existing or later issued patent. */
  131. /* Copyright 2001 Nokia, Inc. All Rights Reserved. Nokia hereby */
  132. /* gives permission for copying this submission for the legitimate */
  133. /* purposes of the 3GPP2 Standards Committee, provided Nokia, Inc. */
  134. /* is credited on all copies. Distribution or reproduction of this */
  135. /* document, by any means, electronic, mechanical, or otherwise, in */
  136. /* its entirety, or any portion thereof, for monetary gain or any */
  137. /* non-3GPP2 purposes is expressly prohibited. */
  138. /* */
  139. /*-------------------------------------------------------------------*/
  140. /* */
  141. /* Nortel Networks grants a free, irrevocable license to 3GPP2 and */
  142. /* its Organizational Partners to incorporate text or other */
  143. /* copyrightable material contained in the contribution and any */
  144. /* modifications thereof in the creation of 3GPP2 publications; to */
  145. /* copyright and sell in Organizational Partner's name any */
  146. /* Organizational Partner's standards publication even though it may */
  147. /* include portions of the contribution; and at the Organizational */
  148. /* Partner's sole discretion to permit others to reproduce in whole */
  149. /* or in part such contributions or the resulting Organizational */
  150. /* Partner's standards publication. The contributor must also be */
  151. /* willing to grant licenses under such contributor copyrights to */
  152. /* third parties on reasonable, non-discriminatory terms and */
  153. /* conditions, as appropriate. */
  154. /* */
  155. /*-------------------------------------------------------------------*/
  156. /* */
  157. /* Notice: */
  158. /* This contribution has been prepared by the contributor to assist */
  159. /* 3GPP2 Technical Specifications Group C. This document is offered */
  160. /* to 3GPP2 Technical Specifications Group C as a basis for */
  161. /* discussion and should not be construed as a binding proposal on */
  162. /* Lucent Technologies Inc. or any other company. Specifically, */
  163. /* Lucent Technologies Inc. reserves the right to modify, amend, or */
  164. /* withdraw the statements contained herein. */
  165. /* */
  166. /* Permission is granted to 3GPP2 Technical Specifications Group C */
  167. /* participants to copy any portion of this document for legitimate */
  168. /* purposes of 3GPP2 Technical Specifications Group C. Copying for */
  169. /* monetary gain or other non-3GPP2 Technical Specifications Group C */
  170. /* purposes is prohibited. */
  171. /* */
  172. /*-------------------------------------------------------------------*/
  173. /* */
  174. /* Grant of license Motorola Inc. grants a free, irrevocable license */
  175. /* to 3GPP2 and its organizational partners to incorporate Motorola- */
  176. /* supplied text or other copyrightable material contained in the */
  177. /* contribution and any modifications thereof in the creation of */
  178. /* 3GPP2 publications, to copyright and sell in organizational */
  179. /* partners name any organizational partners standards publications */
  180. /* even though it may include portions of the contribution; and at */
  181. /* the organizational partners sole discretion to permit others */
  182. /* to reproduce in whole or in part such contributions or the */
  183. /* resulting organizational partners standards publication. Motorola */
  184. /* is also willing to grant licenses under such Motorola copyrights */
  185. /* to third parties on reasonable, non-discriminatory terms and */
  186. /* conditions, as appropriate. */
  187. /* */
  188. /* Notice: */
  189. /* This document has been prepared by Motorola Inc. to assist the */
  190. /* 3GPP2 standards committee. This document is offered to the */
  191. /* committee as a basis for discussion and should not be considered */
  192. /* as a binding proposal on Motorola Inc. or any other company. */
  193. /* Specifically, Motorola Inc. reserves the right to modify, amend, */
  194. /* or withdraw the statement contained herein. Permission is granted */
  195. /* to 3GPP2 and its organizational partners to copy any portion of */
  196. /* this document for the legitimate purposes of the 3GPP2. Copying */
  197. /* this document for monetary gain or other non-3GPP2 purpose is */
  198. /* prohibited. Motorola Inc. may hold one or more patents of */
  199. /* copyrights that cover information contained in this contribution, */
  200. /* and agrees that a license under those rights will be made */
  201. /* available on reasonable and non-discriminatory terms and */
  202. /* conditions, subject to receiving a reciprocal license in return. */
  203. /* Nothing contained herein shall be construed as conferring by */
  204. /* implication, estoppel, or otherwise any license or right under */
  205. /* any patent, whether or not the use of information herein */
  206. /* necessarily employs an invention of any existing or later issued */
  207. /* patent, or copyright. */
  208. /* */
  209. /* Notice */
  210. /* Permission is granted to 3GPP2 participants to copy any portion of*/
  211. /* this contribution for the legitimate purpose of the 3GPP2. */
  212. /* Copying this contribution for monetary gain or other non-3GPP2 */
  213. /* purpose is prohibited. */
  214. /* */
  215. /*===================================================================*/
  216. /* FILE: basic_op40.c */
  217. /*-------------------------------------------------------------------*/
  218. /* PURPOSE : Modified TIA basic operations for 40-bit accumulators */
  219. /*===================================================================*/
  220. /*___________________________________________________________________________
  221. |
  222. | Basic arithmetic operators using 16-bit/40-bit arithmatic
  223. |
  224. | These operators come originally from ETSI TCH-HS library
  225. | (used in GSM EFR 13kbit/s).
  226. | They were modified to support 16-bit/40-bit arithmetic operations.
  227. | Standard 32-bit basic operators are replaced by 40-bit operators to be
  228. | compatible with
  229. | most of the new 40-bit DSP in the industry. The use of 40 bits
  230. | accumulator allow the DSP to avoid overflow/saturation in the calculation
  231. | of energy, correlation, filtering or any other procedure that need more than
  232. | 32 bits in the summation of product. These 40-bit operators should be
  233. | used when the target platform is DSP with 40-bit accumulator.
  234. |
  235. | Word40 is represented using a floating point format (double)
  236. | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  237. | Because some platforms do not allow integer of 40 bits, a floating point
  238. | format is used, with at less 40 bits in the mantissa, to represent long
  239. | integer of 40 bits. MAX_40 and MIN_40 are defined in this file.
  240. |____________________________________________________________________________
  241. */
  242. /***********************************************************************/
  243. /*_________________________________________________________________________
  244. | |
  245. | Include Files |
  246. |_________________________________________________________________________|
  247. */
  248. #include <math.h>
  249. #include "typedef_fx.h"
  250. #include "basic_op40.h"
  251. #ifdef WMOPS_FX
  252. #include "typedef_fx.h"
  253. #include "main_fx.h"
  254. #include "const_fx.h"
  255. #include "ext_var_fx.h"
  256. #include "lib_wmp_fx.h"
  257. #endif
  258. //*************************************************************************
  259. // local functions for 40 bit accumulator
  260. /*___________________________________________________________________________
  261. | |
  262. | Function Name : L_shift_left40 |
  263. | |
  264. | Purpose : |
  265. | |
  266. | Perform an integer arithmetic shift on the double format value. |
  267. | If nbits > 0, arithmetically shift the value left nbits positions. |
  268. | Zero fill the nbits LSB of the value. |
  269. | If nbits < 0, arithmetically shift the value right by -nbits positions |
  270. | with sign extension. |
  271. | No overflow control and saturation. |
  272. |___________________________________________________________________________|
  273. */
  274. static Word40 L_shift_left40(Word40 value, Word16 nbits)
  275. {
  276. if (nbits > 40) nbits = 40; /* to avoid needless loop after overflow */
  277. if (nbits < -40) nbits = -40;
  278. if (nbits < 0)
  279. {
  280. for(; nbits<0; nbits++)
  281. {
  282. value = value * 0.5; /* remove fraction bits */
  283. }
  284. value = floor(value);
  285. }
  286. else
  287. {
  288. value = floor(value); /* remove fraction bits */
  289. for(; nbits>0; nbits--)
  290. {
  291. value = value * 2;
  292. }
  293. }
  294. return(value);
  295. }
  296. /*___________________________________________________________________________
  297. | |
  298. | Function Name : L_saturate40 |
  299. | |
  300. | Purpose : |
  301. | |
  302. | Limit the value to the range of a 40 bit word. |
  303. | |
  304. | Return Value : |
  305. | |
  306. | var_out 40 bit short signed integer (Word40) whose value falls in the |
  307. | range : MIN_40 <= var_out <= MAX_40. |
  308. |___________________________________________________________________________|
  309. */
  310. static Word40 L_saturate40(Word40 value)
  311. {
  312. Word40 L_var_out;
  313. L_var_out = value;
  314. if (L_var_out > MAX_40)
  315. {
  316. L_var_out = MAX_40;
  317. giOverflow = 1;
  318. }
  319. else if (L_var_out < MIN_40)
  320. {
  321. L_var_out = MIN_40;
  322. giOverflow = 1;
  323. }
  324. return(L_var_out);
  325. }
  326. //***************************************************************************
  327. //***************************************************************************
  328. //***************************************************************************
  329. //***************************************************************************
  330. // public functions for 40 bit accumulator
  331. //
  332. // J. Klein, Conexant Systems 12/01/00
  333. /*___________________________________________________________________________
  334. | |
  335. | Function Name : L_add40 |
  336. | |
  337. | Purpose : |
  338. | |
  339. | Add L_var1 by L_var2. Return a 40 bit result without saturation: |
  340. | L_add40 (L_var1, L_var2). |
  341. | |
  342. | Complexity weight : 1 |
  343. | |
  344. | Inputs : |
  345. | |
  346. | L_var1 40 bit long signed integer (Word40) whose value falls in the |
  347. | range : MIN_40 <= L_var1 <= MAX_40. |
  348. | |
  349. | |
  350. | L_var2 32 bit long signed integer (Word32) whose value falls in |
  351. | the : MIN_32 <= L_var2 <= MAX_32. |
  352. | |
  353. | Return Value : |
  354. | |
  355. | L_var_out |
  356. | 40 bit long signed integer (Word40) whose value falls in the |
  357. | range : MIN_40 <= L_var_out <= MAX_40. |
  358. |___________________________________________________________________________|
  359. */
  360. Word40 L_add40(Word40 L_var1, Word32 L_var2)
  361. {
  362. Word40 L_var_out;
  363. L_var_out = L_var1 + (Word40)L_var2;
  364. L_var_out = L_saturate40(L_var_out);
  365. #ifdef WMOPS_FX
  366. counter_fx.L_add40++;
  367. #endif
  368. return(L_var_out);
  369. }
  370. /*___________________________________________________________________________
  371. | |
  372. | Function Name : L_sub40 |
  373. | |
  374. | Purpose : |
  375. | |
  376. | Substract L_var1 by L_var2. Return a 40 bit result without saturation: |
  377. | L_sub40 (L_var1, L_var2). |
  378. | |
  379. | Complexity weight : 1 |
  380. | |
  381. | Inputs : |
  382. | |
  383. | L_var1 40 bit long signed integer (Word40) whose value falls in the |
  384. | range : MIN_40 <= L_var1 <= MAX_40. |
  385. | |
  386. | |
  387. | L_var2 32 bit long signed integer (Word32) whose value falls in |
  388. | the : MIN_32 <= L_var2 <= MAX_32. |
  389. | |
  390. | Return Value : |
  391. | |
  392. | L_var_out |
  393. | 40 bit long signed integer (Word40) whose value falls in the |
  394. | range : MIN_40 <= L_var_out <= MAX_40. |
  395. |___________________________________________________________________________|
  396. */
  397. Word40 L_sub40(Word40 L_var1, Word32 L_var2)
  398. {
  399. Word40 L_var_out;
  400. L_var_out = L_var1 - (Word40)L_var2;
  401. L_var_out = L_saturate40(L_var_out);
  402. #ifdef WMOPS_FX
  403. counter_fx.L_sub40++;
  404. #endif
  405. return(L_var_out);
  406. }
  407. /*___________________________________________________________________________
  408. | |
  409. | Function Name : L_mac40 |
  410. | |
  411. | Purpose : |
  412. | |
  413. | Multiply var1 by var2 and shift the result left by 1. Add the 40 bit |
  414. | result to L_var3 with saturation, return a 40 bit result: |
  415. | L_mac(L_var3,var1,var2) = L_add(L_var3,(L_mult(var1,var2)). |
  416. | |
  417. | Complexity weight : 1 |
  418. | |
  419. | Inputs : |
  420. | |
  421. | L_var3 40 bit long signed integer (Word40) whose value falls in the |
  422. | range : MIN_40 <= L_var3 <= MAX_40. |
  423. | |
  424. | var1 16 bit short signed integer (Word16) whose value falls in |
  425. | the : MIN_16 <= var1 <= MAX_16. |
  426. | |
  427. | var2 16 bit short signed integer (Word16) whose value falls in |
  428. | the : MIN_16 <= var1 <= MAX_16. |
  429. | |
  430. | Return Value : |
  431. | |
  432. | L_var_out |
  433. | 40 bit long signed integer (Word40) whose value falls in the |
  434. | range : MIN_40 <= L_var_out <= MAX_40. |
  435. |___________________________________________________________________________|
  436. */
  437. Word40 L_mac40(Word40 L_var3, Word16 var1, Word16 var2)
  438. {
  439. Word40 L_var_out;
  440. L_var_out = L_var3 + ((Word40)var1 * (Word40)var2 * 2.0);
  441. L_var_out = L_saturate40(L_var_out);
  442. #ifdef WMOPS_FX
  443. counter_fx.L_mac40++;
  444. #endif
  445. return(L_var_out);
  446. }
  447. /*___________________________________________________________________________
  448. | |
  449. | Function Name : L_msu40 |
  450. | |
  451. | Purpose : |
  452. | |
  453. | Multiply var1 by var2 and shift the result left by 1. Subtract the 40 |
  454. | bit result to L_var3 with saturation, return a 40 bit result: |
  455. | L_msu(L_var3,var1,var2) = L_sub(L_var3,(L_mult(var1,var2)). |
  456. | |
  457. | Complexity weight : 1 |
  458. | |
  459. | Inputs : |
  460. | |
  461. | L_var3 40 bit long signed integer (Word40) whose value falls in the |
  462. | range : MIN_40 <= L_var3 <= MAX_40. |
  463. | |
  464. | var1 16 bit short signed integer (Word16) whose value falls in |
  465. | the range : MIN_16 <= var1 <= MAX_16. |
  466. | |
  467. | var2 16 bit short signed integer (Word16) whose value falls in |
  468. | therange : MIN_16 <= var1 <= MAX_16. |
  469. | |
  470. | Return Value : |
  471. | |
  472. | L_var_out |
  473. | 40 bit long signed integer (Word40) whose value falls in the |
  474. | range : MIN_40 <= L_var_out <= MAX_40. |
  475. |___________________________________________________________________________|
  476. */
  477. Word40 L_msu40(Word40 L_var3, Word16 var1, Word16 var2)
  478. {
  479. Word40 L_var_out;
  480. L_var_out = L_var3 - ((Word40)var1 * (Word40)var2 * 2.0);
  481. L_var_out = L_saturate40(L_var_out);
  482. #ifdef WMOPS_FX
  483. counter_fx.L_msu40++;
  484. #endif
  485. return(L_var_out);
  486. }
  487. /*___________________________________________________________________________
  488. | |
  489. | Function Name : L_shr40 |
  490. | |
  491. | Purpose : |
  492. | |
  493. | Arithmetically shift the 40 bit input L_var1 right var2 positions with |
  494. | sign extension. If var2 is negative, arithmetically shift L_var1 left |
  495. | by -var2 and zero fill the var2 LSB of the result. Saturate the result |
  496. | in case of underflows or overflows. |
  497. | |
  498. | Complexity weight : 2 |
  499. | |
  500. | Inputs : |
  501. | |
  502. | L_var1 40 bit long signed integer (Word40) whose value falls in the |
  503. | range : MIN_40 <= L_var3 <= MAX_40. |
  504. | |
  505. | var2 16 bit short signed integer (Word16) whose value falls in |
  506. | therange : MIN_16 <= var1 <= MAX_16. |
  507. | |
  508. | Return Value : |
  509. | |
  510. | L_var_out |
  511. | 40 bit long signed integer (Word40) whose value falls in the |
  512. | range : MIN_40 <= L_var_out <= MAX_40. |
  513. |___________________________________________________________________________|
  514. */
  515. Word40 L_shr40(Word40 L_var1, Word16 var2)
  516. {
  517. Word40 L_var_out;
  518. Word40 L_shl40(Word40 L_var1, Word16 var2);
  519. if (var2 < 0)
  520. {
  521. L_var_out = L_shl40(L_var1,(Word16)(-var2));
  522. #ifdef WMOPS_FX
  523. counter_fx.L_shl40--;
  524. #endif
  525. }
  526. else
  527. {
  528. L_var_out = L_shift_left40(L_var1, (Word16)(-var2));
  529. }
  530. #ifdef WMOPS_FX
  531. counter_fx.L_shr40++;
  532. #endif
  533. return(L_var_out);
  534. }
  535. /*___________________________________________________________________________
  536. | |
  537. | Function Name : L_shl40 |
  538. | |
  539. | Purpose : |
  540. | |
  541. | Arithmetically shift the 40 bit input L_var1 left var2 positions. Zero |
  542. | fill the var2 LSB of the result. If var2 is negative, L_var1 right by |
  543. | -var2 arithmetically shift with sign extension. Saturate the result in |
  544. | case of underflows or overflows. |
  545. | |
  546. | Complexity weight : 2 |
  547. | |
  548. | Inputs : |
  549. | |
  550. | L_var1 40 bit long signed integer (Word40) whose value falls in the |
  551. | range : MIN_40 <= L_var3 <= MAX_40. |
  552. | |
  553. | var2 16 bit short signed integer (Word16) whose value falls in |
  554. | therange : MIN_16 <= var1 <= MAX_16. |
  555. | |
  556. | Return Value : |
  557. | |
  558. | L_var_out |
  559. | 40 bit long signed integer (Word40) whose value falls in the |
  560. | range : MIN_40 <= L_var_out <= MAX_40. |
  561. |___________________________________________________________________________|
  562. */
  563. Word40 L_shl40(Word40 L_var1, Word16 var2)
  564. {
  565. Word40 L_var_out;
  566. if (var2 <= 0)
  567. {
  568. L_var_out = L_shr40(L_var1,(Word16)(-var2));
  569. #ifdef WMOPS_FX
  570. counter_fx.L_shr40--;
  571. #endif
  572. }
  573. else
  574. {
  575. L_var_out = L_shift_left40(L_var1, var2);
  576. L_var_out = L_saturate40(L_var_out);
  577. }
  578. #ifdef WMOPS_FX
  579. counter_fx.L_shl40++;
  580. #endif
  581. return(L_var_out);
  582. }
  583. /*___________________________________________________________________________
  584. | |
  585. | Function Name : L_sat32_40 |
  586. | |
  587. | Purpose : |
  588. | |
  589. | 40 bit L_var1 is limited to the range MAX_32..MIN_32. L_var1 is set |
  590. | to MAX_32 on overflow or MIN_32 on underflow. |
  591. | |
  592. | Complexity weight : 1 |
  593. | |
  594. | Inputs : |
  595. | |
  596. | L_var1 40 bit long signed integer (Word40) whose value falls in the |
  597. | range : MIN_40 <= var1 <= MAX_40. |
  598. | |
  599. | Return Value : |
  600. | |
  601. | L_var_out |
  602. | 40 bit long signed integer (Word40) whose value falls in the |
  603. | range : MIN_32 <= var_out <= MAX_32. |
  604. |___________________________________________________________________________|
  605. */
  606. Word40 L_sat32_40(Word40 L_var1)
  607. {
  608. Word40 L_var_out;
  609. if (L_var1 > MAX_32)
  610. {
  611. L_var_out = MAX_32;
  612. giOverflow = 1;
  613. }
  614. else if (L_var1 < MIN_32)
  615. {
  616. L_var_out = MIN_32;
  617. giOverflow = 1;
  618. }
  619. else L_var_out = L_var1;
  620. #ifdef WMOPS_FX
  621. counter_fx.L_sat32_40++;
  622. #endif
  623. return (L_var_out);
  624. }
  625. /*___________________________________________________________________________
  626. | |
  627. | Function Name : norm32_l_40 |
  628. | |
  629. | Purpose : |
  630. | |
  631. | Produces the number of left shift needed to normalize in 32 bits format |
  632. | the 40 bit variable l_var1 for positive values on the interval with |
  633. | minimum of (MAX_32+1)/2 and maximum of MAX_32, and for negative values |
  634. | on the interval with minimum of MIN_32 and maximum of (MIN_32/2)+1; |
  635. | in order to normalize the result, the following operation must be done: |
  636. | |
  637. | norm_L_var1 = L_shl40(L_var1,norm32_l(L_var1)). |
  638. | |
  639. | Complexity weight : 3 |
  640. | |
  641. | Inputs : |
  642. | |
  643. | L_var1 40 bit long signed integer (Word40) whose value falls in the |
  644. | range : MIN_40 <= var1 <= MAX_40. |
  645. | |
  646. | Return Value : |
  647. | |
  648. | var_out 16 bit short signed integer (Word16) whose value falls in |
  649. | therange : -8 <= var_out <= 31. |
  650. |___________________________________________________________________________|
  651. */
  652. Word16 norm32_l40(Word40 L_var1)
  653. {
  654. Word16 var_out;
  655. L_var1 = floor(L_var1);
  656. var_out = 0;
  657. if (L_var1 != 0)
  658. {
  659. while ((L_var1 > MIN_32) && (L_var1 < MAX_32))
  660. {
  661. L_var1 = L_shift_left40(L_var1, 1);
  662. var_out++;
  663. }
  664. while ((L_var1 < MIN_32) || (L_var1 > MAX_32))
  665. {
  666. L_var1 = L_shift_left40(L_var1, -1);
  667. var_out--;
  668. }
  669. }
  670. #ifdef WMOPS_FX
  671. counter_fx.norm32_l40++;
  672. #endif
  673. return(var_out);
  674. }
  675. /*___________________________________________________________________________
  676. | |
  677. | Function Name : extract_l40 |
  678. | |
  679. | Purpose : |
  680. | |
  681. | Get the lower 16 bits of a 40-bit variable into a 16-bit variable |
  682. | |
  683. | Complexity weight : 1 |
  684. | |
  685. | Inputs : |
  686. | |
  687. | L_var1 40 bit long signed integer (Word40) whose value falls in the |
  688. | range : MIN_40 <= var1 <= MAX_40. |
  689. | |
  690. | Return Value : |
  691. | |
  692. | var_out 16 bit short signed integer (Word16) which may take any value |
  693. |___________________________________________________________________________|
  694. */
  695. Word16 extract_l40(Word40 L_var1)
  696. {
  697. Word16 out;
  698. out = (Word16)(L_var1 - floor(L_var1/65536.)*65536.);
  699. #ifdef WMOPS_FX
  700. counter_fx.extract_l40++;
  701. #endif
  702. return (out);
  703. }
  704. /*___________________________________________________________________________
  705. | |
  706. | Function Name : L_deposit_l40 |
  707. | |
  708. | Purpose : |
  709. | |
  710. | Replace lower 16 bits of a 40-bit variable by those of 16-bit input |
  711. | variable var_in |
  712. | |
  713. | Complexity weight : 1 |
  714. | |
  715. | Inputs : |
  716. | |
  717. | L_var1 40 bit long signed integer (Word40) whose value falls in the |
  718. | range : MIN_40 <= var1 <= MAX_40. |
  719. | var_in 16 bit short signed integer (Word16) which may take any value |
  720. | |
  721. | Return Value : |
  722. | |
  723. | 40 bit long signed integer (Word40) whose value falls in the |
  724. | range : MIN_40 <= var1 <= MAX_40. |
  725. |___________________________________________________________________________|
  726. */
  727. Word40 L_deposit_l40(Word40 L_var1, Word16 var_in)
  728. {
  729. Word40 out;
  730. Word32 tmp32;
  731. tmp32 = 0x0000ffff & (Word32)var_in; /* no sign extend */
  732. out = L_add40(floor(L_var1/65536.)*65536., tmp32);
  733. #ifdef WMOPS_FX
  734. counter_fx.L_add40--;
  735. counter_fx.L_deposit_l40++;
  736. #endif
  737. return (out);
  738. }