basic_op.c 88 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835
  1. #include "config.h"
  2. #if !PJMEDIA_LIBG7221_FUNCS_INLINED || \
  3. (PJMEDIA_LIBG7221_FUNCS_INLINED && defined(__BASIC_OP_H__))
  4. /*___________________________________________________________________________
  5. | |
  6. | Basic arithmetic operators. |
  7. |___________________________________________________________________________|
  8. */
  9. /*___________________________________________________________________________
  10. | |
  11. | Include-Files |
  12. |___________________________________________________________________________|
  13. */
  14. //#include <stdio.h>
  15. //#include <stdlib.h>
  16. #include "typedef.h"
  17. #include "basic_op.h"
  18. #include <pj/assert.h>
  19. #if (WMOPS)
  20. #include "count.h"
  21. extern BASIC_OP multiCounter[MAXCOUNTERS];
  22. extern int currCounter;
  23. #endif
  24. /*___________________________________________________________________________
  25. | |
  26. | Constants and Globals |
  27. |___________________________________________________________________________|
  28. */
  29. #if INCLUDE_UNSAFE
  30. Flag g7221_Overflow = 0;
  31. Flag g7221_Carry = 0;
  32. #endif
  33. /*___________________________________________________________________________
  34. | |
  35. | Functions |
  36. |___________________________________________________________________________|
  37. */
  38. /*___________________________________________________________________________
  39. | |
  40. | Function Name : shr |
  41. | |
  42. | Purpose : |
  43. | |
  44. | Arithmetically shift the 16 bit input var1 right var2 positions with |
  45. | sign extension. If var2 is negative, arithmetically shift var1 left by |
  46. | -var2 with sign extension. Saturate the result in case of underflows or |
  47. | overflows. |
  48. | |
  49. | Complexity weight : 1 |
  50. | |
  51. | Inputs : |
  52. | |
  53. | var1 |
  54. | 16 bit short signed integer (Word16) whose value falls in the |
  55. | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
  56. | |
  57. | var2 |
  58. | 16 bit short signed integer (Word16) whose value falls in the |
  59. | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
  60. | |
  61. | Outputs : |
  62. | |
  63. | none |
  64. | |
  65. | Return Value : |
  66. | |
  67. | var_out |
  68. | 16 bit short signed integer (Word16) whose value falls in the |
  69. | range : 0xffff 8000 <= var_out <= 0x0000 7fff. |
  70. |___________________________________________________________________________|
  71. */
  72. LIBG7221_DEF(Word16) shr (Word16 var1, Word16 var2)
  73. {
  74. if (var2 < 0)
  75. {
  76. if (var2 < -16)
  77. var2 = -16;
  78. return shl_nocheck(var1, (Word16) -var2);
  79. }
  80. else
  81. {
  82. return shr_nocheck(var1, var2);
  83. }
  84. }
  85. /* ------------------------- End of shr() ------------------------- */
  86. /*___________________________________________________________________________
  87. | |
  88. | Function Name : shl |
  89. | |
  90. | Purpose : |
  91. | |
  92. | Arithmetically shift the 16 bit input var1 left var2 positions.Zero fill|
  93. | the var2 LSB of the result. If var2 is negative, arithmetically shift |
  94. | var1 right by -var2 with sign extension. Saturate the result in case of |
  95. | underflows or overflows. |
  96. | |
  97. | Complexity weight : 1 |
  98. | |
  99. | Inputs : |
  100. | |
  101. | var1 |
  102. | 16 bit short signed integer (Word16) whose value falls in the |
  103. | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
  104. | |
  105. | var2 |
  106. | 16 bit short signed integer (Word16) whose value falls in the |
  107. | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
  108. | |
  109. | Outputs : |
  110. | |
  111. | none |
  112. | |
  113. | Return Value : |
  114. | |
  115. | var_out |
  116. | 16 bit short signed integer (Word16) whose value falls in the |
  117. | range : 0xffff 8000 <= var_out <= 0x0000 7fff. |
  118. |___________________________________________________________________________|
  119. */
  120. LIBG7221_DEF(Word16) shl (Word16 var1, Word16 var2)
  121. {
  122. if (var2 < 0)
  123. {
  124. return shr_nocheck(var1, (Word16) -var2);
  125. }
  126. else
  127. {
  128. return shl_nocheck(var1, var2);
  129. }
  130. }
  131. /* ------------------------- End of shl() ------------------------- */
  132. /*___________________________________________________________________________
  133. | |
  134. | Function Name : mult |
  135. | |
  136. | Purpose : |
  137. | |
  138. | Performs the multiplication of var1 by var2 and gives a 16 bit result |
  139. | which is scaled i.e.: |
  140. | mult(var1,var2) = extract_l(L_shr((var1 times var2),15)) and |
  141. | mult(-32768,-32768) = 32767. |
  142. | |
  143. | Complexity weight : 1 |
  144. | |
  145. | Inputs : |
  146. | |
  147. | var1 |
  148. | 16 bit short signed integer (Word16) whose value falls in the |
  149. | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
  150. | |
  151. | var2 |
  152. | 16 bit short signed integer (Word16) whose value falls in the |
  153. | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
  154. | |
  155. | Outputs : |
  156. | |
  157. | none |
  158. | |
  159. | Return Value : |
  160. | |
  161. | var_out |
  162. | 16 bit short signed integer (Word16) whose value falls in the |
  163. | range : 0xffff 8000 <= var_out <= 0x0000 7fff. |
  164. |___________________________________________________________________________|
  165. */
  166. LIBG7221_DEF(Word16) mult (Word16 var1, Word16 var2)
  167. {
  168. Word16 var_out;
  169. Word32 L_product;
  170. L_product = (Word32) var1 *(Word32) var2;
  171. L_product = (L_product & (Word32) 0xffff8000L) >> 15;
  172. if (L_product & (Word32) 0x00010000L)
  173. L_product = L_product | (Word32) 0xffff0000L;
  174. var_out = saturate (L_product);
  175. #if (WMOPS)
  176. multiCounter[currCounter].mult++;
  177. #endif
  178. return (var_out);
  179. }
  180. /* ------------------------- End of mult() ------------------------- */
  181. /*___________________________________________________________________________
  182. | |
  183. | Function Name : L_msu |
  184. | |
  185. | Purpose : |
  186. | |
  187. | Multiply var1 by var2 and shift the result left by 1. Subtract the 32 |
  188. | bit result to L_var3 with saturation, return a 32 bit result: |
  189. | L_msu(L_var3,var1,var2) = L_sub(L_var3,L_mult(var1,var2)). |
  190. | |
  191. | Complexity weight : 1 |
  192. | |
  193. | Inputs : |
  194. | |
  195. | L_var3 32 bit long signed integer (Word32) whose value falls in the |
  196. | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. |
  197. | |
  198. | var1 |
  199. | 16 bit short signed integer (Word16) whose value falls in the |
  200. | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
  201. | |
  202. | var2 |
  203. | 16 bit short signed integer (Word16) whose value falls in the |
  204. | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
  205. | |
  206. | Outputs : |
  207. | |
  208. | none |
  209. | |
  210. | Return Value : |
  211. | |
  212. | L_var_out |
  213. | 32 bit long signed integer (Word32) whose value falls in the |
  214. | range : 0x8000 0000 <= L_var_out <= 0x7fff ffff. |
  215. |___________________________________________________________________________|
  216. */
  217. LIBG7221_DEF(Word32) L_msu (Word32 L_var3, Word16 var1, Word16 var2)
  218. {
  219. Word32 L_var_out;
  220. Word32 L_product;
  221. L_product = L_mult (var1, var2);
  222. #if (WMOPS)
  223. multiCounter[currCounter].L_mult--;
  224. #endif
  225. L_var_out = L_sub (L_var3, L_product);
  226. #if (WMOPS)
  227. multiCounter[currCounter].L_sub--;
  228. multiCounter[currCounter].L_msu++;
  229. #endif
  230. return (L_var_out);
  231. }
  232. /* ------------------------- End of L_msu() ------------------------- */
  233. #if INCLUDE_UNSAFE
  234. /*___________________________________________________________________________
  235. | |
  236. | Function Name : L_macNs |
  237. | |
  238. | Purpose : |
  239. | |
  240. | Multiply var1 by var2 and shift the result left by 1. Add the 32 bit |
  241. | result to L_var3 without saturation, return a 32 bit result. Generate |
  242. | carry and overflow values : |
  243. | L_macNs(L_var3,var1,var2) = L_add_c(L_var3,L_mult(var1,var2)). |
  244. | |
  245. | Complexity weight : 1 |
  246. | |
  247. | Inputs : |
  248. | |
  249. | L_var3 32 bit long signed integer (Word32) whose value falls in the |
  250. | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. |
  251. | |
  252. | var1 |
  253. | 16 bit short signed integer (Word16) whose value falls in the |
  254. | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
  255. | |
  256. | var2 |
  257. | 16 bit short signed integer (Word16) whose value falls in the |
  258. | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
  259. | |
  260. | Outputs : |
  261. | |
  262. | none |
  263. | |
  264. | Return Value : |
  265. | |
  266. | L_var_out |
  267. | 32 bit long signed integer (Word32) whose value falls in the |
  268. | range : 0x8000 0000 <= L_var_out <= 0x7fff ffff. |
  269. | |
  270. | Caution : |
  271. | |
  272. | In some cases the Carry flag has to be cleared or set before using |
  273. | operators which take into account its value. |
  274. |___________________________________________________________________________|
  275. */
  276. LIBG7221_DEF(Word32) L_macNs (Word32 L_var3, Word16 var1, Word16 var2)
  277. {
  278. Word32 L_var_out;
  279. L_var_out = L_mult (var1, var2);
  280. #if (WMOPS)
  281. multiCounter[currCounter].L_mult--;
  282. #endif
  283. L_var_out = L_add_c (L_var3, L_var_out);
  284. #if (WMOPS)
  285. multiCounter[currCounter].L_add_c--;
  286. multiCounter[currCounter].L_macNs++;
  287. #endif
  288. return (L_var_out);
  289. }
  290. #endif
  291. /* ------------------------- End of L_macNs() ------------------------- */
  292. #if INCLUDE_UNSAFE
  293. /*___________________________________________________________________________
  294. | |
  295. | Function Name : L_msuNs |
  296. | |
  297. | Purpose : |
  298. | |
  299. | Multiply var1 by var2 and shift the result left by 1. Subtract the 32 |
  300. | bit result from L_var3 without saturation, return a 32 bit result. Ge- |
  301. | nerate carry and overflow values : |
  302. | L_msuNs(L_var3,var1,var2) = L_sub_c(L_var3,L_mult(var1,var2)). |
  303. | |
  304. | Complexity weight : 1 |
  305. | |
  306. | Inputs : |
  307. | |
  308. | L_var3 32 bit long signed integer (Word32) whose value falls in the |
  309. | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. |
  310. | |
  311. | var1 |
  312. | 16 bit short signed integer (Word16) whose value falls in the |
  313. | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
  314. | |
  315. | var2 |
  316. | 16 bit short signed integer (Word16) whose value falls in the |
  317. | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
  318. | |
  319. | Outputs : |
  320. | |
  321. | none |
  322. | |
  323. | Return Value : |
  324. | |
  325. | L_var_out |
  326. | 32 bit long signed integer (Word32) whose value falls in the |
  327. | range : 0x8000 0000 <= L_var_out <= 0x7fff ffff. |
  328. | |
  329. | Caution : |
  330. | |
  331. | In some cases the Carry flag has to be cleared or set before using |
  332. | operators which take into account its value. |
  333. |___________________________________________________________________________|
  334. */
  335. LIBG7221_DEF(Word32) L_msuNs (Word32 L_var3, Word16 var1, Word16 var2)
  336. {
  337. Word32 L_var_out;
  338. L_var_out = L_mult (var1, var2);
  339. #if (WMOPS)
  340. multiCounter[currCounter].L_mult--;
  341. #endif
  342. L_var_out = L_sub_c (L_var3, L_var_out);
  343. #if (WMOPS)
  344. multiCounter[currCounter].L_sub_c--;
  345. multiCounter[currCounter].L_msuNs++;
  346. #endif
  347. return (L_var_out);
  348. }
  349. #endif
  350. /* ------------------------- End of L_msuNs() ------------------------- */
  351. #if INCLUDE_UNSAFE
  352. /*___________________________________________________________________________
  353. | |
  354. | Function Name : L_add_c |
  355. | |
  356. | Purpose : |
  357. | |
  358. | Performs 32 bits addition of the two 32 bits variables (L_var1+L_var2+C)|
  359. | with carry. No saturation. Generate carry and Overflow values. The car- |
  360. | ry and overflow values are binary variables which can be tested and as- |
  361. | signed values. |
  362. | |
  363. | Complexity weight : 2 |
  364. | |
  365. | Inputs : |
  366. | |
  367. | L_var1 32 bit long signed integer (Word32) whose value falls in the |
  368. | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. |
  369. | |
  370. | L_var2 32 bit long signed integer (Word32) whose value falls in the |
  371. | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. |
  372. | |
  373. | Outputs : |
  374. | |
  375. | none |
  376. | |
  377. | Return Value : |
  378. | |
  379. | L_var_out |
  380. | 32 bit long signed integer (Word32) whose value falls in the |
  381. | range : 0x8000 0000 <= L_var_out <= 0x7fff ffff. |
  382. | |
  383. | Caution : |
  384. | |
  385. | In some cases the Carry flag has to be cleared or set before using |
  386. | operators which take into account its value. |
  387. |___________________________________________________________________________|
  388. */
  389. LIBG7221_DEF(Word32) L_add_c (Word32 L_var1, Word32 L_var2)
  390. {
  391. Word32 L_var_out;
  392. Word32 L_test;
  393. Flag carry_int = 0;
  394. L_var_out = L_var1 + L_var2 + GET_CARRY();
  395. L_test = L_var1 + L_var2;
  396. if ((L_var1 > 0) && (L_var2 > 0) && (L_test < 0))
  397. {
  398. SET_OVERFLOW(1);
  399. carry_int = 0;
  400. }
  401. else
  402. {
  403. if ((L_var1 < 0) && (L_var2 < 0))
  404. {
  405. if (L_test >= 0)
  406. {
  407. SET_OVERFLOW(1);
  408. carry_int = 1;
  409. }
  410. else
  411. {
  412. SET_OVERFLOW(0);
  413. carry_int = 1;
  414. }
  415. }
  416. else
  417. {
  418. if (((L_var1 ^ L_var2) < 0) && (L_test >= 0))
  419. {
  420. SET_OVERFLOW(0);
  421. carry_int = 1;
  422. }
  423. else
  424. {
  425. SET_OVERFLOW(0);
  426. carry_int = 0;
  427. }
  428. }
  429. }
  430. if (GET_CARRY())
  431. {
  432. if (L_test == MAX_32)
  433. {
  434. SET_OVERFLOW(1);
  435. SET_CARRY(carry_int);
  436. }
  437. else
  438. {
  439. if (L_test == (Word32) 0xFFFFFFFFL)
  440. {
  441. SET_CARRY(1);
  442. }
  443. else
  444. {
  445. SET_CARRY(carry_int);
  446. }
  447. }
  448. }
  449. else
  450. {
  451. SET_CARRY(carry_int);
  452. }
  453. #if (WMOPS)
  454. multiCounter[currCounter].L_add_c++;
  455. #endif
  456. return (L_var_out);
  457. }
  458. #endif
  459. /* ------------------------- End of L_add_c() ------------------------- */
  460. #if INCLUDE_UNSAFE
  461. /*___________________________________________________________________________
  462. | |
  463. | Function Name : L_sub_c |
  464. | |
  465. | Purpose : |
  466. | |
  467. | Performs 32 bits subtraction of the two 32 bits variables with carry |
  468. | (borrow) : L_var1-L_var2-C. No saturation. Generate carry and Overflow |
  469. | values. The carry and overflow values are binary variables which can |
  470. | be tested and assigned values. |
  471. | |
  472. | Complexity weight : 2 |
  473. | |
  474. | Inputs : |
  475. | |
  476. | L_var1 32 bit long signed integer (Word32) whose value falls in the |
  477. | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. |
  478. | |
  479. | L_var2 32 bit long signed integer (Word32) whose value falls in the |
  480. | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. |
  481. | |
  482. | Outputs : |
  483. | |
  484. | none |
  485. | |
  486. | Return Value : |
  487. | |
  488. | L_var_out |
  489. | 32 bit long signed integer (Word32) whose value falls in the |
  490. | range : 0x8000 0000 <= L_var_out <= 0x7fff ffff. |
  491. | |
  492. | Caution : |
  493. | |
  494. | In some cases the Carry flag has to be cleared or set before using |
  495. | operators which take into account its value. |
  496. |___________________________________________________________________________|
  497. */
  498. LIBG7221_DEF(Word32) L_sub_c (Word32 L_var1, Word32 L_var2)
  499. {
  500. Word32 L_var_out;
  501. Word32 L_test;
  502. Flag carry_int = 0;
  503. if (GET_CARRY())
  504. {
  505. SET_CARRY(0);
  506. if (L_var2 != MIN_32)
  507. {
  508. L_var_out = L_add_c (L_var1, -L_var2);
  509. #if (WMOPS)
  510. multiCounter[currCounter].L_add_c--;
  511. #endif
  512. }
  513. else
  514. {
  515. L_var_out = L_var1 - L_var2;
  516. if (L_var1 > 0L)
  517. {
  518. SET_OVERFLOW(1);
  519. SET_CARRY(0);
  520. }
  521. }
  522. }
  523. else
  524. {
  525. L_var_out = L_var1 - L_var2 - (Word32) 0X00000001L;
  526. L_test = L_var1 - L_var2;
  527. if ((L_test < 0) && (L_var1 > 0) && (L_var2 < 0))
  528. {
  529. SET_OVERFLOW(1);
  530. carry_int = 0;
  531. }
  532. else if ((L_test > 0) && (L_var1 < 0) && (L_var2 > 0))
  533. {
  534. SET_OVERFLOW(1);
  535. carry_int = 1;
  536. }
  537. else if ((L_test > 0) && ((L_var1 ^ L_var2) > 0))
  538. {
  539. SET_OVERFLOW(0);
  540. carry_int = 1;
  541. }
  542. if (L_test == MIN_32)
  543. {
  544. SET_OVERFLOW(1);
  545. SET_CARRY(carry_int);
  546. }
  547. else
  548. {
  549. SET_CARRY(carry_int);
  550. }
  551. }
  552. #if (WMOPS)
  553. multiCounter[currCounter].L_sub_c++;
  554. #endif
  555. return (L_var_out);
  556. }
  557. #endif
  558. /* ------------------------- End of L_sub_c() ------------------------- */
  559. /*___________________________________________________________________________
  560. | |
  561. | Function Name : L_negate |
  562. | |
  563. | Purpose : |
  564. | |
  565. | Negate the 32 bit variable L_var1 with saturation; saturate in the case |
  566. | where input is -2147483648 (0x8000 0000). |
  567. | |
  568. | Complexity weight : 2 |
  569. | |
  570. | Inputs : |
  571. | |
  572. | L_var1 32 bit long signed integer (Word32) whose value falls in the |
  573. | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. |
  574. | |
  575. | Outputs : |
  576. | |
  577. | none |
  578. | |
  579. | Return Value : |
  580. | |
  581. | L_var_out |
  582. | 32 bit long signed integer (Word32) whose value falls in the |
  583. | range : 0x8000 0000 <= L_var_out <= 0x7fff ffff. |
  584. |___________________________________________________________________________|
  585. */
  586. LIBG7221_DEF(Word32) L_negate (Word32 L_var1)
  587. {
  588. Word32 L_var_out;
  589. L_var_out = (L_var1 == MIN_32) ? MAX_32 : -L_var1;
  590. #if (WMOPS)
  591. multiCounter[currCounter].L_negate++;
  592. #endif
  593. return (L_var_out);
  594. }
  595. /* ------------------------- End of L_negate() ------------------------- */
  596. /*___________________________________________________________________________
  597. | |
  598. | Function Name : mult_r |
  599. | |
  600. | Purpose : |
  601. | |
  602. | Same as mult with rounding, i.e.: |
  603. | mult_r(var1,var2) = extract_l(L_shr(((var1 * var2) + 16384),15)) and |
  604. | mult_r(-32768,-32768) = 32767. |
  605. | |
  606. | Complexity weight : 2 |
  607. | |
  608. | Inputs : |
  609. | |
  610. | var1 |
  611. | 16 bit short signed integer (Word16) whose value falls in the |
  612. | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
  613. | |
  614. | var2 |
  615. | 16 bit short signed integer (Word16) whose value falls in the |
  616. | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
  617. | |
  618. | Outputs : |
  619. | |
  620. | none |
  621. | |
  622. | Return Value : |
  623. | |
  624. | var_out |
  625. | 16 bit short signed integer (Word16) whose value falls in the |
  626. | range : 0xffff 8000 <= var_out <= 0x0000 7fff. |
  627. |___________________________________________________________________________|
  628. */
  629. LIBG7221_DEF(Word16) mult_r (Word16 var1, Word16 var2)
  630. {
  631. Word16 var_out;
  632. Word32 L_product_arr;
  633. L_product_arr = (Word32) var1 *(Word32) var2; /* product */
  634. L_product_arr += (Word32) 0x00004000L; /* round */
  635. L_product_arr &= (Word32) 0xffff8000L;
  636. L_product_arr >>= 15; /* shift */
  637. if (L_product_arr & (Word32) 0x00010000L) /* sign extend when necessary */
  638. {
  639. L_product_arr |= (Word32) 0xffff0000L;
  640. }
  641. var_out = saturate (L_product_arr);
  642. #if (WMOPS)
  643. multiCounter[currCounter].mult_r++;
  644. #endif
  645. return (var_out);
  646. }
  647. /* ------------------------- End of mult_r() ------------------------- */
  648. /*___________________________________________________________________________
  649. | |
  650. | Function Name : shr_r |
  651. | |
  652. | Purpose : |
  653. | |
  654. | Same as shr(var1,var2) but with rounding. Saturate the result in case of|
  655. | underflows or overflows : |
  656. | - If var2 is greater than zero : |
  657. | if (sub(shl(shr(var1,var2),1),shr(var1,sub(var2,1)))) |
  658. | is equal to zero |
  659. | then |
  660. | shr_r(var1,var2) = shr(var1,var2) |
  661. | else |
  662. | shr_r(var1,var2) = add(shr(var1,var2),1) |
  663. | - If var2 is less than or equal to zero : |
  664. | shr_r(var1,var2) = shr(var1,var2). |
  665. | |
  666. | Complexity weight : 2 |
  667. | |
  668. | Inputs : |
  669. | |
  670. | var1 |
  671. | 16 bit short signed integer (Word16) whose value falls in the |
  672. | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
  673. | |
  674. | var2 |
  675. | 16 bit short signed integer (Word16) whose value falls in the |
  676. | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
  677. | |
  678. | Outputs : |
  679. | |
  680. | none |
  681. | |
  682. | Return Value : |
  683. | |
  684. | var_out |
  685. | 16 bit short signed integer (Word16) whose value falls in the |
  686. | range : 0xffff 8000 <= var_out <= 0x0000 7fff. |
  687. |___________________________________________________________________________|
  688. */
  689. LIBG7221_DEF(Word16) shr_r (Word16 var1, Word16 var2)
  690. {
  691. Word16 var_out;
  692. if (var2 > 15)
  693. {
  694. var_out = 0;
  695. }
  696. else
  697. {
  698. var_out = shr (var1, var2);
  699. #if (WMOPS)
  700. multiCounter[currCounter].shr--;
  701. #endif
  702. if (var2 > 0)
  703. {
  704. if ((var1 & ((Word16) 1 << (var2 - 1))) != 0)
  705. {
  706. var_out++;
  707. }
  708. }
  709. }
  710. #if (WMOPS)
  711. multiCounter[currCounter].shr_r++;
  712. #endif
  713. return (var_out);
  714. }
  715. /* ------------------------- End of shr_r() ------------------------- */
  716. /*___________________________________________________________________________
  717. | |
  718. | Function Name : mac_r |
  719. | |
  720. | Purpose : |
  721. | |
  722. | Multiply var1 by var2 and shift the result left by 1. Add the 32 bit |
  723. | result to L_var3 with saturation. Round the LS 16 bits of the result |
  724. | into the MS 16 bits with saturation and shift the result right by 16. |
  725. | Return a 16 bit result. |
  726. | mac_r(L_var3,var1,var2) = round(L_mac(L_var3,var1,var2)) |
  727. | |
  728. | Complexity weight : 2 |
  729. | |
  730. | Inputs : |
  731. | |
  732. | L_var3 32 bit long signed integer (Word32) whose value falls in the |
  733. | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. |
  734. | |
  735. | var1 |
  736. | 16 bit short signed integer (Word16) whose value falls in the |
  737. | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
  738. | |
  739. | var2 |
  740. | 16 bit short signed integer (Word16) whose value falls in the |
  741. | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
  742. | |
  743. | Outputs : |
  744. | |
  745. | none |
  746. | |
  747. | Return Value : |
  748. | |
  749. | var_out |
  750. | 16 bit short signed integer (Word16) whose value falls in the |
  751. | range : 0x0000 8000 <= L_var_out <= 0x0000 7fff. |
  752. |___________________________________________________________________________|
  753. */
  754. LIBG7221_DEF(Word16) mac_r (Word32 L_var3, Word16 var1, Word16 var2)
  755. {
  756. Word16 var_out;
  757. L_var3 = L_mac (L_var3, var1, var2);
  758. #if (WMOPS)
  759. multiCounter[currCounter].L_mac--;
  760. #endif
  761. L_var3 = L_add (L_var3, (Word32) 0x00008000L);
  762. #if (WMOPS)
  763. multiCounter[currCounter].L_add--;
  764. #endif
  765. var_out = extract_h (L_var3);
  766. #if (WMOPS)
  767. multiCounter[currCounter].extract_h--;
  768. multiCounter[currCounter].mac_r++;
  769. #endif
  770. return (var_out);
  771. }
  772. /* ------------------------- End of mac_r() ------------------------- */
  773. /*___________________________________________________________________________
  774. | |
  775. | Function Name : msu_r |
  776. | |
  777. | Purpose : |
  778. | |
  779. | Multiply var1 by var2 and shift the result left by 1. Subtract the 32 |
  780. | bit result to L_var3 with saturation. Round the LS 16 bits of the res- |
  781. | ult into the MS 16 bits with saturation and shift the result right by |
  782. | 16. Return a 16 bit result. |
  783. | msu_r(L_var3,var1,var2) = round(L_msu(L_var3,var1,var2)) |
  784. | |
  785. | Complexity weight : 2 |
  786. | |
  787. | Inputs : |
  788. | |
  789. | L_var3 32 bit long signed integer (Word32) whose value falls in the |
  790. | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. |
  791. | |
  792. | var1 |
  793. | 16 bit short signed integer (Word16) whose value falls in the |
  794. | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
  795. | |
  796. | var2 |
  797. | 16 bit short signed integer (Word16) whose value falls in the |
  798. | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
  799. | |
  800. | Outputs : |
  801. | |
  802. | none |
  803. | |
  804. | Return Value : |
  805. | |
  806. | var_out |
  807. | 16 bit short signed integer (Word16) whose value falls in the |
  808. | range : 0x0000 8000 <= L_var_out <= 0x0000 7fff. |
  809. |___________________________________________________________________________|
  810. */
  811. LIBG7221_DEF(Word16) msu_r (Word32 L_var3, Word16 var1, Word16 var2)
  812. {
  813. Word16 var_out;
  814. L_var3 = L_msu (L_var3, var1, var2);
  815. #if (WMOPS)
  816. multiCounter[currCounter].L_msu--;
  817. #endif
  818. L_var3 = L_add (L_var3, (Word32) 0x00008000L);
  819. #if (WMOPS)
  820. multiCounter[currCounter].L_add--;
  821. #endif
  822. var_out = extract_h (L_var3);
  823. #if (WMOPS)
  824. multiCounter[currCounter].extract_h--;
  825. multiCounter[currCounter].msu_r++;
  826. #endif
  827. return (var_out);
  828. }
  829. /* ------------------------- End of msu_r() ------------------------- */
  830. /*___________________________________________________________________________
  831. | |
  832. | Function Name : L_deposit_h |
  833. | |
  834. | Purpose : |
  835. | |
  836. | Deposit the 16 bit var1 into the 16 MS bits of the 32 bit output. The |
  837. | 16 LS bits of the output are zeroed. |
  838. | |
  839. | Complexity weight : 2 |
  840. | |
  841. | Inputs : |
  842. | |
  843. | var1 |
  844. | 16 bit short signed integer (Word16) whose value falls in the |
  845. | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
  846. | |
  847. | Outputs : |
  848. | |
  849. | none |
  850. | |
  851. | Return Value : |
  852. | |
  853. | L_var_out |
  854. | 32 bit long signed integer (Word32) whose value falls in the |
  855. | range : 0x8000 0000 <= var_out <= 0x7fff 0000. |
  856. |___________________________________________________________________________|
  857. */
  858. LIBG7221_DEF(Word32) L_deposit_h (Word16 var1)
  859. {
  860. Word32 L_var_out;
  861. L_var_out = (Word32) var1 << 16;
  862. #if (WMOPS)
  863. multiCounter[currCounter].L_deposit_h++;
  864. #endif
  865. return (L_var_out);
  866. }
  867. /* ------------------------- End of L_deposit_h() ------------------------- */
  868. /*___________________________________________________________________________
  869. | |
  870. | Function Name : L_deposit_l |
  871. | |
  872. | Purpose : |
  873. | |
  874. | Deposit the 16 bit var1 into the 16 LS bits of the 32 bit output. The |
  875. | 16 MS bits of the output are sign extended. |
  876. | |
  877. | Complexity weight : 2 |
  878. | |
  879. | Inputs : |
  880. | |
  881. | var1 |
  882. | 16 bit short signed integer (Word16) whose value falls in the |
  883. | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
  884. | |
  885. | Outputs : |
  886. | |
  887. | none |
  888. | |
  889. | Return Value : |
  890. | |
  891. | L_var_out |
  892. | 32 bit long signed integer (Word32) whose value falls in the |
  893. | range : 0xFFFF 8000 <= var_out <= 0x0000 7fff. |
  894. |___________________________________________________________________________|
  895. */
  896. LIBG7221_DEF(Word32) L_deposit_l (Word16 var1)
  897. {
  898. Word32 L_var_out;
  899. L_var_out = (Word32) var1;
  900. #if (WMOPS)
  901. multiCounter[currCounter].L_deposit_l++;
  902. #endif
  903. return (L_var_out);
  904. }
  905. /* ------------------------- End of L_deposit_l() ------------------------- */
  906. /*___________________________________________________________________________
  907. | |
  908. | Function Name : L_shr_r |
  909. | |
  910. | Purpose : |
  911. | |
  912. | Same as L_shr(L_var1,var2) but with rounding. Saturate the result in |
  913. | case of underflows or overflows : |
  914. | - If var2 is greater than zero : |
  915. | if (L_sub(L_shl(L_shr(L_var1,var2),1),L_shr(L_var1,sub(var2,1))))|
  916. | is equal to zero |
  917. | then |
  918. | L_shr_r(L_var1,var2) = L_shr(L_var1,var2) |
  919. | else |
  920. | L_shr_r(L_var1,var2) = L_add(L_shr(L_var1,var2),1) |
  921. | - If var2 is less than or equal to zero : |
  922. | L_shr_r(L_var1,var2) = L_shr(L_var1,var2). |
  923. | |
  924. | Complexity weight : 3 |
  925. | |
  926. | Inputs : |
  927. | |
  928. | L_var1 |
  929. | 32 bit long signed integer (Word32) whose value falls in the |
  930. | range : 0x8000 0000 <= var1 <= 0x7fff ffff. |
  931. | |
  932. | var2 |
  933. | 16 bit short signed integer (Word16) whose value falls in the |
  934. | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
  935. | |
  936. | Outputs : |
  937. | |
  938. | none |
  939. | |
  940. | Return Value : |
  941. | |
  942. | L_var_out |
  943. | 32 bit long signed integer (Word32) whose value falls in the |
  944. | range : 0x8000 0000 <= var_out <= 0x7fff ffff. |
  945. |___________________________________________________________________________|
  946. */
  947. LIBG7221_DEF(Word32) L_shr_r (Word32 L_var1, Word16 var2)
  948. {
  949. Word32 L_var_out;
  950. if (var2 > 31)
  951. {
  952. L_var_out = 0;
  953. }
  954. else
  955. {
  956. L_var_out = L_shr (L_var1, var2);
  957. #if (WMOPS)
  958. multiCounter[currCounter].L_shr--;
  959. #endif
  960. if (var2 > 0)
  961. {
  962. if ((L_var1 & ((Word32) 1 << (var2 - 1))) != 0)
  963. {
  964. L_var_out++;
  965. }
  966. }
  967. }
  968. #if (WMOPS)
  969. multiCounter[currCounter].L_shr_r++;
  970. #endif
  971. return (L_var_out);
  972. }
  973. /* ------------------------- End of L_shr_r() ------------------------- */
  974. /*___________________________________________________________________________
  975. | |
  976. | Function Name : L_abs |
  977. | |
  978. | Purpose : |
  979. | |
  980. | Absolute value of L_var1; Saturate in case where the input is |
  981. | -214783648 |
  982. | |
  983. | Complexity weight : 3 |
  984. | |
  985. | Inputs : |
  986. | |
  987. | L_var1 |
  988. | 32 bit long signed integer (Word32) whose value falls in the |
  989. | range : 0x8000 0000 <= var1 <= 0x7fff ffff. |
  990. | |
  991. | Outputs : |
  992. | |
  993. | none |
  994. | |
  995. | Return Value : |
  996. | |
  997. | L_var_out |
  998. | 32 bit long signed integer (Word32) whose value falls in the |
  999. | range : 0x0000 0000 <= var_out <= 0x7fff ffff. |
  1000. |___________________________________________________________________________|
  1001. */
  1002. LIBG7221_DEF(Word32) L_abs (Word32 L_var1)
  1003. {
  1004. Word32 L_var_out;
  1005. if (L_var1 == MIN_32)
  1006. {
  1007. L_var_out = MAX_32;
  1008. }
  1009. else
  1010. {
  1011. if (L_var1 < 0)
  1012. {
  1013. L_var_out = -L_var1;
  1014. }
  1015. else
  1016. {
  1017. L_var_out = L_var1;
  1018. }
  1019. }
  1020. #if (WMOPS)
  1021. multiCounter[currCounter].L_abs++;
  1022. #endif
  1023. return (L_var_out);
  1024. }
  1025. /* ------------------------- End of L_abs() ------------------------- */
  1026. /*___________________________________________________________________________
  1027. | |
  1028. | Function Name : norm_s |
  1029. | |
  1030. | Purpose : |
  1031. | |
  1032. | Produces the number of left shift needed to normalize the 16 bit varia- |
  1033. | ble var1 for positive values on the interval with minimum of 16384 and |
  1034. | maximum of 32767, and for negative values on the interval with minimum |
  1035. | of -32768 and maximum of -16384; in order to normalize the result, the |
  1036. | following operation must be done : |
  1037. | norm_var1 = shl(var1,norm_s(var1)). |
  1038. | |
  1039. | Complexity weight : 15 |
  1040. | |
  1041. | Inputs : |
  1042. | |
  1043. | var1 |
  1044. | 16 bit short signed integer (Word16) whose value falls in the |
  1045. | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
  1046. | |
  1047. | Outputs : |
  1048. | |
  1049. | none |
  1050. | |
  1051. | Return Value : |
  1052. | |
  1053. | var_out |
  1054. | 16 bit short signed integer (Word16) whose value falls in the |
  1055. | range : 0x0000 0000 <= var_out <= 0x0000 000f. |
  1056. |___________________________________________________________________________|
  1057. */
  1058. LIBG7221_DEF(Word16) norm_s (Word16 var1)
  1059. {
  1060. Word16 var_out;
  1061. if (var1 == 0)
  1062. {
  1063. var_out = 0;
  1064. }
  1065. else
  1066. {
  1067. if ((UWord16)var1 == (UWord16)0xffff)
  1068. {
  1069. var_out = 15;
  1070. }
  1071. else
  1072. {
  1073. if (var1 < 0)
  1074. {
  1075. var1 = (Word16)(~var1);
  1076. }
  1077. for (var_out = 0; var1 < 0x4000; var_out++)
  1078. {
  1079. var1 <<= 1;
  1080. }
  1081. }
  1082. }
  1083. #if (WMOPS)
  1084. multiCounter[currCounter].norm_s++;
  1085. #endif
  1086. return (var_out);
  1087. }
  1088. /* ------------------------- End of norm_s() ------------------------- */
  1089. /*___________________________________________________________________________
  1090. | |
  1091. | Function Name : div_s |
  1092. | |
  1093. | Purpose : |
  1094. | |
  1095. | Produces a result which is the fractional integer division of var1 by |
  1096. | var2; var1 and var2 must be positive and var2 must be greater or equal |
  1097. | to var1; the result is positive (leading bit equal to 0) and truncated |
  1098. | to 16 bits. |
  1099. | If var1 = var2 then div(var1,var2) = 32767. |
  1100. | |
  1101. | Complexity weight : 18 |
  1102. | |
  1103. | Inputs : |
  1104. | |
  1105. | var1 |
  1106. | 16 bit short signed integer (Word16) whose value falls in the |
  1107. | range : 0x0000 0000 <= var1 <= var2 and var2 != 0. |
  1108. | |
  1109. | var2 |
  1110. | 16 bit short signed integer (Word16) whose value falls in the |
  1111. | range : var1 <= var2 <= 0x0000 7fff and var2 != 0. |
  1112. | |
  1113. | Outputs : |
  1114. | |
  1115. | none |
  1116. | |
  1117. | Return Value : |
  1118. | |
  1119. | var_out |
  1120. | 16 bit short signed integer (Word16) whose value falls in the |
  1121. | range : 0x0000 0000 <= var_out <= 0x0000 7fff. |
  1122. | It's a Q15 value (point between b15 and b14). |
  1123. |___________________________________________________________________________|
  1124. */
  1125. LIBG7221_DEF(Word16) div_s (Word16 var1, Word16 var2)
  1126. {
  1127. Word16 var_out = 0;
  1128. Word16 iteration;
  1129. Word32 L_num;
  1130. Word32 L_denom;
  1131. if ((var1 > var2) || (var1 < 0) || (var2 < 0))
  1132. {
  1133. //printf ("Division Error var1=%d var2=%d\n", var1, var2);
  1134. //abort(); /* exit (0); */
  1135. pj_assert(!"Division Error");
  1136. }
  1137. if (var2 == 0)
  1138. {
  1139. //printf ("Division by 0, Fatal error \n");
  1140. //abort(); /* exit (0); */
  1141. assert(!"Division by 0");
  1142. }
  1143. if (var1 == 0)
  1144. {
  1145. var_out = 0;
  1146. }
  1147. else
  1148. {
  1149. if (var1 == var2)
  1150. {
  1151. var_out = MAX_16;
  1152. }
  1153. else
  1154. {
  1155. L_num = L_deposit_l (var1);
  1156. #if (WMOPS)
  1157. multiCounter[currCounter].L_deposit_l--;
  1158. #endif
  1159. L_denom = L_deposit_l (var2);
  1160. #if (WMOPS)
  1161. multiCounter[currCounter].L_deposit_l--;
  1162. #endif
  1163. for (iteration = 0; iteration < 15; iteration++)
  1164. {
  1165. var_out <<= 1;
  1166. L_num <<= 1;
  1167. if (L_num >= L_denom)
  1168. {
  1169. L_num = L_sub (L_num, L_denom);
  1170. #if (WMOPS)
  1171. multiCounter[currCounter].L_sub--;
  1172. #endif
  1173. var_out = add (var_out, 1);
  1174. #if (WMOPS)
  1175. multiCounter[currCounter].add--;
  1176. #endif
  1177. }
  1178. }
  1179. }
  1180. }
  1181. #if (WMOPS)
  1182. multiCounter[currCounter].div_s++;
  1183. #endif
  1184. return (var_out);
  1185. }
  1186. /* ------------------------- End of div_s() ------------------------- */
  1187. /*___________________________________________________________________________
  1188. | |
  1189. | Function Name : norm_l |
  1190. | |
  1191. | Purpose : |
  1192. | |
  1193. | Produces the number of left shifts needed to normalize the 32 bit varia-|
  1194. | ble L_var1 for positive values on the interval with minimum of |
  1195. | 1073741824 and maximum of 2147483647, and for negative values on the in-|
  1196. | terval with minimum of -2147483648 and maximum of -1073741824; in order |
  1197. | to normalize the result, the following operation must be done : |
  1198. | norm_L_var1 = L_shl(L_var1,norm_l(L_var1)). |
  1199. | |
  1200. | Complexity weight : 30 |
  1201. | |
  1202. | Inputs : |
  1203. | |
  1204. | L_var1 |
  1205. | 32 bit long signed integer (Word32) whose value falls in the |
  1206. | range : 0x8000 0000 <= var1 <= 0x7fff ffff. |
  1207. | |
  1208. | Outputs : |
  1209. | |
  1210. | none |
  1211. | |
  1212. | Return Value : |
  1213. | |
  1214. | var_out |
  1215. | 16 bit short signed integer (Word16) whose value falls in the |
  1216. | range : 0x0000 0000 <= var_out <= 0x0000 001f. |
  1217. |___________________________________________________________________________|
  1218. */
  1219. LIBG7221_DEF(Word16) norm_l (Word32 L_var1)
  1220. {
  1221. Word16 var_out;
  1222. if (L_var1 == 0)
  1223. {
  1224. var_out = 0;
  1225. }
  1226. else
  1227. {
  1228. if (L_var1 == (Word32) 0xffffffffL)
  1229. {
  1230. var_out = 31;
  1231. }
  1232. else
  1233. {
  1234. if (L_var1 < 0)
  1235. {
  1236. L_var1 = ~L_var1;
  1237. }
  1238. for (var_out = 0; L_var1 < (Word32) 0x40000000L; var_out++)
  1239. {
  1240. L_var1 <<= 1;
  1241. }
  1242. }
  1243. }
  1244. #if (WMOPS)
  1245. multiCounter[currCounter].norm_l++;
  1246. #endif
  1247. return (var_out);
  1248. }
  1249. /* ------------------------- End of norm_l() ------------------------- */
  1250. /*
  1251. *****************************************************************
  1252. Additional operators extracted from the G.723.1 Library
  1253. Adapted for WMOPS calculations
  1254. *****************************************************************
  1255. */
  1256. /*___________________________________________________________________________
  1257. | |
  1258. | Function Name : L_mls |
  1259. | |
  1260. | Purpose : |
  1261. | |
  1262. | Multiplies a 16 bit word v by a 32 bit word Lv and returns a 32 bit |
  1263. | word (multiplying 16 by 32 bit words gives 48 bit word; the function |
  1264. | extracts the 32 MSB and shift the result to the left by 1). |
  1265. | |
  1266. | A 32 bit word can be written as |
  1267. | Lv = a + b * 2^16 |
  1268. | where a= unsigned 16 LSBs and b= signed 16 MSBs. |
  1269. | The function returns v * Lv / 2^15 which is equivalent to |
  1270. | a*v / 2^15 + b*v*2 |
  1271. | |
  1272. | Complexity weight : 6 [to be confirmed] |
  1273. | |
  1274. | Inputs : |
  1275. | |
  1276. | Lv |
  1277. | 32 bit long signed integer (Word32) whose value falls in the |
  1278. | range : 0x8000 0000 <= var1 <= 0x7fff ffff. |
  1279. | v |
  1280. | 16 bit short signed integer (Word16) whose value falls in the |
  1281. | range : 0x8000 <= var1 <= 0x7fff. |
  1282. | |
  1283. | Outputs : |
  1284. | |
  1285. | none |
  1286. | |
  1287. | Return Value : |
  1288. | |
  1289. | var_out |
  1290. | 32 bit long signed integer (Word32) whose value falls in the |
  1291. | range : 0x8000 0000 <= var_out <= 0x7fff ffff. |
  1292. | |
  1293. |___________________________________________________________________________|
  1294. */
  1295. LIBG7221_DEF(Word32) L_mls (Word32 Lv, Word16 v)
  1296. {
  1297. Word32 Temp ;
  1298. Temp = Lv & (Word32) 0x0000ffff ;
  1299. Temp = Temp * (Word32) v ;
  1300. Temp = L_shr_nocheck( Temp, (Word16) 15 ) ;
  1301. Temp = L_mac( Temp, v, extract_h(Lv) ) ;
  1302. #if (WMOPS)
  1303. multiCounter[currCounter].L_shr--;
  1304. multiCounter[currCounter].L_mac--;
  1305. multiCounter[currCounter].extract_h--;
  1306. multiCounter[currCounter].L_mls++;
  1307. #endif
  1308. return Temp ;
  1309. }
  1310. /* ------------------------- End of L_mls() ------------------------- */
  1311. /*__________________________________________________________________________
  1312. | |
  1313. | Function Name : div_l |
  1314. | |
  1315. | Purpose : |
  1316. | |
  1317. | Produces a result which is the fractional integer division of L_var1 by|
  1318. | var2; L_var1 and var2 must be positive and var2 << 16 must be greater or|
  1319. | equal to L_var1; the result is positive (leading bit equal to 0) and |
  1320. | truncated to 16 bits. |
  1321. | If L_var1 == var2 << 16 then div_l(L_var1,var2) = 32767. |
  1322. | |
  1323. | Complexity weight : 20 |
  1324. | |
  1325. | Inputs : |
  1326. | |
  1327. | L_var1 |
  1328. | 32 bit long signed integer (Word32) whose value falls in the |
  1329. | range : 0x0000 0000 <= var1 <= (var2 << 16) and var2 != 0. |
  1330. | L_var1 must be considered as a Q.31 value |
  1331. | |
  1332. | var2 |
  1333. | 16 bit short signed integer (Word16) whose value falls in the |
  1334. | range : var1 <= (var2<< 16) <= 0x7fff0000 and var2 != 0. |
  1335. | var2 must be considered as a Q.15 value |
  1336. | |
  1337. | Outputs : |
  1338. | |
  1339. | none |
  1340. | |
  1341. | Return Value : |
  1342. | |
  1343. | var_out |
  1344. | 16 bit short signed integer (Word16) whose value falls in the |
  1345. | range : 0x0000 0000 <= var_out <= 0x0000 7fff. |
  1346. | It's a Q15 value (point between b15 and b14). |
  1347. |___________________________________________________________________________|
  1348. */
  1349. LIBG7221_DEF(Word16) div_l (Word32 L_num, Word16 den)
  1350. {
  1351. Word16 var_out = (Word16)0;
  1352. Word32 L_den;
  1353. Word16 iteration;
  1354. #if (WMOPS)
  1355. multiCounter[currCounter].div_l++;
  1356. #endif
  1357. if ( den == (Word16) 0 ) {
  1358. //printf("Division by 0 in div_l, Fatal error \n");
  1359. //exit(0);
  1360. assert(!"Division by 0");
  1361. }
  1362. if ( (L_num < (Word32) 0) || (den < (Word16) 0) ) {
  1363. //printf("Division Error in div_l, Fatal error \n");
  1364. //exit(0);
  1365. assert(!"Division Error");
  1366. }
  1367. L_den = L_deposit_h( den ) ;
  1368. #if (WMOPS)
  1369. multiCounter[currCounter].L_deposit_h--;
  1370. #endif
  1371. if ( L_num >= L_den ){
  1372. return MAX_16 ;
  1373. }
  1374. else {
  1375. L_num = L_shr_nocheck(L_num, (Word16)1) ;
  1376. L_den = L_shr_nocheck(L_den, (Word16)1);
  1377. #if (WMOPS)
  1378. multiCounter[currCounter].L_shr-=2;
  1379. #endif
  1380. for(iteration=(Word16)0; iteration< (Word16)15;iteration++) {
  1381. var_out = shl_nocheck( var_out, (Word16)1);
  1382. L_num = L_shl_nocheck( L_num, (Word16)1);
  1383. #if (WMOPS)
  1384. multiCounter[currCounter].shl--;
  1385. multiCounter[currCounter].L_shl--;
  1386. #endif
  1387. if (L_num >= L_den) {
  1388. L_num = L_sub(L_num,L_den);
  1389. var_out = add(var_out, (Word16)1);
  1390. #if (WMOPS)
  1391. multiCounter[currCounter].L_sub--;
  1392. multiCounter[currCounter].add--;
  1393. #endif
  1394. }
  1395. }
  1396. return var_out;
  1397. }
  1398. }
  1399. /* ------------------------- End of div_l() ------------------------- */
  1400. /*__________________________________________________________________________
  1401. | |
  1402. | Function Name : i_mult |
  1403. | |
  1404. | Purpose : |
  1405. | |
  1406. | Integer 16-bit multiplication. No overflow protection is performed if |
  1407. | ORIGINAL_G7231 is defined. |
  1408. | |
  1409. | Complexity weight : TBD |
  1410. | |
  1411. | Inputs : |
  1412. | |
  1413. | a |
  1414. | 16 bit short signed integer (Word16). |
  1415. | |
  1416. | b |
  1417. | 16 bit short signed integer (Word16). |
  1418. | |
  1419. | Outputs : |
  1420. | |
  1421. | none |
  1422. | |
  1423. | Return Value : |
  1424. | |
  1425. | 16 bit short signed integer (Word16). No overflow checks |
  1426. | are performed if ORIGINAL_G7231 is defined. |
  1427. |___________________________________________________________________________|
  1428. */
  1429. LIBG7221_DEF(Word16) i_mult (Word16 a, Word16 b)
  1430. {
  1431. #ifdef ORIGINAL_G7231
  1432. return a*b ;
  1433. #else
  1434. register Word32 c=a*b;
  1435. #if (WMOPS)
  1436. multiCounter[currCounter].i_mult++;
  1437. #endif
  1438. return saturate(c) ;
  1439. #endif
  1440. }
  1441. /* ------------------------- End of i_mult() ------------------------- */
  1442. /*
  1443. **********************************************************************
  1444. The following three operators are not part of the original
  1445. G.729/G.723.1 set of basic operators and implement shiftless
  1446. accumulation operation.
  1447. **********************************************************************
  1448. */
  1449. /*___________________________________________________________________________
  1450. |
  1451. | Function Name : L_mult0
  1452. |
  1453. | Purpose :
  1454. |
  1455. | L_mult0 is the 32 bit result of the multiplication of var1 times var2
  1456. | without one left shift.
  1457. |
  1458. | Complexity weight : 1
  1459. |
  1460. | Inputs :
  1461. |
  1462. | var1 16 bit short signed integer (Word16) whose value falls in the
  1463. | range : 0xffff 8000 <= var1 <= 0x0000 7fff.
  1464. |
  1465. | var2 16 bit short signed integer (Word16) whose value falls in the
  1466. | range : 0xffff 8000 <= var1 <= 0x0000 7fff.
  1467. |
  1468. | Return Value :
  1469. |
  1470. | L_var_out
  1471. | 32 bit long signed integer (Word32) whose value falls in the
  1472. | range : 0x8000 0000 <= L_var_out <= 0x7fff ffff.
  1473. |___________________________________________________________________________
  1474. */
  1475. LIBG7221_DEF(Word32) L_mult0 (Word16 var1,Word16 var2)
  1476. {
  1477. Word32 L_var_out;
  1478. L_var_out = (Word32)var1 * (Word32)var2;
  1479. #if (WMOPS)
  1480. multiCounter[currCounter].L_mult0++;
  1481. #endif
  1482. return(L_var_out);
  1483. }
  1484. /* ------------------------- End of L_mult0() ------------------------- */
  1485. /*___________________________________________________________________________
  1486. |
  1487. | Function Name : L_mac0
  1488. |
  1489. | Purpose :
  1490. |
  1491. | Multiply var1 by var2 (without left shift) and add the 32 bit result to
  1492. | L_var3 with saturation, return a 32 bit result:
  1493. | L_mac0(L_var3,var1,var2) = L_add(L_var3,(L_mult0(var1,var2)).
  1494. |
  1495. | Complexity weight : 1
  1496. |
  1497. | Inputs :
  1498. |
  1499. | L_var3 32 bit long signed integer (Word32) whose value falls in the
  1500. | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff.
  1501. |
  1502. | var1 16 bit short signed integer (Word16) whose value falls in the
  1503. | range : 0xffff 8000 <= var1 <= 0x0000 7fff.
  1504. |
  1505. | var2 16 bit short signed integer (Word16) whose value falls in the
  1506. | range : 0xffff 8000 <= var1 <= 0x0000 7fff.
  1507. |
  1508. | Return Value :
  1509. |
  1510. | L_var_out
  1511. | 32 bit long signed integer (Word32) whose value falls in the
  1512. | range : 0x8000 0000 <= L_var_out <= 0x7fff ffff.
  1513. |___________________________________________________________________________
  1514. */
  1515. LIBG7221_DEF(Word32) L_mac0 (Word32 L_var3, Word16 var1, Word16 var2)
  1516. {
  1517. Word32 L_var_out;
  1518. Word32 L_product;
  1519. L_product = L_mult0(var1,var2);
  1520. L_var_out = L_add(L_var3,L_product);
  1521. #if (WMOPS)
  1522. multiCounter[currCounter].L_mac0++;
  1523. multiCounter[currCounter].L_mult0--;
  1524. multiCounter[currCounter].L_add--;
  1525. #endif
  1526. return(L_var_out);
  1527. }
  1528. /* ------------------------- End of L_mac0() ------------------------- */
  1529. /*___________________________________________________________________________
  1530. |
  1531. | Function Name : L_msu0
  1532. |
  1533. | Purpose :
  1534. |
  1535. | Multiply var1 by var2 (without left shift) and subtract the 32 bit
  1536. | result to L_var3 with saturation, return a 32 bit result:
  1537. | L_msu0(L_var3,var1,var2) = L_sub(L_var3,(L_mult0(var1,var2)).
  1538. |
  1539. | Complexity weight : 1
  1540. |
  1541. | Inputs :
  1542. |
  1543. | L_var3 32 bit long signed integer (Word32) whose value falls in the
  1544. | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff.
  1545. |
  1546. | var1 16 bit short signed integer (Word16) whose value falls in the
  1547. | range : 0xffff 8000 <= var1 <= 0x0000 7fff.
  1548. |
  1549. | var2 16 bit short signed integer (Word16) whose value falls in the
  1550. | range : 0xffff 8000 <= var1 <= 0x0000 7fff.
  1551. |
  1552. | Return Value :
  1553. |
  1554. | L_var_out
  1555. | 32 bit long signed integer (Word32) whose value falls in the
  1556. | range : 0x8000 0000 <= L_var_out <= 0x7fff ffff.
  1557. |___________________________________________________________________________
  1558. */
  1559. LIBG7221_DEF(Word32) L_msu0 (Word32 L_var3, Word16 var1, Word16 var2)
  1560. {
  1561. Word32 L_var_out;
  1562. Word32 L_product;
  1563. L_product = L_mult0(var1,var2);
  1564. L_var_out = L_sub(L_var3,L_product);
  1565. #if (WMOPS)
  1566. multiCounter[currCounter].L_msu0++;
  1567. multiCounter[currCounter].L_mult0--;
  1568. multiCounter[currCounter].L_sub--;
  1569. #endif
  1570. return(L_var_out);
  1571. }
  1572. /* ------------------------- End of L_msu0() ------------------------- */
  1573. /*___________________________________________________________________________
  1574. | |
  1575. | Function Name : LU_shl |
  1576. | |
  1577. | Purpose : |
  1578. | |
  1579. | Arithmetically shift the 32 bit input L_var1 left var2 positions. Zero |
  1580. | fill the var2 LSB of the result. If var2 is negative, arithmetically |
  1581. | shift L_var1 right by -var2 with sign extension. Saturate the result in |
  1582. | case of underflows or overflows. |
  1583. | |
  1584. | Complexity weight : 2 |
  1585. | |
  1586. | Inputs : |
  1587. | |
  1588. | L_var1 32 bit long signed integer (Word32) whose value falls in the |
  1589. | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. |
  1590. | |
  1591. | var2 |
  1592. | 16 bit short signed integer (Word16) whose value falls in the |
  1593. | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
  1594. | |
  1595. | Outputs : |
  1596. | |
  1597. | none |
  1598. | |
  1599. | Return Value : |
  1600. | |
  1601. | L_var_out |
  1602. | 32 bit long signed integer (Word32) whose value falls in the |
  1603. | range : 0x8000 0000 <= L_var_out <= 0x7fff ffff. |
  1604. |___________________________________________________________________________|
  1605. */
  1606. LIBG7221_DEF(UWord32) LU_shl (UWord32 L_var1, Word16 var2)
  1607. {
  1608. Word16 neg_var2;
  1609. UWord32 L_var_out = 0;
  1610. if (var2 <= 0)
  1611. {
  1612. if (var2 < -32)
  1613. var2 = -32;
  1614. neg_var2 = negate(var2);
  1615. L_var_out = LU_shr (L_var1, neg_var2);
  1616. #if (WMOPS)
  1617. multiCounter[currCounter].negate--;
  1618. multiCounter[currCounter].LU_shr--;
  1619. #endif
  1620. }
  1621. else
  1622. {
  1623. for (; var2 > 0; var2--)
  1624. {
  1625. if (L_var1 > (UWord32) 0X7fffffffL)
  1626. {
  1627. SET_OVERFLOW(1);
  1628. L_var_out = UMAX_32;
  1629. break;
  1630. }
  1631. else
  1632. {
  1633. if (L_var1 < (UWord32) 0x00000001L)
  1634. {
  1635. SET_OVERFLOW(1);
  1636. L_var_out = (UWord32)MIN_32;
  1637. break;
  1638. }
  1639. }
  1640. L_var1 *= 2;
  1641. L_var_out = L_var1;
  1642. }
  1643. }
  1644. #if (WMOPS)
  1645. multiCounter[currCounter].LU_shl++;
  1646. #endif
  1647. return (L_var_out);
  1648. }
  1649. /* ------------------------- End of LU_shl() ------------------------- */
  1650. /*___________________________________________________________________________
  1651. | |
  1652. | Function Name : LU_shr |
  1653. | |
  1654. | Purpose : |
  1655. | |
  1656. | Arithmetically shift the 32 bit input L_var1 right var2 positions with |
  1657. | sign extension. If var2 is negative, arithmetically shift L_var1 left |
  1658. | by -var2 and zero fill the -var2 LSB of the result. Saturate the result |
  1659. | in case of underflows or overflows. |
  1660. | |
  1661. | Complexity weight : 2 |
  1662. | |
  1663. | Inputs : |
  1664. | |
  1665. | L_var1 32 bit long signed integer (Word32) whose value falls in the |
  1666. | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. |
  1667. | |
  1668. | var2 |
  1669. | 16 bit short signed integer (Word16) whose value falls in the |
  1670. | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
  1671. | |
  1672. | Outputs : |
  1673. | |
  1674. | none |
  1675. | |
  1676. | Return Value : |
  1677. | |
  1678. | L_var_out |
  1679. | 32 bit long signed integer (Word32) whose value falls in the |
  1680. | range : 0x8000 0000 <= L_var_out <= 0x7fff ffff. |
  1681. |___________________________________________________________________________|
  1682. */
  1683. LIBG7221_DEF(UWord32) LU_shr (UWord32 L_var1, Word16 var2)
  1684. {
  1685. Word16 neg_var2;
  1686. UWord32 L_var_out;
  1687. if (var2 < 0)
  1688. {
  1689. if (var2 < -32)
  1690. var2 = -32;
  1691. neg_var2 = negate(var2);
  1692. L_var_out = LU_shl (L_var1, neg_var2);
  1693. #if (WMOPS)
  1694. multiCounter[currCounter].negate--;
  1695. multiCounter[currCounter].LU_shl--;
  1696. #endif
  1697. }
  1698. else
  1699. {
  1700. if (var2 >= 32)
  1701. {
  1702. L_var_out = 0L;
  1703. }
  1704. else
  1705. {
  1706. L_var_out = L_var1 >> var2;
  1707. }
  1708. }
  1709. #if (WMOPS)
  1710. multiCounter[currCounter].LU_shr++;
  1711. #endif
  1712. return (L_var_out);
  1713. }
  1714. /* ------------------------- End of LU_shr() ------------------------- */
  1715. #endif /* PJMEDIA_LIBG7221_FUNCS_INLINED */
  1716. /* ************************** END OF BASOP32.C ************************** */