basic_op_i.h 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. /*___________________________________________________________________________
  2. | |
  3. | Function Name : extract_h |
  4. | |
  5. | Purpose : |
  6. | |
  7. | Return the 16 MSB of L_var1. |
  8. | |
  9. | Complexity weight : 1 |
  10. | |
  11. | Inputs : |
  12. | |
  13. | L_var1 |
  14. | 32 bit long signed integer (Word32 ) whose value falls in the |
  15. | range : 0x8000 0000 <= L_var1 <= 0x7fff ffff. |
  16. | |
  17. | Outputs : |
  18. | |
  19. | none |
  20. | |
  21. | Return Value : |
  22. | |
  23. | var_out |
  24. | 16 bit short signed integer (Word16) whose value falls in the |
  25. | range : 0xffff 8000 <= var_out <= 0x0000 7fff. |
  26. |___________________________________________________________________________|
  27. */
  28. PJ_INLINE(Word16) extract_h (Word32 L_var1)
  29. {
  30. return (Word16) (L_var1 >> 16);
  31. }
  32. /* ------------------------- End of extract_h() ------------------------- */
  33. /*___________________________________________________________________________
  34. | |
  35. | Function Name : extract_l |
  36. | |
  37. | Purpose : |
  38. | |
  39. | Return the 16 LSB of L_var1. |
  40. | |
  41. | Complexity weight : 1 |
  42. | |
  43. | Inputs : |
  44. | |
  45. | L_var1 |
  46. | 32 bit long signed integer (Word32 ) whose value falls in the |
  47. | range : 0x8000 0000 <= L_var1 <= 0x7fff ffff. |
  48. | |
  49. | Outputs : |
  50. | |
  51. | none |
  52. | |
  53. | Return Value : |
  54. | |
  55. | var_out |
  56. | 16 bit short signed integer (Word16) whose value falls in the |
  57. | range : 0xffff 8000 <= var_out <= 0x0000 7fff. |
  58. |___________________________________________________________________________|
  59. */
  60. PJ_INLINE(Word16) extract_l (Word32 L_var1)
  61. {
  62. return (Word16) L_var1;
  63. }
  64. /* ------------------------- End of extract_l() ------------------------- */
  65. /*___________________________________________________________________________
  66. | |
  67. | Function Name : saturate |
  68. | |
  69. | Purpose : |
  70. | |
  71. | Limit the 32 bit input to the range of a 16 bit word. |
  72. | |
  73. | Inputs : |
  74. | |
  75. | L_var1 |
  76. | 32 bit long signed integer (Word32) whose value falls in the |
  77. | range : 0x8000 0000 <= L_var1 <= 0x7fff ffff. |
  78. | |
  79. | Outputs : |
  80. | |
  81. | none |
  82. | |
  83. | Return Value : |
  84. | |
  85. | var_out |
  86. | 16 bit short signed integer (Word16) whose value falls in the |
  87. | range : 0xffff 8000 <= var_out <= 0x0000 7fff. |
  88. |___________________________________________________________________________|
  89. */
  90. PJ_INLINE(Word16) saturate (Word32 L_var1)
  91. {
  92. Word16 val16 = (Word16) L_var1;
  93. if (val16 == L_var1)
  94. return val16;
  95. if (L_var1 > MAX_16)
  96. return MAX_16;
  97. return MIN_16;
  98. }
  99. /* ------------------------- End of saturate() ------------------------- */
  100. #if PJ_HAS_INT64
  101. PJ_INLINE(Word32) L_saturate (pj_int64_t LL_var1)
  102. {
  103. pj_int32_t L_var1 = (pj_int32_t)LL_var1;
  104. if (LL_var1 == L_var1)
  105. return L_var1;
  106. else if (LL_var1 > MAX_32)
  107. return MAX_32;
  108. else
  109. return MIN_32;
  110. }
  111. #endif
  112. /*___________________________________________________________________________
  113. | |
  114. | Function Name : add |
  115. | |
  116. | Purpose : |
  117. | |
  118. | Performs the addition (var1+var2) with overflow control and saturation;|
  119. | the 16 bit result is set at +32767 when overflow occurs or at -32768 |
  120. | when underflow occurs. |
  121. | |
  122. | Complexity weight : 1 |
  123. | |
  124. | Inputs : |
  125. | |
  126. | var1 |
  127. | 16 bit short signed integer (Word16) whose value falls in the |
  128. | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
  129. | |
  130. | var2 |
  131. | 16 bit short signed integer (Word16) whose value falls in the |
  132. | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
  133. | |
  134. | Outputs : |
  135. | |
  136. | none |
  137. | |
  138. | Return Value : |
  139. | |
  140. | var_out |
  141. | 16 bit short signed integer (Word16) whose value falls in the |
  142. | range : 0xffff 8000 <= var_out <= 0x0000 7fff. |
  143. |___________________________________________________________________________|
  144. */
  145. PJ_INLINE(Word16) add (Word16 var1, Word16 var2)
  146. {
  147. return saturate (var1 + var2);
  148. }
  149. /* ------------------------- End of add() ------------------------- */
  150. /*___________________________________________________________________________
  151. | |
  152. | Function Name : sub |
  153. | |
  154. | Purpose : |
  155. | |
  156. | Performs the subtraction (var1+var2) with overflow control and satu- |
  157. | ration; the 16 bit result is set at +32767 when overflow occurs or at |
  158. | -32768 when underflow occurs. |
  159. | |
  160. | Complexity weight : 1 |
  161. | |
  162. | Inputs : |
  163. | |
  164. | var1 |
  165. | 16 bit short signed integer (Word16) whose value falls in the |
  166. | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
  167. | |
  168. | var2 |
  169. | 16 bit short signed integer (Word16) whose value falls in the |
  170. | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
  171. | |
  172. | Outputs : |
  173. | |
  174. | none |
  175. | |
  176. | Return Value : |
  177. | |
  178. | var_out |
  179. | 16 bit short signed integer (Word16) whose value falls in the |
  180. | range : 0xffff 8000 <= var_out <= 0x0000 7fff. |
  181. |___________________________________________________________________________|
  182. */
  183. PJ_INLINE(Word16) sub (Word16 var1, Word16 var2)
  184. {
  185. return saturate ((Word32) var1 - var2);
  186. }
  187. /* ------------------------- End of sub() ------------------------- */
  188. /*___________________________________________________________________________
  189. | |
  190. | Function Name : negate |
  191. | |
  192. | Purpose : |
  193. | |
  194. | Negate var1 with saturation, saturate in the case where input is -32768:|
  195. | negate(var1) = sub(0,var1). |
  196. | |
  197. | Complexity weight : 1 |
  198. | |
  199. | Inputs : |
  200. | |
  201. | var1 |
  202. | 16 bit short signed integer (Word16) whose value falls in the |
  203. | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
  204. | |
  205. | Outputs : |
  206. | |
  207. | none |
  208. | |
  209. | Return Value : |
  210. | |
  211. | var_out |
  212. | 16 bit short signed integer (Word16) whose value falls in the |
  213. | range : 0xffff 8000 <= var_out <= 0x0000 7fff. |
  214. |___________________________________________________________________________|
  215. */
  216. PJ_INLINE(Word16) negate (Word16 var1)
  217. {
  218. return (Word16)((var1 == MIN_16) ? MAX_16 : -var1);
  219. }
  220. /* ------------------------- End of negate() ------------------------- */
  221. /*___________________________________________________________________________
  222. | |
  223. | Function Name : L_add |
  224. | |
  225. | Purpose : |
  226. | |
  227. | 32 bits addition of the two 32 bits variables (L_var1+L_var2) with |
  228. | overflow control and saturation; the result is set at +2147483647 when |
  229. | overflow occurs or at -2147483648 when underflow occurs. |
  230. | |
  231. | Complexity weight : 2 |
  232. | |
  233. | Inputs : |
  234. | |
  235. | L_var1 32 bit long signed integer (Word32) whose value falls in the |
  236. | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. |
  237. | |
  238. | L_var2 32 bit long signed integer (Word32) whose value falls in the |
  239. | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. |
  240. | |
  241. | Outputs : |
  242. | |
  243. | none |
  244. | |
  245. | Return Value : |
  246. | |
  247. | L_var_out |
  248. | 32 bit long signed integer (Word32) whose value falls in the |
  249. | range : 0x8000 0000 <= L_var_out <= 0x7fff ffff. |
  250. |___________________________________________________________________________|
  251. */
  252. PJ_INLINE(Word32) L_add (Word32 L_var1, Word32 L_var2)
  253. {
  254. #if PJ_HAS_INT64
  255. return L_saturate(((pj_int64_t)L_var1) + L_var2);
  256. #else
  257. Word32 L_var_out;
  258. L_var_out = L_var1 + L_var2;
  259. if (((L_var1 ^ L_var2) & MIN_32) == 0)
  260. {
  261. if ((L_var_out ^ L_var1) & MIN_32)
  262. {
  263. SET_OVERFLOW(1);
  264. L_var_out = (L_var1 < 0) ? MIN_32 : MAX_32;
  265. }
  266. }
  267. return (L_var_out);
  268. #endif
  269. }
  270. /* ------------------------- End of L_add() ------------------------- */
  271. /*___________________________________________________________________________
  272. | |
  273. | Function Name : L_sub |
  274. | |
  275. | Purpose : |
  276. | |
  277. | 32 bits subtraction of the two 32 bits variables (L_var1-L_var2) with |
  278. | overflow control and saturation; the result is set at +2147483647 when |
  279. | overflow occurs or at -2147483648 when underflow occurs. |
  280. | |
  281. | Complexity weight : 2 |
  282. | |
  283. | Inputs : |
  284. | |
  285. | L_var1 32 bit long signed integer (Word32) whose value falls in the |
  286. | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. |
  287. | |
  288. | L_var2 32 bit long signed integer (Word32) whose value falls in the |
  289. | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. |
  290. | |
  291. | Outputs : |
  292. | |
  293. | none |
  294. | |
  295. | Return Value : |
  296. | |
  297. | L_var_out |
  298. | 32 bit long signed integer (Word32) whose value falls in the |
  299. | range : 0x8000 0000 <= L_var_out <= 0x7fff ffff. |
  300. |___________________________________________________________________________|
  301. */
  302. PJ_INLINE(Word32) L_sub (Word32 L_var1, Word32 L_var2)
  303. {
  304. #if PJ_HAS_INT64
  305. return L_saturate((pj_int64_t)L_var1 - L_var2);
  306. #else
  307. Word32 L_var_out;
  308. L_var_out = L_var1 - L_var2;
  309. if (((L_var1 ^ L_var2) & MIN_32) != 0)
  310. {
  311. if ((L_var_out ^ L_var1) & MIN_32)
  312. {
  313. L_var_out = (L_var1 < 0L) ? MIN_32 : MAX_32;
  314. SET_OVERFLOW(1);
  315. }
  316. }
  317. return (L_var_out);
  318. #endif
  319. }
  320. /* ------------------------- End of L_sub() ------------------------- */
  321. /*___________________________________________________________________________
  322. | |
  323. | Function Name : L_mult |
  324. | |
  325. | Purpose : |
  326. | |
  327. | L_mult is the 32 bit result of the multiplication of var1 times var2 |
  328. | with one shift left i.e.: |
  329. | L_mult(var1,var2) = L_shl((var1 times var2),1) and |
  330. | L_mult(-32768,-32768) = 2147483647. |
  331. | |
  332. | Complexity weight : 1 |
  333. | |
  334. | Inputs : |
  335. | |
  336. | var1 |
  337. | 16 bit short signed integer (Word16) whose value falls in the |
  338. | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
  339. | |
  340. | var2 |
  341. | 16 bit short signed integer (Word16) whose value falls in the |
  342. | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
  343. | |
  344. | Outputs : |
  345. | |
  346. | none |
  347. | |
  348. | Return Value : |
  349. | |
  350. | L_var_out |
  351. | 32 bit long signed integer (Word32) whose value falls in the |
  352. | range : 0x8000 0000 <= L_var_out <= 0x7fff ffff. |
  353. |___________________________________________________________________________|
  354. */
  355. PJ_INLINE(Word32) L_mult (Word16 var1, Word16 var2)
  356. {
  357. Word32 L_var_out;
  358. L_var_out = (Word32) var1 *(Word32) var2;
  359. if (L_var_out != (Word32) 0x40000000L)
  360. {
  361. return L_var_out << 1;
  362. }
  363. else
  364. {
  365. SET_OVERFLOW(1);
  366. return MAX_32;
  367. }
  368. }
  369. /* ------------------------- End of L_mult() ------------------------- */
  370. /*___________________________________________________________________________
  371. | |
  372. | Function Name : L_mac |
  373. | |
  374. | Purpose : |
  375. | |
  376. | Multiply var1 by var2 and shift the result left by 1. Add the 32 bit |
  377. | result to L_var3 with saturation, return a 32 bit result: |
  378. | L_mac(L_var3,var1,var2) = L_add(L_var3,L_mult(var1,var2)). |
  379. | |
  380. | Complexity weight : 1 |
  381. | |
  382. | Inputs : |
  383. | |
  384. | L_var3 32 bit long signed integer (Word32) whose value falls in the |
  385. | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. |
  386. | |
  387. | var1 |
  388. | 16 bit short signed integer (Word16) whose value falls in the |
  389. | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
  390. | |
  391. | var2 |
  392. | 16 bit short signed integer (Word16) whose value falls in the |
  393. | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
  394. | |
  395. | Outputs : |
  396. | |
  397. | none |
  398. | |
  399. | Return Value : |
  400. | |
  401. | L_var_out |
  402. | 32 bit long signed integer (Word32) whose value falls in the |
  403. | range : 0x8000 0000 <= L_var_out <= 0x7fff ffff. |
  404. |___________________________________________________________________________|
  405. */
  406. PJ_INLINE(Word32) L_mac (Word32 L_var3, Word16 var1, Word16 var2)
  407. {
  408. return L_add (L_var3, L_mult (var1, var2));
  409. }
  410. /* ------------------------- End of L_mac() ------------------------- */
  411. /*___________________________________________________________________________
  412. | |
  413. | Function Name : round |
  414. | |
  415. | Purpose : |
  416. | |
  417. | Round the lower 16 bits of the 32 bit input number into the MS 16 bits |
  418. | with saturation. Shift the resulting bits right by 16 and return the 16 |
  419. | bit number: |
  420. | round(L_var1) = extract_h(L_add(L_var1,32768)) |
  421. | |
  422. | Complexity weight : 1 |
  423. | |
  424. | Inputs : |
  425. | |
  426. | L_var1 |
  427. | 32 bit long signed integer (Word32 ) whose value falls in the |
  428. | range : 0x8000 0000 <= L_var1 <= 0x7fff ffff. |
  429. | |
  430. | Outputs : |
  431. | |
  432. | none |
  433. | |
  434. | Return Value : |
  435. | |
  436. | var_out |
  437. | 16 bit short signed integer (Word16) whose value falls in the |
  438. | range : 0xffff 8000 <= var_out <= 0x0000 7fff. |
  439. |___________________________________________________________________________|
  440. */
  441. PJ_INLINE(Word16) itu_round (Word32 L_var1)
  442. {
  443. return extract_h (L_add (L_var1, (Word32) 0x00008000L));
  444. }
  445. /* ------------------------- End of round() ------------------------- */
  446. /*___________________________________________________________________________
  447. | |
  448. | Function Name : L_shr |
  449. | |
  450. | Purpose : |
  451. | |
  452. | Arithmetically shift the 32 bit input L_var1 right var2 positions with |
  453. | sign extension. If var2 is negative, arithmetically shift L_var1 left |
  454. | by -var2 and zero fill the -var2 LSB of the result. Saturate the result |
  455. | in case of underflows or overflows. |
  456. | |
  457. | Complexity weight : 2 |
  458. | |
  459. | Inputs : |
  460. | |
  461. | L_var1 32 bit long signed integer (Word32) whose value falls in the |
  462. | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. |
  463. | |
  464. | var2 |
  465. | 16 bit short signed integer (Word16) whose value falls in the |
  466. | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
  467. | |
  468. | Outputs : |
  469. | |
  470. | none |
  471. | |
  472. | Return Value : |
  473. | |
  474. | L_var_out |
  475. | 32 bit long signed integer (Word32) whose value falls in the |
  476. | range : 0x8000 0000 <= L_var_out <= 0x7fff ffff. |
  477. |___________________________________________________________________________|
  478. */
  479. PJ_INLINE(Word32) L_shr_nocheck(Word32 L_var1, Word16 var2)
  480. {
  481. #if 1
  482. return L_var1 >> var2;
  483. #else
  484. if (var2 >= 31)
  485. {
  486. return (L_var1 < 0L) ? -1 : 0;
  487. }
  488. else
  489. {
  490. if (L_var1 < 0)
  491. {
  492. return ~((~L_var1) >> var2);
  493. }
  494. else
  495. {
  496. return L_var1 >> var2;
  497. }
  498. }
  499. #endif
  500. }
  501. PJ_INLINE(Word32) L_shl_nocheck (Word32 L_var1, Word16 var2)
  502. {
  503. #if PJ_HAS_INT64
  504. return L_saturate( ((pj_int64_t)L_var1) << var2 );
  505. #else
  506. for (; var2 > 0; var2--)
  507. {
  508. if (L_var1 > (Word32) 0X3fffffffL)
  509. {
  510. SET_OVERFLOW(1);
  511. return MAX_32;
  512. }
  513. else
  514. {
  515. if (L_var1 < (Word32) 0xc0000000L)
  516. {
  517. SET_OVERFLOW(1);
  518. return MIN_32;
  519. }
  520. }
  521. L_var1 <<= 1;
  522. }
  523. return (L_var1);
  524. #endif
  525. }
  526. PJ_INLINE(Word32) L_shr (Word32 L_var1, Word16 var2)
  527. {
  528. if (var2 < 0)
  529. {
  530. if (var2 < -32)
  531. var2 = -32;
  532. return L_shl_nocheck (L_var1, (Word16) -var2);
  533. }
  534. else
  535. {
  536. return L_shr_nocheck(L_var1, var2);
  537. }
  538. }
  539. /* ------------------------- End of L_shr() ------------------------- */
  540. /*___________________________________________________________________________
  541. | |
  542. | Function Name : L_shl |
  543. | |
  544. | Purpose : |
  545. | |
  546. | Arithmetically shift the 32 bit input L_var1 left var2 positions. Zero |
  547. | fill the var2 LSB of the result. If var2 is negative, arithmetically |
  548. | shift L_var1 right by -var2 with sign extension. Saturate the result in |
  549. | case of underflows or overflows. |
  550. | |
  551. | Complexity weight : 2 |
  552. | |
  553. | Inputs : |
  554. | |
  555. | L_var1 32 bit long signed integer (Word32) whose value falls in the |
  556. | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. |
  557. | |
  558. | var2 |
  559. | 16 bit short signed integer (Word16) whose value falls in the |
  560. | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
  561. | |
  562. | Outputs : |
  563. | |
  564. | none |
  565. | |
  566. | Return Value : |
  567. | |
  568. | L_var_out |
  569. | 32 bit long signed integer (Word32) whose value falls in the |
  570. | range : 0x8000 0000 <= L_var_out <= 0x7fff ffff. |
  571. |___________________________________________________________________________|
  572. */
  573. PJ_INLINE(Word32) L_shl (Word32 L_var1, Word16 var2)
  574. {
  575. if (var2 <= 0)
  576. {
  577. if (var2 < -32)
  578. var2 = -32;
  579. return L_shr_nocheck(L_var1, (Word16) -var2);
  580. }
  581. else
  582. {
  583. return L_shl_nocheck(L_var1, var2);
  584. }
  585. }
  586. /* ------------------------- End of L_shl() ------------------------- */
  587. /*___________________________________________________________________________
  588. | |
  589. | Function Name : abs_s |
  590. | |
  591. | Purpose : |
  592. | |
  593. | Absolute value of var1; abs_s(-32768) = 32767. |
  594. | |
  595. | Complexity weight : 1 |
  596. | |
  597. | Inputs : |
  598. | |
  599. | var1 |
  600. | 16 bit short signed integer (Word16) whose value falls in the |
  601. | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
  602. | |
  603. | Outputs : |
  604. | |
  605. | none |
  606. | |
  607. | Return Value : |
  608. | |
  609. | var_out |
  610. | 16 bit short signed integer (Word16) whose value falls in the |
  611. | range : 0x0000 0000 <= var_out <= 0x0000 7fff. |
  612. |___________________________________________________________________________|
  613. */
  614. PJ_INLINE(Word16) abs_s (Word16 var1)
  615. {
  616. #if 1
  617. if (var1 >= 0)
  618. return var1;
  619. else if (var1 == MIN_16)
  620. return MAX_16;
  621. else
  622. return (Word16)-var1;
  623. #else
  624. if (var1 == MIN_16)
  625. {
  626. return MAX_16;
  627. }
  628. else
  629. {
  630. if (var1 < 0)
  631. {
  632. return (Word16)-var1;
  633. }
  634. else
  635. {
  636. return var1;
  637. }
  638. }
  639. #endif
  640. }
  641. /* ------------------------- End of abs_s() ------------------------- */
  642. PJ_INLINE(Word16) shl_nocheck(Word16 var1, Word16 var2)
  643. {
  644. #if 1
  645. /* blp: this should be more optimized */
  646. return saturate (((Word32)var1) << var2);
  647. #else
  648. /* Original algorithm */
  649. Word32 result = (Word32) var1 *((Word32) 1 << var2);
  650. if ((var2 > 15 && var1 != 0) || (result != (Word32) ((Word16) result)))
  651. {
  652. SET_OVERFLOW(1);
  653. return (Word16) ((var1 > 0) ? MAX_16 : MIN_16);
  654. }
  655. else
  656. {
  657. return extract_l (result);
  658. }
  659. #endif
  660. }
  661. PJ_INLINE(Word16) shr_nocheck(Word16 var1, Word16 var2)
  662. {
  663. #if 1
  664. /* blp: this should yield the same value */
  665. return (Word16) (var1 >> var2);
  666. #else
  667. /* Original algorithm */
  668. if (var2 >= 15)
  669. {
  670. return (Word16)((var1 < 0) ? -1 : 0);
  671. }
  672. else
  673. {
  674. if (var1 < 0)
  675. {
  676. return (Word16) (~((~var1) >> var2));
  677. }
  678. else
  679. {
  680. return (Word16)(var1 >> var2);
  681. }
  682. }
  683. #endif
  684. }