blob: 86d97dd623b80347bff79c8898d930e15dae8252 [file] [log] [blame]
Nanang Izzuddin57b88572009-04-01 12:05:34 +00001/* v.1.0 - 26.Jan.2000
2 =============================================================================
3
4 U U GGG SSSS TTTTT
5 U U G S T
6 U U G GG SSSS T
7 U U G G S T
8 UUU GG SSS T
9
10 ========================================
11 ITU-T - USER'S GROUP ON SOFTWARE TOOLS
12 ========================================
13
14 =============================================================
15 COPYRIGHT NOTE: This source code, and all of its derivations,
16 is subject to the "ITU-T General Public License". Please have
17 it read in the distribution disk, or in the ITU-T
18 Recommendation G.191 on "SOFTWARE TOOLS FOR SPEECH AND AUDIO
19 CODING STANDARDS".
20 =============================================================
21
22MODULE: BASOP, BASIC OPERATORS
23
24ORIGINAL BY:
25 Incorporated from anonymous contributions for
26 ETSI Standards as well as G.723.1, G.729, and G.722.1
27
28DESCRIPTION:
29 This file contains the definition of 16- and 32-bit basic
30 operators to be used in the implementation of signal
31 processing algorithms. The basic operators try to resemble
32 assembly language instructions that are commonly found in
33 digital signal processor (DSP) CPUs, thus allowing algorithm
34 C-code implementations more directly mapeable to DSP assembly
35 code.
36
37 *********************************************************
38 NOTE: so far, this module does not have a demo program!
39 *********************************************************
40
41FUNCTIONS:
42 Defined in basop.h. Self-documentation within each function.
43
44HISTORY:
45 26.Jan.00 v1.0 Incorporated to the STL from updated G.723.1/G.729
46 basic operator library (based on basicop2.c) and
47 G.723.1's basop.c [L_mls(), div_l(), i_mult()]
48 05.Jul.00 v1.1 Added 32-bit shiftless accumulation basic
49 operators (L_msu0, L_mac0, L_mult0). Improved
50 documentation for i_mult().
51 =============================================================================
52*/
53
54/*___________________________________________________________________________
55 | |
56 | Basic arithmetic operators. |
57 | |
58 | $Id $
59 |___________________________________________________________________________|
60*/
61
62/*___________________________________________________________________________
63 | |
64 | Include-Files |
65 |___________________________________________________________________________|
66*/
67
68#include <stdio.h>
69#include <stdlib.h>
70#include "typedef.h"
71#include "basop32.h"
72
73#if (WMOPS)
74#include "count.h"
75extern BASIC_OP multiCounter[MAXCOUNTERS];
76extern int currCounter;
77
78#endif
79
80/*___________________________________________________________________________
81 | |
82 | Local Functions |
83 |___________________________________________________________________________|
84*/
85Word16 saturate (Word32 L_var1);
86
87/*___________________________________________________________________________
88 | |
89 | Constants and Globals |
90 |___________________________________________________________________________|
91*/
92Flag Overflow = 0;
93Flag Carry = 0;
94
95/*___________________________________________________________________________
96 | |
97 | Functions |
98 |___________________________________________________________________________|
99*/
100
101/*___________________________________________________________________________
102 | |
103 | Function Name : saturate |
104 | |
105 | Purpose : |
106 | |
107 | Limit the 32 bit input to the range of a 16 bit word. |
108 | |
109 | Inputs : |
110 | |
111 | L_var1 |
112 | 32 bit long signed integer (Word32) whose value falls in the |
113 | range : 0x8000 0000 <= L_var1 <= 0x7fff ffff. |
114 | |
115 | Outputs : |
116 | |
117 | none |
118 | |
119 | Return Value : |
120 | |
121 | var_out |
122 | 16 bit short signed integer (Word16) whose value falls in the |
123 | range : 0xffff 8000 <= var_out <= 0x0000 7fff. |
124 |___________________________________________________________________________|
125*/
126Word16 saturate (Word32 L_var1)
127{
128 Word16 var_out;
129
130 if (L_var1 > 0X00007fffL)
131 {
132 Overflow = 1;
133 var_out = MAX_16;
134 }
135 else if (L_var1 < (Word32) 0xffff8000L)
136 {
137 Overflow = 1;
138 var_out = MIN_16;
139 }
140 else
141 {
142 var_out = extract_l (L_var1);
143#if (WMOPS)
144 multiCounter[currCounter].extract_l--;
145#endif
146 }
147
148 return (var_out);
149}
150/* ------------------------- End of saturate() ------------------------- */
151
152
153/*___________________________________________________________________________
154 | |
155 | Function Name : add |
156 | |
157 | Purpose : |
158 | |
159 | Performs the addition (var1+var2) with overflow control and saturation;|
160 | the 16 bit result is set at +32767 when overflow occurs or at -32768 |
161 | when underflow occurs. |
162 | |
163 | Complexity weight : 1 |
164 | |
165 | Inputs : |
166 | |
167 | var1 |
168 | 16 bit short signed integer (Word16) whose value falls in the |
169 | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
170 | |
171 | var2 |
172 | 16 bit short signed integer (Word16) whose value falls in the |
173 | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
174 | |
175 | Outputs : |
176 | |
177 | none |
178 | |
179 | Return Value : |
180 | |
181 | var_out |
182 | 16 bit short signed integer (Word16) whose value falls in the |
183 | range : 0xffff 8000 <= var_out <= 0x0000 7fff. |
184 |___________________________________________________________________________|
185*/
186Word16 add (Word16 var1, Word16 var2)
187{
188 Word16 var_out;
189 Word32 L_sum;
190
191 L_sum = (Word32) var1 + var2;
192 var_out = saturate (L_sum);
193#if (WMOPS)
194 multiCounter[currCounter].add++;
195#endif
196 return (var_out);
197}
198/* ------------------------- End of add() ------------------------- */
199
200
201/*___________________________________________________________________________
202 | |
203 | Function Name : sub |
204 | |
205 | Purpose : |
206 | |
207 | Performs the subtraction (var1+var2) with overflow control and satu- |
208 | ration; the 16 bit result is set at +32767 when overflow occurs or at |
209 | -32768 when underflow occurs. |
210 | |
211 | Complexity weight : 1 |
212 | |
213 | Inputs : |
214 | |
215 | var1 |
216 | 16 bit short signed integer (Word16) whose value falls in the |
217 | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
218 | |
219 | var2 |
220 | 16 bit short signed integer (Word16) whose value falls in the |
221 | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
222 | |
223 | Outputs : |
224 | |
225 | none |
226 | |
227 | Return Value : |
228 | |
229 | var_out |
230 | 16 bit short signed integer (Word16) whose value falls in the |
231 | range : 0xffff 8000 <= var_out <= 0x0000 7fff. |
232 |___________________________________________________________________________|
233*/
234Word16 sub (Word16 var1, Word16 var2)
235{
236 Word16 var_out;
237 Word32 L_diff;
238
239 L_diff = (Word32) var1 - var2;
240 var_out = saturate (L_diff);
241#if (WMOPS)
242 multiCounter[currCounter].sub++;
243#endif
244 return (var_out);
245}
246/* ------------------------- End of sub() ------------------------- */
247
248
249/*___________________________________________________________________________
250 | |
251 | Function Name : abs_s |
252 | |
253 | Purpose : |
254 | |
255 | Absolute value of var1; abs_s(-32768) = 32767. |
256 | |
257 | Complexity weight : 1 |
258 | |
259 | Inputs : |
260 | |
261 | var1 |
262 | 16 bit short signed integer (Word16) whose value falls in the |
263 | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
264 | |
265 | Outputs : |
266 | |
267 | none |
268 | |
269 | Return Value : |
270 | |
271 | var_out |
272 | 16 bit short signed integer (Word16) whose value falls in the |
273 | range : 0x0000 0000 <= var_out <= 0x0000 7fff. |
274 |___________________________________________________________________________|
275*/
276Word16 abs_s (Word16 var1)
277{
278 Word16 var_out;
279
280 if (var1 == (Word16) 0X8000)
281 {
282 var_out = MAX_16;
283 }
284 else
285 {
286 if (var1 < 0)
287 {
288 var_out = -var1;
289 }
290 else
291 {
292 var_out = var1;
293 }
294 }
295#if (WMOPS)
296 multiCounter[currCounter].abs_s++;
297#endif
298 return (var_out);
299}
300/* ------------------------- End of abs_s() ------------------------- */
301
302
303/*___________________________________________________________________________
304 | |
305 | Function Name : shl |
306 | |
307 | Purpose : |
308 | |
309 | Arithmetically shift the 16 bit input var1 left var2 positions.Zero fill|
310 | the var2 LSB of the result. If var2 is negative, arithmetically shift |
311 | var1 right by -var2 with sign extension. Saturate the result in case of |
312 | underflows or overflows. |
313 | |
314 | Complexity weight : 1 |
315 | |
316 | Inputs : |
317 | |
318 | var1 |
319 | 16 bit short signed integer (Word16) whose value falls in the |
320 | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
321 | |
322 | var2 |
323 | 16 bit short signed integer (Word16) whose value falls in the |
324 | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
325 | |
326 | Outputs : |
327 | |
328 | none |
329 | |
330 | Return Value : |
331 | |
332 | var_out |
333 | 16 bit short signed integer (Word16) whose value falls in the |
334 | range : 0xffff 8000 <= var_out <= 0x0000 7fff. |
335 |___________________________________________________________________________|
336*/
337Word16 shl (Word16 var1, Word16 var2)
338{
339 Word16 var_out;
340 Word32 result;
341
342 if (var2 < 0)
343 {
344 if (var2 < -16)
345 var2 = -16;
346 var_out = shr (var1, (Word16) -var2);
347#if (WMOPS)
348 multiCounter[currCounter].shr--;
349#endif
350 }
351 else
352 {
353 result = (Word32) var1 *((Word32) 1 << var2);
354
355 if ((var2 > 15 && var1 != 0) || (result != (Word32) ((Word16) result)))
356 {
357 Overflow = 1;
358 var_out = (var1 > 0) ? MAX_16 : MIN_16;
359 }
360 else
361 {
362 var_out = extract_l (result);
363#if (WMOPS)
364 multiCounter[currCounter].extract_l--;
365#endif
366 }
367 }
368#if (WMOPS)
369 multiCounter[currCounter].shl++;
370#endif
371 return (var_out);
372}
373/* ------------------------- End of shl() ------------------------- */
374
375
376/*___________________________________________________________________________
377 | |
378 | Function Name : shr |
379 | |
380 | Purpose : |
381 | |
382 | Arithmetically shift the 16 bit input var1 right var2 positions with |
383 | sign extension. If var2 is negative, arithmetically shift var1 left by |
384 | -var2 with sign extension. Saturate the result in case of underflows or |
385 | overflows. |
386 | |
387 | Complexity weight : 1 |
388 | |
389 | Inputs : |
390 | |
391 | var1 |
392 | 16 bit short signed integer (Word16) whose value falls in the |
393 | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
394 | |
395 | var2 |
396 | 16 bit short signed integer (Word16) whose value falls in the |
397 | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
398 | |
399 | Outputs : |
400 | |
401 | none |
402 | |
403 | Return Value : |
404 | |
405 | var_out |
406 | 16 bit short signed integer (Word16) whose value falls in the |
407 | range : 0xffff 8000 <= var_out <= 0x0000 7fff. |
408 |___________________________________________________________________________|
409*/
410Word16 shr (Word16 var1, Word16 var2)
411{
412 Word16 var_out;
413
414 if (var2 < 0)
415 {
416 if (var2 < -16)
417 var2 = -16;
418 var_out = shl (var1, (Word16) -var2);
419#if (WMOPS)
420 multiCounter[currCounter].shl--;
421#endif
422 }
423 else
424 {
425 if (var2 >= 15)
426 {
427 var_out = (var1 < 0) ? -1 : 0;
428 }
429 else
430 {
431 if (var1 < 0)
432 {
433 var_out = ~((~var1) >> var2);
434 }
435 else
436 {
437 var_out = var1 >> var2;
438 }
439 }
440 }
441
442#if (WMOPS)
443 multiCounter[currCounter].shr++;
444#endif
445 return (var_out);
446}
447/* ------------------------- End of shr() ------------------------- */
448
449
450/*___________________________________________________________________________
451 | |
452 | Function Name : mult |
453 | |
454 | Purpose : |
455 | |
456 | Performs the multiplication of var1 by var2 and gives a 16 bit result |
457 | which is scaled i.e.: |
458 | mult(var1,var2) = extract_l(L_shr((var1 times var2),15)) and |
459 | mult(-32768,-32768) = 32767. |
460 | |
461 | Complexity weight : 1 |
462 | |
463 | Inputs : |
464 | |
465 | var1 |
466 | 16 bit short signed integer (Word16) whose value falls in the |
467 | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
468 | |
469 | var2 |
470 | 16 bit short signed integer (Word16) whose value falls in the |
471 | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
472 | |
473 | Outputs : |
474 | |
475 | none |
476 | |
477 | Return Value : |
478 | |
479 | var_out |
480 | 16 bit short signed integer (Word16) whose value falls in the |
481 | range : 0xffff 8000 <= var_out <= 0x0000 7fff. |
482 |___________________________________________________________________________|
483*/
484Word16 mult (Word16 var1, Word16 var2)
485{
486 Word16 var_out;
487 Word32 L_product;
488
489 L_product = (Word32) var1 *(Word32) var2;
490
491 L_product = (L_product & (Word32) 0xffff8000L) >> 15;
492
493 if (L_product & (Word32) 0x00010000L)
494 L_product = L_product | (Word32) 0xffff0000L;
495
496 var_out = saturate (L_product);
497#if (WMOPS)
498 multiCounter[currCounter].mult++;
499#endif
500 return (var_out);
501}
502/* ------------------------- End of mult() ------------------------- */
503
504
505/*___________________________________________________________________________
506 | |
507 | Function Name : L_mult |
508 | |
509 | Purpose : |
510 | |
511 | L_mult is the 32 bit result of the multiplication of var1 times var2 |
512 | with one shift left i.e.: |
513 | L_mult(var1,var2) = L_shl((var1 times var2),1) and |
514 | L_mult(-32768,-32768) = 2147483647. |
515 | |
516 | Complexity weight : 1 |
517 | |
518 | Inputs : |
519 | |
520 | var1 |
521 | 16 bit short signed integer (Word16) whose value falls in the |
522 | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
523 | |
524 | var2 |
525 | 16 bit short signed integer (Word16) whose value falls in the |
526 | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
527 | |
528 | Outputs : |
529 | |
530 | none |
531 | |
532 | Return Value : |
533 | |
534 | L_var_out |
535 | 32 bit long signed integer (Word32) whose value falls in the |
536 | range : 0x8000 0000 <= L_var_out <= 0x7fff ffff. |
537 |___________________________________________________________________________|
538*/
539Word32 L_mult (Word16 var1, Word16 var2)
540{
541 Word32 L_var_out;
542
543 L_var_out = (Word32) var1 *(Word32) var2;
544
545 if (L_var_out != (Word32) 0x40000000L)
546 {
547 L_var_out *= 2;
548 }
549 else
550 {
551 Overflow = 1;
552 L_var_out = MAX_32;
553 }
554
555#if (WMOPS)
556 multiCounter[currCounter].L_mult++;
557#endif
558 return (L_var_out);
559}
560/* ------------------------- End of L_mult() ------------------------- */
561
562
563/*___________________________________________________________________________
564 | |
565 | Function Name : negate |
566 | |
567 | Purpose : |
568 | |
569 | Negate var1 with saturation, saturate in the case where input is -32768:|
570 | negate(var1) = sub(0,var1). |
571 | |
572 | Complexity weight : 1 |
573 | |
574 | Inputs : |
575 | |
576 | var1 |
577 | 16 bit short signed integer (Word16) whose value falls in the |
578 | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
579 | |
580 | Outputs : |
581 | |
582 | none |
583 | |
584 | Return Value : |
585 | |
586 | var_out |
587 | 16 bit short signed integer (Word16) whose value falls in the |
588 | range : 0xffff 8000 <= var_out <= 0x0000 7fff. |
589 |___________________________________________________________________________|
590*/
591Word16 negate (Word16 var1)
592{
593 Word16 var_out;
594
595 var_out = (var1 == MIN_16) ? MAX_16 : -var1;
596#if (WMOPS)
597 multiCounter[currCounter].negate++;
598#endif
599 return (var_out);
600}
601/* ------------------------- End of negate() ------------------------- */
602
603
604/*___________________________________________________________________________
605 | |
606 | Function Name : extract_h |
607 | |
608 | Purpose : |
609 | |
610 | Return the 16 MSB of L_var1. |
611 | |
612 | Complexity weight : 1 |
613 | |
614 | Inputs : |
615 | |
616 | L_var1 |
617 | 32 bit long signed integer (Word32 ) whose value falls in the |
618 | range : 0x8000 0000 <= L_var1 <= 0x7fff ffff. |
619 | |
620 | Outputs : |
621 | |
622 | none |
623 | |
624 | Return Value : |
625 | |
626 | var_out |
627 | 16 bit short signed integer (Word16) whose value falls in the |
628 | range : 0xffff 8000 <= var_out <= 0x0000 7fff. |
629 |___________________________________________________________________________|
630*/
631Word16 extract_h (Word32 L_var1)
632{
633 Word16 var_out;
634
635 var_out = (Word16) (L_var1 >> 16);
636#if (WMOPS)
637 multiCounter[currCounter].extract_h++;
638#endif
639 return (var_out);
640}
641/* ------------------------- End of extract_h() ------------------------- */
642
643
644/*___________________________________________________________________________
645 | |
646 | Function Name : extract_l |
647 | |
648 | Purpose : |
649 | |
650 | Return the 16 LSB of L_var1. |
651 | |
652 | Complexity weight : 1 |
653 | |
654 | Inputs : |
655 | |
656 | L_var1 |
657 | 32 bit long signed integer (Word32 ) whose value falls in the |
658 | range : 0x8000 0000 <= L_var1 <= 0x7fff ffff. |
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*/
671Word16 extract_l (Word32 L_var1)
672{
673 Word16 var_out;
674
675 var_out = (Word16) L_var1;
676#if (WMOPS)
677 multiCounter[currCounter].extract_l++;
678#endif
679 return (var_out);
680}
681/* ------------------------- End of extract_l() ------------------------- */
682
683
684/*___________________________________________________________________________
685 | |
686 | Function Name : round |
687 | |
688 | Purpose : |
689 | |
690 | Round the lower 16 bits of the 32 bit input number into the MS 16 bits |
691 | with saturation. Shift the resulting bits right by 16 and return the 16 |
692 | bit number: |
693 | round(L_var1) = extract_h(L_add(L_var1,32768)) |
694 | |
695 | Complexity weight : 1 |
696 | |
697 | Inputs : |
698 | |
699 | L_var1 |
700 | 32 bit long signed integer (Word32 ) whose value falls in the |
701 | range : 0x8000 0000 <= L_var1 <= 0x7fff ffff. |
702 | |
703 | Outputs : |
704 | |
705 | none |
706 | |
707 | Return Value : |
708 | |
709 | var_out |
710 | 16 bit short signed integer (Word16) whose value falls in the |
711 | range : 0xffff 8000 <= var_out <= 0x0000 7fff. |
712 |___________________________________________________________________________|
713*/
714Word16 round (Word32 L_var1)
715{
716 Word16 var_out;
717 Word32 L_rounded;
718
719 L_rounded = L_add (L_var1, (Word32) 0x00008000L);
720#if (WMOPS)
721 multiCounter[currCounter].L_add--;
722#endif
723 var_out = extract_h (L_rounded);
724#if (WMOPS)
725 multiCounter[currCounter].extract_h--;
726 multiCounter[currCounter].round++;
727#endif
728 return (var_out);
729}
730/* ------------------------- End of round() ------------------------- */
731
732
733/*___________________________________________________________________________
734 | |
735 | Function Name : L_mac |
736 | |
737 | Purpose : |
738 | |
739 | Multiply var1 by var2 and shift the result left by 1. Add the 32 bit |
740 | result to L_var3 with saturation, return a 32 bit result: |
741 | L_mac(L_var3,var1,var2) = L_add(L_var3,L_mult(var1,var2)). |
742 | |
743 | Complexity weight : 1 |
744 | |
745 | Inputs : |
746 | |
747 | L_var3 32 bit long signed integer (Word32) whose value falls in the |
748 | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. |
749 | |
750 | var1 |
751 | 16 bit short signed integer (Word16) whose value falls in the |
752 | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
753 | |
754 | var2 |
755 | 16 bit short signed integer (Word16) whose value falls in the |
756 | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
757 | |
758 | Outputs : |
759 | |
760 | none |
761 | |
762 | Return Value : |
763 | |
764 | L_var_out |
765 | 32 bit long signed integer (Word32) whose value falls in the |
766 | range : 0x8000 0000 <= L_var_out <= 0x7fff ffff. |
767 |___________________________________________________________________________|
768*/
769Word32 L_mac (Word32 L_var3, Word16 var1, Word16 var2)
770{
771 Word32 L_var_out;
772 Word32 L_product;
773
774 L_product = L_mult (var1, var2);
775#if (WMOPS)
776 multiCounter[currCounter].L_mult--;
777#endif
778 L_var_out = L_add (L_var3, L_product);
779#if (WMOPS)
780 multiCounter[currCounter].L_add--;
781 multiCounter[currCounter].L_mac++;
782#endif
783 return (L_var_out);
784}
785/* ------------------------- End of L_mac() ------------------------- */
786
787
788/*___________________________________________________________________________
789 | |
790 | Function Name : L_msu |
791 | |
792 | Purpose : |
793 | |
794 | Multiply var1 by var2 and shift the result left by 1. Subtract the 32 |
795 | bit result to L_var3 with saturation, return a 32 bit result: |
796 | L_msu(L_var3,var1,var2) = L_sub(L_var3,L_mult(var1,var2)). |
797 | |
798 | Complexity weight : 1 |
799 | |
800 | Inputs : |
801 | |
802 | L_var3 32 bit long signed integer (Word32) whose value falls in the |
803 | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. |
804 | |
805 | var1 |
806 | 16 bit short signed integer (Word16) whose value falls in the |
807 | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
808 | |
809 | var2 |
810 | 16 bit short signed integer (Word16) whose value falls in the |
811 | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
812 | |
813 | Outputs : |
814 | |
815 | none |
816 | |
817 | Return Value : |
818 | |
819 | L_var_out |
820 | 32 bit long signed integer (Word32) whose value falls in the |
821 | range : 0x8000 0000 <= L_var_out <= 0x7fff ffff. |
822 |___________________________________________________________________________|
823*/
824Word32 L_msu (Word32 L_var3, Word16 var1, Word16 var2)
825{
826 Word32 L_var_out;
827 Word32 L_product;
828
829 L_product = L_mult (var1, var2);
830#if (WMOPS)
831 multiCounter[currCounter].L_mult--;
832#endif
833 L_var_out = L_sub (L_var3, L_product);
834#if (WMOPS)
835 multiCounter[currCounter].L_sub--;
836 multiCounter[currCounter].L_msu++;
837#endif
838 return (L_var_out);
839}
840/* ------------------------- End of L_msu() ------------------------- */
841
842
843/*___________________________________________________________________________
844 | |
845 | Function Name : L_macNs |
846 | |
847 | Purpose : |
848 | |
849 | Multiply var1 by var2 and shift the result left by 1. Add the 32 bit |
850 | result to L_var3 without saturation, return a 32 bit result. Generate |
851 | carry and overflow values : |
852 | L_macNs(L_var3,var1,var2) = L_add_c(L_var3,L_mult(var1,var2)). |
853 | |
854 | Complexity weight : 1 |
855 | |
856 | Inputs : |
857 | |
858 | L_var3 32 bit long signed integer (Word32) whose value falls in the |
859 | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. |
860 | |
861 | var1 |
862 | 16 bit short signed integer (Word16) whose value falls in the |
863 | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
864 | |
865 | var2 |
866 | 16 bit short signed integer (Word16) whose value falls in the |
867 | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
868 | |
869 | Outputs : |
870 | |
871 | none |
872 | |
873 | Return Value : |
874 | |
875 | L_var_out |
876 | 32 bit long signed integer (Word32) whose value falls in the |
877 | range : 0x8000 0000 <= L_var_out <= 0x7fff ffff. |
878 | |
879 | Caution : |
880 | |
881 | In some cases the Carry flag has to be cleared or set before using |
882 | operators which take into account its value. |
883 |___________________________________________________________________________|
884*/
885Word32 L_macNs (Word32 L_var3, Word16 var1, Word16 var2)
886{
887 Word32 L_var_out;
888
889 L_var_out = L_mult (var1, var2);
890#if (WMOPS)
891 multiCounter[currCounter].L_mult--;
892#endif
893 L_var_out = L_add_c (L_var3, L_var_out);
894#if (WMOPS)
895 multiCounter[currCounter].L_add_c--;
896 multiCounter[currCounter].L_macNs++;
897#endif
898 return (L_var_out);
899}
900/* ------------------------- End of L_macNs() ------------------------- */
901
902
903/*___________________________________________________________________________
904 | |
905 | Function Name : L_msuNs |
906 | |
907 | Purpose : |
908 | |
909 | Multiply var1 by var2 and shift the result left by 1. Subtract the 32 |
910 | bit result from L_var3 without saturation, return a 32 bit result. Ge- |
911 | nerate carry and overflow values : |
912 | L_msuNs(L_var3,var1,var2) = L_sub_c(L_var3,L_mult(var1,var2)). |
913 | |
914 | Complexity weight : 1 |
915 | |
916 | Inputs : |
917 | |
918 | L_var3 32 bit long signed integer (Word32) whose value falls in the |
919 | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. |
920 | |
921 | var1 |
922 | 16 bit short signed integer (Word16) whose value falls in the |
923 | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
924 | |
925 | var2 |
926 | 16 bit short signed integer (Word16) whose value falls in the |
927 | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
928 | |
929 | Outputs : |
930 | |
931 | none |
932 | |
933 | Return Value : |
934 | |
935 | L_var_out |
936 | 32 bit long signed integer (Word32) whose value falls in the |
937 | range : 0x8000 0000 <= L_var_out <= 0x7fff ffff. |
938 | |
939 | Caution : |
940 | |
941 | In some cases the Carry flag has to be cleared or set before using |
942 | operators which take into account its value. |
943 |___________________________________________________________________________|
944*/
945Word32 L_msuNs (Word32 L_var3, Word16 var1, Word16 var2)
946{
947 Word32 L_var_out;
948
949 L_var_out = L_mult (var1, var2);
950#if (WMOPS)
951 multiCounter[currCounter].L_mult--;
952#endif
953 L_var_out = L_sub_c (L_var3, L_var_out);
954#if (WMOPS)
955 multiCounter[currCounter].L_sub_c--;
956 multiCounter[currCounter].L_msuNs++;
957#endif
958 return (L_var_out);
959}
960/* ------------------------- End of L_msuNs() ------------------------- */
961
962
963/*___________________________________________________________________________
964 | |
965 | Function Name : L_add |
966 | |
967 | Purpose : |
968 | |
969 | 32 bits addition of the two 32 bits variables (L_var1+L_var2) with |
970 | overflow control and saturation; the result is set at +2147483647 when |
971 | overflow occurs or at -2147483648 when underflow occurs. |
972 | |
973 | Complexity weight : 2 |
974 | |
975 | Inputs : |
976 | |
977 | L_var1 32 bit long signed integer (Word32) whose value falls in the |
978 | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. |
979 | |
980 | L_var2 32 bit long signed integer (Word32) whose value falls in the |
981 | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. |
982 | |
983 | Outputs : |
984 | |
985 | none |
986 | |
987 | Return Value : |
988 | |
989 | L_var_out |
990 | 32 bit long signed integer (Word32) whose value falls in the |
991 | range : 0x8000 0000 <= L_var_out <= 0x7fff ffff. |
992 |___________________________________________________________________________|
993*/
994Word32 L_add (Word32 L_var1, Word32 L_var2)
995{
996 Word32 L_var_out;
997
998 L_var_out = L_var1 + L_var2;
999
1000 if (((L_var1 ^ L_var2) & MIN_32) == 0)
1001 {
1002 if ((L_var_out ^ L_var1) & MIN_32)
1003 {
1004 L_var_out = (L_var1 < 0) ? MIN_32 : MAX_32;
1005 Overflow = 1;
1006 }
1007 }
1008#if (WMOPS)
1009 multiCounter[currCounter].L_add++;
1010#endif
1011 return (L_var_out);
1012}
1013/* ------------------------- End of L_add() ------------------------- */
1014
1015
1016/*___________________________________________________________________________
1017 | |
1018 | Function Name : L_sub |
1019 | |
1020 | Purpose : |
1021 | |
1022 | 32 bits subtraction of the two 32 bits variables (L_var1-L_var2) with |
1023 | overflow control and saturation; the result is set at +2147483647 when |
1024 | overflow occurs or at -2147483648 when underflow occurs. |
1025 | |
1026 | Complexity weight : 2 |
1027 | |
1028 | Inputs : |
1029 | |
1030 | L_var1 32 bit long signed integer (Word32) whose value falls in the |
1031 | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. |
1032 | |
1033 | L_var2 32 bit long signed integer (Word32) whose value falls in the |
1034 | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. |
1035 | |
1036 | Outputs : |
1037 | |
1038 | none |
1039 | |
1040 | Return Value : |
1041 | |
1042 | L_var_out |
1043 | 32 bit long signed integer (Word32) whose value falls in the |
1044 | range : 0x8000 0000 <= L_var_out <= 0x7fff ffff. |
1045 |___________________________________________________________________________|
1046*/
1047Word32 L_sub (Word32 L_var1, Word32 L_var2)
1048{
1049 Word32 L_var_out;
1050
1051 L_var_out = L_var1 - L_var2;
1052
1053 if (((L_var1 ^ L_var2) & MIN_32) != 0)
1054 {
1055 if ((L_var_out ^ L_var1) & MIN_32)
1056 {
1057 L_var_out = (L_var1 < 0L) ? MIN_32 : MAX_32;
1058 Overflow = 1;
1059 }
1060 }
1061#if (WMOPS)
1062 multiCounter[currCounter].L_sub++;
1063#endif
1064 return (L_var_out);
1065}
1066/* ------------------------- End of L_sub() ------------------------- */
1067
1068
1069/*___________________________________________________________________________
1070 | |
1071 | Function Name : L_add_c |
1072 | |
1073 | Purpose : |
1074 | |
1075 | Performs 32 bits addition of the two 32 bits variables (L_var1+L_var2+C)|
1076 | with carry. No saturation. Generate carry and Overflow values. The car- |
1077 | ry and overflow values are binary variables which can be tested and as- |
1078 | signed values. |
1079 | |
1080 | Complexity weight : 2 |
1081 | |
1082 | Inputs : |
1083 | |
1084 | L_var1 32 bit long signed integer (Word32) whose value falls in the |
1085 | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. |
1086 | |
1087 | L_var2 32 bit long signed integer (Word32) whose value falls in the |
1088 | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. |
1089 | |
1090 | Outputs : |
1091 | |
1092 | none |
1093 | |
1094 | Return Value : |
1095 | |
1096 | L_var_out |
1097 | 32 bit long signed integer (Word32) whose value falls in the |
1098 | range : 0x8000 0000 <= L_var_out <= 0x7fff ffff. |
1099 | |
1100 | Caution : |
1101 | |
1102 | In some cases the Carry flag has to be cleared or set before using |
1103 | operators which take into account its value. |
1104 |___________________________________________________________________________|
1105*/
1106Word32 L_add_c (Word32 L_var1, Word32 L_var2)
1107{
1108 Word32 L_var_out;
1109 Word32 L_test;
1110 Flag carry_int = 0;
1111
1112 L_var_out = L_var1 + L_var2 + Carry;
1113
1114 L_test = L_var1 + L_var2;
1115
1116 if ((L_var1 > 0) && (L_var2 > 0) && (L_test < 0))
1117 {
1118 Overflow = 1;
1119 carry_int = 0;
1120 }
1121 else
1122 {
1123 if ((L_var1 < 0) && (L_var2 < 0))
1124 {
1125 if (L_test >= 0)
1126 {
1127 Overflow = 1;
1128 carry_int = 1;
1129 }
1130 else
1131 {
1132 Overflow = 0;
1133 carry_int = 1;
1134 }
1135 }
1136 else
1137 {
1138 if (((L_var1 ^ L_var2) < 0) && (L_test >= 0))
1139 {
1140 Overflow = 0;
1141 carry_int = 1;
1142 }
1143 else
1144 {
1145 Overflow = 0;
1146 carry_int = 0;
1147 }
1148 }
1149 }
1150
1151 if (Carry)
1152 {
1153 if (L_test == MAX_32)
1154 {
1155 Overflow = 1;
1156 Carry = carry_int;
1157 }
1158 else
1159 {
1160 if (L_test == (Word32) 0xFFFFFFFFL)
1161 {
1162 Carry = 1;
1163 }
1164 else
1165 {
1166 Carry = carry_int;
1167 }
1168 }
1169 }
1170 else
1171 {
1172 Carry = carry_int;
1173 }
1174
1175#if (WMOPS)
1176 multiCounter[currCounter].L_add_c++;
1177#endif
1178 return (L_var_out);
1179}
1180/* ------------------------- End of L_add_c() ------------------------- */
1181
1182
1183/*___________________________________________________________________________
1184 | |
1185 | Function Name : L_sub_c |
1186 | |
1187 | Purpose : |
1188 | |
1189 | Performs 32 bits subtraction of the two 32 bits variables with carry |
1190 | (borrow) : L_var1-L_var2-C. No saturation. Generate carry and Overflow |
1191 | values. The carry and overflow values are binary variables which can |
1192 | be tested and assigned values. |
1193 | |
1194 | Complexity weight : 2 |
1195 | |
1196 | Inputs : |
1197 | |
1198 | L_var1 32 bit long signed integer (Word32) whose value falls in the |
1199 | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. |
1200 | |
1201 | L_var2 32 bit long signed integer (Word32) whose value falls in the |
1202 | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. |
1203 | |
1204 | Outputs : |
1205 | |
1206 | none |
1207 | |
1208 | Return Value : |
1209 | |
1210 | L_var_out |
1211 | 32 bit long signed integer (Word32) whose value falls in the |
1212 | range : 0x8000 0000 <= L_var_out <= 0x7fff ffff. |
1213 | |
1214 | Caution : |
1215 | |
1216 | In some cases the Carry flag has to be cleared or set before using |
1217 | operators which take into account its value. |
1218 |___________________________________________________________________________|
1219*/
1220Word32 L_sub_c (Word32 L_var1, Word32 L_var2)
1221{
1222 Word32 L_var_out;
1223 Word32 L_test;
1224 Flag carry_int = 0;
1225
1226 if (Carry)
1227 {
1228 Carry = 0;
1229 if (L_var2 != MIN_32)
1230 {
1231 L_var_out = L_add_c (L_var1, -L_var2);
1232#if (WMOPS)
1233 multiCounter[currCounter].L_add_c--;
1234#endif
1235 }
1236 else
1237 {
1238 L_var_out = L_var1 - L_var2;
1239 if (L_var1 > 0L)
1240 {
1241 Overflow = 1;
1242 Carry = 0;
1243 }
1244 }
1245 }
1246 else
1247 {
1248 L_var_out = L_var1 - L_var2 - (Word32) 0X00000001L;
1249 L_test = L_var1 - L_var2;
1250
1251 if ((L_test < 0) && (L_var1 > 0) && (L_var2 < 0))
1252 {
1253 Overflow = 1;
1254 carry_int = 0;
1255 }
1256 else if ((L_test > 0) && (L_var1 < 0) && (L_var2 > 0))
1257 {
1258 Overflow = 1;
1259 carry_int = 1;
1260 }
1261 else if ((L_test > 0) && ((L_var1 ^ L_var2) > 0))
1262 {
1263 Overflow = 0;
1264 carry_int = 1;
1265 }
1266 if (L_test == MIN_32)
1267 {
1268 Overflow = 1;
1269 Carry = carry_int;
1270 }
1271 else
1272 {
1273 Carry = carry_int;
1274 }
1275 }
1276
1277#if (WMOPS)
1278 multiCounter[currCounter].L_sub_c++;
1279#endif
1280 return (L_var_out);
1281}
1282/* ------------------------- End of L_sub_c() ------------------------- */
1283
1284
1285/*___________________________________________________________________________
1286 | |
1287 | Function Name : L_negate |
1288 | |
1289 | Purpose : |
1290 | |
1291 | Negate the 32 bit variable L_var1 with saturation; saturate in the case |
1292 | where input is -2147483648 (0x8000 0000). |
1293 | |
1294 | Complexity weight : 2 |
1295 | |
1296 | Inputs : |
1297 | |
1298 | L_var1 32 bit long signed integer (Word32) whose value falls in the |
1299 | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. |
1300 | |
1301 | Outputs : |
1302 | |
1303 | none |
1304 | |
1305 | Return Value : |
1306 | |
1307 | L_var_out |
1308 | 32 bit long signed integer (Word32) whose value falls in the |
1309 | range : 0x8000 0000 <= L_var_out <= 0x7fff ffff. |
1310 |___________________________________________________________________________|
1311*/
1312Word32 L_negate (Word32 L_var1)
1313{
1314 Word32 L_var_out;
1315
1316 L_var_out = (L_var1 == MIN_32) ? MAX_32 : -L_var1;
1317#if (WMOPS)
1318 multiCounter[currCounter].L_negate++;
1319#endif
1320 return (L_var_out);
1321}
1322/* ------------------------- End of L_negate() ------------------------- */
1323
1324
1325/*___________________________________________________________________________
1326 | |
1327 | Function Name : mult_r |
1328 | |
1329 | Purpose : |
1330 | |
1331 | Same as mult with rounding, i.e.: |
1332 | mult_r(var1,var2) = extract_l(L_shr(((var1 * var2) + 16384),15)) and |
1333 | mult_r(-32768,-32768) = 32767. |
1334 | |
1335 | Complexity weight : 2 |
1336 | |
1337 | Inputs : |
1338 | |
1339 | var1 |
1340 | 16 bit short signed integer (Word16) whose value falls in the |
1341 | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
1342 | |
1343 | var2 |
1344 | 16 bit short signed integer (Word16) whose value falls in the |
1345 | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
1346 | |
1347 | Outputs : |
1348 | |
1349 | none |
1350 | |
1351 | Return Value : |
1352 | |
1353 | var_out |
1354 | 16 bit short signed integer (Word16) whose value falls in the |
1355 | range : 0xffff 8000 <= var_out <= 0x0000 7fff. |
1356 |___________________________________________________________________________|
1357*/
1358Word16 mult_r (Word16 var1, Word16 var2)
1359{
1360 Word16 var_out;
1361 Word32 L_product_arr;
1362
1363 L_product_arr = (Word32) var1 *(Word32) var2; /* product */
1364 L_product_arr += (Word32) 0x00004000L; /* round */
1365 L_product_arr &= (Word32) 0xffff8000L;
1366 L_product_arr >>= 15; /* shift */
1367
1368 if (L_product_arr & (Word32) 0x00010000L) /* sign extend when necessary */
1369 {
1370 L_product_arr |= (Word32) 0xffff0000L;
1371 }
1372 var_out = saturate (L_product_arr);
1373#if (WMOPS)
1374 multiCounter[currCounter].mult_r++;
1375#endif
1376 return (var_out);
1377}
1378/* ------------------------- End of mult_r() ------------------------- */
1379
1380
1381/*___________________________________________________________________________
1382 | |
1383 | Function Name : L_shl |
1384 | |
1385 | Purpose : |
1386 | |
1387 | Arithmetically shift the 32 bit input L_var1 left var2 positions. Zero |
1388 | fill the var2 LSB of the result. If var2 is negative, arithmetically |
1389 | shift L_var1 right by -var2 with sign extension. Saturate the result in |
1390 | case of underflows or overflows. |
1391 | |
1392 | Complexity weight : 2 |
1393 | |
1394 | Inputs : |
1395 | |
1396 | L_var1 32 bit long signed integer (Word32) whose value falls in the |
1397 | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. |
1398 | |
1399 | var2 |
1400 | 16 bit short signed integer (Word16) whose value falls in the |
1401 | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
1402 | |
1403 | Outputs : |
1404 | |
1405 | none |
1406 | |
1407 | Return Value : |
1408 | |
1409 | L_var_out |
1410 | 32 bit long signed integer (Word32) whose value falls in the |
1411 | range : 0x8000 0000 <= L_var_out <= 0x7fff ffff. |
1412 |___________________________________________________________________________|
1413*/
1414Word32 L_shl (Word32 L_var1, Word16 var2)
1415{
1416 Word32 L_var_out;
1417
1418 if (var2 <= 0)
1419 {
1420 if (var2 < -32)
1421 var2 = -32;
1422 L_var_out = L_shr (L_var1, (Word16) -var2);
1423#if (WMOPS)
1424 multiCounter[currCounter].L_shr--;
1425#endif
1426 }
1427 else
1428 {
1429 for (; var2 > 0; var2--)
1430 {
1431 if (L_var1 > (Word32) 0X3fffffffL)
1432 {
1433 Overflow = 1;
1434 L_var_out = MAX_32;
1435 break;
1436 }
1437 else
1438 {
1439 if (L_var1 < (Word32) 0xc0000000L)
1440 {
1441 Overflow = 1;
1442 L_var_out = MIN_32;
1443 break;
1444 }
1445 }
1446 L_var1 *= 2;
1447 L_var_out = L_var1;
1448 }
1449 }
1450#if (WMOPS)
1451 multiCounter[currCounter].L_shl++;
1452#endif
1453 return (L_var_out);
1454}
1455/* ------------------------- End of L_shl() ------------------------- */
1456
1457
1458/*___________________________________________________________________________
1459 | |
1460 | Function Name : L_shr |
1461 | |
1462 | Purpose : |
1463 | |
1464 | Arithmetically shift the 32 bit input L_var1 right var2 positions with |
1465 | sign extension. If var2 is negative, arithmetically shift L_var1 left |
1466 | by -var2 and zero fill the -var2 LSB of the result. Saturate the result |
1467 | in case of underflows or overflows. |
1468 | |
1469 | Complexity weight : 2 |
1470 | |
1471 | Inputs : |
1472 | |
1473 | L_var1 32 bit long signed integer (Word32) whose value falls in the |
1474 | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. |
1475 | |
1476 | var2 |
1477 | 16 bit short signed integer (Word16) whose value falls in the |
1478 | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
1479 | |
1480 | Outputs : |
1481 | |
1482 | none |
1483 | |
1484 | Return Value : |
1485 | |
1486 | L_var_out |
1487 | 32 bit long signed integer (Word32) whose value falls in the |
1488 | range : 0x8000 0000 <= L_var_out <= 0x7fff ffff. |
1489 |___________________________________________________________________________|
1490*/
1491Word32 L_shr (Word32 L_var1, Word16 var2)
1492{
1493 Word32 L_var_out;
1494
1495 if (var2 < 0)
1496 {
1497 if (var2 < -32)
1498 var2 = -32;
1499 L_var_out = L_shl (L_var1, (Word16) -var2);
1500#if (WMOPS)
1501 multiCounter[currCounter].L_shl--;
1502#endif
1503 }
1504 else
1505 {
1506 if (var2 >= 31)
1507 {
1508 L_var_out = (L_var1 < 0L) ? -1 : 0;
1509 }
1510 else
1511 {
1512 if (L_var1 < 0)
1513 {
1514 L_var_out = ~((~L_var1) >> var2);
1515 }
1516 else
1517 {
1518 L_var_out = L_var1 >> var2;
1519 }
1520 }
1521 }
1522#if (WMOPS)
1523 multiCounter[currCounter].L_shr++;
1524#endif
1525 return (L_var_out);
1526}
1527/* ------------------------- End of L_shr() ------------------------- */
1528
1529
1530/*___________________________________________________________________________
1531 | |
1532 | Function Name : shr_r |
1533 | |
1534 | Purpose : |
1535 | |
1536 | Same as shr(var1,var2) but with rounding. Saturate the result in case of|
1537 | underflows or overflows : |
1538 | - If var2 is greater than zero : |
1539 | if (sub(shl(shr(var1,var2),1),shr(var1,sub(var2,1)))) |
1540 | is equal to zero |
1541 | then |
1542 | shr_r(var1,var2) = shr(var1,var2) |
1543 | else |
1544 | shr_r(var1,var2) = add(shr(var1,var2),1) |
1545 | - If var2 is less than or equal to zero : |
1546 | shr_r(var1,var2) = shr(var1,var2). |
1547 | |
1548 | Complexity weight : 2 |
1549 | |
1550 | Inputs : |
1551 | |
1552 | var1 |
1553 | 16 bit short signed integer (Word16) whose value falls in the |
1554 | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
1555 | |
1556 | var2 |
1557 | 16 bit short signed integer (Word16) whose value falls in the |
1558 | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
1559 | |
1560 | Outputs : |
1561 | |
1562 | none |
1563 | |
1564 | Return Value : |
1565 | |
1566 | var_out |
1567 | 16 bit short signed integer (Word16) whose value falls in the |
1568 | range : 0xffff 8000 <= var_out <= 0x0000 7fff. |
1569 |___________________________________________________________________________|
1570*/
1571Word16 shr_r (Word16 var1, Word16 var2)
1572{
1573 Word16 var_out;
1574
1575 if (var2 > 15)
1576 {
1577 var_out = 0;
1578 }
1579 else
1580 {
1581 var_out = shr (var1, var2);
1582#if (WMOPS)
1583 multiCounter[currCounter].shr--;
1584#endif
1585
1586 if (var2 > 0)
1587 {
1588 if ((var1 & ((Word16) 1 << (var2 - 1))) != 0)
1589 {
1590 var_out++;
1591 }
1592 }
1593 }
1594#if (WMOPS)
1595 multiCounter[currCounter].shr_r++;
1596#endif
1597 return (var_out);
1598}
1599/* ------------------------- End of shr_r() ------------------------- */
1600
1601
1602/*___________________________________________________________________________
1603 | |
1604 | Function Name : mac_r |
1605 | |
1606 | Purpose : |
1607 | |
1608 | Multiply var1 by var2 and shift the result left by 1. Add the 32 bit |
1609 | result to L_var3 with saturation. Round the LS 16 bits of the result |
1610 | into the MS 16 bits with saturation and shift the result right by 16. |
1611 | Return a 16 bit result. |
1612 | mac_r(L_var3,var1,var2) = round(L_mac(L_var3,var1,var2)) |
1613 | |
1614 | Complexity weight : 2 |
1615 | |
1616 | Inputs : |
1617 | |
1618 | L_var3 32 bit long signed integer (Word32) whose value falls in the |
1619 | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. |
1620 | |
1621 | var1 |
1622 | 16 bit short signed integer (Word16) whose value falls in the |
1623 | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
1624 | |
1625 | var2 |
1626 | 16 bit short signed integer (Word16) whose value falls in the |
1627 | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
1628 | |
1629 | Outputs : |
1630 | |
1631 | none |
1632 | |
1633 | Return Value : |
1634 | |
1635 | var_out |
1636 | 16 bit short signed integer (Word16) whose value falls in the |
1637 | range : 0x0000 8000 <= L_var_out <= 0x0000 7fff. |
1638 |___________________________________________________________________________|
1639*/
1640Word16 mac_r (Word32 L_var3, Word16 var1, Word16 var2)
1641{
1642 Word16 var_out;
1643
1644 L_var3 = L_mac (L_var3, var1, var2);
1645#if (WMOPS)
1646 multiCounter[currCounter].L_mac--;
1647#endif
1648 L_var3 = L_add (L_var3, (Word32) 0x00008000L);
1649#if (WMOPS)
1650 multiCounter[currCounter].L_add--;
1651#endif
1652 var_out = extract_h (L_var3);
1653#if (WMOPS)
1654 multiCounter[currCounter].extract_h--;
1655 multiCounter[currCounter].mac_r++;
1656#endif
1657 return (var_out);
1658}
1659/* ------------------------- End of mac_r() ------------------------- */
1660
1661
1662/*___________________________________________________________________________
1663 | |
1664 | Function Name : msu_r |
1665 | |
1666 | Purpose : |
1667 | |
1668 | Multiply var1 by var2 and shift the result left by 1. Subtract the 32 |
1669 | bit result to L_var3 with saturation. Round the LS 16 bits of the res- |
1670 | ult into the MS 16 bits with saturation and shift the result right by |
1671 | 16. Return a 16 bit result. |
1672 | msu_r(L_var3,var1,var2) = round(L_msu(L_var3,var1,var2)) |
1673 | |
1674 | Complexity weight : 2 |
1675 | |
1676 | Inputs : |
1677 | |
1678 | L_var3 32 bit long signed integer (Word32) whose value falls in the |
1679 | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. |
1680 | |
1681 | var1 |
1682 | 16 bit short signed integer (Word16) whose value falls in the |
1683 | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
1684 | |
1685 | var2 |
1686 | 16 bit short signed integer (Word16) whose value falls in the |
1687 | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
1688 | |
1689 | Outputs : |
1690 | |
1691 | none |
1692 | |
1693 | Return Value : |
1694 | |
1695 | var_out |
1696 | 16 bit short signed integer (Word16) whose value falls in the |
1697 | range : 0x0000 8000 <= L_var_out <= 0x0000 7fff. |
1698 |___________________________________________________________________________|
1699*/
1700Word16 msu_r (Word32 L_var3, Word16 var1, Word16 var2)
1701{
1702 Word16 var_out;
1703
1704 L_var3 = L_msu (L_var3, var1, var2);
1705#if (WMOPS)
1706 multiCounter[currCounter].L_msu--;
1707#endif
1708 L_var3 = L_add (L_var3, (Word32) 0x00008000L);
1709#if (WMOPS)
1710 multiCounter[currCounter].L_add--;
1711#endif
1712 var_out = extract_h (L_var3);
1713#if (WMOPS)
1714 multiCounter[currCounter].extract_h--;
1715 multiCounter[currCounter].msu_r++;
1716#endif
1717 return (var_out);
1718}
1719/* ------------------------- End of msu_r() ------------------------- */
1720
1721
1722/*___________________________________________________________________________
1723 | |
1724 | Function Name : L_deposit_h |
1725 | |
1726 | Purpose : |
1727 | |
1728 | Deposit the 16 bit var1 into the 16 MS bits of the 32 bit output. The |
1729 | 16 LS bits of the output are zeroed. |
1730 | |
1731 | Complexity weight : 2 |
1732 | |
1733 | Inputs : |
1734 | |
1735 | var1 |
1736 | 16 bit short signed integer (Word16) whose value falls in the |
1737 | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
1738 | |
1739 | Outputs : |
1740 | |
1741 | none |
1742 | |
1743 | Return Value : |
1744 | |
1745 | L_var_out |
1746 | 32 bit long signed integer (Word32) whose value falls in the |
1747 | range : 0x8000 0000 <= var_out <= 0x7fff 0000. |
1748 |___________________________________________________________________________|
1749*/
1750Word32 L_deposit_h (Word16 var1)
1751{
1752 Word32 L_var_out;
1753
1754 L_var_out = (Word32) var1 << 16;
1755#if (WMOPS)
1756 multiCounter[currCounter].L_deposit_h++;
1757#endif
1758 return (L_var_out);
1759}
1760/* ------------------------- End of L_deposit_h() ------------------------- */
1761
1762
1763/*___________________________________________________________________________
1764 | |
1765 | Function Name : L_deposit_l |
1766 | |
1767 | Purpose : |
1768 | |
1769 | Deposit the 16 bit var1 into the 16 LS bits of the 32 bit output. The |
1770 | 16 MS bits of the output are sign extended. |
1771 | |
1772 | Complexity weight : 2 |
1773 | |
1774 | Inputs : |
1775 | |
1776 | var1 |
1777 | 16 bit short signed integer (Word16) whose value falls in the |
1778 | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
1779 | |
1780 | Outputs : |
1781 | |
1782 | none |
1783 | |
1784 | Return Value : |
1785 | |
1786 | L_var_out |
1787 | 32 bit long signed integer (Word32) whose value falls in the |
1788 | range : 0xFFFF 8000 <= var_out <= 0x0000 7fff. |
1789 |___________________________________________________________________________|
1790*/
1791Word32 L_deposit_l (Word16 var1)
1792{
1793 Word32 L_var_out;
1794
1795 L_var_out = (Word32) var1;
1796#if (WMOPS)
1797 multiCounter[currCounter].L_deposit_l++;
1798#endif
1799 return (L_var_out);
1800}
1801/* ------------------------- End of L_deposit_l() ------------------------- */
1802
1803
1804/*___________________________________________________________________________
1805 | |
1806 | Function Name : L_shr_r |
1807 | |
1808 | Purpose : |
1809 | |
1810 | Same as L_shr(L_var1,var2) but with rounding. Saturate the result in |
1811 | case of underflows or overflows : |
1812 | - If var2 is greater than zero : |
1813 | if (L_sub(L_shl(L_shr(L_var1,var2),1),L_shr(L_var1,sub(var2,1))))|
1814 | is equal to zero |
1815 | then |
1816 | L_shr_r(L_var1,var2) = L_shr(L_var1,var2) |
1817 | else |
1818 | L_shr_r(L_var1,var2) = L_add(L_shr(L_var1,var2),1) |
1819 | - If var2 is less than or equal to zero : |
1820 | L_shr_r(L_var1,var2) = L_shr(L_var1,var2). |
1821 | |
1822 | Complexity weight : 3 |
1823 | |
1824 | Inputs : |
1825 | |
1826 | L_var1 |
1827 | 32 bit long signed integer (Word32) whose value falls in the |
1828 | range : 0x8000 0000 <= var1 <= 0x7fff ffff. |
1829 | |
1830 | var2 |
1831 | 16 bit short signed integer (Word16) whose value falls in the |
1832 | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
1833 | |
1834 | Outputs : |
1835 | |
1836 | none |
1837 | |
1838 | Return Value : |
1839 | |
1840 | L_var_out |
1841 | 32 bit long signed integer (Word32) whose value falls in the |
1842 | range : 0x8000 0000 <= var_out <= 0x7fff ffff. |
1843 |___________________________________________________________________________|
1844*/
1845Word32 L_shr_r (Word32 L_var1, Word16 var2)
1846{
1847 Word32 L_var_out;
1848
1849 if (var2 > 31)
1850 {
1851 L_var_out = 0;
1852 }
1853 else
1854 {
1855 L_var_out = L_shr (L_var1, var2);
1856#if (WMOPS)
1857 multiCounter[currCounter].L_shr--;
1858#endif
1859 if (var2 > 0)
1860 {
1861 if ((L_var1 & ((Word32) 1 << (var2 - 1))) != 0)
1862 {
1863 L_var_out++;
1864 }
1865 }
1866 }
1867#if (WMOPS)
1868 multiCounter[currCounter].L_shr_r++;
1869#endif
1870 return (L_var_out);
1871}
1872/* ------------------------- End of L_shr_r() ------------------------- */
1873
1874
1875/*___________________________________________________________________________
1876 | |
1877 | Function Name : L_abs |
1878 | |
1879 | Purpose : |
1880 | |
1881 | Absolute value of L_var1; Saturate in case where the input is |
1882 | -214783648 |
1883 | |
1884 | Complexity weight : 3 |
1885 | |
1886 | Inputs : |
1887 | |
1888 | L_var1 |
1889 | 32 bit long signed integer (Word32) whose value falls in the |
1890 | range : 0x8000 0000 <= var1 <= 0x7fff ffff. |
1891 | |
1892 | Outputs : |
1893 | |
1894 | none |
1895 | |
1896 | Return Value : |
1897 | |
1898 | L_var_out |
1899 | 32 bit long signed integer (Word32) whose value falls in the |
1900 | range : 0x0000 0000 <= var_out <= 0x7fff ffff. |
1901 |___________________________________________________________________________|
1902*/
1903Word32 L_abs (Word32 L_var1)
1904{
1905 Word32 L_var_out;
1906
1907 if (L_var1 == MIN_32)
1908 {
1909 L_var_out = MAX_32;
1910 }
1911 else
1912 {
1913 if (L_var1 < 0)
1914 {
1915 L_var_out = -L_var1;
1916 }
1917 else
1918 {
1919 L_var_out = L_var1;
1920 }
1921 }
1922
1923#if (WMOPS)
1924 multiCounter[currCounter].L_abs++;
1925#endif
1926 return (L_var_out);
1927}
1928/* ------------------------- End of L_abs() ------------------------- */
1929
1930
1931/*___________________________________________________________________________
1932 | |
1933 | Function Name : L_sat |
1934 | |
1935 | Purpose : |
1936 | |
1937 | 32 bit L_var1 is set to 2147483647 if an overflow occured or to |
1938 | -2147483648 if an underflow occured on the most recent L_add_c, |
1939 | L_sub_c, L_macNs or L_msuNs operations. The carry and overflow values |
1940 | are binary values which can be tested and assigned values. |
1941 | |
1942 | Complexity weight : 4 |
1943 | |
1944 | Inputs : |
1945 | |
1946 | L_var1 |
1947 | 32 bit long signed integer (Word32) whose value falls in the |
1948 | range : 0x8000 0000 <= var1 <= 0x7fff ffff. |
1949 | |
1950 | Outputs : |
1951 | |
1952 | none |
1953 | |
1954 | Return Value : |
1955 | |
1956 | L_var_out |
1957 | 32 bit long signed integer (Word32) whose value falls in the |
1958 | range : 0x8000 0000 <= var_out <= 0x7fff ffff. |
1959 |___________________________________________________________________________|
1960*/
1961Word32 L_sat (Word32 L_var1)
1962{
1963 Word32 L_var_out;
1964
1965 L_var_out = L_var1;
1966
1967 if (Overflow)
1968 {
1969
1970 if (Carry)
1971 {
1972 L_var_out = MIN_32;
1973 }
1974 else
1975 {
1976 L_var_out = MAX_32;
1977 }
1978
1979 Carry = 0;
1980 Overflow = 0;
1981 }
1982#if (WMOPS)
1983 multiCounter[currCounter].L_sat++;
1984#endif
1985 return (L_var_out);
1986}
1987/* ------------------------- End of L_sat() ------------------------- */
1988
1989
1990/*___________________________________________________________________________
1991 | |
1992 | Function Name : norm_s |
1993 | |
1994 | Purpose : |
1995 | |
1996 | Produces the number of left shift needed to normalize the 16 bit varia- |
1997 | ble var1 for positive values on the interval with minimum of 16384 and |
1998 | maximum of 32767, and for negative values on the interval with minimum |
1999 | of -32768 and maximum of -16384; in order to normalize the result, the |
2000 | following operation must be done : |
2001 | norm_var1 = shl(var1,norm_s(var1)). |
2002 | |
2003 | Complexity weight : 15 |
2004 | |
2005 | Inputs : |
2006 | |
2007 | var1 |
2008 | 16 bit short signed integer (Word16) whose value falls in the |
2009 | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
2010 | |
2011 | Outputs : |
2012 | |
2013 | none |
2014 | |
2015 | Return Value : |
2016 | |
2017 | var_out |
2018 | 16 bit short signed integer (Word16) whose value falls in the |
2019 | range : 0x0000 0000 <= var_out <= 0x0000 000f. |
2020 |___________________________________________________________________________|
2021*/
2022Word16 norm_s (Word16 var1)
2023{
2024 Word16 var_out;
2025
2026 if (var1 == 0)
2027 {
2028 var_out = 0;
2029 }
2030 else
2031 {
2032 if (var1 == (Word16) 0xffff)
2033 {
2034 var_out = 15;
2035 }
2036 else
2037 {
2038 if (var1 < 0)
2039 {
2040 var1 = ~var1;
2041 }
2042 for (var_out = 0; var1 < 0x4000; var_out++)
2043 {
2044 var1 <<= 1;
2045 }
2046 }
2047 }
2048
2049#if (WMOPS)
2050 multiCounter[currCounter].norm_s++;
2051#endif
2052 return (var_out);
2053}
2054/* ------------------------- End of norm_s() ------------------------- */
2055
2056
2057/*___________________________________________________________________________
2058 | |
2059 | Function Name : div_s |
2060 | |
2061 | Purpose : |
2062 | |
2063 | Produces a result which is the fractional integer division of var1 by |
2064 | var2; var1 and var2 must be positive and var2 must be greater or equal |
2065 | to var1; the result is positive (leading bit equal to 0) and truncated |
2066 | to 16 bits. |
2067 | If var1 = var2 then div(var1,var2) = 32767. |
2068 | |
2069 | Complexity weight : 18 |
2070 | |
2071 | Inputs : |
2072 | |
2073 | var1 |
2074 | 16 bit short signed integer (Word16) whose value falls in the |
2075 | range : 0x0000 0000 <= var1 <= var2 and var2 != 0. |
2076 | |
2077 | var2 |
2078 | 16 bit short signed integer (Word16) whose value falls in the |
2079 | range : var1 <= var2 <= 0x0000 7fff and var2 != 0. |
2080 | |
2081 | Outputs : |
2082 | |
2083 | none |
2084 | |
2085 | Return Value : |
2086 | |
2087 | var_out |
2088 | 16 bit short signed integer (Word16) whose value falls in the |
2089 | range : 0x0000 0000 <= var_out <= 0x0000 7fff. |
2090 | It's a Q15 value (point between b15 and b14). |
2091 |___________________________________________________________________________|
2092*/
2093Word16 div_s (Word16 var1, Word16 var2)
2094{
2095 Word16 var_out = 0;
2096 Word16 iteration;
2097 Word32 L_num;
2098 Word32 L_denom;
2099
2100 if ((var1 > var2) || (var1 < 0) || (var2 < 0))
2101 {
2102 printf ("Division Error var1=%d var2=%d\n", var1, var2);
2103 abort(); /* exit (0); */
2104 }
2105 if (var2 == 0)
2106 {
2107 printf ("Division by 0, Fatal error \n");
2108 abort(); /* exit (0); */
2109 }
2110 if (var1 == 0)
2111 {
2112 var_out = 0;
2113 }
2114 else
2115 {
2116 if (var1 == var2)
2117 {
2118 var_out = MAX_16;
2119 }
2120 else
2121 {
2122 L_num = L_deposit_l (var1);
2123#if (WMOPS)
2124 multiCounter[currCounter].L_deposit_l--;
2125#endif
2126 L_denom = L_deposit_l (var2);
2127#if (WMOPS)
2128 multiCounter[currCounter].L_deposit_l--;
2129#endif
2130
2131 for (iteration = 0; iteration < 15; iteration++)
2132 {
2133 var_out <<= 1;
2134 L_num <<= 1;
2135
2136 if (L_num >= L_denom)
2137 {
2138 L_num = L_sub (L_num, L_denom);
2139#if (WMOPS)
2140 multiCounter[currCounter].L_sub--;
2141#endif
2142 var_out = add (var_out, 1);
2143#if (WMOPS)
2144 multiCounter[currCounter].add--;
2145#endif
2146 }
2147 }
2148 }
2149 }
2150
2151#if (WMOPS)
2152 multiCounter[currCounter].div_s++;
2153#endif
2154 return (var_out);
2155}
2156/* ------------------------- End of div_s() ------------------------- */
2157
2158
2159/*___________________________________________________________________________
2160 | |
2161 | Function Name : norm_l |
2162 | |
2163 | Purpose : |
2164 | |
2165 | Produces the number of left shifts needed to normalize the 32 bit varia-|
2166 | ble L_var1 for positive values on the interval with minimum of |
2167 | 1073741824 and maximum of 2147483647, and for negative values on the in-|
2168 | terval with minimum of -2147483648 and maximum of -1073741824; in order |
2169 | to normalize the result, the following operation must be done : |
2170 | norm_L_var1 = L_shl(L_var1,norm_l(L_var1)). |
2171 | |
2172 | Complexity weight : 30 |
2173 | |
2174 | Inputs : |
2175 | |
2176 | L_var1 |
2177 | 32 bit long signed integer (Word32) whose value falls in the |
2178 | range : 0x8000 0000 <= var1 <= 0x7fff ffff. |
2179 | |
2180 | Outputs : |
2181 | |
2182 | none |
2183 | |
2184 | Return Value : |
2185 | |
2186 | var_out |
2187 | 16 bit short signed integer (Word16) whose value falls in the |
2188 | range : 0x0000 0000 <= var_out <= 0x0000 001f. |
2189 |___________________________________________________________________________|
2190*/
2191Word16 norm_l (Word32 L_var1)
2192{
2193 Word16 var_out;
2194
2195 if (L_var1 == 0)
2196 {
2197 var_out = 0;
2198 }
2199 else
2200 {
2201 if (L_var1 == (Word32) 0xffffffffL)
2202 {
2203 var_out = 31;
2204 }
2205 else
2206 {
2207 if (L_var1 < 0)
2208 {
2209 L_var1 = ~L_var1;
2210 }
2211 for (var_out = 0; L_var1 < (Word32) 0x40000000L; var_out++)
2212 {
2213 L_var1 <<= 1;
2214 }
2215 }
2216 }
2217
2218#if (WMOPS)
2219 multiCounter[currCounter].norm_l++;
2220#endif
2221 return (var_out);
2222}
2223/* ------------------------- End of norm_l() ------------------------- */
2224
2225
2226/*
2227 *****************************************************************
2228 Additional operators extracted from the G.723.1 Library
2229 Adapted for WMOPS calculations
2230 *****************************************************************
2231*/
2232
2233/*___________________________________________________________________________
2234 | |
2235 | Function Name : L_mls |
2236 | |
2237 | Purpose : |
2238 | |
2239 | Multiplies a 16 bit word v by a 32 bit word Lv and returns a 32 bit |
2240 | word (multiplying 16 by 32 bit words gives 48 bit word; the function |
2241 | extracts the 32 MSB and shift the result to the left by 1). |
2242 | |
2243 | A 32 bit word can be written as |
2244 | Lv = a + b * 2^16 |
2245 | where a= unsigned 16 LSBs and b= signed 16 MSBs. |
2246 | The function returns v * Lv / 2^15 which is equivalent to |
2247 | a*v / 2^15 + b*v*2 |
2248 | |
2249 | Complexity weight : 6 [to be confirmed] |
2250 | |
2251 | Inputs : |
2252 | |
2253 | Lv |
2254 | 32 bit long signed integer (Word32) whose value falls in the |
2255 | range : 0x8000 0000 <= var1 <= 0x7fff ffff. |
2256 | v |
2257 | 16 bit short signed integer (Word16) whose value falls in the |
2258 | range : 0x8000 <= var1 <= 0x7fff. |
2259 | |
2260 | Outputs : |
2261 | |
2262 | none |
2263 | |
2264 | Return Value : |
2265 | |
2266 | var_out |
2267 | 32 bit long signed integer (Word32) whose value falls in the |
2268 | range : 0x8000 0000 <= var_out <= 0x7fff ffff. |
2269 | |
2270 |___________________________________________________________________________|
2271*/
2272Word32 L_mls (Word32 Lv, Word16 v)
2273{
2274 Word32 Temp ;
2275
2276 Temp = Lv & (Word32) 0x0000ffff ;
2277 Temp = Temp * (Word32) v ;
2278 Temp = L_shr( Temp, (Word16) 15 ) ;
2279 Temp = L_mac( Temp, v, extract_h(Lv) ) ;
2280
2281#if (WMOPS)
2282 multiCounter[currCounter].L_shr--;
2283 multiCounter[currCounter].L_mac--;
2284 multiCounter[currCounter].extract_h--;
2285 multiCounter[currCounter].L_mls++;
2286#endif
2287
2288 return Temp ;
2289}
2290/* ------------------------- End of L_mls() ------------------------- */
2291
2292
2293/*__________________________________________________________________________
2294| |
2295| Function Name : div_l |
2296| |
2297| Purpose : |
2298| |
2299| Produces a result which is the fractional integer division of L_var1 by|
2300| var2; L_var1 and var2 must be positive and var2 << 16 must be greater or|
2301| equal to L_var1; the result is positive (leading bit equal to 0) and |
2302| truncated to 16 bits. |
2303| If L_var1 == var2 << 16 then div_l(L_var1,var2) = 32767. |
2304| |
2305| Complexity weight : 20 |
2306| |
2307| Inputs : |
2308| |
2309| L_var1 |
2310| 32 bit long signed integer (Word32) whose value falls in the |
2311| range : 0x0000 0000 <= var1 <= (var2 << 16) and var2 != 0. |
2312| L_var1 must be considered as a Q.31 value |
2313| |
2314| var2 |
2315| 16 bit short signed integer (Word16) whose value falls in the |
2316| range : var1 <= (var2<< 16) <= 0x7fff0000 and var2 != 0. |
2317| var2 must be considered as a Q.15 value |
2318| |
2319| Outputs : |
2320| |
2321| none |
2322| |
2323| Return Value : |
2324| |
2325| var_out |
2326| 16 bit short signed integer (Word16) whose value falls in the |
2327| range : 0x0000 0000 <= var_out <= 0x0000 7fff. |
2328| It's a Q15 value (point between b15 and b14). |
2329|___________________________________________________________________________|
2330*/
2331Word16 div_l (Word32 L_num, Word16 den)
2332{
2333 Word16 var_out = (Word16)0;
2334 Word32 L_den;
2335 Word16 iteration;
2336
2337#if (WMOPS)
2338 multiCounter[currCounter].div_l++;
2339#endif
2340
2341 if ( den == (Word16) 0 ) {
2342 printf("Division by 0 in div_l, Fatal error \n");
2343 exit(0);
2344 }
2345
2346 if ( (L_num < (Word32) 0) || (den < (Word16) 0) ) {
2347 printf("Division Error in div_l, Fatal error \n");
2348 exit(0);
2349 }
2350
2351 L_den = L_deposit_h( den ) ;
2352#if (WMOPS)
2353 multiCounter[currCounter].L_deposit_h--;
2354#endif
2355
2356 if ( L_num >= L_den ){
2357 return MAX_16 ;
2358 }
2359 else {
2360 L_num = L_shr(L_num, (Word16)1) ;
2361 L_den = L_shr(L_den, (Word16)1);
2362#if (WMOPS)
2363 multiCounter[currCounter].L_shr-=2;
2364#endif
2365 for(iteration=(Word16)0; iteration< (Word16)15;iteration++) {
2366 var_out = shl( var_out, (Word16)1);
2367 L_num = L_shl( L_num, (Word16)1);
2368#if (WMOPS)
2369 multiCounter[currCounter].shl--;
2370 multiCounter[currCounter].L_shl--;
2371#endif
2372 if (L_num >= L_den) {
2373 L_num = L_sub(L_num,L_den);
2374 var_out = add(var_out, (Word16)1);
2375#if (WMOPS)
2376 multiCounter[currCounter].L_sub--;
2377 multiCounter[currCounter].add--;
2378#endif
2379 }
2380 }
2381
2382 return var_out;
2383 }
2384}
2385/* ------------------------- End of div_l() ------------------------- */
2386
2387
2388/*__________________________________________________________________________
2389| |
2390| Function Name : i_mult |
2391| |
2392| Purpose : |
2393| |
2394| Integer 16-bit multiplication. No overflow protection is performed if |
2395| ORIGINAL_G7231 is defined. |
2396| |
2397| Complexity weight : TBD |
2398| |
2399| Inputs : |
2400| |
2401| a |
2402| 16 bit short signed integer (Word16). |
2403| |
2404| b |
2405| 16 bit short signed integer (Word16). |
2406| |
2407| Outputs : |
2408| |
2409| none |
2410| |
2411| Return Value : |
2412| |
2413| 16 bit short signed integer (Word16). No overflow checks |
2414| are performed if ORIGINAL_G7231 is defined. |
2415|___________________________________________________________________________|
2416*/
2417Word16 i_mult (Word16 a, Word16 b)
2418{
2419#ifdef ORIGINAL_G7231
2420 return a*b ;
2421#else
2422 Word32 register c=a*b;
2423#if (WMOPS)
2424 multiCounter[currCounter].i_mult++;
2425#endif
2426 return saturate(c) ;
2427#endif
2428}
2429/* ------------------------- End of i_mult() ------------------------- */
2430
2431
2432/*
2433 **********************************************************************
2434 The following three operators are not part of the original
2435 G.729/G.723.1 set of basic operators and implement shiftless
2436 accumulation operation.
2437 **********************************************************************
2438*/
2439
2440/*___________________________________________________________________________
2441 |
2442 | Function Name : L_mult0
2443 |
2444 | Purpose :
2445 |
2446 | L_mult0 is the 32 bit result of the multiplication of var1 times var2
2447 | without one left shift.
2448 |
2449 | Complexity weight : 1
2450 |
2451 | Inputs :
2452 |
2453 | var1 16 bit short signed integer (Word16) whose value falls in the
2454 | range : 0xffff 8000 <= var1 <= 0x0000 7fff.
2455 |
2456 | var2 16 bit short signed integer (Word16) whose value falls in the
2457 | range : 0xffff 8000 <= var1 <= 0x0000 7fff.
2458 |
2459 | Return Value :
2460 |
2461 | L_var_out
2462 | 32 bit long signed integer (Word32) whose value falls in the
2463 | range : 0x8000 0000 <= L_var_out <= 0x7fff ffff.
2464 |___________________________________________________________________________
2465*/
2466Word32 L_mult0 (Word16 var1,Word16 var2)
2467{
2468 Word32 L_var_out;
2469
2470 L_var_out = (Word32)var1 * (Word32)var2;
2471
2472#if (WMOPS)
2473 multiCounter[currCounter].L_mult0++;
2474#endif
2475 return(L_var_out);
2476}
2477/* ------------------------- End of L_mult0() ------------------------- */
2478
2479
2480/*___________________________________________________________________________
2481 |
2482 | Function Name : L_mac0
2483 |
2484 | Purpose :
2485 |
2486 | Multiply var1 by var2 (without left shift) and add the 32 bit result to
2487 | L_var3 with saturation, return a 32 bit result:
2488 | L_mac0(L_var3,var1,var2) = L_add(L_var3,(L_mult0(var1,var2)).
2489 |
2490 | Complexity weight : 1
2491 |
2492 | Inputs :
2493 |
2494 | L_var3 32 bit long signed integer (Word32) whose value falls in the
2495 | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff.
2496 |
2497 | var1 16 bit short signed integer (Word16) whose value falls in the
2498 | range : 0xffff 8000 <= var1 <= 0x0000 7fff.
2499 |
2500 | var2 16 bit short signed integer (Word16) whose value falls in the
2501 | range : 0xffff 8000 <= var1 <= 0x0000 7fff.
2502 |
2503 | Return Value :
2504 |
2505 | L_var_out
2506 | 32 bit long signed integer (Word32) whose value falls in the
2507 | range : 0x8000 0000 <= L_var_out <= 0x7fff ffff.
2508 |___________________________________________________________________________
2509*/
2510Word32 L_mac0 (Word32 L_var3, Word16 var1, Word16 var2)
2511{
2512 Word32 L_var_out;
2513 Word32 L_product;
2514
2515 L_product = L_mult0(var1,var2);
2516 L_var_out = L_add(L_var3,L_product);
2517
2518#if (WMOPS)
2519 multiCounter[currCounter].L_mac0++;
2520 multiCounter[currCounter].L_mult0--;
2521 multiCounter[currCounter].L_add--;
2522#endif
2523 return(L_var_out);
2524}
2525/* ------------------------- End of L_mac0() ------------------------- */
2526
2527
2528/*___________________________________________________________________________
2529 |
2530 | Function Name : L_msu0
2531 |
2532 | Purpose :
2533 |
2534 | Multiply var1 by var2 (without left shift) and subtract the 32 bit
2535 | result to L_var3 with saturation, return a 32 bit result:
2536 | L_msu0(L_var3,var1,var2) = L_sub(L_var3,(L_mult0(var1,var2)).
2537 |
2538 | Complexity weight : 1
2539 |
2540 | Inputs :
2541 |
2542 | L_var3 32 bit long signed integer (Word32) whose value falls in the
2543 | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff.
2544 |
2545 | var1 16 bit short signed integer (Word16) whose value falls in the
2546 | range : 0xffff 8000 <= var1 <= 0x0000 7fff.
2547 |
2548 | var2 16 bit short signed integer (Word16) whose value falls in the
2549 | range : 0xffff 8000 <= var1 <= 0x0000 7fff.
2550 |
2551 | Return Value :
2552 |
2553 | L_var_out
2554 | 32 bit long signed integer (Word32) whose value falls in the
2555 | range : 0x8000 0000 <= L_var_out <= 0x7fff ffff.
2556 |___________________________________________________________________________
2557*/
2558Word32 L_msu0 (Word32 L_var3, Word16 var1, Word16 var2)
2559{
2560 Word32 L_var_out;
2561 Word32 L_product;
2562
2563 L_product = L_mult0(var1,var2);
2564 L_var_out = L_sub(L_var3,L_product);
2565
2566#if (WMOPS)
2567 multiCounter[currCounter].L_msu0++;
2568 multiCounter[currCounter].L_mult0--;
2569 multiCounter[currCounter].L_sub--;
2570#endif
2571 return(L_var_out);
2572}
2573/* ------------------------- End of L_msu0() ------------------------- */
2574
2575
2576/*___________________________________________________________________________
2577 | |
2578 | Function Name : LU_shl |
2579 | |
2580 | Purpose : |
2581 | |
2582 | Arithmetically shift the 32 bit input L_var1 left var2 positions. Zero |
2583 | fill the var2 LSB of the result. If var2 is negative, arithmetically |
2584 | shift L_var1 right by -var2 with sign extension. Saturate the result in |
2585 | case of underflows or overflows. |
2586 | |
2587 | Complexity weight : 2 |
2588 | |
2589 | Inputs : |
2590 | |
2591 | L_var1 32 bit long signed integer (Word32) whose value falls in the |
2592 | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. |
2593 | |
2594 | var2 |
2595 | 16 bit short signed integer (Word16) whose value falls in the |
2596 | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
2597 | |
2598 | Outputs : |
2599 | |
2600 | none |
2601 | |
2602 | Return Value : |
2603 | |
2604 | L_var_out |
2605 | 32 bit long signed integer (Word32) whose value falls in the |
2606 | range : 0x8000 0000 <= L_var_out <= 0x7fff ffff. |
2607 |___________________________________________________________________________|
2608*/
2609UWord32 LU_shl (UWord32 L_var1, Word16 var2)
2610{
2611 Word16 neg_var2;
2612 UWord32 L_var_out;
2613
2614 if (var2 <= 0)
2615 {
2616 if (var2 < -32)
2617 var2 = -32;
2618 neg_var2 = negate(var2);
2619 L_var_out = LU_shr (L_var1, neg_var2);
2620#if (WMOPS)
2621 multiCounter[currCounter].negate--;
2622 multiCounter[currCounter].LU_shr--;
2623#endif
2624 }
2625 else
2626 {
2627 for (; var2 > 0; var2--)
2628 {
2629 if (L_var1 > (UWord32) 0X7fffffffL)
2630 {
2631 Overflow = 1;
2632 L_var_out = UMAX_32;
2633 break;
2634 }
2635 else
2636 {
2637 if (L_var1 < (UWord32) 0x00000001L)
2638 {
2639 Overflow = 1;
2640 L_var_out = MIN_32;
2641 break;
2642 }
2643 }
2644 L_var1 *= 2;
2645 L_var_out = L_var1;
2646 }
2647 }
2648#if (WMOPS)
2649 multiCounter[currCounter].LU_shl++;
2650#endif
2651 return (L_var_out);
2652}
2653/* ------------------------- End of LU_shl() ------------------------- */
2654
2655
2656/*___________________________________________________________________________
2657 | |
2658 | Function Name : LU_shr |
2659 | |
2660 | Purpose : |
2661 | |
2662 | Arithmetically shift the 32 bit input L_var1 right var2 positions with |
2663 | sign extension. If var2 is negative, arithmetically shift L_var1 left |
2664 | by -var2 and zero fill the -var2 LSB of the result. Saturate the result |
2665 | in case of underflows or overflows. |
2666 | |
2667 | Complexity weight : 2 |
2668 | |
2669 | Inputs : |
2670 | |
2671 | L_var1 32 bit long signed integer (Word32) whose value falls in the |
2672 | range : 0x8000 0000 <= L_var3 <= 0x7fff ffff. |
2673 | |
2674 | var2 |
2675 | 16 bit short signed integer (Word16) whose value falls in the |
2676 | range : 0xffff 8000 <= var1 <= 0x0000 7fff. |
2677 | |
2678 | Outputs : |
2679 | |
2680 | none |
2681 | |
2682 | Return Value : |
2683 | |
2684 | L_var_out |
2685 | 32 bit long signed integer (Word32) whose value falls in the |
2686 | range : 0x8000 0000 <= L_var_out <= 0x7fff ffff. |
2687 |___________________________________________________________________________|
2688*/
2689UWord32 LU_shr (UWord32 L_var1, Word16 var2)
2690{
2691 Word16 neg_var2;
2692 UWord32 L_var_out;
2693
2694 if (var2 < 0)
2695 {
2696 if (var2 < -32)
2697 var2 = -32;
2698 neg_var2 = negate(var2);
2699 L_var_out = LU_shl (L_var1, neg_var2);
2700#if (WMOPS)
2701 multiCounter[currCounter].negate--;
2702 multiCounter[currCounter].LU_shl--;
2703#endif
2704 }
2705 else
2706 {
2707 if (var2 >= 32)
2708 {
2709 L_var_out = 0L;
2710 }
2711 else
2712 {
2713 L_var_out = L_var1 >> var2;
2714 }
2715 }
2716#if (WMOPS)
2717 multiCounter[currCounter].LU_shr++;
2718#endif
2719 return (L_var_out);
2720}
2721/* ------------------------- End of LU_shr() ------------------------- */
2722
2723
2724/* ************************** END OF BASOP32.C ************************** */