1/*
2 ** Copyright 2003-2010, VisualOn, Inc.
3 **
4 ** Licensed under the Apache License, Version 2.0 (the "License");
5 ** you may not use this file except in compliance with the License.
6 ** You may obtain a copy of the License at
7 **
8 **     http://www.apache.org/licenses/LICENSE-2.0
9 **
10 ** Unless required by applicable law or agreed to in writing, software
11 ** distributed under the License is distributed on an "AS IS" BASIS,
12 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 ** See the License for the specific language governing permissions and
14 ** limitations under the License.
15 */
16/*******************************************************************************
17	File:		basicop2.c
18
19	Content:	Basic arithmetic operators.
20
21*******************************************************************************/
22
23#include "typedef.h"
24#include "basic_op.h"
25
26
27/*___________________________________________________________________________
28 |                                                                           |
29 |   Functions                                                               |
30 |___________________________________________________________________________|
31*/
32
33/*___________________________________________________________________________
34 |                                                                           |
35 |   Function Name : saturate                                                |
36 |                                                                           |
37 |   Purpose :                                                               |
38 |                                                                           |
39 |    Limit the 32 bit input to the range of a 16 bit word.                  |
40 |                                                                           |
41 |   Inputs :                                                                |
42 |                                                                           |
43 |    L_var1                                                                 |
44 |             32 bit long signed integer (Word32) whose value falls in the  |
45 |             range : 0x8000 0000 <= L_var1 <= 0x7fff ffff.                 |
46 |                                                                           |
47 |   Outputs :                                                               |
48 |                                                                           |
49 |    none                                                                   |
50 |                                                                           |
51 |   Return Value :                                                          |
52 |                                                                           |
53 |    var_out                                                                |
54 |             16 bit short signed integer (Word16) whose value falls in the |
55 |             range : 0xffff 8000 <= var_out <= 0x0000 7fff.                |
56 |___________________________________________________________________________|
57*/
58
59#if (!SATRUATE_IS_INLINE)
60Word16 saturate(Word32 L_var1)
61{
62    Word16 var_out;
63
64    if (L_var1 > 0X00007fffL)
65    {
66        var_out = MAX_16;
67    }
68    else if (L_var1 < (Word32) 0xffff8000L)
69    {
70        var_out = MIN_16;
71    }
72    else
73    {
74        var_out = extract_l(L_var1);
75    }
76
77    return (var_out);
78}
79#endif
80
81/*___________________________________________________________________________
82 |                                                                           |
83 |   Function Name : add                                                     |
84 |                                                                           |
85 |   Purpose :                                                               |
86 |                                                                           |
87 |    Performs the addition (var1+var2) with overflow control and saturation;|
88 |    the 16 bit result is set at +32767 when overflow occurs or at -32768   |
89 |    when underflow occurs.                                                 |
90 |                                                                           |
91 |   Complexity weight : 1                                                   |
92 |                                                                           |
93 |   Inputs :                                                                |
94 |                                                                           |
95 |    var1                                                                   |
96 |             16 bit short signed integer (Word16) whose value falls in the |
97 |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
98 |                                                                           |
99 |    var2                                                                   |
100 |             16 bit short signed integer (Word16) whose value falls in the |
101 |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
102 |                                                                           |
103 |   Outputs :                                                               |
104 |                                                                           |
105 |    none                                                                   |
106 |                                                                           |
107 |   Return Value :                                                          |
108 |                                                                           |
109 |    var_out                                                                |
110 |             16 bit short signed integer (Word16) whose value falls in the |
111 |             range : 0xffff 8000 <= var_out <= 0x0000 7fff.                |
112 |___________________________________________________________________________|
113*/
114
115#if (!ADD_IS_INLINE)
116Word16 add (Word16 var1, Word16 var2)
117{
118    Word16 var_out;
119    Word32 L_sum;
120
121    L_sum = (Word32)var1 + (Word32)var2;
122    var_out = saturate(L_sum);
123
124    return (var_out);
125}
126#endif
127
128/*___________________________________________________________________________
129 |                                                                           |
130 |   Function Name : sub                                                     |
131 |                                                                           |
132 |   Purpose :                                                               |
133 |                                                                           |
134 |    Performs the subtraction (var1+var2) with overflow control and satu-   |
135 |    ration; the 16 bit result is set at +32767 when overflow occurs or at  |
136 |    -32768 when underflow occurs.                                          |
137 |                                                                           |
138 |   Complexity weight : 1                                                   |
139 |                                                                           |
140 |   Inputs :                                                                |
141 |                                                                           |
142 |    var1                                                                   |
143 |             16 bit short signed integer (Word16) whose value falls in the |
144 |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
145 |                                                                           |
146 |    var2                                                                   |
147 |             16 bit short signed integer (Word16) whose value falls in the |
148 |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
149 |                                                                           |
150 |   Outputs :                                                               |
151 |                                                                           |
152 |    none                                                                   |
153 |                                                                           |
154 |   Return Value :                                                          |
155 |                                                                           |
156 |    var_out                                                                |
157 |             16 bit short signed integer (Word16) whose value falls in the |
158 |             range : 0xffff 8000 <= var_out <= 0x0000 7fff.                |
159 |___________________________________________________________________________|
160*/
161#if (!SUB_IS_INLINE)
162Word16 sub(Word16 var1, Word16 var2)
163{
164    Word16 var_out;
165    Word32 L_diff;
166
167    L_diff = (Word32) var1 - var2;
168    var_out = saturate(L_diff);
169
170    return (var_out);
171}
172#endif
173
174/*___________________________________________________________________________
175 |                                                                           |
176 |   Function Name : abs_s                                                   |
177 |                                                                           |
178 |   Purpose :                                                               |
179 |                                                                           |
180 |    Absolute value of var1; abs_s(-32768) = 32767.                         |
181 |                                                                           |
182 |   Complexity weight : 1                                                   |
183 |                                                                           |
184 |   Inputs :                                                                |
185 |                                                                           |
186 |    var1                                                                   |
187 |             16 bit short signed integer (Word16) whose value falls in the |
188 |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
189 |                                                                           |
190 |   Outputs :                                                               |
191 |                                                                           |
192 |    none                                                                   |
193 |                                                                           |
194 |   Return Value :                                                          |
195 |                                                                           |
196 |    var_out                                                                |
197 |             16 bit short signed integer (Word16) whose value falls in the |
198 |             range : 0x0000 0000 <= var_out <= 0x0000 7fff.                |
199 |___________________________________________________________________________|
200*/
201//Word16 abs_s (Word16 var1)
202//{
203//    Word16 var_out;
204//
205//    if (var1 == MIN_16)
206//    {
207//        var_out = MAX_16;
208//    }
209//    else
210//    {
211//        if (var1 < 0)
212//        {
213//            var_out = (Word16)-var1;
214//        }
215//        else
216//        {
217//            var_out = var1;
218//        }
219//    }
220//
221//    return (var_out);
222//}
223
224
225/*___________________________________________________________________________
226 |                                                                           |
227 |   Function Name : shl                                                     |
228 |                                                                           |
229 |   Purpose :                                                               |
230 |                                                                           |
231 |   Arithmetically shift the 16 bit input var1 left var2 positions.Zero fill|
232 |   the var2 LSB of the result. If var2 is negative, arithmetically shift   |
233 |   var1 right by -var2 with sign extension. Saturate the result in case of |
234 |   underflows or overflows.                                                |
235 |                                                                           |
236 |   Complexity weight : 1                                                   |
237 |                                                                           |
238 |   Inputs :                                                                |
239 |                                                                           |
240 |    var1                                                                   |
241 |             16 bit short signed integer (Word16) whose value falls in the |
242 |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
243 |                                                                           |
244 |    var2                                                                   |
245 |             16 bit short signed integer (Word16) whose value falls in the |
246 |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
247 |                                                                           |
248 |   Outputs :                                                               |
249 |                                                                           |
250 |    none                                                                   |
251 |                                                                           |
252 |   Return Value :                                                          |
253 |                                                                           |
254 |    var_out                                                                |
255 |             16 bit short signed integer (Word16) whose value falls in the |
256 |             range : 0xffff 8000 <= var_out <= 0x0000 7fff.                |
257 |___________________________________________________________________________|
258*/
259
260#if (!SHL_IS_INLINE)
261Word16 shl (Word16 var1, Word16 var2)
262{
263    Word16 var_out;
264    Word32 result;
265
266    if (var2 < 0)
267    {
268        if (var2 < -16)
269            var2 = -16;
270        var_out = shr (var1, (Word16)-var2);
271    }
272    else
273    {
274        result = (Word32) var1 *((Word32) 1 << var2);
275
276        if ((var2 > 15 && var1 != 0) || (result != (Word32) ((Word16) result)))
277        {
278            //Overflow = 1;
279            var_out = (Word16)((var1 > 0) ? MAX_16 : MIN_16);
280        }
281        else
282        {
283            var_out = extract_l(result);
284        }
285    }
286
287    return (var_out);
288}
289#endif
290// end
291
292/*___________________________________________________________________________
293 |                                                                           |
294 |   Function Name : shr                                                     |
295 |                                                                           |
296 |   Purpose :                                                               |
297 |                                                                           |
298 |   Arithmetically shift the 16 bit input var1 right var2 positions with    |
299 |   sign extension. If var2 is negative, arithmetically shift var1 left by  |
300 |   -var2 with sign extension. Saturate the result in case of underflows or |
301 |   overflows.                                                              |
302 |                                                                           |
303 |   Complexity weight : 1                                                   |
304 |                                                                           |
305 |   Inputs :                                                                |
306 |                                                                           |
307 |    var1                                                                   |
308 |             16 bit short signed integer (Word16) whose value falls in the |
309 |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
310 |                                                                           |
311 |    var2                                                                   |
312 |             16 bit short signed integer (Word16) whose value falls in the |
313 |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
314 |                                                                           |
315 |   Outputs :                                                               |
316 |                                                                           |
317 |    none                                                                   |
318 |                                                                           |
319 |   Return Value :                                                          |
320 |                                                                           |
321 |    var_out                                                                |
322 |             16 bit short signed integer (Word16) whose value falls in the |
323 |             range : 0xffff 8000 <= var_out <= 0x0000 7fff.                |
324 |___________________________________________________________________________|
325*/
326
327#if (!SHR_IS_INLINE)
328Word16 shr (Word16 var1, Word16 var2)
329{
330    Word16 var_out;
331
332    if (var2 < 0)
333    {
334        if (var2 < -16)
335            var2 = -16;
336        var_out = shl (var1, (Word16)-var2);
337    }
338    else
339    {
340        if (var2 >= 15)
341        {
342            var_out = (Word16)((var1 < 0) ? -1 : 0);
343        }
344        else
345        {
346            if (var1 < 0)
347            {
348                var_out = (Word16)(~((~var1) >> var2));
349            }
350            else
351            {
352                var_out = (Word16)(var1 >> var2);
353            }
354        }
355    }
356
357    return (var_out);
358}
359#endif
360
361
362/*___________________________________________________________________________
363 |                                                                           |
364 |   Function Name : mult                                                    |
365 |                                                                           |
366 |   Purpose :                                                               |
367 |                                                                           |
368 |    Performs the multiplication of var1 by var2 and gives a 16 bit result  |
369 |    which is scaled i.e.:                                                  |
370 |             mult(var1,var2) = extract_l(L_shr((var1 times var2),15)) and  |
371 |             mult(-32768,-32768) = 32767.                                  |
372 |                                                                           |
373 |   Complexity weight : 1                                                   |
374 |                                                                           |
375 |   Inputs :                                                                |
376 |                                                                           |
377 |    var1                                                                   |
378 |             16 bit short signed integer (Word16) whose value falls in the |
379 |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
380 |                                                                           |
381 |    var2                                                                   |
382 |             16 bit short signed integer (Word16) whose value falls in the |
383 |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
384 |                                                                           |
385 |   Outputs :                                                               |
386 |                                                                           |
387 |    none                                                                   |
388 |                                                                           |
389 |   Return Value :                                                          |
390 |                                                                           |
391 |    var_out                                                                |
392 |             16 bit short signed integer (Word16) whose value falls in the |
393 |             range : 0xffff 8000 <= var_out <= 0x0000 7fff.                |
394 |___________________________________________________________________________|
395*/
396#if (!MULT_IS_INLINE)
397Word16 mult (Word16 var1, Word16 var2)
398{
399    Word16 var_out;
400    Word32 L_product;
401
402    L_product = (Word32) var1 *(Word32) var2;
403
404    L_product = (L_product & (Word32) 0xffff8000L) >> 15;
405
406    if (L_product & (Word32) 0x00010000L)
407        L_product = L_product | (Word32) 0xffff0000L;
408
409    var_out = saturate(L_product);
410
411    return (var_out);
412}
413#endif
414
415/*___________________________________________________________________________
416 |                                                                           |
417 |   Function Name : L_mult                                                  |
418 |                                                                           |
419 |   Purpose :                                                               |
420 |                                                                           |
421 |   L_mult is the 32 bit result of the multiplication of var1 times var2    |
422 |   with one shift left i.e.:                                               |
423 |        L_mult(var1,var2) = L_shl((var1 times var2),1) and                 |
424 |        L_mult(-32768,-32768) = 2147483647.                                |
425 |                                                                           |
426 |   Complexity weight : 1                                                   |
427 |                                                                           |
428 |   Inputs :                                                                |
429 |                                                                           |
430 |    var1                                                                   |
431 |             16 bit short signed integer (Word16) whose value falls in the |
432 |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
433 |                                                                           |
434 |    var2                                                                   |
435 |             16 bit short signed integer (Word16) whose value falls in the |
436 |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
437 |                                                                           |
438 |   Outputs :                                                               |
439 |                                                                           |
440 |    none                                                                   |
441 |                                                                           |
442 |   Return Value :                                                          |
443 |                                                                           |
444 |    L_var_out                                                              |
445 |             32 bit long signed integer (Word32) whose value falls in the  |
446 |             range : 0x8000 0000 <= L_var_out <= 0x7fff ffff.              |
447 |___________________________________________________________________________|
448*/
449
450#if (!L_MULT_IS_INLINE)
451Word32 L_mult(Word16 var1, Word16 var2)
452{
453    Word32 L_var_out;
454
455    L_var_out = (Word32) var1 *(Word32) var2;
456
457    if (L_var_out != (Word32) 0x40000000L)
458    {
459        L_var_out <<= 1;
460    }
461    else
462    {
463        L_var_out = MAX_32;
464    }
465
466    return (L_var_out);
467}
468#endif
469// end
470
471/*___________________________________________________________________________
472 |                                                                           |
473 |   Function Name : negate                                                  |
474 |                                                                           |
475 |   Purpose :                                                               |
476 |                                                                           |
477 |   Negate var1 with saturation, saturate in the case where input is -32768:|
478 |                negate(var1) = sub(0,var1).                                |
479 |                                                                           |
480 |   Complexity weight : 1                                                   |
481 |                                                                           |
482 |   Inputs :                                                                |
483 |                                                                           |
484 |    var1                                                                   |
485 |             16 bit short signed integer (Word16) whose value falls in the |
486 |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
487 |                                                                           |
488 |   Outputs :                                                               |
489 |                                                                           |
490 |    none                                                                   |
491 |                                                                           |
492 |   Return Value :                                                          |
493 |                                                                           |
494 |    var_out                                                                |
495 |             16 bit short signed integer (Word16) whose value falls in the |
496 |             range : 0xffff 8000 <= var_out <= 0x0000 7fff.                |
497 |___________________________________________________________________________|
498*/
499//Word16 negate (Word16 var1)
500//{
501//    Word16 var_out;
502//
503//    var_out = (Word16)((var1 == MIN_16) ? MAX_16 : -var1);
504//
505//    return (var_out);
506//}
507
508
509/*___________________________________________________________________________
510 |                                                                           |
511 |   Function Name : extract_h                                               |
512 |                                                                           |
513 |   Purpose :                                                               |
514 |                                                                           |
515 |   Return the 16 MSB of L_var1.                                            |
516 |                                                                           |
517 |   Complexity weight : 1                                                   |
518 |                                                                           |
519 |   Inputs :                                                                |
520 |                                                                           |
521 |    L_var1                                                                 |
522 |             32 bit long signed integer (Word32 ) whose value falls in the |
523 |             range : 0x8000 0000 <= L_var1 <= 0x7fff ffff.                 |
524 |                                                                           |
525 |   Outputs :                                                               |
526 |                                                                           |
527 |    none                                                                   |
528 |                                                                           |
529 |   Return Value :                                                          |
530 |                                                                           |
531 |    var_out                                                                |
532 |             16 bit short signed integer (Word16) whose value falls in the |
533 |             range : 0xffff 8000 <= var_out <= 0x0000 7fff.                |
534 |___________________________________________________________________________|
535*/
536#if (!EXTRACT_H_IS_INLINE)
537Word16 extract_h (Word32 L_var1)
538{
539    Word16 var_out;
540
541    var_out = (Word16) (L_var1 >> 16);
542
543    return (var_out);
544}
545#endif
546
547/*___________________________________________________________________________
548 |                                                                           |
549 |   Function Name : extract_l                                               |
550 |                                                                           |
551 |   Purpose :                                                               |
552 |                                                                           |
553 |   Return the 16 LSB of L_var1.                                            |
554 |                                                                           |
555 |   Complexity weight : 1                                                   |
556 |                                                                           |
557 |   Inputs :                                                                |
558 |                                                                           |
559 |    L_var1                                                                 |
560 |             32 bit long signed integer (Word32 ) whose value falls in the |
561 |             range : 0x8000 0000 <= L_var1 <= 0x7fff ffff.                 |
562 |                                                                           |
563 |   Outputs :                                                               |
564 |                                                                           |
565 |    none                                                                   |
566 |                                                                           |
567 |   Return Value :                                                          |
568 |                                                                           |
569 |    var_out                                                                |
570 |             16 bit short signed integer (Word16) whose value falls in the |
571 |             range : 0xffff 8000 <= var_out <= 0x0000 7fff.                |
572 |___________________________________________________________________________|
573*/
574#if (!EXTRACT_L_IS_INLINE)
575Word16 extract_l(Word32 L_var1)
576{
577    Word16 var_out;
578
579    var_out = (Word16) L_var1;
580
581    return (var_out);
582}
583#endif
584
585/*___________________________________________________________________________
586 |                                                                           |
587 |   Function Name : round                                                   |
588 |                                                                           |
589 |   Purpose :                                                               |
590 |                                                                           |
591 |   Round the lower 16 bits of the 32 bit input number into the MS 16 bits  |
592 |   with saturation. Shift the resulting bits right by 16 and return the 16 |
593 |   bit number:                                                             |
594 |               round(L_var1) = extract_h(L_add(L_var1,32768))              |
595 |                                                                           |
596 |   Complexity weight : 1                                                   |
597 |                                                                           |
598 |   Inputs :                                                                |
599 |                                                                           |
600 |    L_var1                                                                 |
601 |             32 bit long signed integer (Word32 ) whose value falls in the |
602 |             range : 0x8000 0000 <= L_var1 <= 0x7fff ffff.                 |
603 |                                                                           |
604 |   Outputs :                                                               |
605 |                                                                           |
606 |    none                                                                   |
607 |                                                                           |
608 |   Return Value :                                                          |
609 |                                                                           |
610 |    var_out                                                                |
611 |             16 bit short signed integer (Word16) whose value falls in the |
612 |             range : 0xffff 8000 <= var_out <= 0x0000 7fff.                |
613 |___________________________________________________________________________|
614*/
615
616#if (!ROUND_IS_INLINE)
617Word16 round16(Word32 L_var1)
618{
619    Word16 var_out;
620    Word32 L_rounded;
621
622    L_rounded = L_add (L_var1, (Word32) 0x00008000L);
623    var_out = extract_h (L_rounded);
624
625    return (var_out);
626}
627#endif
628// end
629
630/*___________________________________________________________________________
631 |                                                                           |
632 |   Function Name : L_mac                                                   |
633 |                                                                           |
634 |   Purpose :                                                               |
635 |                                                                           |
636 |   Multiply var1 by var2 and shift the result left by 1. Add the 32 bit    |
637 |   result to L_var3 with saturation, return a 32 bit result:               |
638 |        L_mac(L_var3,var1,var2) = L_add(L_var3,L_mult(var1,var2)).         |
639 |                                                                           |
640 |   Complexity weight : 1                                                   |
641 |                                                                           |
642 |   Inputs :                                                                |
643 |                                                                           |
644 |    L_var3   32 bit long signed integer (Word32) whose value falls in the  |
645 |             range : 0x8000 0000 <= L_var3 <= 0x7fff ffff.                 |
646 |                                                                           |
647 |    var1                                                                   |
648 |             16 bit short signed integer (Word16) whose value falls in the |
649 |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
650 |                                                                           |
651 |    var2                                                                   |
652 |             16 bit short signed integer (Word16) whose value falls in the |
653 |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
654 |                                                                           |
655 |   Outputs :                                                               |
656 |                                                                           |
657 |    none                                                                   |
658 |                                                                           |
659 |   Return Value :                                                          |
660 |                                                                           |
661 |    L_var_out                                                              |
662 |             32 bit long signed integer (Word32) whose value falls in the  |
663 |             range : 0x8000 0000 <= L_var_out <= 0x7fff ffff.              |
664 |___________________________________________________________________________|
665*/
666#if (!L_MSU_IS_INLINE)
667Word32 L_mac (Word32 L_var3, Word16 var1, Word16 var2)
668{
669    Word32 L_var_out;
670    Word32 L_product;
671
672    L_product = L_mult(var1, var2);
673    L_var_out = L_add (L_var3, L_product);
674
675    return (L_var_out);
676}
677#endif
678
679/*___________________________________________________________________________
680 |                                                                           |
681 |   Function Name : L_msu                                                   |
682 |                                                                           |
683 |   Purpose :                                                               |
684 |                                                                           |
685 |   Multiply var1 by var2 and shift the result left by 1. Subtract the 32   |
686 |   bit result to L_var3 with saturation, return a 32 bit result:           |
687 |        L_msu(L_var3,var1,var2) = L_sub(L_var3,L_mult(var1,var2)).         |
688 |                                                                           |
689 |   Complexity weight : 1                                                   |
690 |                                                                           |
691 |   Inputs :                                                                |
692 |                                                                           |
693 |    L_var3   32 bit long signed integer (Word32) whose value falls in the  |
694 |             range : 0x8000 0000 <= L_var3 <= 0x7fff ffff.                 |
695 |                                                                           |
696 |    var1                                                                   |
697 |             16 bit short signed integer (Word16) whose value falls in the |
698 |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
699 |                                                                           |
700 |    var2                                                                   |
701 |             16 bit short signed integer (Word16) whose value falls in the |
702 |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
703 |                                                                           |
704 |   Outputs :                                                               |
705 |                                                                           |
706 |    none                                                                   |
707 |                                                                           |
708 |   Return Value :                                                          |
709 |                                                                           |
710 |    L_var_out                                                              |
711 |             32 bit long signed integer (Word32) whose value falls in the  |
712 |             range : 0x8000 0000 <= L_var_out <= 0x7fff ffff.              |
713 |___________________________________________________________________________|
714*/
715
716#if (!L_MSU_IS_INLINE)
717Word32 L_msu (Word32 L_var3, Word16 var1, Word16 var2)
718{
719    Word32 L_var_out;
720    Word32 L_product;
721
722    L_product = L_mult(var1, var2);
723    L_var_out = L_sub (L_var3, L_product);
724
725    return (L_var_out);
726}
727#endif
728
729
730/*___________________________________________________________________________
731 |                                                                           |
732 |   Function Name : L_add                                                   |
733 |                                                                           |
734 |   Purpose :                                                               |
735 |                                                                           |
736 |   32 bits addition of the two 32 bits variables (L_var1+L_var2) with      |
737 |   overflow control and saturation; the result is set at +2147483647 when  |
738 |   overflow occurs or at -2147483648 when underflow occurs.                |
739 |                                                                           |
740 |   Complexity weight : 2                                                   |
741 |                                                                           |
742 |   Inputs :                                                                |
743 |                                                                           |
744 |    L_var1   32 bit long signed integer (Word32) whose value falls in the  |
745 |             range : 0x8000 0000 <= L_var3 <= 0x7fff ffff.                 |
746 |                                                                           |
747 |    L_var2   32 bit long signed integer (Word32) whose value falls in the  |
748 |             range : 0x8000 0000 <= L_var3 <= 0x7fff ffff.                 |
749 |                                                                           |
750 |   Outputs :                                                               |
751 |                                                                           |
752 |    none                                                                   |
753 |                                                                           |
754 |   Return Value :                                                          |
755 |                                                                           |
756 |    L_var_out                                                              |
757 |             32 bit long signed integer (Word32) whose value falls in the  |
758 |             range : 0x8000 0000 <= L_var_out <= 0x7fff ffff.              |
759 |___________________________________________________________________________|
760*/
761#if (!L_ADD_IS_INLINE)
762Word32 L_add (Word32 L_var1, Word32 L_var2)
763{
764    Word32 L_var_out;
765
766    L_var_out = L_var1 + L_var2;
767
768    if (((L_var1 ^ L_var2) & MIN_32) == 0)
769    {
770        if ((L_var_out ^ L_var1) & MIN_32)
771        {
772            L_var_out = (L_var1 < 0) ? MIN_32 : MAX_32;
773            //Overflow = 1;
774        }
775    }
776
777    return (L_var_out);
778}
779#endif
780
781/*___________________________________________________________________________
782 |                                                                           |
783 |   Function Name : L_sub                                                   |
784 |                                                                           |
785 |   Purpose :                                                               |
786 |                                                                           |
787 |   32 bits subtraction of the two 32 bits variables (L_var1-L_var2) with   |
788 |   overflow control and saturation; the result is set at +2147483647 when  |
789 |   overflow occurs or at -2147483648 when underflow occurs.                |
790 |                                                                           |
791 |   Complexity weight : 2                                                   |
792 |                                                                           |
793 |   Inputs :                                                                |
794 |                                                                           |
795 |    L_var1   32 bit long signed integer (Word32) whose value falls in the  |
796 |             range : 0x8000 0000 <= L_var3 <= 0x7fff ffff.                 |
797 |                                                                           |
798 |    L_var2   32 bit long signed integer (Word32) whose value falls in the  |
799 |             range : 0x8000 0000 <= L_var3 <= 0x7fff ffff.                 |
800 |                                                                           |
801 |   Outputs :                                                               |
802 |                                                                           |
803 |    none                                                                   |
804 |                                                                           |
805 |   Return Value :                                                          |
806 |                                                                           |
807 |    L_var_out                                                              |
808 |             32 bit long signed integer (Word32) whose value falls in the  |
809 |             range : 0x8000 0000 <= L_var_out <= 0x7fff ffff.              |
810 |___________________________________________________________________________|
811*/
812#if (!L_SUB_IS_INLINE)
813Word32 L_sub(Word32 L_var1, Word32 L_var2)
814{
815    Word32 L_var_out;
816
817    L_var_out = L_var1 - L_var2;
818
819    if (((L_var1 ^ L_var2) & MIN_32) != 0)
820    {
821        if ((L_var_out ^ L_var1) & MIN_32)
822        {
823            L_var_out = (L_var1 < 0L) ? MIN_32 : MAX_32;
824            //Overflow = 1;
825        }
826    }
827
828    return (L_var_out);
829}
830#endif
831
832
833/*___________________________________________________________________________
834 |                                                                           |
835 |   Function Name : L_negate                                                |
836 |                                                                           |
837 |   Purpose :                                                               |
838 |                                                                           |
839 |   Negate the 32 bit variable L_var1 with saturation; saturate in the case |
840 |   where input is -2147483648 (0x8000 0000).                               |
841 |                                                                           |
842 |   Complexity weight : 2                                                   |
843 |                                                                           |
844 |   Inputs :                                                                |
845 |                                                                           |
846 |    L_var1   32 bit long signed integer (Word32) whose value falls in the  |
847 |             range : 0x8000 0000 <= L_var3 <= 0x7fff ffff.                 |
848 |                                                                           |
849 |   Outputs :                                                               |
850 |                                                                           |
851 |    none                                                                   |
852 |                                                                           |
853 |   Return Value :                                                          |
854 |                                                                           |
855 |    L_var_out                                                              |
856 |             32 bit long signed integer (Word32) whose value falls in the  |
857 |             range : 0x8000 0000 <= L_var_out <= 0x7fff ffff.              |
858 |___________________________________________________________________________|
859*/
860//Word32 L_negate (Word32 L_var1)
861//{
862//    Word32 L_var_out;
863//
864//    L_var_out = (L_var1 == MIN_32) ? MAX_32 : -L_var1;
865//
866//    return (L_var_out);
867//}
868
869
870/*___________________________________________________________________________
871 |                                                                           |
872 |   Function Name : mult_r                                                  |
873 |                                                                           |
874 |   Purpose :                                                               |
875 |                                                                           |
876 |   Same as mult with rounding, i.e.:                                       |
877 |     mult_r(var1,var2) = extract_l(L_shr(((var1 * var2) + 16384),15)) and  |
878 |     mult_r(-32768,-32768) = 32767.                                        |
879 |                                                                           |
880 |   Complexity weight : 2                                                   |
881 |                                                                           |
882 |   Inputs :                                                                |
883 |                                                                           |
884 |    var1                                                                   |
885 |             16 bit short signed integer (Word16) whose value falls in the |
886 |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
887 |                                                                           |
888 |    var2                                                                   |
889 |             16 bit short signed integer (Word16) whose value falls in the |
890 |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
891 |                                                                           |
892 |   Outputs :                                                               |
893 |                                                                           |
894 |    none                                                                   |
895 |                                                                           |
896 |   Return Value :                                                          |
897 |                                                                           |
898 |    var_out                                                                |
899 |             16 bit short signed integer (Word16) whose value falls in the |
900 |             range : 0xffff 8000 <= var_out <= 0x0000 7fff.                |
901 |___________________________________________________________________________|
902*/
903#if (!MULT_R_IS_INLINE)
904Word16 mult_r (Word16 var1, Word16 var2)
905{
906    Word16 var_out;
907    Word32 L_product_arr;
908
909    L_product_arr = (Word32) var1 *(Word32) var2;       /* product */
910    L_product_arr += (Word32) 0x00004000L;      /* round */
911    L_product_arr &= (Word32) 0xffff8000L;
912    L_product_arr >>= 15;       /* shift */
913
914    if (L_product_arr & (Word32) 0x00010000L)   /* sign extend when necessary */
915    {
916        L_product_arr |= (Word32) 0xffff0000L;
917    }
918    var_out = saturate(L_product_arr);
919
920    return (var_out);
921}
922#endif
923
924/*___________________________________________________________________________
925 |                                                                           |
926 |   Function Name : L_shl                                                   |
927 |                                                                           |
928 |   Purpose :                                                               |
929 |                                                                           |
930 |   Arithmetically shift the 32 bit input L_var1 left var2 positions. Zero  |
931 |   fill the var2 LSB of the result. If var2 is negative, arithmetically    |
932 |   shift L_var1 right by -var2 with sign extension. Saturate the result in |
933 |   case of underflows or overflows.                                        |
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 |    var2                                                                   |
943 |             16 bit short signed integer (Word16) whose value falls in the |
944 |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
945 |                                                                           |
946 |   Outputs :                                                               |
947 |                                                                           |
948 |    none                                                                   |
949 |                                                                           |
950 |   Return Value :                                                          |
951 |                                                                           |
952 |    L_var_out                                                              |
953 |             32 bit long signed integer (Word32) whose value falls in the  |
954 |             range : 0x8000 0000 <= L_var_out <= 0x7fff ffff.              |
955 |___________________________________________________________________________|
956*/
957
958#if (!L_SHL_IS_INLINE)
959Word32 L_shl (Word32 L_var1, Word16 var2)
960{
961    Word32 L_var_out = 0L;
962
963    if (var2 <= 0)
964    {
965        L_var1 = L_shr(L_var1, (Word16)-var2);
966    }
967    else
968    {
969        for (; var2 > 0; var2--)
970        {
971            if (L_var1 > (Word32) 0X3fffffffL)
972            {
973                return MAX_32;
974            }
975            else
976            {
977                if (L_var1 < (Word32) 0xc0000000L)
978                {
979                    return MIN_32;
980                }
981            }
982            L_var1 <<= 1;
983        }
984    }
985    return (L_var1);
986}
987#endif
988
989/*___________________________________________________________________________
990 |                                                                           |
991 |   Function Name : L_shr                                                   |
992 |                                                                           |
993 |   Purpose :                                                               |
994 |                                                                           |
995 |   Arithmetically shift the 32 bit input L_var1 right var2 positions with  |
996 |   sign extension. If var2 is negative, arithmetically shift L_var1 left   |
997 |   by -var2 and zero fill the -var2 LSB of the result. Saturate the result |
998 |   in case of underflows or overflows.                                     |
999 |                                                                           |
1000 |   Complexity weight : 2                                                   |
1001 |                                                                           |
1002 |   Inputs :                                                                |
1003 |                                                                           |
1004 |    L_var1   32 bit long signed integer (Word32) whose value falls in the  |
1005 |             range : 0x8000 0000 <= L_var3 <= 0x7fff ffff.                 |
1006 |                                                                           |
1007 |    var2                                                                   |
1008 |             16 bit short signed integer (Word16) whose value falls in the |
1009 |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
1010 |                                                                           |
1011 |   Outputs :                                                               |
1012 |                                                                           |
1013 |    none                                                                   |
1014 |                                                                           |
1015 |   Return Value :                                                          |
1016 |                                                                           |
1017 |    L_var_out                                                              |
1018 |             32 bit long signed integer (Word32) whose value falls in the  |
1019 |             range : 0x8000 0000 <= L_var_out <= 0x7fff ffff.              |
1020 |___________________________________________________________________________|
1021*/
1022
1023#if (!L_SHR_IS_INLINE)
1024Word32 L_shr (Word32 L_var1, Word16 var2)
1025{
1026    Word32 L_var_out;
1027
1028    if (var2 < 0)
1029    {
1030        L_var_out = L_shl (L_var1, (Word16)-var2);
1031    }
1032    else
1033    {
1034        if (var2 >= 31)
1035        {
1036            L_var_out = (L_var1 < 0L) ? -1 : 0;
1037        }
1038        else
1039        {
1040            if (L_var1 < 0)
1041            {
1042                L_var_out = ~((~L_var1) >> var2);
1043            }
1044            else
1045            {
1046                L_var_out = L_var1 >> var2;
1047            }
1048        }
1049    }
1050    return (L_var_out);
1051}
1052#endif
1053
1054/*___________________________________________________________________________
1055 |                                                                           |
1056 |   Function Name : shr_r                                                   |
1057 |                                                                           |
1058 |   Purpose :                                                               |
1059 |                                                                           |
1060 |   Same as shr(var1,var2) but with rounding. Saturate the result in case of|
1061 |   underflows or overflows :                                               |
1062 |    - If var2 is greater than zero :                                       |
1063 |          if (sub(shl(shr(var1,var2),1),shr(var1,sub(var2,1))))            |
1064 |          is equal to zero                                                 |
1065 |                     then                                                  |
1066 |                     shr_r(var1,var2) = shr(var1,var2)                     |
1067 |                     else                                                  |
1068 |                     shr_r(var1,var2) = add(shr(var1,var2),1)              |
1069 |    - If var2 is less than or equal to zero :                              |
1070 |                     shr_r(var1,var2) = shr(var1,var2).                    |
1071 |                                                                           |
1072 |   Complexity weight : 2                                                   |
1073 |                                                                           |
1074 |   Inputs :                                                                |
1075 |                                                                           |
1076 |    var1                                                                   |
1077 |             16 bit short signed integer (Word16) whose value falls in the |
1078 |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
1079 |                                                                           |
1080 |    var2                                                                   |
1081 |             16 bit short signed integer (Word16) whose value falls in the |
1082 |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
1083 |                                                                           |
1084 |   Outputs :                                                               |
1085 |                                                                           |
1086 |    none                                                                   |
1087 |                                                                           |
1088 |   Return Value :                                                          |
1089 |                                                                           |
1090 |    var_out                                                                |
1091 |             16 bit short signed integer (Word16) whose value falls in the |
1092 |             range : 0xffff 8000 <= var_out <= 0x0000 7fff.                |
1093 |___________________________________________________________________________|
1094*/
1095#if (!SHR_R_IS_INLINE)
1096Word16 shr_r (Word16 var1, Word16 var2)
1097{
1098    Word16 var_out;
1099
1100    if (var2 > 15)
1101    {
1102        var_out = 0;
1103    }
1104    else
1105    {
1106        var_out = shr (var1, var2);
1107
1108        if (var2 > 0)
1109        {
1110            if ((var1 & ((Word16) 1 << (var2 - 1))) != 0)
1111            {
1112                var_out++;
1113            }
1114        }
1115    }
1116
1117    return (var_out);
1118}
1119#endif
1120
1121/*___________________________________________________________________________
1122 |                                                                           |
1123 |   Function Name : mac_r                                                   |
1124 |                                                                           |
1125 |   Purpose :                                                               |
1126 |                                                                           |
1127 |   Multiply var1 by var2 and shift the result left by 1. Add the 32 bit    |
1128 |   result to L_var3 with saturation. Round the LS 16 bits of the result    |
1129 |   into the MS 16 bits with saturation and shift the result right by 16.   |
1130 |   Return a 16 bit result.                                                 |
1131 |            mac_r(L_var3,var1,var2) = round(L_mac(L_var3,var1,var2))       |
1132 |                                                                           |
1133 |   Complexity weight : 2                                                   |
1134 |                                                                           |
1135 |   Inputs :                                                                |
1136 |                                                                           |
1137 |    L_var3   32 bit long signed integer (Word32) whose value falls in the  |
1138 |             range : 0x8000 0000 <= L_var3 <= 0x7fff ffff.                 |
1139 |                                                                           |
1140 |    var1                                                                   |
1141 |             16 bit short signed integer (Word16) whose value falls in the |
1142 |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
1143 |                                                                           |
1144 |    var2                                                                   |
1145 |             16 bit short signed integer (Word16) whose value falls in the |
1146 |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
1147 |                                                                           |
1148 |   Outputs :                                                               |
1149 |                                                                           |
1150 |    none                                                                   |
1151 |                                                                           |
1152 |   Return Value :                                                          |
1153 |                                                                           |
1154 |    var_out                                                                |
1155 |             16 bit short signed integer (Word16) whose value falls in the |
1156 |             range : 0x0000 8000 <= L_var_out <= 0x0000 7fff.              |
1157 |___________________________________________________________________________|
1158*/
1159#if (!MAC_R_IS_INLINE)
1160Word16 mac_r (Word32 L_var3, Word16 var1, Word16 var2)
1161{
1162    Word16 var_out;
1163
1164    L_var3 = L_mac (L_var3, var1, var2);
1165    L_var3 = L_add (L_var3, (Word32) 0x00008000L);
1166    var_out = extract_h (L_var3);
1167
1168    return (var_out);
1169}
1170#endif
1171
1172/*___________________________________________________________________________
1173 |                                                                           |
1174 |   Function Name : msu_r                                                   |
1175 |                                                                           |
1176 |   Purpose :                                                               |
1177 |                                                                           |
1178 |   Multiply var1 by var2 and shift the result left by 1. Subtract the 32   |
1179 |   bit result to L_var3 with saturation. Round the LS 16 bits of the res-  |
1180 |   ult into the MS 16 bits with saturation and shift the result right by   |
1181 |   16. Return a 16 bit result.                                             |
1182 |            msu_r(L_var3,var1,var2) = round(L_msu(L_var3,var1,var2))       |
1183 |                                                                           |
1184 |   Complexity weight : 2                                                   |
1185 |                                                                           |
1186 |   Inputs :                                                                |
1187 |                                                                           |
1188 |    L_var3   32 bit long signed integer (Word32) whose value falls in the  |
1189 |             range : 0x8000 0000 <= L_var3 <= 0x7fff ffff.                 |
1190 |                                                                           |
1191 |    var1                                                                   |
1192 |             16 bit short signed integer (Word16) whose value falls in the |
1193 |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
1194 |                                                                           |
1195 |    var2                                                                   |
1196 |             16 bit short signed integer (Word16) whose value falls in the |
1197 |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
1198 |                                                                           |
1199 |   Outputs :                                                               |
1200 |                                                                           |
1201 |    none                                                                   |
1202 |                                                                           |
1203 |   Return Value :                                                          |
1204 |                                                                           |
1205 |    var_out                                                                |
1206 |             16 bit short signed integer (Word16) whose value falls in the |
1207 |             range : 0x0000 8000 <= L_var_out <= 0x0000 7fff.              |
1208 |___________________________________________________________________________|
1209*/
1210#if (!MSU_R_IS_INLINE)
1211Word16 msu_r (Word32 L_var3, Word16 var1, Word16 var2)
1212{
1213    Word16 var_out;
1214
1215    L_var3 = L_msu (L_var3, var1, var2);
1216    L_var3 = L_add (L_var3, (Word32) 0x00008000L);
1217    var_out = extract_h (L_var3);
1218
1219    return (var_out);
1220}
1221#endif
1222
1223/*___________________________________________________________________________
1224 |                                                                           |
1225 |   Function Name : L_deposit_h                                             |
1226 |                                                                           |
1227 |   Purpose :                                                               |
1228 |                                                                           |
1229 |   Deposit the 16 bit var1 into the 16 MS bits of the 32 bit output. The   |
1230 |   16 LS bits of the output are zeroed.                                    |
1231 |                                                                           |
1232 |   Complexity weight : 2                                                   |
1233 |                                                                           |
1234 |   Inputs :                                                                |
1235 |                                                                           |
1236 |    var1                                                                   |
1237 |             16 bit short signed integer (Word16) whose value falls in the |
1238 |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
1239 |                                                                           |
1240 |   Outputs :                                                               |
1241 |                                                                           |
1242 |    none                                                                   |
1243 |                                                                           |
1244 |   Return Value :                                                          |
1245 |                                                                           |
1246 |    L_var_out                                                              |
1247 |             32 bit long signed integer (Word32) whose value falls in the  |
1248 |             range : 0x8000 0000 <= var_out <= 0x7fff 0000.                |
1249 |___________________________________________________________________________|
1250*/
1251//Word32 L_deposit_h (Word16 var1)
1252//{
1253//    Word32 L_var_out;
1254//
1255//    L_var_out = (Word32) var1 << 16;
1256//
1257//    return (L_var_out);
1258//}
1259
1260
1261/*___________________________________________________________________________
1262 |                                                                           |
1263 |   Function Name : L_deposit_l                                             |
1264 |                                                                           |
1265 |   Purpose :                                                               |
1266 |                                                                           |
1267 |   Deposit the 16 bit var1 into the 16 LS bits of the 32 bit output. The   |
1268 |   16 MS bits of the output are sign extended.                             |
1269 |                                                                           |
1270 |   Complexity weight : 2                                                   |
1271 |                                                                           |
1272 |   Inputs :                                                                |
1273 |                                                                           |
1274 |    var1                                                                   |
1275 |             16 bit short signed integer (Word16) whose value falls in the |
1276 |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
1277 |                                                                           |
1278 |   Outputs :                                                               |
1279 |                                                                           |
1280 |    none                                                                   |
1281 |                                                                           |
1282 |   Return Value :                                                          |
1283 |                                                                           |
1284 |    L_var_out                                                              |
1285 |             32 bit long signed integer (Word32) whose value falls in the  |
1286 |             range : 0xFFFF 8000 <= var_out <= 0x0000 7fff.                |
1287 |___________________________________________________________________________|
1288*/
1289//Word32 L_deposit_l (Word16 var1)
1290//{
1291//    Word32 L_var_out;
1292//
1293//    L_var_out = (Word32) var1;
1294//
1295//    return (L_var_out);
1296//}
1297
1298
1299/*___________________________________________________________________________
1300 |                                                                           |
1301 |   Function Name : L_shr_r                                                 |
1302 |                                                                           |
1303 |   Purpose :                                                               |
1304 |                                                                           |
1305 |   Same as L_shr(L_var1,var2) but with rounding. Saturate the result in    |
1306 |   case of underflows or overflows :                                       |
1307 |    - If var2 is greater than zero :                                       |
1308 |          if (L_sub(L_shl(L_shr(L_var1,var2),1),L_shr(L_var1,sub(var2,1))))|
1309 |          is equal to zero                                                 |
1310 |                     then                                                  |
1311 |                     L_shr_r(L_var1,var2) = L_shr(L_var1,var2)             |
1312 |                     else                                                  |
1313 |                     L_shr_r(L_var1,var2) = L_add(L_shr(L_var1,var2),1)    |
1314 |    - If var2 is less than or equal to zero :                              |
1315 |                     L_shr_r(L_var1,var2) = L_shr(L_var1,var2).            |
1316 |                                                                           |
1317 |   Complexity weight : 3                                                   |
1318 |                                                                           |
1319 |   Inputs :                                                                |
1320 |                                                                           |
1321 |    L_var1                                                                 |
1322 |             32 bit long signed integer (Word32) whose value falls in the  |
1323 |             range : 0x8000 0000 <= var1 <= 0x7fff ffff.                   |
1324 |                                                                           |
1325 |    var2                                                                   |
1326 |             16 bit short signed integer (Word16) whose value falls in the |
1327 |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
1328 |                                                                           |
1329 |   Outputs :                                                               |
1330 |                                                                           |
1331 |    none                                                                   |
1332 |                                                                           |
1333 |   Return Value :                                                          |
1334 |                                                                           |
1335 |    L_var_out                                                              |
1336 |             32 bit long signed integer (Word32) whose value falls in the  |
1337 |             range : 0x8000 0000 <= var_out <= 0x7fff ffff.                |
1338 |___________________________________________________________________________|
1339*/
1340#if (!L_SHR_R_IS_INLINE)
1341Word32 L_shr_r (Word32 L_var1, Word16 var2)
1342{
1343    Word32 L_var_out;
1344
1345    if (var2 > 31)
1346    {
1347        L_var_out = 0;
1348    }
1349    else
1350    {
1351        L_var_out = L_shr (L_var1, var2);
1352
1353        if (var2 > 0)
1354        {
1355            if ((L_var1 & ((Word32) 1 << (var2 - 1))) != 0)
1356            {
1357                L_var_out++;
1358            }
1359        }
1360    }
1361
1362    return (L_var_out);
1363}
1364#endif
1365
1366/*___________________________________________________________________________
1367 |                                                                           |
1368 |   Function Name : L_abs                                                   |
1369 |                                                                           |
1370 |   Purpose :                                                               |
1371 |                                                                           |
1372 |    Absolute value of L_var1; Saturate in case where the input is          |
1373 |                                                               -214783648  |
1374 |                                                                           |
1375 |   Complexity weight : 3                                                   |
1376 |                                                                           |
1377 |   Inputs :                                                                |
1378 |                                                                           |
1379 |    L_var1                                                                 |
1380 |             32 bit long signed integer (Word32) whose value falls in the  |
1381 |             range : 0x8000 0000 <= var1 <= 0x7fff ffff.                   |
1382 |                                                                           |
1383 |   Outputs :                                                               |
1384 |                                                                           |
1385 |    none                                                                   |
1386 |                                                                           |
1387 |   Return Value :                                                          |
1388 |                                                                           |
1389 |    L_var_out                                                              |
1390 |             32 bit long signed integer (Word32) whose value falls in the  |
1391 |             range : 0x0000 0000 <= var_out <= 0x7fff ffff.                |
1392 |___________________________________________________________________________|
1393*/
1394//Word32 L_abs (Word32 L_var1)
1395//{
1396//    Word32 L_var_out;
1397//
1398//    if (L_var1 == MIN_32)
1399//    {
1400//        L_var_out = MAX_32;
1401//    }
1402//    else
1403//    {
1404//        if (L_var1 < 0)
1405//        {
1406//            L_var_out = -L_var1;
1407//        }
1408//        else
1409//        {
1410//            L_var_out = L_var1;
1411//        }
1412//    }
1413//
1414//    return (L_var_out);
1415//}
1416
1417/*___________________________________________________________________________
1418 |                                                                           |
1419 |   Function Name : norm_s                                                  |
1420 |                                                                           |
1421 |   Purpose :                                                               |
1422 |                                                                           |
1423 |   Produces the number of left shift needed to normalize the 16 bit varia- |
1424 |   ble var1 for positive values on the interval with minimum of 16384 and  |
1425 |   maximum of 32767, and for negative values on the interval with minimum  |
1426 |   of -32768 and maximum of -16384; in order to normalize the result, the  |
1427 |   following operation must be done :                                      |
1428 |                    norm_var1 = shl(var1,norm_s(var1)).                    |
1429 |                                                                           |
1430 |   Complexity weight : 15                                                  |
1431 |                                                                           |
1432 |   Inputs :                                                                |
1433 |                                                                           |
1434 |    var1                                                                   |
1435 |             16 bit short signed integer (Word16) whose value falls in the |
1436 |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
1437 |                                                                           |
1438 |   Outputs :                                                               |
1439 |                                                                           |
1440 |    none                                                                   |
1441 |                                                                           |
1442 |   Return Value :                                                          |
1443 |                                                                           |
1444 |    var_out                                                                |
1445 |             16 bit short signed integer (Word16) whose value falls in the |
1446 |             range : 0x0000 0000 <= var_out <= 0x0000 000f.                |
1447 |___________________________________________________________________________|
1448*/
1449
1450#if (!NORM_S_IS_INLINE)
1451Word16 norm_s (Word16 var1)
1452{
1453    Word16 var_out;
1454
1455    if (var1 == 0)
1456    {
1457        var_out = 0;
1458    }
1459    else
1460    {
1461        if (var1 == -1)
1462        {
1463            var_out = 15;
1464        }
1465        else
1466        {
1467            if (var1 < 0)
1468            {
1469                var1 = (Word16)~var1;
1470            }
1471            for (var_out = 0; var1 < 0x4000; var_out++)
1472            {
1473                var1 <<= 1;
1474            }
1475        }
1476    }
1477
1478    return (var_out);
1479}
1480#endif
1481
1482/*___________________________________________________________________________
1483 |                                                                           |
1484 |   Function Name : div_s                                                   |
1485 |                                                                           |
1486 |   Purpose :                                                               |
1487 |                                                                           |
1488 |   Produces a result which is the fractional integer division of var1  by  |
1489 |   var2; var1 and var2 must be positive and var2 must be greater or equal  |
1490 |   to var1; the result is positive (leading bit equal to 0) and truncated  |
1491 |   to 16 bits.                                                             |
1492 |   If var1 = var2 then div(var1,var2) = 32767.                             |
1493 |                                                                           |
1494 |   Complexity weight : 18                                                  |
1495 |                                                                           |
1496 |   Inputs :                                                                |
1497 |                                                                           |
1498 |    var1                                                                   |
1499 |             16 bit short signed integer (Word16) whose value falls in the |
1500 |             range : 0x0000 0000 <= var1 <= var2 and var2 != 0.            |
1501 |                                                                           |
1502 |    var2                                                                   |
1503 |             16 bit short signed integer (Word16) whose value falls in the |
1504 |             range : var1 <= var2 <= 0x0000 7fff and var2 != 0.            |
1505 |                                                                           |
1506 |   Outputs :                                                               |
1507 |                                                                           |
1508 |    none                                                                   |
1509 |                                                                           |
1510 |   Return Value :                                                          |
1511 |                                                                           |
1512 |    var_out                                                                |
1513 |             16 bit short signed integer (Word16) whose value falls in the |
1514 |             range : 0x0000 0000 <= var_out <= 0x0000 7fff.                |
1515 |             It's a Q15 value (point between b15 and b14).                 |
1516 |___________________________________________________________________________|
1517*/
1518
1519#if (!DIV_S_IS_INLINE)
1520Word16 div_s (Word16 var1, Word16 var2)
1521{
1522    Word16 var_out = 0;
1523    Word16 iteration;
1524    Word32 L_num;
1525    Word32 L_denom;
1526
1527    if (var1 == 0)
1528    {
1529        var_out = 0;
1530    }
1531    else
1532    {
1533        if (var1 == var2)
1534        {
1535            var_out = MAX_16;
1536        }
1537        else
1538        {
1539            L_num = L_deposit_l (var1);
1540            L_denom = L_deposit_l (var2);
1541
1542            for (iteration = 0; iteration < 15; iteration++)
1543            {
1544                var_out <<= 1;
1545                L_num <<= 1;
1546
1547                if (L_num >= L_denom)
1548                {
1549                    L_num = L_sub(L_num, L_denom);
1550                    var_out = add (var_out, 1);
1551                }
1552            }
1553        }
1554    }
1555
1556    return (var_out);
1557}
1558#endif
1559
1560/*___________________________________________________________________________
1561 |                                                                           |
1562 |   Function Name : norm_l                                                  |
1563 |                                                                           |
1564 |   Purpose :                                                               |
1565 |                                                                           |
1566 |   Produces the number of left shifts needed to normalize the 32 bit varia-|
1567 |   ble L_var1 for positive values on the interval with minimum of          |
1568 |   1073741824 and maximum of 2147483647, and for negative values on the in-|
1569 |   terval with minimum of -2147483648 and maximum of -1073741824; in order |
1570 |   to normalize the result, the following operation must be done :         |
1571 |                   norm_L_var1 = L_shl(L_var1,norm_l(L_var1)).             |
1572 |                                                                           |
1573 |   Complexity weight : 30                                                  |
1574 |                                                                           |
1575 |   Inputs :                                                                |
1576 |                                                                           |
1577 |    L_var1                                                                 |
1578 |             32 bit long signed integer (Word32) whose value falls in the  |
1579 |             range : 0x8000 0000 <= var1 <= 0x7fff ffff.                   |
1580 |                                                                           |
1581 |   Outputs :                                                               |
1582 |                                                                           |
1583 |    none                                                                   |
1584 |                                                                           |
1585 |   Return Value :                                                          |
1586 |                                                                           |
1587 |    var_out                                                                |
1588 |             16 bit short signed integer (Word16) whose value falls in the |
1589 |             range : 0x0000 0000 <= var_out <= 0x0000 001f.                |
1590 |___________________________________________________________________________|
1591*/
1592
1593#if (!NORM_L_IS_INLINE)
1594Word16 norm_l (Word32 L_var1)
1595{
1596    Word16 var_out;
1597
1598    if (L_var1 == 0)
1599    {
1600        var_out = 0;
1601    }
1602    else
1603    {
1604        if (L_var1 == (Word32) 0xffffffffL)
1605        {
1606            var_out = 31;
1607        }
1608        else
1609        {
1610            if (L_var1 < 0)
1611            {
1612                L_var1 = ~L_var1;
1613            }
1614            for (var_out = 0; L_var1 < (Word32) 0x40000000L; var_out++)
1615            {
1616                L_var1 <<= 1;
1617            }
1618        }
1619    }
1620
1621    return (var_out);
1622}
1623#endif
1624
1625