1 /******************************************************************************
2 *
3 * Copyright (C) 2018 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 *****************************************************************************
18 * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
19 */
20
21 /*****************************************************************************/
22 /* */
23 /* File name : ia_basic_ops32.h */
24 /* */
25 /* Description : this file has 32bit basic operations */
26 /* */
27 /* List of functions: 1. min32 */
28 /* 2. max32 */
29 /* 3. mult16x16in32 */
30 /* 4. mult16x16in32_shl */
31 /* 5. mult16x16in32_shl_sat */
32 /* 6. shl32 */
33 /* 7. shl32_sat */
34 /* 8. shl32_dir */
35 /* 9. shl32_dir_sat */
36 /* 10. shr32 */
37 /* 11. shr32_dir */
38 /* 12. shr32_dir_sat */
39 /* 13. add32 */
40 /* 14. sub32 */
41 /* 15. add32_sat */
42 /* 16. sub32_sat */
43 /* 17. norm32 */
44 /* 18. bin_expo32 */
45 /* 19. abs32 */
46 /* 20. abs32_sat */
47 /* 21. negate32 */
48 /* 22. negate32_sat */
49 /* 23. div32 */
50 /* 24. mac16x16in32 */
51 /* 25. mac16x16in32_shl */
52 /* 26. mac16x16in32_shl_sat */
53 /* 27. msu16x16in32 */
54 /* 28. msu16x16in32_shl */
55 /* 29. msu16x16in32_shl_sat */
56 /* 30. add32_shr */
57 /* 31. sub32_shr */
58 /* */
59 /* Issues / problems: none */
60 /* */
61 /* Revision history : */
62 /* */
63 /* DD MM YYYY author changes */
64 /* 20 11 2003 aadithya kamath created */
65 /* 15 11 2004 tejaswi/vishal modified(bug fixes/cleanup) */
66 /* */
67 /*****************************************************************************/
68
69 /*****************************************************************************/
70 /* File includes */
71 /* ia_type_def.h */
72 /* ia_constants.h */
73 /*****************************************************************************/
74
75 #ifndef __IA_BASIC_OPS32_H__
76 #define __IA_BASIC_OPS32_H__
77
78 /*****************************************************************************/
79 /* */
80 /* Function name : min32 */
81 /* */
82 /* Description : returns the minima of 2 32 bit variables */
83 /* */
84 /* Inputs : WORD32 a, WORD32 b */
85 /* */
86 /* Globals : none */
87 /* */
88 /* Processing : minimum of 2 inputs */
89 /* */
90 /* Outputs : none */
91 /* */
92 /* Returns : WORD32 min_val - 32 bit signed value */
93 /* */
94 /* Issues : none */
95 /* */
96 /* Revision history : */
97 /* */
98 /* DD MM YYYY author changes */
99 /* 20 11 2003 aadithya kamath created */
100 /* 15 11 2004 tejaswi/vishal modified(bug fixes/cleanup) */
101 /* */
102 /*****************************************************************************/
103
min32(WORD32 a,WORD32 b)104 static PLATFORM_INLINE WORD32 min32(WORD32 a, WORD32 b)
105 {
106 WORD32 min_val;
107
108 min_val = (a < b) ? a : b;
109
110 return min_val;
111 }
112
113 /*****************************************************************************/
114 /* */
115 /* Function name : max32 */
116 /* */
117 /* Description : returns the maxima of 2 32 bit variables */
118 /* */
119 /* Inputs : WORD32 a, WORD32 b */
120 /* */
121 /* Globals : none */
122 /* */
123 /* Processing : maximum of 2 inputs */
124 /* */
125 /* Outputs : none */
126 /* */
127 /* Returns : WORD32 max_val - 32 bit signed value */
128 /* */
129 /* Issues : none */
130 /* */
131 /* Revision history : */
132 /* */
133 /* DD MM YYYY author changes */
134 /* 20 11 2003 aadithya kamath created */
135 /* 15 11 2004 tejaswi/vishal modified(bug fixes/cleanup) */
136 /* */
137 /*****************************************************************************/
138
max32(WORD32 a,WORD32 b)139 static PLATFORM_INLINE WORD32 max32(WORD32 a, WORD32 b)
140 {
141 WORD32 max_val;
142
143 max_val = (a > b) ? a : b;
144
145 return max_val;
146 }
147
148 /*****************************************************************************/
149 /* */
150 /* Function name : shl32 */
151 /* */
152 /* Description : shifts a 32-bit value left by specificed bits */
153 /* */
154 /* Inputs : WORD32 a, WORD b */
155 /* */
156 /* Globals : none */
157 /* */
158 /* Processing : shift a by b */
159 /* */
160 /* Outputs : none */
161 /* */
162 /* Returns : WORD32 out_val - 32 bit signed value */
163 /* */
164 /* assumptions : 0 <= b <= 31 */
165 /* */
166 /* Issues : none */
167 /* */
168 /* Revision history : */
169 /* */
170 /* DD MM YYYY author changes */
171 /* 20 11 2003 aadithya kamath created */
172 /* 15 11 2004 tejaswi/vishal modified(bug fixes/cleanup) */
173 /* */
174 /*****************************************************************************/
shl32(WORD32 a,WORD b)175 static PLATFORM_INLINE WORD32 shl32(WORD32 a, WORD b)
176 {
177 WORD32 out_val;
178
179 if(b > 31)
180 out_val = 0;
181 else
182 out_val = (WORD32)a << b;
183
184 return out_val;
185 }
186 /*****************************************************************************/
187 /* */
188 /* Function name : shr32 */
189 /* */
190 /* Description : shifts a 32-bit value right by specificed bits */
191 /* */
192 /* Inputs : WORD32 a, WORD b */
193 /* */
194 /* Globals : none */
195 /* */
196 /* Processing : shift right a by b */
197 /* */
198 /* Outputs : none */
199 /* */
200 /* Returns : WORD32 out_val - 32 bit signed value */
201 /* */
202 /* assumptions : 0 <= b <= 31 */
203 /* */
204 /* Issues : none */
205 /* */
206 /* Revision history : */
207 /* */
208 /* DD MM YYYY author changes */
209 /* 20 11 2003 aadithya kamath created */
210 /* 15 11 2004 tejaswi/vishal modified(bug fixes/cleanup) */
211 /* */
212 /*****************************************************************************/
213
shr32(WORD32 a,WORD b)214 static PLATFORM_INLINE WORD32 shr32(WORD32 a, WORD b)
215 {
216 WORD32 out_val;
217
218 if(b > 31)
219 {
220 if(a < 0)
221 out_val = -1;
222 else
223 out_val = 0;
224 }
225 else
226 {
227 out_val = (WORD32)a >> b;
228 }
229
230 return out_val;
231 }
232
233 /*****************************************************************************/
234 /* */
235 /* Function name : shl32_sat */
236 /* */
237 /* Description : shifts a 32-bit value left by specificed bits and */
238 /* saturates it to 32 bits */
239 /* */
240 /* Inputs : WORD32 a, WORD b */
241 /* */
242 /* Globals : none */
243 /* */
244 /* Processing : shift a by 1 b times if crosses 32_bits saturate */
245 /* */
246 /* Outputs : none */
247 /* */
248 /* Returns : WORD32 out_val - 32 bit signed value */
249 /* */
250 /* assumptions : 0 <= b <= 31 */
251 /* */
252 /* Issues : none */
253 /* */
254 /* Revision history : */
255 /* */
256 /* DD MM YYYY author changes */
257 /* 20 11 2003 aadithya kamath created */
258 /* 15 11 2004 tejaswi/vishal modified(bug fixes/cleanup) */
259 /* */
260 /*****************************************************************************/
261
shl32_sat(WORD32 a,WORD b)262 static PLATFORM_INLINE WORD32 shl32_sat(WORD32 a, WORD b)
263 {
264 WORD32 out_val = a;
265 /*clip the max shift value to avoid unnecessary looping*/
266 if(b > (WORD)((sizeof(WORD32) * 8)))
267 b = (sizeof(WORD32) * 8);
268 for(; b > 0; b--)
269 {
270 if(a > (WORD32)0X3fffffffL)
271 {
272 out_val = MAX_32;
273 break;
274 }
275 else if(a < (WORD32)0xc0000000L)
276 {
277 out_val = MIN_32;
278 break;
279 }
280
281 a = shl32(a, 1);
282 out_val = a;
283 }
284 return (out_val);
285 }
286
287 /*****************************************************************************/
288 /* */
289 /* Function name : shl32_dir */
290 /* */
291 /* Description : shifts a 32-bit value left by specificed bits, shifts */
292 /* it right if specified no. of bits is negative */
293 /* */
294 /* Inputs : WORD32 a, WORD b */
295 /* */
296 /* Globals : none */
297 /* */
298 /* Processing : if b -ve shift right else shift left */
299 /* */
300 /* Outputs : none */
301 /* */
302 /* Returns : WORD32 out_val - 32 bit signed value */
303 /* */
304 /* assumptions : -31 <= b <= 31 */
305 /* */
306 /* Issues : none */
307 /* */
308 /* Revision history : */
309 /* */
310 /* DD MM YYYY author changes */
311 /* 20 11 2003 aadithya kamath created */
312 /* 15 11 2004 tejaswi/vishal modified(bug fixes/cleanup) */
313 /* */
314 /*****************************************************************************/
315
shl32_dir(WORD32 a,WORD b)316 static PLATFORM_INLINE WORD32 shl32_dir(WORD32 a, WORD b)
317 {
318 WORD32 out_val;
319
320 if(b < 0)
321 {
322 out_val = shr32(a, -b);
323 }
324 else
325 {
326 out_val = shl32(a, b);
327 }
328
329 return out_val;
330 }
331
332 /*****************************************************************************/
333 /* */
334 /* Function name : shl32_dir_sat */
335 /* */
336 /* Description : shifts a 32-bit value left by specificed bits with sat, */
337 /* shifts it right if specified no. of bits is negative */
338 /* */
339 /* Inputs : WORD32 a, WORD b */
340 /* */
341 /* Globals : none */
342 /* */
343 /* Processing : if b -ve shift right else shift left with sat */
344 /* */
345 /* Outputs : none */
346 /* */
347 /* Returns : WORD32 out_val - 32 bit signed value */
348 /* */
349 /* assumptions : -31 <= b <= 31 */
350 /* */
351 /* Issues : none */
352 /* */
353 /* Revision history : */
354 /* */
355 /* DD MM YYYY author changes */
356 /* 20 11 2003 aadithya kamath created */
357 /* 15 11 2004 tejaswi/vishal modified(bug fixes/cleanup) */
358 /* */
359 /*****************************************************************************/
360
shl32_dir_sat(WORD32 a,WORD b)361 static PLATFORM_INLINE WORD32 shl32_dir_sat(WORD32 a, WORD b)
362 {
363 WORD32 out_val;
364
365 if(b < 0)
366 {
367 out_val = shr32(a, -b);
368 }
369 else
370 {
371 out_val = shl32_sat(a, b);
372 }
373
374 return out_val;
375 }
376
377 /*****************************************************************************/
378 /* */
379 /* Function name : shr32_dir */
380 /* */
381 /* Description : shifts a 32-bit value right by specificed bits, shifts */
382 /* it left if specified no. of bits is negative */
383 /* */
384 /* Inputs : WORD32 a, WORD b */
385 /* */
386 /* Globals : none */
387 /* */
388 /* Processing : if b +ve shift right else shift left */
389 /* */
390 /* Outputs : none */
391 /* */
392 /* Returns : WORD32 out_val - 32 bit signed value */
393 /* */
394 /* assumptions : -31 <= b <= 31 */
395 /* */
396 /* Issues : none */
397 /* */
398 /* Revision history : */
399 /* */
400 /* DD MM YYYY author changes */
401 /* 20 11 2003 aadithya kamath created */
402 /* 15 11 2004 tejaswi/vishal modified(bug fixes/cleanup) */
403 /* */
404 /*****************************************************************************/
405
shr32_dir(WORD32 a,WORD b)406 static PLATFORM_INLINE WORD32 shr32_dir(WORD32 a, WORD b)
407 {
408 WORD32 out_val;
409
410 if(b < 0)
411 {
412 out_val = shl32(a, -b);
413 }
414 else
415 {
416 out_val = shr32(a, b);
417 }
418
419 return out_val;
420 }
421
422 /*****************************************************************************/
423 /* */
424 /* Function name : shr32_dir_sat */
425 /* */
426 /* Description : shifts a 32-bit value right by specificed bits, shifts */
427 /* it left with sat if specified no. of bits is negative */
428 /* */
429 /* Inputs : WORD32 a, WORD b */
430 /* */
431 /* Globals : none */
432 /* */
433 /* Processing : if b +ve shift right else shift left with sat */
434 /* */
435 /* Outputs : none */
436 /* */
437 /* Returns : WORD32 out_val - 32 bit signed value */
438 /* */
439 /* assumptions : -31 <= b <= 31 */
440 /* */
441 /* Issues : none */
442 /* */
443 /* Revision history : */
444 /* */
445 /* DD MM YYYY author changes */
446 /* 20 11 2003 aadithya kamath created */
447 /* 15 11 2004 tejaswi/vishal modified(bug fixes/cleanup) */
448 /* */
449 /*****************************************************************************/
450
shr32_dir_sat(WORD32 a,WORD b)451 static PLATFORM_INLINE WORD32 shr32_dir_sat(WORD32 a, WORD b)
452 {
453 WORD32 out_val;
454
455 if(b < 0)
456 {
457 out_val = shl32_sat(a, -b);
458 }
459 else
460 {
461 out_val = shr32(a, b);
462 }
463
464 return out_val;
465 }
466
467 /*****************************************************************************/
468 /* */
469 /* Function name : mult16x16in32 */
470 /* */
471 /* Description : multiplies two 16 bit numbers and returns their 32-bit */
472 /* result */
473 /* */
474 /* Inputs : WORD16 a, WORD16 b */
475 /* */
476 /* Globals : none */
477 /* */
478 /* Processing : multiply 2 inputs */
479 /* */
480 /* Outputs : none */
481 /* */
482 /* Returns : WORD32 product - 32 bit signed value */
483 /* */
484 /* Issues : none */
485 /* */
486 /* Revision history : */
487 /* */
488 /* DD MM YYYY author changes */
489 /* 20 11 2003 aadithya kamath created */
490 /* 15 11 2004 tejaswi/vishal modified(bug fixes/cleanup) */
491 /* */
492 /*****************************************************************************/
493
mult16x16in32(WORD16 a,WORD16 b)494 static PLATFORM_INLINE WORD32 mult16x16in32(WORD16 a, WORD16 b)
495 {
496 WORD32 product;
497
498 product = (WORD32)a * (WORD32)b;
499
500 return product;
501 }
502
503 /*****************************************************************************/
504 /* */
505 /* Function name : mult16x16in32_shl */
506 /* */
507 /* Description : multiplies two 16 bit numbers and returns their 32-bit */
508 /* result after removing 1 redundant sign bit. no sat */
509 /* */
510 /* Inputs : WORD16 a, WORD16 b */
511 /* */
512 /* Globals : none */
513 /* */
514 /* Processing : multiply 2 inputs, shift left by 1 */
515 /* */
516 /* Outputs : none */
517 /* */
518 /* Returns : WORD32 product - 32 bit signed value */
519 /* */
520 /* Issues : none */
521 /* */
522 /* Revision history : */
523 /* */
524 /* DD MM YYYY author changes */
525 /* 20 11 2003 aadithya kamath created */
526 /* 15 11 2004 tejaswi/vishal modified(bug fixes/cleanup) */
527 /* */
528 /*****************************************************************************/
529
mult16x16in32_shl(WORD16 a,WORD16 b)530 static PLATFORM_INLINE WORD32 mult16x16in32_shl(WORD16 a, WORD16 b)
531 {
532 WORD32 product;
533
534 product = shl32(mult16x16in32(a, b), 1);
535
536 return product;
537 }
538
539 /*****************************************************************************/
540 /* */
541 /* Function name : mult16x16in32_shl_sat */
542 /* */
543 /* Description : multiplies two 16 bit numbers and returns their 32-bit */
544 /* result after removing 1 redundant sign bit with sat */
545 /* */
546 /* Inputs : WORD16 a, WORD16 b */
547 /* */
548 /* Globals : none */
549 /* */
550 /* Processing : if inputs mi_ns return MAX32 else */
551 /* multiply 2 inputs shift left by 1 */
552 /* */
553 /* Outputs : none */
554 /* */
555 /* Returns : WORD32 product - 32 bit signed value */
556 /* */
557 /* Issues : none */
558 /* */
559 /* Revision history : */
560 /* */
561 /* DD MM YYYY author changes */
562 /* 20 11 2003 aadithya kamath created */
563 /* 15 11 2004 tejaswi/vishal modified(bug fixes/cleanup) */
564 /* */
565 /*****************************************************************************/
566
mult16x16in32_shl_sat(WORD16 a,WORD16 b)567 static PLATFORM_INLINE WORD32 mult16x16in32_shl_sat(WORD16 a, WORD16 b)
568 {
569 WORD32 product;
570 product = (WORD32)a * (WORD32)b;
571 if(product != (WORD32)0x40000000L)
572 {
573 product = shl32(product, 1);
574 }
575 else
576 {
577 product = MAX_32;
578 }
579 return product;
580 }
581
582 /*****************************************************************************/
583 /* */
584 /* Function name : add32 */
585 /* */
586 /* Description : adds 2 32 bit variables without sat */
587 /* */
588 /* Inputs : WORD32 a, WORD32 b */
589 /* */
590 /* Globals : none */
591 /* */
592 /* Processing : add 2 inputs */
593 /* */
594 /* Outputs : none */
595 /* */
596 /* Returns : WORD32 sum - 32 bit signed value */
597 /* */
598 /* Issues : none */
599 /* */
600 /* Revision history : */
601 /* */
602 /* DD MM YYYY author changes */
603 /* 20 11 2003 aadithya kamath created */
604 /* 15 11 2004 tejaswi/vishal modified(bug fixes/cleanup) */
605 /* */
606 /*****************************************************************************/
607
add32(WORD32 a,WORD32 b)608 static PLATFORM_INLINE WORD32 add32(WORD32 a, WORD32 b)
609 {
610 WORD32 sum;
611
612 sum = (WORD32)a + (WORD32)b;
613
614 return sum;
615 }
616
617 /*****************************************************************************/
618 /* */
619 /* Function name : sub32 */
620 /* */
621 /* Description : subs 2 32 bit variables without sat */
622 /* */
623 /* Inputs : WORD32 a, WORD32 b */
624 /* */
625 /* Globals : none */
626 /* */
627 /* Processing : sub 2 inputs */
628 /* */
629 /* Outputs : none */
630 /* */
631 /* Returns : WORD32 diff - 32 bit signed value */
632 /* */
633 /* Issues : none */
634 /* */
635 /* Revision history : */
636 /* */
637 /* DD MM YYYY author changes */
638 /* 20 11 2003 aadithya kamath created */
639 /* 15 11 2004 tejaswi/vishal modified(bug fixes/cleanup) */
640 /* */
641 /*****************************************************************************/
642
sub32(WORD32 a,WORD32 b)643 static PLATFORM_INLINE WORD32 sub32(WORD32 a, WORD32 b)
644 {
645 WORD32 diff;
646
647 diff = (WORD32)a - (WORD32)b;
648
649 return diff;
650 }
651
652 /*****************************************************************************/
653 /* */
654 /* Function name : add32_sat */
655 /* */
656 /* Description : adds 2 32 bit variables with sat */
657 /* */
658 /* Inputs : WORD32 a, WORD32 b */
659 /* */
660 /* Globals : none */
661 /* */
662 /* Processing : add 2 inputs if overflow saturate */
663 /* */
664 /* Outputs : none */
665 /* */
666 /* Returns : WORD32 sum - 32 bit signed value */
667 /* */
668 /* Issues : none */
669 /* */
670 /* Revision history : */
671 /* */
672 /* DD MM YYYY author changes */
673 /* 20 11 2003 aadithya kamath created */
674 /* 15 11 2004 tejaswi/vishal modified(bug fixes/cleanup) */
675 /* */
676 /*****************************************************************************/
677
add32_sat(WORD32 a,WORD32 b)678 static PLATFORM_INLINE WORD32 add32_sat(WORD32 a, WORD32 b)
679 {
680 WORD32 sum;
681
682 sum = add32(a, b);
683
684 if((((WORD32)a ^ (WORD32)b) & (WORD32)MIN_32) == 0)
685 {
686 if(((WORD32)sum ^ (WORD32)a) & (WORD32)MIN_32)
687 {
688 sum = (a < 0) ? MIN_32 : MAX_32;
689 }
690 }
691
692 return sum;
693 }
694
695 /*****************************************************************************/
696 /* */
697 /* Function name : sub32_sat */
698 /* */
699 /* Description : subs 2 32 bit variables with sat */
700 /* */
701 /* Inputs : WORD32 a, WORD32 b */
702 /* */
703 /* Globals : none */
704 /* */
705 /* Processing : sub 2 inputs, if overflow saturate */
706 /* */
707 /* Outputs : none */
708 /* */
709 /* Returns : WORD32 diff - 32 bit signed value */
710 /* */
711 /* Issues : none */
712 /* */
713 /* Revision history : */
714 /* */
715 /* DD MM YYYY author changes */
716 /* 20 11 2003 aadithya kamath created */
717 /* 15 11 2004 tejaswi/vishal modified(bug fixes/cleanup) */
718 /* */
719 /*****************************************************************************/
720
sub32_sat(WORD32 a,WORD32 b)721 static PLATFORM_INLINE WORD32 sub32_sat(WORD32 a, WORD32 b)
722 {
723 WORD32 diff;
724
725 diff = sub32(a, b);
726
727 if((((WORD32)a ^ (WORD32)b) & (WORD32)MIN_32) != 0)
728 {
729 if(((WORD32)diff ^ (WORD32)a) & (WORD32)MIN_32)
730 {
731 diff = (a < 0L) ? MIN_32 : MAX_32;
732 }
733 }
734
735 return (diff);
736 }
737
738 /*****************************************************************************/
739 /* */
740 /* Function name : norm32 */
741 /* */
742 /* Description : returns number of redundant sign bits in a */
743 /* 32-bit value. for a value of 0, returns 0 */
744 /* */
745 /* Inputs : WORD32 a */
746 /* */
747 /* Globals : none */
748 /* */
749 /* Processing : abs input, left shift till reaches 0x40000000 */
750 /* return no. of left shifts */
751 /* */
752 /* Outputs : none */
753 /* */
754 /* Returns : WORD norm_val - 0 <= norm_val < 32 */
755 /* */
756 /* Issues : none */
757 /* */
758 /* Revision history : */
759 /* */
760 /* DD MM YYYY author changes */
761 /* 20 11 2003 aadithya kamath created */
762 /* 15 11 2004 tejaswi/vishal modified(bug fixes/cleanup) */
763 /* */
764 /*****************************************************************************/
765
norm32(WORD32 a)766 static PLATFORM_INLINE WORD norm32(WORD32 a)
767 {
768 WORD norm_val;
769
770 if(a == 0)
771 {
772 norm_val = 31;
773 }
774 else
775 {
776 if(a == (WORD32)0xffffffffL)
777 {
778 norm_val = 31;
779 }
780 else
781 {
782 if(a < 0)
783 {
784 a = ~a;
785 }
786 for(norm_val = 0; a < (WORD32)0x40000000L; norm_val++)
787 {
788 a <<= 1;
789 }
790 }
791 }
792
793 return norm_val;
794 }
795
796 /*****************************************************************************/
797 /* */
798 /* Function name : bin_expo32 */
799 /* */
800 /* Description : returns the position of the most significant bit. */
801 /* for negative numbers, it ignores leading zeros to */
802 /* determine the position of most significant bit. */
803 /* note: for a value of zero returns 31 */
804 /* */
805 /* Inputs : WORD32 a */
806 /* */
807 /* Globals : none */
808 /* */
809 /* Processing : substract 31 from norm_val */
810 /* */
811 /* Outputs : none */
812 /* */
813 /* Returns : WORD bin_expo_val - 0 <= val < 32 */
814 /* */
815 /* Issues : none */
816 /* */
817 /* Revision history : */
818 /* */
819 /* DD MM YYYY author changes */
820 /* 20 11 2003 aadithya kamath created */
821 /* 15 11 2004 tejaswi/vishal modified(bug fixes/cleanup) */
822 /* */
823 /*****************************************************************************/
824
bin_expo32(WORD32 a)825 static PLATFORM_INLINE WORD bin_expo32(WORD32 a)
826 {
827 WORD bin_expo_val;
828
829 bin_expo_val = 31 - norm32(a);
830
831 return bin_expo_val;
832 }
833
834 /*****************************************************************************/
835 /* */
836 /* Function name : abs32 */
837 /* */
838 /* Description : returns the value of 32-bit number without sat. */
839 /* */
840 /* Inputs : WORD32 a */
841 /* */
842 /* Globals : none */
843 /* */
844 /* Processing : if -ve then negate */
845 /* */
846 /* Outputs : none */
847 /* */
848 /* Returns : WORD32 abs_val - 32 bit signed value */
849 /* */
850 /* Issues : none */
851 /* */
852 /* Revision history : */
853 /* */
854 /* DD MM YYYY author changes */
855 /* 20 11 2003 aadithya kamath created */
856 /* 15 11 2004 tejaswi/vishal modified(bug fixes/cleanup) */
857 /* */
858 /*****************************************************************************/
859
abs32(WORD32 a)860 static PLATFORM_INLINE WORD32 abs32(WORD32 a)
861 {
862 WORD32 abs_val;
863
864 abs_val = a;
865
866 if(a < 0)
867 {
868 abs_val = -a;
869 }
870
871 return abs_val;
872 }
873
874 /*****************************************************************************/
875 /* */
876 /* Function name : abs32_sat */
877 /* */
878 /* Description : returns the value of 32-bit number with sat. */
879 /* */
880 /* Inputs : WORD32 a */
881 /* */
882 /* Globals : none */
883 /* */
884 /* Processing : if -ve then negate, abs(-32768) is 32767 */
885 /* */
886 /* Outputs : none */
887 /* */
888 /* Returns : WORD32 abs_val - 32 bit signed value */
889 /* */
890 /* Issues : none */
891 /* */
892 /* Revision history : */
893 /* */
894 /* DD MM YYYY author changes */
895 /* 20 11 2003 aadithya kamath created */
896 /* 15 11 2004 tejaswi/vishal modified(bug fixes/cleanup) */
897 /* */
898 /*****************************************************************************/
899
abs32_sat(WORD32 a)900 static PLATFORM_INLINE WORD32 abs32_sat(WORD32 a)
901 {
902 WORD32 abs_val;
903
904 abs_val = a;
905
906 if(a == (WORD32)MIN_32)
907 {
908 abs_val = MAX_32;
909 }
910 else if(a < 0)
911 {
912 abs_val = -a;
913 }
914
915 return abs_val;
916 }
917
918 /*****************************************************************************/
919 /* */
920 /* Function name : negate32 */
921 /* */
922 /* Description : returns the negated value of 32-bit number without sat. */
923 /* */
924 /* Inputs : WORD32 a */
925 /* */
926 /* Globals : none */
927 /* */
928 /* Processing : negate input */
929 /* */
930 /* Outputs : none */
931 /* */
932 /* Returns : WORD32 neg_val - 32 bit signed value */
933 /* */
934 /* Issues : none */
935 /* */
936 /* Revision history : */
937 /* */
938 /* DD MM YYYY author changes */
939 /* 20 11 2003 aadithya kamath created */
940 /* 15 11 2004 tejaswi/vishal modified(bug fixes/cleanup) */
941 /* */
942 /*****************************************************************************/
943
negate32(WORD32 a)944 static PLATFORM_INLINE WORD32 negate32(WORD32 a)
945 {
946 WORD32 neg_val;
947
948 neg_val = -a;
949
950 return neg_val;
951 }
952
953 /*****************************************************************************/
954 /* */
955 /* Function name : negate32 */
956 /* */
957 /* Description : returns the negated value of 32-bit number with sat. */
958 /* */
959 /* Inputs : WORD32 a */
960 /* */
961 /* Globals : none */
962 /* */
963 /* Processing : negate input, if -32768 then 32767 */
964 /* */
965 /* Outputs : none */
966 /* */
967 /* Returns : WORD32 neg_val - 32 bit signed value */
968 /* */
969 /* Issues : none */
970 /* */
971 /* Revision history : */
972 /* */
973 /* DD MM YYYY author changes */
974 /* 20 11 2003 aadithya kamath created */
975 /* 15 11 2004 tejaswi/vishal modified(bug fixes/cleanup) */
976 /* */
977 /*****************************************************************************/
978
negate32_sat(WORD32 a)979 static PLATFORM_INLINE WORD32 negate32_sat(WORD32 a)
980 {
981 WORD32 neg_val;
982
983 neg_val = -a;
984 if(a == (WORD32)MIN_32)
985 {
986 neg_val = MAX_32;
987 }
988
989 return neg_val;
990 }
991 /*****************************************************************************/
992 /* */
993 /* Function name : subc_32 */
994 /* */
995 /* Description : implemnets the subc operation c64x . */
996 /* */
997 /* Inputs : WORD32 a, WORD32 b */
998 /* */
999 /* Globals : none */
1000 /* */
1001 /* Processing : implemnets the subc operation c64x */
1002 /* */
1003 /* Outputs : none */
1004 /* */
1005 /* Returns : WORD32 neg_val - 32 bit signed value */
1006 /* */
1007 /* Issues : none */
1008 /* */
1009 /* Revision history : */
1010 /* */
1011 /* DD MM YYYY author changes */
1012 /* 20 11 2003 Mudit Mehrotra created */
1013 /* */
1014 /*****************************************************************************/
subc_32(UWORD32 nr,UWORD32 dr)1015 static PLATFORM_INLINE UWORD32 subc_32(UWORD32 nr, UWORD32 dr)
1016 {
1017 UWORD32 result;
1018 if(nr >= dr)
1019 {
1020 result = (((nr - dr) << 1) + 1);
1021 }
1022 else
1023 {
1024 result = (UWORD32)nr << 1;
1025 }
1026 return (result);
1027 }
1028
1029 /*****************************************************************************/
1030 /* */
1031 /* Function name : div32 */
1032 /* */
1033 /* Description : divides 2 32 bit variables and returns the quotient */
1034 /* the q-format of the result is modified */
1035 /* ( a/b to Q30 precision) */
1036 /* */
1037 /* Inputs : WORD32 a, WORD32 b, WORD16 *q_format */
1038 /* */
1039 /* Globals : none */
1040 /* */
1041 /* Processing : non-restoration algo(shift & substract) */
1042 /* */
1043 /* Outputs : none */
1044 /* */
1045 /* Returns : WORD32 quotient - 32 bit signed value */
1046 /* */
1047 /* Issues : none */
1048 /* */
1049 /* Revision history : */
1050 /* */
1051 /* DD MM YYYY author changes */
1052 /* 20 11 2003 aadithya kamath created */
1053 /* 15 11 2004 tejaswi/vishal modified(bug fixes/cleanup) */
1054 /* */
1055 /*****************************************************************************/
div32(WORD32 a,WORD32 b,WORD * q_format)1056 static PLATFORM_INLINE WORD32 div32(WORD32 a, WORD32 b, WORD *q_format)
1057 {
1058 WORD32 quotient;
1059 UWORD32 mantissa_nr, mantissa_dr;
1060 WORD sign = 0;
1061
1062 LOOPINDEX i;
1063 WORD q_nr, q_dr;
1064
1065 mantissa_nr = a;
1066 mantissa_dr = b;
1067 quotient = 0;
1068
1069 if((a < 0) && (0 != b))
1070 {
1071 a = -a;
1072 sign = (WORD)(sign ^ -1);
1073 }
1074
1075 if(b < 0)
1076 {
1077 b = -b;
1078 sign = (WORD)(sign ^ -1);
1079 }
1080
1081 if(0 == b)
1082 {
1083 *q_format = 0;
1084 return (a);
1085 }
1086
1087 quotient = 0;
1088
1089 q_nr = norm32(a);
1090 mantissa_nr = (UWORD32)a << (q_nr);
1091 q_dr = norm32(b);
1092 mantissa_dr = (UWORD32)b << (q_dr);
1093 *q_format = (WORD)(30 + q_nr - q_dr);
1094
1095 for(i = 0; i < 31; i++)
1096 {
1097 /* quotient = quotient << 1; */
1098 WORD32 bit;
1099
1100 /*if(mantissa_nr >= mantissa_dr)
1101 {
1102
1103 mantissa_nr = (((mantissa_nr - mantissa_dr) << 1) + 1);
1104 }
1105 else
1106 {
1107 mantissa_nr = (UWORD32)mantissa_nr << 1;
1108 }
1109 */
1110 mantissa_nr = subc_32(mantissa_nr, mantissa_dr);
1111 bit = (mantissa_nr & 0x00000001);
1112 mantissa_nr = mantissa_nr & 0xfffffffe;
1113 quotient = (quotient << 1) + bit;
1114 }
1115
1116 if(sign < 0)
1117 {
1118 quotient = -quotient;
1119 }
1120
1121 return quotient;
1122 }
1123
1124 /*****************************************************************************/
1125 /* */
1126 /* Function name : mac16x16in32 */
1127 /* */
1128 /* Description : multiplies two 16 bit numbers and accumulates their */
1129 /* result in a 32 bit variable without sat */
1130 /* */
1131 /* Inputs : WORD32 a, WORD32 b, WORD16 c */
1132 /* */
1133 /* Globals : none */
1134 /* */
1135 /* Processing : multiply & add */
1136 /* */
1137 /* Outputs : none */
1138 /* */
1139 /* Returns : WORD32 acc - 32 bit signed value */
1140 /* */
1141 /* Issues : none */
1142 /* */
1143 /* Revision history : */
1144 /* */
1145 /* DD MM YYYY author changes */
1146 /* 20 11 2003 aadithya kamath created */
1147 /* 15 11 2004 tejaswi/vishal modified(bug fixes/cleanup) */
1148 /* */
1149 /*****************************************************************************/
1150
mac16x16in32(WORD32 a,WORD16 b,WORD16 c)1151 static PLATFORM_INLINE WORD32 mac16x16in32(WORD32 a, WORD16 b, WORD16 c)
1152 {
1153 WORD32 acc;
1154
1155 acc = mult16x16in32(b, c);
1156
1157 acc = add32(a, acc);
1158
1159 return acc;
1160 }
1161
1162 /*****************************************************************************/
1163 /* */
1164 /* Function name : mac16x16in32_shl */
1165 /* */
1166 /* Description : multiplies two 16 bit numbers and accumulates their */
1167 /* result in a 32 bit variable without sat */
1168 /* after removing a redundant sign bit in the product */
1169 /* */
1170 /* Inputs : WORD32 a, WORD16 b, WORD16 c */
1171 /* */
1172 /* Globals : none */
1173 /* */
1174 /* Processing : multiply, shift left by 1 & add */
1175 /* */
1176 /* Outputs : none */
1177 /* */
1178 /* Returns : WORD32 acc - 32 bit signed value */
1179 /* */
1180 /* Issues : none */
1181 /* */
1182 /* Revision history : */
1183 /* */
1184 /* DD MM YYYY author changes */
1185 /* 20 11 2003 aadithya kamath created */
1186 /* 15 11 2004 tejaswi/vishal modified(bug fixes/cleanup) */
1187 /* */
1188 /*****************************************************************************/
1189
mac16x16in32_shl(WORD32 a,WORD16 b,WORD16 c)1190 static PLATFORM_INLINE WORD32 mac16x16in32_shl(WORD32 a, WORD16 b, WORD16 c)
1191 {
1192 WORD32 acc;
1193
1194 acc = mult16x16in32_shl(b, c);
1195
1196 acc = add32(a, acc);
1197
1198 return acc;
1199 }
1200
1201 /*****************************************************************************/
1202 /* */
1203 /* Function name : mac16x16in32_shlsat */
1204 /* */
1205 /* Description : multiplies two 16 bit numbers and accumulates their */
1206 /* result in a 32 bit variable with sat */
1207 /* after removing a redundant sign bit in the product */
1208 /* */
1209 /* Inputs : WORD32 a, WORD16 b, WORD16 c */
1210 /* */
1211 /* Globals : none */
1212 /* */
1213 /* Processing : multiply, shift left by 1 & add with sat */
1214 /* */
1215 /* Outputs : none */
1216 /* */
1217 /* Returns : WORD32 acc - 32 bit signed value */
1218 /* */
1219 /* Issues : none */
1220 /* */
1221 /* Revision history : */
1222 /* */
1223 /* DD MM YYYY author changes */
1224 /* 20 11 2003 aadithya kamath created */
1225 /* 15 11 2004 tejaswi/vishal modified(bug fixes/cleanup) */
1226 /* */
1227 /*****************************************************************************/
1228
mac16x16in32_shl_sat(WORD32 a,WORD16 b,WORD16 c)1229 static PLATFORM_INLINE WORD32 mac16x16in32_shl_sat(WORD32 a, WORD16 b, WORD16 c)
1230 {
1231 WORD32 acc;
1232
1233 acc = mult16x16in32_shl_sat(b, c);
1234
1235 acc = add32_sat(a, acc);
1236
1237 return acc;
1238 }
1239
1240 /*****************************************************************************/
1241 /* */
1242 /* Function name : msu16x16in32 */
1243 /* */
1244 /* Description : multiplies two 16 bit numbers and substracts their */
1245 /* result from a 32 bit variable without sat */
1246 /* */
1247 /* Inputs : WORD32 a, WORD32 b, WORD16 c */
1248 /* */
1249 /* Globals : none */
1250 /* */
1251 /* Processing : multiply & sub */
1252 /* */
1253 /* Outputs : none */
1254 /* */
1255 /* Returns : WORD32 acc - 32 bit signed value */
1256 /* */
1257 /* Issues : none */
1258 /* */
1259 /* Revision history : */
1260 /* */
1261 /* DD MM YYYY author changes */
1262 /* 20 11 2003 aadithya kamath created */
1263 /* 15 11 2004 tejaswi/vishal modified(bug fixes/cleanup) */
1264 /* */
1265 /*****************************************************************************/
1266
msu16x16in32(WORD32 a,WORD16 b,WORD16 c)1267 static PLATFORM_INLINE WORD32 msu16x16in32(WORD32 a, WORD16 b, WORD16 c)
1268 {
1269 WORD32 acc;
1270
1271 acc = mult16x16in32(b, c);
1272
1273 acc = sub32(a, acc);
1274
1275 return acc;
1276 }
1277
1278 /*****************************************************************************/
1279 /* */
1280 /* Function name : msu16x16in32_shl */
1281 /* */
1282 /* Description : multiplies two 16 bit numbers and substracts their */
1283 /* result from a 32 bit variable without sat */
1284 /* after removing a redundant sign bit in the product */
1285 /* */
1286 /* Inputs : WORD32 a, WORD16 b, WORD16 c */
1287 /* */
1288 /* Globals : none */
1289 /* */
1290 /* Processing : multiply, shift left by 1 & sub */
1291 /* */
1292 /* Outputs : none */
1293 /* */
1294 /* Returns : WORD32 acc - 32 bit signed value */
1295 /* */
1296 /* Issues : none */
1297 /* */
1298 /* Revision history : */
1299 /* */
1300 /* DD MM YYYY author changes */
1301 /* 20 11 2003 aadithya kamath created */
1302 /* 15 11 2004 tejaswi/vishal modified(bug fixes/cleanup) */
1303 /* */
1304 /*****************************************************************************/
1305
msu16x16in32_shl(WORD32 a,WORD16 b,WORD16 c)1306 static PLATFORM_INLINE WORD32 msu16x16in32_shl(WORD32 a, WORD16 b, WORD16 c)
1307 {
1308 WORD32 acc;
1309
1310 acc = mult16x16in32_shl(b, c);
1311
1312 acc = sub32(a, acc);
1313
1314 return acc;
1315 }
1316
1317 /*****************************************************************************/
1318 /* */
1319 /* Function name : msu16x16in32_shlsat */
1320 /* */
1321 /* Description : multiplies two 16 bit numbers and substracts their */
1322 /* result from a 32 bit variable with sat */
1323 /* after removing a redundant sign bit in the product */
1324 /* */
1325 /* Inputs : WORD32 a, WORD16 b, WORD16 c */
1326 /* */
1327 /* Globals : none */
1328 /* */
1329 /* Processing : multiply, shift left by 1 & sub with sat */
1330 /* */
1331 /* Outputs : none */
1332 /* */
1333 /* Returns : WORD32 acc - 32 bit signed value */
1334 /* */
1335 /* Issues : none */
1336 /* */
1337 /* Revision history : */
1338 /* */
1339 /* DD MM YYYY author changes */
1340 /* 20 11 2003 aadithya kamath created */
1341 /* 15 11 2004 tejaswi/vishal modified(bug fixes/cleanup) */
1342 /* */
1343 /*****************************************************************************/
1344
msu16x16in32_shl_sat(WORD32 a,WORD16 b,WORD16 c)1345 static PLATFORM_INLINE WORD32 msu16x16in32_shl_sat(WORD32 a, WORD16 b, WORD16 c)
1346 {
1347 WORD32 acc;
1348
1349 acc = mult16x16in32_shl_sat(b, c);
1350
1351 acc = sub32_sat(a, acc);
1352
1353 return acc;
1354 }
1355
1356 /*****************************************************************************/
1357 /* */
1358 /* Function name : add32_shr */
1359 /* */
1360 /* Description : adding two 32 bit numbers and taking care of overflow */
1361 /* by downshifting both numbers before addition */
1362 /* */
1363 /* Inputs : WORD32 a, WORD16 b, WORD16 c */
1364 /* */
1365 /* Globals : none */
1366 /* */
1367 /* Processing : shift right inputs by 1 & add */
1368 /* */
1369 /* Outputs : none */
1370 /* */
1371 /* Returns : WORD32 sum 32 bit signed value */
1372 /* */
1373 /* Issues : none */
1374 /* */
1375 /* Revision history : */
1376 /* */
1377 /* DD MM YYYY author changes */
1378 /* 20 11 2003 aadithya kamath created */
1379 /* 15 11 2004 tejaswi/vishal modified(bug fixes/cleanup) */
1380 /* */
1381 /*****************************************************************************/
1382
add32_shr(WORD32 a,WORD32 b)1383 static PLATFORM_INLINE WORD32 add32_shr(WORD32 a, WORD32 b)
1384 {
1385 WORD32 sum;
1386
1387 a = shr32(a, 1);
1388 b = shr32(b, 1);
1389
1390 sum = add32(a, b);
1391
1392 return sum;
1393 }
1394
1395 /*****************************************************************************/
1396 /* */
1397 /* Function name : sub32_shr */
1398 /* */
1399 /* Description : substracting two 32 bit numbers and taking care of */
1400 /* overflow by downshifting both numbers before addition */
1401 /* */
1402 /* Inputs : WORD32 a, WORD16 b, WORD16 c */
1403 /* */
1404 /* Globals : none */
1405 /* */
1406 /* Processing : shift right inputs by 1 & sub */
1407 /* */
1408 /* Outputs : none */
1409 /* */
1410 /* Returns : WORD32 diff - 32 bit signed value */
1411 /* */
1412 /* Issues : none */
1413 /* */
1414 /* Revision history : */
1415 /* */
1416 /* DD MM YYYY author changes */
1417 /* 20 11 2003 aadithya kamath created */
1418 /* 15 11 2004 tejaswi/vishal modified(bug fixes/cleanup) */
1419 /* */
1420 /*****************************************************************************/
1421
sub32_shr(WORD32 a,WORD32 b)1422 static PLATFORM_INLINE WORD32 sub32_shr(WORD32 a, WORD32 b)
1423 {
1424 WORD32 diff;
1425
1426 a = shr32(a, 1);
1427 b = shr32(b, 1);
1428
1429 diff = sub32(a, b);
1430
1431 return diff;
1432 }
1433 #endif /* __IA_BASIC_OPS32_H__ */
1434