comparison libtwamr/basicop2.c @ 252:57b4053559ff

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