comparison libgsmefr/basicop2.c @ 38:38326102fc43

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