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