mathevrc.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300
  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. /* Enhanced Variable Rate Codec - Bit-Exact C Specification */
  52. /* Copyright (C) 1997-1998 Telecommunications Industry Association. */
  53. /* All rights reserved. */
  54. /*----------------------------------------------------------------------*/
  55. /* Note: Reproduction and use of this software for the design and */
  56. /* development of North American Wideband CDMA Digital */
  57. /* Cellular Telephony Standards is authorized by the TIA. */
  58. /* The TIA does not authorize the use of this software for any */
  59. /* other purpose. */
  60. /* */
  61. /* The availability of this software does not provide any license */
  62. /* by implication, estoppel, or otherwise under any patent rights */
  63. /* of TIA member companies or others covering any use of the */
  64. /* contents herein. */
  65. /* */
  66. /* Any copies of this software or derivative works must include */
  67. /* this and all other proprietary notices. */
  68. /*======================================================================*/
  69. /***************************************************************************
  70. *
  71. * File Name: mathevrc.c
  72. *
  73. * Purpose: Contains functions which implement the primitive
  74. * arithmetic operations.
  75. *
  76. * The functions in this file are listed below. Some of them are
  77. * defined in terms of other basic operations. One of the
  78. * routines, saturate() is static. This is not a basic
  79. * operation, and is not referenced outside the scope of this
  80. * file.
  81. *
  82. *
  83. * abs_s()
  84. * add()
  85. * clearOverflow()
  86. * divide_s()
  87. * extract_h()
  88. * extract_l()
  89. * isOverflow()
  90. * L_abs()
  91. * L_add()
  92. * L_deposit_h()
  93. * L_deposit_l()
  94. * L_mac()
  95. * L_msu()
  96. * L_mult()
  97. * L_negate()
  98. * L_shift_r()
  99. * L_shl()
  100. * L_shr()
  101. * L_sub()
  102. * mac_r()
  103. * msu_r()
  104. * mult()
  105. * mult_r()
  106. * negate()
  107. * norm_l()
  108. * norm_s()
  109. * popOverflow()
  110. * round32()
  111. * saturate()
  112. * setOverflow()
  113. * shift_r()
  114. * shl()
  115. * shr()
  116. * sub()
  117. *
  118. **************************************************************************/
  119. /*_________________________________________________________________________
  120. | |
  121. | Include Files |
  122. |_________________________________________________________________________|
  123. */
  124. #include "typedefs.h"
  125. #include "mathevrc.h"
  126. //#include <math.h>
  127. /***************************************************************************
  128. *
  129. * FUNCTION NAME: saturate
  130. *
  131. * PURPOSE:
  132. *
  133. * Limit the 32 bit input to the range of a 16 bit word.
  134. *
  135. *
  136. * INPUTS:
  137. *
  138. * L_var1
  139. * 32 bit long signed integer (Longword) whose value
  140. * falls in the range
  141. * 0x8000 0000 <= L_var1 <= 0x7fff ffff.
  142. *
  143. * OUTPUTS:
  144. *
  145. * none
  146. *
  147. * RETURN VALUE:
  148. *
  149. * swOut
  150. * 16 bit short signed integer (Shortword) whose value
  151. * falls in the range
  152. * 0xffff 8000 <= swOut <= 0x0000 7fff.
  153. *
  154. * KEYWORDS: saturation, limiting, limit, saturate, 16 bits
  155. *
  156. *************************************************************************/
  157. static Shortword saturate(Longword L_var1)
  158. {
  159. Shortword swOut;
  160. if (L_var1 > SW_MAX)
  161. {
  162. swOut = SW_MAX;
  163. giOverflow = 1;
  164. }
  165. else if (L_var1 < SW_MIN)
  166. {
  167. swOut = SW_MIN;
  168. giOverflow = 1;
  169. }
  170. else
  171. swOut = (Shortword) L_var1; /* automatic type conversion */
  172. return (swOut);
  173. }
  174. /***************************************************************************
  175. *
  176. * FUNCTION NAME: L_saturate
  177. *
  178. * PURPOSE:
  179. *
  180. * Limit the input double (possibly exceeding 32 bit dynamic
  181. * range) having to the 32 output wordsize.
  182. *
  183. * INPUTS:
  184. *
  185. * dVar1
  186. * A double whose range is
  187. * 0x8 0000 0000 <= L_var1 <= 0x7 ffff ffff.
  188. * i.e. a 33 bit number. Not modified.
  189. *
  190. * OUTPUTS:
  191. *
  192. * none
  193. *
  194. * RETURN VALUE:
  195. *
  196. * L_Out
  197. * 32 bit long integer (Longword) where the DSP's
  198. * rules of saturation are followed:
  199. * for: 0x8000 0000 <= dVal1 <= 0x7fff ffff
  200. * input == output, no saturation
  201. * for: 0x8000 0000 > dVal1 output 0x8000 0000
  202. * for: dVal1 > 0x7fff ffff output 0x7fff ffff
  203. *
  204. * KEYWORDS: saturation, limiting, limit, saturate, 32 bits
  205. *
  206. *************************************************************************/
  207. Longword L_saturate(double dVar1)
  208. {
  209. OP_COUNT(4); /* Complexity Count -- LT 6/96 */
  210. if (dVar1 > (double) LW_MAX)
  211. {
  212. dVar1 = (double) LW_MAX;
  213. giOverflow = 1;
  214. }
  215. else if (dVar1 < (double) LW_MIN)
  216. {
  217. dVar1 = (double) LW_MIN;
  218. giOverflow = 1;
  219. }
  220. return ((Longword) dVar1);
  221. }
  222. /***************************************************************************
  223. *
  224. * FUNCTION NAME: abs_s
  225. *
  226. * PURPOSE:
  227. *
  228. * Take the absolute value of the 16 bit input. An input of
  229. * -0x8000 results in a return value of 0x7fff.
  230. *
  231. * INPUTS:
  232. *
  233. * var1
  234. * 16 bit short signed integer (Shortword) whose value
  235. * falls in the range 0xffff 8000 <= var1 <= 0x0000 7fff.
  236. *
  237. * OUTPUTS:
  238. *
  239. * none
  240. *
  241. * RETURN VALUE:
  242. *
  243. * swOut
  244. * 16 bit short signed integer (Shortword) whose value
  245. * falls in the range
  246. * 0x0000 0000 <= swOut <= 0x0000 7fff.
  247. *
  248. * IMPLEMENTATION:
  249. *
  250. * Take the absolute value of the 16 bit input. An input of
  251. * -0x8000 results in a return value of 0x7fff.
  252. *
  253. * KEYWORDS: absolute value, abs
  254. *
  255. *************************************************************************/
  256. Shortword abs_s(Shortword var1)
  257. {
  258. Shortword swOut;
  259. OP_COUNT(1); /* Complexity Count -- LT 6/96 */
  260. if (var1 == SW_MIN)
  261. {
  262. swOut = SW_MAX;
  263. giOverflow = 1;
  264. }
  265. else
  266. {
  267. if (var1 < 0)
  268. swOut = -var1;
  269. else
  270. swOut = var1;
  271. }
  272. return (swOut);
  273. }
  274. /***************************************************************************
  275. *
  276. * FUNCTION NAME: add
  277. *
  278. * PURPOSE:
  279. *
  280. * Perform the addition of the two 16 bit input variable with
  281. * saturation.
  282. *
  283. * INPUTS:
  284. *
  285. * var1
  286. * 16 bit short signed integer (Shortword) whose value
  287. * falls in the range 0xffff 8000 <= var1 <= 0x0000 7fff.
  288. * var2
  289. * 16 bit short signed integer (Shortword) whose value
  290. * falls in the range 0xffff 8000 <= var2 <= 0x0000 7fff.
  291. *
  292. * OUTPUTS:
  293. *
  294. * none
  295. *
  296. * RETURN VALUE:
  297. *
  298. * swOut
  299. * 16 bit short signed integer (Shortword) whose value
  300. * falls in the range
  301. * 0xffff 8000 <= swOut <= 0x0000 7fff.
  302. *
  303. * IMPLEMENTATION:
  304. *
  305. * Perform the addition of the two 16 bit input variable with
  306. * saturation.
  307. *
  308. * swOut = var1 + var2
  309. *
  310. * swOut is set to 0x7fff if the operation results in an
  311. * overflow. swOut is set to 0x8000 if the operation results
  312. * in an underflow.
  313. *
  314. * KEYWORDS: add, addition
  315. *
  316. *************************************************************************/
  317. Shortword add(Shortword var1, Shortword var2)
  318. {
  319. Longword L_sum;
  320. Shortword swOut;
  321. OP_COUNT(1); /* Complexity Count -- LT 6/96 */
  322. L_sum = (Longword) var1 + var2;
  323. swOut = saturate(L_sum);
  324. return (swOut);
  325. }
  326. /****************************************************************************
  327. *
  328. * FUNCTION NAME: clearOverflow
  329. *
  330. * PURPOSE:
  331. *
  332. * Clear the overflow flag
  333. *
  334. * INPUTS:
  335. *
  336. * none
  337. *
  338. *
  339. * OUTPUTS: global overflow flag is cleared
  340. * previous value stored in giOldOverflow
  341. *
  342. * RETURN VALUE: previous value of overflow
  343. *
  344. *
  345. * KEYWORDS: saturation, limit, overflow
  346. *
  347. ***************************************************************************/
  348. int clearOverflow(void)
  349. {
  350. OP_COUNT(1); /* Complexity Count -- LT 6/96 */
  351. giOldOverflow = giOverflow;
  352. giOverflow = 0;
  353. return (giOldOverflow);
  354. }
  355. /***************************************************************************
  356. *
  357. * FUNCTION NAME: divide_s
  358. *
  359. * PURPOSE:
  360. *
  361. * Divide var1 by var2. Note that both must be positive, and
  362. * var1 >= var2. The output is set to 0 if invalid input is
  363. * provided.
  364. *
  365. * INPUTS:
  366. *
  367. * var1
  368. * 16 bit short signed integer (Shortword) whose value
  369. * falls in the range 0xffff 8000 <= var1 <= 0x0000 7fff.
  370. * var2
  371. * 16 bit short signed integer (Shortword) whose value
  372. * falls in the range 0xffff 8000 <= var2 <= 0x0000 7fff.
  373. *
  374. * OUTPUTS:
  375. *
  376. * none
  377. *
  378. * RETURN VALUE:
  379. *
  380. * swOut
  381. * 16 bit short signed integer (Shortword) whose value
  382. * falls in the range
  383. * 0xffff 8000 <= swOut <= 0x0000 7fff.
  384. *
  385. * IMPLEMENTATION:
  386. *
  387. * In the case where var1==var2 the function returns 0x7fff. The output
  388. * is undefined for invalid inputs. This implementation returns zero
  389. * and issues a warning via stdio if invalid input is presented.
  390. *
  391. * KEYWORDS: divide
  392. *
  393. *************************************************************************/
  394. Shortword divide_s(Shortword var1, Shortword var2)
  395. {
  396. Longword L_div;
  397. Shortword swOut;
  398. OP_COUNT(18); /* Complexity Count -- LT 6/96 */
  399. if (var1 < 0 || var2 < 0 || var1 > var2)
  400. {
  401. /* undefined output for invalid input into divide_s */
  402. return (0);
  403. }
  404. if (var1 == var2)
  405. {
  406. giOverflow = 1;
  407. return (0x7fff);
  408. }
  409. L_div = ((0x00008000L * (Longword) var1) / (Longword) var2);
  410. swOut = saturate(L_div);
  411. return (swOut);
  412. }
  413. /***************************************************************************
  414. *
  415. * FUNCTION NAME: extract_h
  416. *
  417. * PURPOSE:
  418. *
  419. * Extract the 16 MS bits of a 32 bit Longword. Return the 16 bit
  420. * number as a Shortword. This is used as a "truncation" of a fractional
  421. * number.
  422. *
  423. * INPUTS:
  424. *
  425. * L_var1
  426. * 32 bit long signed integer (Longword) whose value
  427. * falls in the range
  428. * 0x8000 0000 <= L_var1 <= 0x7fff ffff.
  429. *
  430. * OUTPUTS:
  431. *
  432. * none
  433. *
  434. * RETURN VALUE:
  435. *
  436. * swOut
  437. * 16 bit short signed integer (Shortword) whose value
  438. * falls in the range
  439. * 0xffff 8000 <= swOut <= 0x0000 7fff.
  440. *
  441. * IMPLEMENTATION:
  442. *
  443. * KEYWORDS: assign, truncate
  444. *
  445. *************************************************************************/
  446. Shortword extract_h(Longword L_var1)
  447. {
  448. Shortword var2;
  449. OP_COUNT(1); /* Complexity Count -- LT 6/96 */
  450. var2 = (Shortword) (0x0000ffffL & (L_var1 >> 16));
  451. return (var2);
  452. }
  453. /***************************************************************************
  454. *
  455. * FUNCTION NAME: extract_l
  456. *
  457. * PURPOSE:
  458. *
  459. * Extract the 16 LS bits of a 32 bit Longword. Return the 16 bit
  460. * number as a Shortword. The upper portion of the input Longword
  461. * has no impact whatsoever on the output.
  462. *
  463. * INPUTS:
  464. *
  465. * L_var1
  466. * 32 bit long signed integer (Longword) whose value
  467. * falls in the range
  468. * 0x8000 0000 <= L_var1 <= 0x7fff ffff.
  469. *
  470. * OUTPUTS:
  471. *
  472. * none
  473. *
  474. * RETURN VALUE:
  475. *
  476. * swOut
  477. * 16 bit short signed integer (Shortword) whose value
  478. * falls in the range
  479. * 0xffff 8000 <= swOut <= 0x0000 7fff.
  480. *
  481. *
  482. * KEYWORDS: extract, assign
  483. *
  484. *************************************************************************/
  485. Shortword extract_l(Longword L_var1)
  486. {
  487. Shortword var2;
  488. OP_COUNT(1); /* Complexity Count -- LT 6/96 */
  489. var2 = (Shortword) (0x0000ffffL & L_var1);
  490. return (var2);
  491. }
  492. /****************************************************************************
  493. *
  494. * FUNCTION NAME: isOverflow
  495. *
  496. * PURPOSE:
  497. *
  498. * Check to see whether an overflow/saturation/limiting has occurred
  499. *
  500. * INPUTS:
  501. *
  502. * none
  503. *
  504. *
  505. * OUTPUTS: none
  506. *
  507. * RETURN VALUE: 1 if overflow has been flagged
  508. * 0 otherwise
  509. *
  510. * KEYWORDS: saturation, limit, overflow
  511. *
  512. ***************************************************************************/
  513. int isOverflow(void)
  514. {
  515. return (giOverflow);
  516. }
  517. /***************************************************************************
  518. *
  519. * FUNCTION NAME: L_abs
  520. *
  521. * PURPOSE:
  522. *
  523. * Take the absolute value of the 32 bit input. An input of
  524. * -0x8000 0000 results in a return value of 0x7fff ffff.
  525. *
  526. * INPUTS:
  527. *
  528. * L_var1
  529. * 32 bit long signed integer (Longword) whose value
  530. * falls in the range
  531. * 0x8000 0000 <= L_var1 <= 0x7fff ffff.
  532. *
  533. * OUTPUTS:
  534. *
  535. * none
  536. *
  537. * RETURN VALUE:
  538. *
  539. * L_Out
  540. * 32 bit long signed integer (Longword) whose value
  541. * falls in the range
  542. * 0x8000 0000 <= L_var1 <= 0x7fff ffff.
  543. *
  544. *
  545. *
  546. * KEYWORDS: absolute value, abs
  547. *
  548. *************************************************************************/
  549. Longword L_abs(Longword L_var1)
  550. {
  551. Longword L_Out;
  552. OP_COUNT(3); /* Complexity Count -- LT 6/96 */
  553. if (L_var1 == LW_MIN)
  554. {
  555. L_Out = LW_MAX;
  556. giOverflow = 1;
  557. }
  558. else
  559. {
  560. if (L_var1 < 0)
  561. L_Out = -L_var1;
  562. else
  563. L_Out = L_var1;
  564. }
  565. return (L_Out);
  566. }
  567. /***************************************************************************
  568. *
  569. * FUNCTION NAME: L_add
  570. *
  571. * PURPOSE:
  572. *
  573. * Perform the addition of the two 32 bit input variables with
  574. * saturation.
  575. *
  576. * INPUTS:
  577. *
  578. * L_var1
  579. * 32 bit long signed integer (Longword) whose value
  580. * falls in the range
  581. * 0x8000 0000 <= L_var1 <= 0x7fff ffff.
  582. * L_var2
  583. * 32 bit long signed integer (Longword) whose value
  584. * falls in the range
  585. * 0x8000 0000 <= L_var2 <= 0x7fff ffff.
  586. *
  587. * OUTPUTS:
  588. *
  589. * none
  590. *
  591. * RETURN VALUE:
  592. *
  593. * L_Out
  594. * 32 bit long signed integer (Longword) whose value
  595. * falls in the range
  596. * 0x8000 0000 <= L_var1 <= 0x7fff ffff.
  597. *
  598. * IMPLEMENTATION:
  599. *
  600. * Perform the addition of the two 32 bit input variables with
  601. * saturation.
  602. *
  603. * L_Out = L_var1 + L_var2
  604. *
  605. * L_Out is set to 0x7fff ffff if the operation results in an
  606. * overflow. L_Out is set to 0x8000 0000 if the operation
  607. * results in an underflow.
  608. *
  609. * KEYWORDS: add, addition
  610. *
  611. *************************************************************************/
  612. Longword L_add(Longword L_var1, Longword L_var2)
  613. {
  614. Longword L_Sum;
  615. double dSum;
  616. OP_COUNT(2); /* Complexity Count -- LT 6/96 */
  617. dSum = (double) L_var1 + (double) L_var2;
  618. L_Sum = L_var1 + L_var2;
  619. if (dSum != (double) L_Sum)
  620. {
  621. /* overflow occurred */
  622. L_Sum = L_saturate(dSum); OP_COUNT(-4);
  623. }
  624. return (L_Sum);
  625. }
  626. /***************************************************************************
  627. *
  628. * FUNCTION NAME: L_deposit_h
  629. *
  630. * PURPOSE:
  631. *
  632. * Put the 16 bit input into the 16 MSB's of the output Longword. The
  633. * LS 16 bits are zeroed.
  634. *
  635. * INPUTS:
  636. *
  637. * var1
  638. * 16 bit short signed integer (Shortword) whose value
  639. * falls in the range 0xffff 8000 <= var1 <= 0x0000 7fff.
  640. *
  641. * OUTPUTS:
  642. *
  643. * none
  644. *
  645. * RETURN VALUE:
  646. *
  647. * L_Out
  648. * 32 bit long signed integer (Longword) whose value
  649. * falls in the range
  650. * 0x8000 0000 <= L_var1 <= 0x7fff 0000.
  651. *
  652. *
  653. * KEYWORDS: deposit, assign, fractional assign
  654. *
  655. *************************************************************************/
  656. Longword L_deposit_h(Shortword var1)
  657. {
  658. Longword L_var2;
  659. OP_COUNT(1); /* Complexity Count -- LT 6/96 */
  660. L_var2 = (Longword) var1 << 16;
  661. return (L_var2);
  662. }
  663. /***************************************************************************
  664. *
  665. * FUNCTION NAME: L_deposit_l
  666. *
  667. * PURPOSE:
  668. *
  669. * Put the 16 bit input into the 16 LSB's of the output Longword with
  670. * sign extension i.e. the top 16 bits are set to either 0 or 0xffff.
  671. *
  672. * INPUTS:
  673. *
  674. * var1
  675. * 16 bit short signed integer (Shortword) whose value
  676. * falls in the range 0xffff 8000 <= var1 <= 0x0000 7fff.
  677. *
  678. * OUTPUTS:
  679. *
  680. * none
  681. *
  682. * RETURN VALUE:
  683. *
  684. * L_Out
  685. * 32 bit long signed integer (Longword) whose value
  686. * falls in the range
  687. * 0xffff 8000 <= L_var1 <= 0x0000 7fff.
  688. *
  689. * KEYWORDS: deposit, assign
  690. *
  691. *************************************************************************/
  692. Longword L_deposit_l(Shortword var1)
  693. {
  694. Longword L_Out;
  695. OP_COUNT(1); /* Complexity Count -- LT 6/96 */
  696. L_Out = var1;
  697. return (L_Out);
  698. }
  699. /***************************************************************************
  700. *
  701. * FUNCTION NAME: L_mac
  702. *
  703. * PURPOSE:
  704. *
  705. * Multiply accumulate. Fractionally multiply two 16 bit
  706. * numbers together with saturation. Add that result to the
  707. * 32 bit input with saturation. Return the 32 bit result.
  708. *
  709. * INPUTS:
  710. *
  711. * var1
  712. * 16 bit short signed integer (Shortword) whose value
  713. * falls in the range 0xffff 8000 <= var1 <= 0x0000 7fff.
  714. * var2
  715. * 16 bit short signed integer (Shortword) whose value
  716. * falls in the range 0xffff 8000 <= var2 <= 0x0000 7fff.
  717. * L_var3
  718. * 32 bit long signed integer (Longword) whose value
  719. * falls in the range
  720. * 0x8000 0000 <= L_var2 <= 0x7fff ffff.
  721. *
  722. * OUTPUTS:
  723. *
  724. * none
  725. *
  726. * RETURN VALUE:
  727. *
  728. * L_Out
  729. * 32 bit long signed integer (Longword) whose value
  730. * falls in the range
  731. * 0x8000 0000 <= L_var1 <= 0x7fff ffff.
  732. *
  733. * IMPLEMENTATION:
  734. *
  735. * Fractionally multiply two 16 bit numbers together with
  736. * saturation. The only numbers which will cause saturation on
  737. * the multiply are 0x8000 * 0x8000.
  738. *
  739. * Add that result to the 32 bit input with saturation.
  740. * Return the 32 bit result.
  741. *
  742. * Please note that this is not a true multiply accumulate as
  743. * most processors would implement it. The 0x8000*0x8000
  744. * causes and overflow for this instruction. On most
  745. * processors this would cause an overflow only if the 32 bit
  746. * input added to it were positive or zero.
  747. *
  748. * KEYWORDS: mac, multiply accumulate
  749. *
  750. *************************************************************************/
  751. Longword L_mac(Longword L_var3, Shortword var1, Shortword var2)
  752. {
  753. Longword L_product;
  754. double dlwA, dlwB;
  755. OP_COUNT(1); /* Complexity Count -- LT 6/96 */
  756. L_product = (Longword) var1 *var2; /* integer multiply */
  757. if (L_product == (Longword) 0x40000000)
  758. {
  759. /* the event 0x8000 * 0x8000, the only possible saturation
  760. * in the multiply */
  761. L_product = L_saturate(2147483648.0 + (double) L_var3); OP_COUNT(-4);
  762. }
  763. else
  764. {
  765. /* no overflow possible in mult */
  766. L_product = L_product << 1;
  767. L_product = L_add(L_var3, L_product); OP_COUNT(-2);
  768. }
  769. return (L_product);
  770. }
  771. /***************************************************************************
  772. *
  773. * FUNCTION NAME: L_msu
  774. *
  775. * PURPOSE:
  776. *
  777. * Multiply and subtract. Fractionally multiply two 16 bit
  778. * numbers together with saturation. Subtract that result from
  779. * the 32 bit input with saturation. Return the 32 bit result.
  780. *
  781. * INPUTS:
  782. *
  783. * var1
  784. * 16 bit short signed integer (Shortword) whose value
  785. * falls in the range 0xffff 8000 <= var1 <= 0x0000 7fff.
  786. * var2
  787. * 16 bit short signed integer (Shortword) whose value
  788. * falls in the range 0xffff 8000 <= var2 <= 0x0000 7fff.
  789. * L_var3
  790. * 32 bit long signed integer (Longword) whose value
  791. * falls in the range
  792. * 0x8000 0000 <= L_var2 <= 0x7fff ffff.
  793. *
  794. * OUTPUTS:
  795. *
  796. * none
  797. *
  798. * RETURN VALUE:
  799. *
  800. * L_Out
  801. * 32 bit long signed integer (Longword) whose value
  802. * falls in the range
  803. * 0x8000 0000 <= L_var1 <= 0x7fff ffff.
  804. *
  805. * IMPLEMENTATION:
  806. *
  807. * Fractionally multiply two 16 bit numbers together with
  808. * saturation. The only numbers which will cause saturation on
  809. * the multiply are 0x8000 * 0x8000.
  810. *
  811. * Subtract that result from the 32 bit input with saturation.
  812. * Return the 32 bit result.
  813. *
  814. * Please note that this is not a true multiply accumulate as
  815. * most processors would implement it. The 0x8000*0x8000
  816. * causes and overflow for this instruction. On most
  817. * processors this would cause an overflow only if the 32 bit
  818. * input added to it were negative or zero.
  819. *
  820. * KEYWORDS: mac, multiply accumulate, msu
  821. *
  822. *************************************************************************/
  823. Longword L_msu(Longword L_var3, Shortword var1, Shortword var2)
  824. {
  825. Longword L_product;
  826. double dlwA, dlwB;
  827. OP_COUNT(1); /* Complexity Count -- LT 6/96 */
  828. L_product = (Longword) var1 *var2; /* integer multiply */
  829. if (L_product == (Longword) 0x40000000)
  830. {
  831. /* the event 0x8000 * 0x8000, the only possible saturation
  832. * in the multiply */
  833. L_product = L_saturate((double) L_var3 - 2147483648.0); OP_COUNT(-4);
  834. }
  835. else
  836. {
  837. /* no overflow possible in mult */
  838. L_product <<= 1;
  839. L_product = L_sub(L_var3, L_product); OP_COUNT(-2); /* LT 6/96 */
  840. }
  841. return (L_product);
  842. }
  843. /***************************************************************************
  844. *
  845. * FUNCTION NAME: L_mult
  846. *
  847. * PURPOSE:
  848. *
  849. * Perform a fractional multipy of the two 16 bit input numbers
  850. * with saturation. Output a 32 bit number.
  851. *
  852. * INPUTS:
  853. *
  854. * var1
  855. * 16 bit short signed integer (Shortword) whose value
  856. * falls in the range 0xffff 8000 <= var1 <= 0x0000 7fff.
  857. * var2
  858. * 16 bit short signed integer (Shortword) whose value
  859. * falls in the range 0xffff 8000 <= var2 <= 0x0000 7fff.
  860. *
  861. * OUTPUTS:
  862. *
  863. * none
  864. *
  865. * RETURN VALUE:
  866. *
  867. * L_Out
  868. * 32 bit long signed integer (Longword) whose value
  869. * falls in the range
  870. * 0x8000 0000 <= L_var1 <= 0x7fff ffff.
  871. *
  872. * IMPLEMENTATION:
  873. *
  874. * Multiply the two the two 16 bit input numbers. If the
  875. * result is within this range, left shift the result by one
  876. * and output the 32 bit number. The only possible overflow
  877. * occurs when var1==var2==-0x8000. In this case output
  878. * 0x7fff ffff.
  879. *
  880. * KEYWORDS: multiply, mult, mpy
  881. *
  882. *************************************************************************/
  883. Longword L_mult(Shortword var1, Shortword var2)
  884. {
  885. Longword L_product;
  886. OP_COUNT(1); /* Complexity Count -- LT 6/96 */
  887. if (var1 == SW_MIN && var2 == SW_MIN)
  888. {
  889. L_product = LW_MAX; /* overflow */
  890. giOverflow = 1;
  891. }
  892. else
  893. {
  894. L_product = (Longword) var1 *var2; /* integer multiply */
  895. L_product = L_product << 1;
  896. }
  897. return (L_product);
  898. }
  899. /***************************************************************************
  900. *
  901. * FUNCTION NAME: L_negate
  902. *
  903. * PURPOSE:
  904. *
  905. * Negate the 32 bit input. 0x8000 0000's negated value is
  906. * 0x7fff ffff.
  907. *
  908. * INPUTS:
  909. *
  910. * L_var1
  911. * 32 bit long signed integer (Longword) whose value
  912. * falls in the range
  913. * 0x8000 0000 <= L_var1 <= 0x7fff ffff.
  914. *
  915. * OUTPUTS:
  916. *
  917. * none
  918. *
  919. * RETURN VALUE:
  920. *
  921. * L_Out
  922. * 32 bit long signed integer (Longword) whose value
  923. * falls in the range
  924. * 0x8000 0001 <= L_var1 <= 0x7fff ffff.
  925. *
  926. * KEYWORDS: negate, negative
  927. *
  928. *************************************************************************/
  929. Longword L_negate(Longword L_var1)
  930. {
  931. Longword L_Out;
  932. OP_COUNT(2); /* Complexity Count -- LT 6/96 */
  933. if (L_var1 == LW_MIN)
  934. {
  935. L_Out = LW_MAX;
  936. giOverflow = 1;
  937. }
  938. else
  939. L_Out = -L_var1;
  940. return (L_Out);
  941. }
  942. /***************************************************************************
  943. *
  944. * FUNCTION NAME: L_shift_r
  945. *
  946. * PURPOSE:
  947. *
  948. * Shift and round. Perform a shift right. After shifting, use
  949. * the last bit shifted out of the LSB to round the result up
  950. * or down.
  951. *
  952. * INPUTS:
  953. *
  954. * L_var1
  955. * 32 bit long signed integer (Longword) whose value
  956. * falls in the range
  957. * 0x8000 0000 <= L_var1 <= 0x7fff ffff.
  958. * var2
  959. * 16 bit short signed integer (Shortword) whose value
  960. * falls in the range 0xffff 8000 <= var2 <= 0x0000 7fff.
  961. *
  962. * OUTPUTS:
  963. *
  964. * none
  965. *
  966. * RETURN VALUE:
  967. *
  968. * L_var1
  969. * 32 bit long signed integer (Longword) whose value
  970. * falls in the range
  971. * 0x8000 0000 <= L_var1 <= 0x7fff ffff.
  972. *
  973. *
  974. * IMPLEMENTATION:
  975. *
  976. * Shift and round. Perform a shift right. After shifting, use
  977. * the last bit shifted out of the LSB to round the result up
  978. * or down. This is just like shift_r above except that the
  979. * input/output is 32 bits as opposed to 16.
  980. *
  981. * if var2 is positve perform a arithmetic left shift
  982. * with saturation (see L_shl() above).
  983. *
  984. * If var2 is zero simply return L_var1.
  985. *
  986. * If var2 is negative perform a arithmetic right shift (L_shr)
  987. * of L_var1 by (-var2)+1. Add the LS bit of the result to
  988. * L_var1 shifted right (L_shr) by -var2.
  989. *
  990. * Note that there is no constraint on var2, so if var2 is
  991. * -0xffff 8000 then -var2 is 0x0000 8000, not 0x0000 7fff.
  992. * This is the reason the L_shl function is used.
  993. *
  994. *
  995. * KEYWORDS:
  996. *
  997. *************************************************************************/
  998. Longword L_shift_r(Longword L_var1, Shortword var2)
  999. {
  1000. Longword L_Out, L_rnd;
  1001. OP_COUNT(3); /* Complexity Count -- LT 6/96 */
  1002. if (var2 < -31)
  1003. {
  1004. L_Out = 0;
  1005. }
  1006. else if (var2 < 0)
  1007. {
  1008. /* right shift */
  1009. L_rnd = L_shl(L_var1, var2 + 1) & 0x1; OP_COUNT(-2); /* LT 6/96 */
  1010. L_Out = L_add(L_shl(L_var1, var2), L_rnd); OP_COUNT(-2); /* LT 6/96 */
  1011. }
  1012. else
  1013. {
  1014. L_Out = L_shl(L_var1, var2); OP_COUNT(-2); /* LT 6/96 */
  1015. }
  1016. return (L_Out);
  1017. }
  1018. /***************************************************************************
  1019. *
  1020. * FUNCTION NAME: L_shl
  1021. *
  1022. * PURPOSE:
  1023. *
  1024. * Arithmetic shift left (or right).
  1025. * Arithmetically shift the input left by var2. If var2 is
  1026. * negative then an arithmetic shift right (L_shr) of L_var1 by
  1027. * -var2 is performed.
  1028. *
  1029. * INPUTS:
  1030. *
  1031. * var2
  1032. * 16 bit short signed integer (Shortword) whose value
  1033. * falls in the range 0xffff 8000 <= var2 <= 0x0000 7fff.
  1034. * L_var1
  1035. * 32 bit long signed integer (Longword) whose value
  1036. * falls in the range
  1037. * 0x8000 0000 <= L_var1 <= 0x7fff ffff.
  1038. * OUTPUTS:
  1039. *
  1040. * none
  1041. *
  1042. * RETURN VALUE:
  1043. *
  1044. * L_Out
  1045. * 32 bit long signed integer (Longword) whose value
  1046. * falls in the range
  1047. * 0x8000 0000 <= L_var1 <= 0x7fff ffff.
  1048. *
  1049. *
  1050. * IMPLEMENTATION:
  1051. *
  1052. * Arithmetically shift the 32 bit input left by var2. This
  1053. * operation maintains the sign of the input number. If var2 is
  1054. * negative then an arithmetic shift right (L_shr) of L_var1 by
  1055. * -var2 is performed. See description of L_shr for details.
  1056. *
  1057. * Equivalent to the Full-Rate GSM ">> n" operation. Note that
  1058. * ANSI-C does not guarantee operation of the C ">>" or "<<"
  1059. * operator for negative numbers.
  1060. *
  1061. * KEYWORDS: shift, arithmetic shift left,
  1062. *
  1063. *************************************************************************/
  1064. Longword L_shl(Longword L_var1, Shortword var2)
  1065. {
  1066. Longword L_Mask, L_Out;
  1067. int i, iOverflow = 0;
  1068. OP_COUNT(2); /* Complexity Count -- LT 6/96 */
  1069. if (var2 == 0 || L_var1 == 0)
  1070. {
  1071. L_Out = L_var1;
  1072. }
  1073. else if (var2 < 0)
  1074. {
  1075. if (var2 <= -31)
  1076. {
  1077. if (L_var1 > 0)
  1078. L_Out = 0;
  1079. else
  1080. L_Out = 0xffffffffL;
  1081. }
  1082. else
  1083. {
  1084. L_Out = L_shr(L_var1, -var2);
  1085. OP_COUNT(-2); /* Complexity Count -- LT 6/96 */
  1086. }
  1087. }
  1088. else
  1089. {
  1090. if (var2 >= 31)
  1091. iOverflow = 1;
  1092. else
  1093. {
  1094. if (L_var1 < 0)
  1095. L_Mask = LW_SIGN; /* sign bit mask */
  1096. else
  1097. L_Mask = 0x0;
  1098. L_Out = L_var1;
  1099. for (i = 0; i < var2 && !iOverflow; i++)
  1100. {
  1101. /* check the sign bit */
  1102. L_Out = (L_Out & 0x7fffffffL) << 1;
  1103. if ((L_Mask ^ L_Out) & LW_SIGN)
  1104. iOverflow = 1;
  1105. }
  1106. }
  1107. if (iOverflow)
  1108. {
  1109. /* saturate */
  1110. if (L_var1 > 0)
  1111. L_Out = LW_MAX;
  1112. else
  1113. L_Out = LW_MIN;
  1114. giOverflow = 1;
  1115. }
  1116. }
  1117. return (L_Out);
  1118. }
  1119. /***************************************************************************
  1120. *
  1121. * FUNCTION NAME: L_shr
  1122. *
  1123. * PURPOSE:
  1124. *
  1125. * Arithmetic shift right (or left).
  1126. * Arithmetically shift the input right by var2. If var2 is
  1127. * negative then an arithmetic shift left (shl) of var1 by
  1128. * -var2 is performed.
  1129. *
  1130. * INPUTS:
  1131. *
  1132. * var2
  1133. * 16 bit short signed integer (Shortword) whose value
  1134. * falls in the range 0xffff 8000 <= var2 <= 0x0000 7fff.
  1135. * L_var1
  1136. * 32 bit long signed integer (Longword) whose value
  1137. * falls in the range
  1138. * 0x8000 0000 <= L_var1 <= 0x7fff ffff.
  1139. * OUTPUTS:
  1140. *
  1141. * none
  1142. *
  1143. * RETURN VALUE:
  1144. *
  1145. * L_Out
  1146. * 32 bit long signed integer (Longword) whose value
  1147. * falls in the range
  1148. * 0x8000 0000 <= L_var1 <= 0x7fff ffff.
  1149. *
  1150. *
  1151. * IMPLEMENTATION:
  1152. *
  1153. * Arithmetically shift the input right by var2. This
  1154. * operation maintains the sign of the input number. If var2 is
  1155. * negative then an arithmetic shift left (shl) of L_var1 by
  1156. * -var2 is performed. See description of L_shl for details.
  1157. *
  1158. * The input is a 32 bit number, as is the output.
  1159. *
  1160. * Equivalent to the Full-Rate GSM ">> n" operation. Note that
  1161. * ANSI-C does not guarantee operation of the C ">>" or "<<"
  1162. * operator for negative numbers.
  1163. *
  1164. * KEYWORDS: shift, arithmetic shift right,
  1165. *
  1166. *************************************************************************/
  1167. Longword L_shr(Longword L_var1, Shortword var2)
  1168. {
  1169. Longword L_Mask, L_Out;
  1170. OP_COUNT(2); /* Complexity Count -- LT 6/96 */
  1171. if (var2 == 0 || L_var1 == 0)
  1172. {
  1173. L_Out = L_var1;
  1174. }
  1175. else if (var2 < 0)
  1176. {
  1177. /* perform a left shift */
  1178. /*----------------------*/
  1179. if (var2 <= -31)
  1180. {
  1181. /* saturate */
  1182. if (L_var1 > 0)
  1183. {
  1184. L_Out = LW_MAX;
  1185. giOverflow = 1;
  1186. }
  1187. else
  1188. {
  1189. L_Out = LW_MIN;
  1190. giOverflow = 1;
  1191. }
  1192. }
  1193. else
  1194. {
  1195. L_Out = L_shl(L_var1, -var2);
  1196. OP_COUNT(-2); /* Complexity Count -- LT 6/96 */
  1197. }
  1198. }
  1199. else
  1200. {
  1201. if (var2 >= 31)
  1202. {
  1203. if (L_var1 > 0)
  1204. L_Out = 0;
  1205. else
  1206. L_Out = 0xffffffffL;
  1207. }
  1208. else
  1209. {
  1210. L_Mask = 0;
  1211. if (L_var1 < 0)
  1212. {
  1213. L_Mask = ~L_Mask << (32 - var2);
  1214. }
  1215. L_var1 >>= var2;
  1216. L_Out = L_Mask | L_var1;
  1217. }
  1218. }
  1219. return (L_Out);
  1220. }
  1221. /***************************************************************************
  1222. *
  1223. * FUNCTION NAME: L_sub
  1224. *
  1225. * PURPOSE:
  1226. *
  1227. * Perform the subtraction of the two 32 bit input variables with
  1228. * saturation.
  1229. *
  1230. * INPUTS:
  1231. *
  1232. * L_var1
  1233. * 32 bit long signed integer (Longword) whose value
  1234. * falls in the range
  1235. * 0x8000 0000 <= L_var1 <= 0x7fff ffff.
  1236. * L_var2
  1237. * 32 bit long signed integer (Longword) whose value
  1238. * falls in the range
  1239. * 0x8000 0000 <= L_var2 <= 0x7fff ffff.
  1240. *
  1241. * OUTPUTS:
  1242. *
  1243. * none
  1244. *
  1245. * RETURN VALUE:
  1246. *
  1247. * L_Out
  1248. * 32 bit long signed integer (Longword) whose value
  1249. * falls in the range
  1250. * 0x8000 0000 <= L_var1 <= 0x7fff ffff.
  1251. *
  1252. * IMPLEMENTATION:
  1253. *
  1254. * Perform the subtraction of the two 32 bit input variables with
  1255. * saturation.
  1256. *
  1257. * L_Out = L_var1 - L_var2
  1258. *
  1259. * L_Out is set to 0x7fff ffff if the operation results in an
  1260. * overflow. L_Out is set to 0x8000 0000 if the operation
  1261. * results in an underflow.
  1262. *
  1263. * KEYWORDS: sub, subtraction
  1264. *
  1265. *************************************************************************/
  1266. Longword L_sub(Longword L_var1, Longword L_var2)
  1267. {
  1268. Longword L_Sum;
  1269. double dSum;
  1270. OP_COUNT(2); /* Complexity Count -- LT 6/96 */
  1271. dSum = (double) L_var1 - (double) L_var2;
  1272. L_Sum = L_var1 - L_var2;
  1273. if (dSum != L_Sum)
  1274. {
  1275. /* overflow occurred */
  1276. L_Sum = L_saturate(dSum);
  1277. OP_COUNT(-4); /* Complexity Count -- LT 6/96 */
  1278. }
  1279. return (L_Sum);
  1280. }
  1281. /***************************************************************************
  1282. *
  1283. * FUNCTION NAME:mac_r
  1284. *
  1285. * PURPOSE:
  1286. *
  1287. * Multiply accumulate and round. Fractionally multiply two 16
  1288. * bit numbers together with saturation. Add that result to
  1289. * the 32 bit input with saturation. Finally round the result
  1290. * into a 16 bit number.
  1291. *
  1292. *
  1293. * INPUTS:
  1294. *
  1295. * var1
  1296. * 16 bit short signed integer (Shortword) whose value
  1297. * falls in the range 0xffff 8000 <= var1 <= 0x0000 7fff.
  1298. * var2
  1299. * 16 bit short signed integer (Shortword) whose value
  1300. * falls in the range 0xffff 8000 <= var2 <= 0x0000 7fff.
  1301. * L_var3
  1302. * 32 bit long signed integer (Longword) whose value
  1303. * falls in the range
  1304. * 0x8000 0000 <= L_var2 <= 0x7fff ffff.
  1305. *
  1306. * OUTPUTS:
  1307. *
  1308. * none
  1309. *
  1310. * RETURN VALUE:
  1311. *
  1312. * swOut
  1313. * 16 bit short signed integer (Shortword) whose value
  1314. * falls in the range
  1315. * 0xffff 8000 <= swOut <= 0x0000 7fff.
  1316. *
  1317. * IMPLEMENTATION:
  1318. *
  1319. * Fractionally multiply two 16 bit numbers together with
  1320. * saturation. The only numbers which will cause saturation on
  1321. * the multiply are 0x8000 * 0x8000.
  1322. *
  1323. * Add that result to the 32 bit input with saturation.
  1324. * Round the 32 bit result by adding 0x0000 8000 to the input.
  1325. * The result may overflow due to the add. If so, the result
  1326. * is saturated. The 32 bit rounded number is then shifted
  1327. * down 16 bits and returned as a Shortword.
  1328. *
  1329. * Please note that this is not a true multiply accumulate as
  1330. * most processors would implement it. The 0x8000*0x8000
  1331. * causes and overflow for this instruction. On most
  1332. * processors this would cause an overflow only if the 32 bit
  1333. * input added to it were positive or zero.
  1334. *
  1335. * KEYWORDS: mac, multiply accumulate, macr
  1336. *
  1337. *************************************************************************/
  1338. Shortword mac_r(Longword L_var3, Shortword var1, Shortword var2)
  1339. {
  1340. return (round32(L_mac(L_var3, var1, var2)));
  1341. }
  1342. /***************************************************************************
  1343. *
  1344. * FUNCTION NAME: msu_r
  1345. *
  1346. * PURPOSE:
  1347. *
  1348. * Multiply subtract and round. Fractionally multiply two 16
  1349. * bit numbers together with saturation. Subtract that result from
  1350. * the 32 bit input with saturation. Finally round the result
  1351. * into a 16 bit number.
  1352. *
  1353. *
  1354. * INPUTS:
  1355. *
  1356. * var1
  1357. * 16 bit short signed integer (Shortword) whose value
  1358. * falls in the range 0xffff 8000 <= var1 <= 0x0000 7fff.
  1359. * var2
  1360. * 16 bit short signed integer (Shortword) whose value
  1361. * falls in the range 0xffff 8000 <= var2 <= 0x0000 7fff.
  1362. * L_var3
  1363. * 32 bit long signed integer (Longword) whose value
  1364. * falls in the range
  1365. * 0x8000 0000 <= L_var2 <= 0x7fff ffff.
  1366. *
  1367. * OUTPUTS:
  1368. *
  1369. * none
  1370. *
  1371. * RETURN VALUE:
  1372. *
  1373. * swOut
  1374. * 16 bit short signed integer (Shortword) whose value
  1375. * falls in the range
  1376. * 0xffff 8000 <= swOut <= 0x0000 7fff.
  1377. *
  1378. * IMPLEMENTATION:
  1379. *
  1380. * Fractionally multiply two 16 bit numbers together with
  1381. * saturation. The only numbers which will cause saturation on
  1382. * the multiply are 0x8000 * 0x8000.
  1383. *
  1384. * Subtract that result from the 32 bit input with saturation.
  1385. * Round the 32 bit result by adding 0x0000 8000 to the input.
  1386. * The result may overflow due to the add. If so, the result
  1387. * is saturated. The 32 bit rounded number is then shifted
  1388. * down 16 bits and returned as a Shortword.
  1389. *
  1390. * Please note that this is not a true multiply accumulate as
  1391. * most processors would implement it. The 0x8000*0x8000
  1392. * causes and overflow for this instruction. On most
  1393. * processors this would cause an overflow only if the 32 bit
  1394. * input added to it were positive or zero.
  1395. *
  1396. * KEYWORDS: mac, multiply accumulate, macr
  1397. *
  1398. *************************************************************************/
  1399. Shortword msu_r(Longword L_var3, Shortword var1, Shortword var2)
  1400. {
  1401. return (round32(L_msu(L_var3, var1, var2)));
  1402. }
  1403. /***************************************************************************
  1404. *
  1405. * FUNCTION NAME: mult
  1406. *
  1407. * PURPOSE:
  1408. *
  1409. * Perform a fractional multipy of the two 16 bit input numbers
  1410. * with saturation and truncation.
  1411. *
  1412. * INPUTS:
  1413. *
  1414. * var1
  1415. * 16 bit short signed integer (Shortword) whose value
  1416. * falls in the range 0xffff 8000 <= var1 <= 0x0000 7fff.
  1417. * var2
  1418. * 16 bit short signed integer (Shortword) whose value
  1419. * falls in the range 0xffff 8000 <= var2 <= 0x0000 7fff.
  1420. *
  1421. * OUTPUTS:
  1422. *
  1423. * none
  1424. *
  1425. * RETURN VALUE:
  1426. *
  1427. * swOut
  1428. * 16 bit short signed integer (Shortword) whose value
  1429. * falls in the range
  1430. * 0xffff 8000 <= swOut <= 0x0000 7fff.
  1431. *
  1432. * IMPLEMENTATION:
  1433. *
  1434. * Perform a fractional multipy of the two 16 bit input
  1435. * numbers. If var1 == var2 == -0x8000, output 0x7fff.
  1436. * Otherwise output var1*var2 >> 15. The output is a
  1437. * 16 bit number.
  1438. *
  1439. * KEYWORDS: mult, mulitply, mpy
  1440. *
  1441. *************************************************************************/
  1442. Shortword mult(Shortword var1, Shortword var2)
  1443. {
  1444. Longword L_product;
  1445. Shortword swOut;
  1446. OP_COUNT(1); /* Complexity Count -- LT 6/96 */
  1447. L_product = L_mult(var1, var2); OP_COUNT(-1);
  1448. swOut = extract_h(L_product); OP_COUNT(-1);
  1449. return (swOut);
  1450. }
  1451. /***************************************************************************
  1452. *
  1453. * FUNCTION NAME: mult_r
  1454. *
  1455. * PURPOSE:
  1456. *
  1457. * Perform a fractional multipy and round of the two 16 bit
  1458. * input numbers with saturation.
  1459. *
  1460. * INPUTS:
  1461. *
  1462. * var1
  1463. * 16 bit short signed integer (Shortword) whose value
  1464. * falls in the range 0xffff 8000 <= var1 <= 0x0000 7fff.
  1465. * var2
  1466. * 16 bit short signed integer (Shortword) whose value
  1467. * falls in the range 0xffff 8000 <= var2 <= 0x0000 7fff.
  1468. *
  1469. * OUTPUTS:
  1470. *
  1471. * none
  1472. *
  1473. * RETURN VALUE:
  1474. *
  1475. * swOut
  1476. * 16 bit short signed integer (Shortword) whose value
  1477. * falls in the range
  1478. * 0xffff 8000 <= swOut <= 0x0000 7fff.
  1479. *
  1480. * IMPLEMENTATION:
  1481. *
  1482. * This routine is defined as the concatenation of the multiply
  1483. * operation and the round operation.
  1484. *
  1485. * The fractional multiply (L_mult) produces a saturated 32 bit
  1486. * output. This is followed by a an add of 0x0000 8000 to the
  1487. * 32 bit result. The result may overflow due to the add. If
  1488. * so, the result is saturated. The 32 bit rounded number is
  1489. * then shifted down 16 bits and returned as a Shortword.
  1490. *
  1491. *
  1492. * KEYWORDS: multiply and round, round, mult_r, mpyr
  1493. *
  1494. *************************************************************************/
  1495. Shortword mult_r(Shortword var1, Shortword var2)
  1496. {
  1497. Shortword swOut;
  1498. OP_COUNT(2); /* Complexity Count -- LT 6/96 */
  1499. swOut = round32(L_mult(var1, var2));
  1500. OP_COUNT(-2); /* Complexity Count -- LT 6/96 */
  1501. return (swOut);
  1502. }
  1503. /***************************************************************************
  1504. *
  1505. * FUNCTION NAME: negate
  1506. *
  1507. * PURPOSE:
  1508. *
  1509. * Negate the 16 bit input. 0x8000's negated value is 0x7fff.
  1510. *
  1511. * INPUTS:
  1512. *
  1513. * var1
  1514. * 16 bit short signed integer (Shortword) whose value
  1515. * falls in the range 0xffff 8000 <= var1 <= 0x0000 7fff.
  1516. *
  1517. * OUTPUTS:
  1518. *
  1519. * none
  1520. *
  1521. * RETURN VALUE:
  1522. *
  1523. * swOut
  1524. * 16 bit short signed integer (Shortword) whose value
  1525. * falls in the range
  1526. * 0xffff 8001 <= swOut <= 0x0000 7fff.
  1527. *
  1528. * KEYWORDS: negate, negative, invert
  1529. *
  1530. *************************************************************************/
  1531. Shortword negate(Shortword var1)
  1532. {
  1533. Shortword swOut;
  1534. OP_COUNT(1); /* Complexity Count -- LT 6/96 */
  1535. if (var1 == SW_MIN)
  1536. {
  1537. swOut = SW_MAX;
  1538. giOverflow = 1;
  1539. }
  1540. else
  1541. swOut = -var1;
  1542. return (swOut);
  1543. }
  1544. /***************************************************************************
  1545. *
  1546. * FUNCTION NAME: norm_l
  1547. *
  1548. * PURPOSE:
  1549. *
  1550. * Get normalize shift count:
  1551. *
  1552. * A 32 bit number is input (possiblly unnormalized). Output
  1553. * the positive (or zero) shift count required to normalize the
  1554. * input.
  1555. *
  1556. * INPUTS:
  1557. *
  1558. * L_var1
  1559. * 32 bit long signed integer (Longword) whose value
  1560. * falls in the range
  1561. * 0x8000 0000 <= L_var1 <= 0x7fff ffff.
  1562. *
  1563. * OUTPUTS:
  1564. *
  1565. * none
  1566. *
  1567. * RETURN VALUE:
  1568. *
  1569. * swOut
  1570. * 16 bit short signed integer (Shortword) whose value
  1571. * falls in the range
  1572. * 0 <= swOut <= 31
  1573. *
  1574. *
  1575. *
  1576. * IMPLEMENTATION:
  1577. *
  1578. * Get normalize shift count:
  1579. *
  1580. * A 32 bit number is input (possiblly unnormalized). Output
  1581. * the positive (or zero) shift count required to normalize the
  1582. * input.
  1583. *
  1584. * If zero in input, return 0 as the shift count.
  1585. *
  1586. * For non-zero numbers, count the number of left shift
  1587. * required to get the number to fall into the range:
  1588. *
  1589. * 0x4000 0000 >= normlzd number >= 0x7fff ffff (positive number)
  1590. * or
  1591. * 0x8000 0000 <= normlzd number < 0xc000 0000 (negative number)
  1592. *
  1593. * Return the number of shifts.
  1594. *
  1595. * This instruction corresponds exactly to the Full-Rate "norm"
  1596. * instruction.
  1597. *
  1598. * KEYWORDS: norm, normalization
  1599. *
  1600. *************************************************************************/
  1601. Shortword norm_l(Longword L_var1)
  1602. {
  1603. Shortword swShiftCnt;
  1604. // wprintf("sizeof(Longword) = %u sizeof(Shortword) = %u\n",sizeof(Longword),sizeof(Shortword));
  1605. // wprintf("LW_MIN=%d(%x)\n",LW_MIN,LW_MIN);
  1606. // wprintf("0xc0000000 = %d\n",0xc0000000);
  1607. // wprintf("L_var1=%d(%x)\n",L_var1,L_var1);
  1608. // wprintf("L_var1 < (Longword) 0xc0000000L = %s\n",( L_var1 < (Longword) 0xc0000000L ? "true" : "false"));
  1609. // wprintf("L_var1 >= LW_MIN = %s\n",( L_var1 >= LW_MIN ? "true" : "false"));
  1610. OP_COUNT(30); /* Complexity Count -- LT 6/96 */
  1611. if (L_var1 != 0)
  1612. {
  1613. if (!(L_var1 & LW_SIGN))
  1614. {
  1615. /* positive input */
  1616. for (swShiftCnt = 0; !(L_var1 <= LW_MAX && L_var1 >= 0x40000000);
  1617. swShiftCnt++)
  1618. {
  1619. L_var1 = L_var1 << 1;
  1620. }
  1621. }
  1622. else
  1623. {
  1624. /* negative input */
  1625. for (swShiftCnt = 0;
  1626. !(L_var1 >= LW_MIN && L_var1 < (Longword) 0xc0000000);
  1627. swShiftCnt++)
  1628. {
  1629. L_var1 = L_var1 << 1;
  1630. }
  1631. }
  1632. }
  1633. else
  1634. {
  1635. swShiftCnt = 0;
  1636. }
  1637. // wprintf("norm_l end\n");
  1638. return (swShiftCnt);
  1639. }
  1640. /***************************************************************************
  1641. *
  1642. * FUNCTION NAME: norm_s
  1643. *
  1644. * PURPOSE:
  1645. *
  1646. * Get normalize shift count:
  1647. *
  1648. * A 16 bit number is input (possiblly unnormalized). Output
  1649. * the positive (or zero) shift count required to normalize the
  1650. * input.
  1651. *
  1652. * INPUTS:
  1653. *
  1654. * var1
  1655. * 16 bit short signed integer (Shortword) whose value
  1656. * falls in the range 0xffff 8000 <= var1 <= 0x0000 7fff.
  1657. *
  1658. * OUTPUTS:
  1659. *
  1660. * none
  1661. *
  1662. * RETURN VALUE:
  1663. * swOut
  1664. * 16 bit short signed integer (Shortword) whose value
  1665. * falls in the range
  1666. * 0 <= swOut <= 15
  1667. *
  1668. *
  1669. *
  1670. * IMPLEMENTATION:
  1671. *
  1672. * Get normalize shift count:
  1673. *
  1674. * A 16 bit number is input (possiblly unnormalized). Output
  1675. * the positive (or zero) shift count required to normalize the
  1676. * input.
  1677. *
  1678. * If zero in input, return 0 as the shift count.
  1679. *
  1680. * For non-zero numbers, count the number of left shift
  1681. * required to get the number to fall into the range:
  1682. *
  1683. * 0x4000 >= normlzd number >= 0x7fff (positive number)
  1684. * or
  1685. * 0x8000 <= normlzd number < 0xc000 (negative number)
  1686. *
  1687. * Return the number of shifts.
  1688. *
  1689. * This instruction corresponds exactly to the Full-Rate "norm"
  1690. * instruction.
  1691. *
  1692. * KEYWORDS: norm, normalization
  1693. *
  1694. *************************************************************************/
  1695. Shortword norm_s(Shortword var1)
  1696. {
  1697. short swShiftCnt;
  1698. Longword L_var1;
  1699. OP_COUNT(15); /* Complexity Count -- LT 6/96 */
  1700. L_var1 = L_deposit_h(var1); OP_COUNT(-1);
  1701. swShiftCnt = norm_l(L_var1); OP_COUNT(-30);
  1702. return (swShiftCnt);
  1703. }
  1704. /****************************************************************************
  1705. *
  1706. * FUNCTION NAME: popOverflow
  1707. *
  1708. * PURPOSE:
  1709. *
  1710. * Pull the old overflow state from the "stack". Replace the current
  1711. * overflow status with its predecessor.
  1712. *
  1713. * INPUTS:
  1714. *
  1715. * none
  1716. *
  1717. *
  1718. * OUTPUTS: none
  1719. *
  1720. * RETURN VALUE: value of datum about the be lost (usually the
  1721. * temporary saturation state)
  1722. *
  1723. * KEYWORDS: saturation, limit, overflow
  1724. *
  1725. ***************************************************************************/
  1726. int popOverflow(void)
  1727. {
  1728. int i;
  1729. i = giOverflow;
  1730. giOverflow = giOldOverflow;
  1731. return (i);
  1732. }
  1733. /***************************************************************************
  1734. *
  1735. * FUNCTION NAME: round
  1736. *
  1737. * PURPOSE:
  1738. *
  1739. * Round the 32 bit Longword into a 16 bit shortword with saturation.
  1740. *
  1741. * INPUTS:
  1742. *
  1743. * L_var1
  1744. * 32 bit long signed integer (Longword) whose value
  1745. * falls in the range
  1746. * 0x8000 0000 <= L_var1 <= 0x7fff ffff.
  1747. * OUTPUTS:
  1748. *
  1749. * none
  1750. *
  1751. * RETURN VALUE:
  1752. *
  1753. * swOut
  1754. * 16 bit short signed integer (Shortword) whose value
  1755. * falls in the range
  1756. * 0xffff 8000 <= swOut <= 0x0000 7fff.
  1757. *
  1758. * IMPLEMENTATION:
  1759. *
  1760. * Perform a two's complement round on the input Longword with
  1761. * saturation.
  1762. *
  1763. * This is equivalent to adding 0x0000 8000 to the input. The
  1764. * result may overflow due to the add. If so, the result is
  1765. * saturated. The 32 bit rounded number is then shifted down
  1766. * 16 bits and returned as a Shortword.
  1767. *
  1768. *
  1769. * KEYWORDS: round
  1770. *
  1771. *************************************************************************/
  1772. Shortword round32(Longword L_var1)
  1773. {
  1774. Longword L_Prod;
  1775. OP_COUNT(1);
  1776. L_Prod = L_add(L_var1, 0x00008000L); /* round MSP */
  1777. OP_COUNT(-3);
  1778. return (extract_h(L_Prod));
  1779. }
  1780. /****************************************************************************
  1781. *
  1782. * FUNCTION NAME: set overflow
  1783. *
  1784. * PURPOSE:
  1785. *
  1786. * Clear the overflow flag
  1787. *
  1788. * INPUTS:
  1789. *
  1790. * none
  1791. *
  1792. *
  1793. * OUTPUTS: global overflow flag is cleared
  1794. * previous value stored in giOldOverflow
  1795. *
  1796. * RETURN VALUE: previous value of overflow
  1797. *
  1798. *
  1799. * KEYWORDS: saturation, limit, overflow
  1800. *
  1801. ***************************************************************************/
  1802. int setOverflow(void)
  1803. {
  1804. giOldOverflow = giOverflow;
  1805. giOverflow = 1;
  1806. return (giOldOverflow);
  1807. }
  1808. /***************************************************************************
  1809. *
  1810. * FUNCTION NAME: shift_r
  1811. *
  1812. * PURPOSE:
  1813. *
  1814. * Shift and round. Perform a shift right. After shifting, use
  1815. * the last bit shifted out of the LSB to round the result up
  1816. * or down.
  1817. *
  1818. * INPUTS:
  1819. *
  1820. * var1
  1821. * 16 bit short signed integer (Shortword) whose value
  1822. * falls in the range 0xffff 8000 <= var1 <= 0x0000 7fff.
  1823. * var2
  1824. * 16 bit short signed integer (Shortword) whose value
  1825. * falls in the range 0xffff 8000 <= var2 <= 0x0000 7fff.
  1826. *
  1827. * OUTPUTS:
  1828. *
  1829. * none
  1830. *
  1831. * RETURN VALUE:
  1832. *
  1833. * swOut
  1834. * 16 bit short signed integer (Shortword) whose value
  1835. * falls in the range
  1836. * 0xffff 8000 <= swOut <= 0x0000 7fff.
  1837. *
  1838. *
  1839. * IMPLEMENTATION:
  1840. *
  1841. * Shift and round. Perform a shift right. After shifting, use
  1842. * the last bit shifted out of the LSB to round the result up
  1843. * or down.
  1844. *
  1845. * If var2 is positive perform a arithmetic left shift
  1846. * with saturation (see shl() above).
  1847. *
  1848. * If var2 is zero simply return var1.
  1849. *
  1850. * If var2 is negative perform a arithmetic right shift (shr)
  1851. * of var1 by (-var2)+1. Add the LS bit of the result to var1
  1852. * shifted right (shr) by -var2.
  1853. *
  1854. * Note that there is no constraint on var2, so if var2 is
  1855. * -0xffff 8000 then -var2 is 0x0000 8000, not 0x0000 7fff.
  1856. * This is the reason the shl function is used.
  1857. *
  1858. *
  1859. * KEYWORDS:
  1860. *
  1861. *************************************************************************/
  1862. Shortword shift_r(Shortword var1, Shortword var2)
  1863. {
  1864. Shortword swOut, swRnd;
  1865. OP_COUNT(2);
  1866. if (var2 >= 0)
  1867. {
  1868. swOut = shl(var1, var2); OP_COUNT(-1);
  1869. }
  1870. else
  1871. {
  1872. /* right shift */
  1873. if (var2 < -15)
  1874. {
  1875. swOut = 0;
  1876. }
  1877. else
  1878. {
  1879. swRnd = shl(var1, var2 + 1) & 0x1; OP_COUNT(-1);
  1880. swOut = add(shl(var1, var2), swRnd); OP_COUNT(-2);
  1881. }
  1882. }
  1883. return (swOut);
  1884. }
  1885. /***************************************************************************
  1886. *
  1887. * FUNCTION NAME: shl
  1888. *
  1889. * PURPOSE:
  1890. *
  1891. * Arithmetically shift the input left by var2.
  1892. *
  1893. *
  1894. * INPUTS:
  1895. *
  1896. * var1
  1897. * 16 bit short signed integer (Shortword) whose value
  1898. * falls in the range 0xffff 8000 <= var1 <= 0x0000 7fff.
  1899. * var2
  1900. * 16 bit short signed integer (Shortword) whose value
  1901. * falls in the range 0xffff 8000 <= var2 <= 0x0000 7fff.
  1902. *
  1903. * OUTPUTS:
  1904. *
  1905. * none
  1906. *
  1907. * RETURN VALUE:
  1908. *
  1909. * swOut
  1910. * 16 bit short signed integer (Shortword) whose value
  1911. * falls in the range
  1912. * 0xffff 8000 <= swOut <= 0x0000 7fff.
  1913. *
  1914. * IMPLEMENTATION:
  1915. *
  1916. * If Arithmetically shift the input left by var2. If var2 is
  1917. * negative then an arithmetic shift right (shr) of var1 by
  1918. * -var2 is performed. See description of shr for details.
  1919. * When an arithmetic shift left is performed the var2 LS bits
  1920. * are zero filled.
  1921. *
  1922. * The only exception is if the left shift causes an overflow
  1923. * or underflow. In this case the LS bits are not modified.
  1924. * The number returned is 0x8000 in the case of an underflow or
  1925. * 0x7fff in the case of an overflow.
  1926. *
  1927. * The shl is equivalent to the Full-Rate GSM "<< n" operation.
  1928. * Note that ANSI-C does not guarantee operation of the C ">>"
  1929. * or "<<" operator for negative numbers - it is not specified
  1930. * whether this shift is an arithmetic or logical shift.
  1931. *
  1932. * KEYWORDS: asl, arithmetic shift left, shift
  1933. *
  1934. *************************************************************************/
  1935. Shortword shl(Shortword var1, Shortword var2)
  1936. {
  1937. Shortword swOut;
  1938. Longword L_Out;
  1939. OP_COUNT(1);
  1940. if (var2 == 0 || var1 == 0)
  1941. {
  1942. swOut = var1;
  1943. }
  1944. else if (var2 < 0)
  1945. {
  1946. /* perform a right shift */
  1947. /*-----------------------*/
  1948. if (var2 <= -15)
  1949. {
  1950. if (var1 < 0)
  1951. swOut = (Shortword) 0xffff;
  1952. else
  1953. swOut = 0x0;
  1954. }
  1955. else
  1956. {
  1957. swOut = shr(var1, -var2); OP_COUNT(-1);
  1958. }
  1959. }
  1960. else
  1961. {
  1962. /* var2 > 0 */
  1963. if (var2 >= 15)
  1964. {
  1965. /* saturate */
  1966. if (var1 > 0)
  1967. swOut = SW_MAX;
  1968. else
  1969. swOut = SW_MIN;
  1970. giOverflow = 1;
  1971. }
  1972. else
  1973. {
  1974. L_Out = (Longword) var1 *(1 << var2);
  1975. swOut = (Shortword) L_Out; /* copy low portion to swOut, overflow
  1976. * could have hpnd */
  1977. if (swOut != L_Out)
  1978. {
  1979. /* overflow */
  1980. if (var1 > 0)
  1981. swOut = SW_MAX; /* saturate */
  1982. else
  1983. swOut = SW_MIN; /* saturate */
  1984. giOverflow = 1;
  1985. }
  1986. }
  1987. }
  1988. return (swOut);
  1989. }
  1990. /***************************************************************************
  1991. *
  1992. * FUNCTION NAME: shr
  1993. *
  1994. * PURPOSE:
  1995. *
  1996. * Arithmetic shift right (or left).
  1997. * Arithmetically shift the input right by var2. If var2 is
  1998. * negative then an arithmetic shift left (shl) of var1 by
  1999. * -var2 is performed.
  2000. *
  2001. * INPUTS:
  2002. *
  2003. * var1
  2004. * 16 bit short signed integer (Shortword) whose value
  2005. * falls in the range 0xffff 8000 <= var1 <= 0x0000 7fff.
  2006. * var2
  2007. * 16 bit short signed integer (Shortword) whose value
  2008. * falls in the range 0xffff 8000 <= var2 <= 0x0000 7fff.
  2009. *
  2010. * OUTPUTS:
  2011. *
  2012. * none
  2013. *
  2014. * RETURN VALUE:
  2015. *
  2016. * swOut
  2017. * 16 bit short signed integer (Shortword) whose value
  2018. * falls in the range
  2019. * 0xffff 8000 <= swOut <= 0x0000 7fff.
  2020. *
  2021. * IMPLEMENTATION:
  2022. *
  2023. * Arithmetically shift the input right by var2. This
  2024. * operation maintains the sign of the input number. If var2 is
  2025. * negative then an arithmetic shift left (shl) of var1 by
  2026. * -var2 is performed. See description of shl for details.
  2027. *
  2028. * Equivalent to the Full-Rate GSM ">> n" operation. Note that
  2029. * ANSI-C does not guarantee operation of the C ">>" or "<<"
  2030. * operator for negative numbers.
  2031. *
  2032. * KEYWORDS: shift, arithmetic shift right,
  2033. *
  2034. *************************************************************************/
  2035. Shortword shr(Shortword var1, Shortword var2)
  2036. {
  2037. Shortword swMask, swOut;
  2038. OP_COUNT(1);
  2039. if (var2 == 0 || var1 == 0)
  2040. swOut = var1;
  2041. else if (var2 < 0)
  2042. {
  2043. /* perform an arithmetic left shift */
  2044. /*----------------------------------*/
  2045. if (var2 <= -15)
  2046. {
  2047. /* saturate */
  2048. if (var1 > 0)
  2049. swOut = SW_MAX;
  2050. else
  2051. swOut = SW_MIN;
  2052. giOverflow = 1;
  2053. }
  2054. else
  2055. {
  2056. swOut = shl(var1, -var2); OP_COUNT(-1);
  2057. }
  2058. }
  2059. else
  2060. {
  2061. /* positive shift count */
  2062. /*----------------------*/
  2063. if (var2 >= 15)
  2064. {
  2065. if (var1 < 0)
  2066. swOut = (Shortword) 0xffff;
  2067. else
  2068. swOut = 0x0;
  2069. }
  2070. else
  2071. {
  2072. /* take care of sign extension */
  2073. /*-----------------------------*/
  2074. swMask = 0;
  2075. if (var1 < 0)
  2076. {
  2077. swMask = ~swMask << (16 - var2);
  2078. }
  2079. var1 >>= var2;
  2080. swOut = swMask | var1;
  2081. }
  2082. }
  2083. return (swOut);
  2084. }
  2085. /***************************************************************************
  2086. *
  2087. * FUNCTION NAME: sub
  2088. *
  2089. * PURPOSE:
  2090. *
  2091. * Perform the subtraction of the two 16 bit input variable with
  2092. * saturation.
  2093. *
  2094. * INPUTS:
  2095. *
  2096. * var1
  2097. * 16 bit short signed integer (Shortword) whose value
  2098. * falls in the range 0xffff 8000 <= var1 <= 0x0000 7fff.
  2099. * var2
  2100. * 16 bit short signed integer (Shortword) whose value
  2101. * falls in the range 0xffff 8000 <= var2 <= 0x0000 7fff.
  2102. *
  2103. * OUTPUTS:
  2104. *
  2105. * none
  2106. *
  2107. * RETURN VALUE:
  2108. *
  2109. * swOut
  2110. * 16 bit short signed integer (Shortword) whose value
  2111. * falls in the range
  2112. * 0xffff 8000 <= swOut <= 0x0000 7fff.
  2113. *
  2114. * IMPLEMENTATION:
  2115. *
  2116. * Perform the subtraction of the two 16 bit input variable with
  2117. * saturation.
  2118. *
  2119. * swOut = var1 - var2
  2120. *
  2121. * swOut is set to 0x7fff if the operation results in an
  2122. * overflow. swOut is set to 0x8000 if the operation results
  2123. * in an underflow.
  2124. *
  2125. * KEYWORDS: sub, subtraction
  2126. *
  2127. *************************************************************************/
  2128. Shortword sub(Shortword var1, Shortword var2)
  2129. {
  2130. Longword L_diff;
  2131. Shortword swOut;
  2132. OP_COUNT(1);
  2133. L_diff = (Longword) var1 - var2;
  2134. swOut = saturate(L_diff);
  2135. return (swOut);
  2136. }