1 /***********************************************************************
2 Copyright (c) 2006-2010, Skype Limited. All rights reserved.
3 Redistribution and use in source and binary forms, with or without
4 modification, (subject to the limitations in the disclaimer below)
5 are permitted provided that the following conditions are met:
6 - Redistributions of source code must retain the above copyright notice,
7 this list of conditions and the following disclaimer.
8 - Redistributions in binary form must reproduce the above copyright
9 notice, this list of conditions and the following disclaimer in the
10 documentation and/or other materials provided with the distribution.
11 - Neither the name of Skype Limited, nor the names of specific
12 contributors, may be used to endorse or promote products derived from
13 this software without specific prior written permission.
14 NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED
15 BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
16 CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
17 BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
18 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
22 USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
23 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 ***********************************************************************/
27 
28 #ifndef _SKP_SILK_API_C_H_
29 #define _SKP_SILK_API_C_H_
30 
31 // This is an inline header file for general platform.
32 
33 // (a32 * (SKP_int32)((SKP_int16)(b32))) >> 16 output have to be 32bit int
34 #define SKP_SMULWB(a32, b32)            ((((a32) >> 16) * (SKP_int32)((SKP_int16)(b32))) + ((((a32) & 0x0000FFFF) * (SKP_int32)((SKP_int16)(b32))) >> 16))
35 
36 // a32 + (b32 * (SKP_int32)((SKP_int16)(c32))) >> 16 output have to be 32bit int
37 #define SKP_SMLAWB(a32, b32, c32)       ((a32) + ((((b32) >> 16) * (SKP_int32)((SKP_int16)(c32))) + ((((b32) & 0x0000FFFF) * (SKP_int32)((SKP_int16)(c32))) >> 16)))
38 
39 // (a32 * (b32 >> 16)) >> 16
40 #define SKP_SMULWT(a32, b32)            (((a32) >> 16) * ((b32) >> 16) + ((((a32) & 0x0000FFFF) * ((b32) >> 16)) >> 16))
41 
42 // a32 + (b32 * (c32 >> 16)) >> 16
43 #define SKP_SMLAWT(a32, b32, c32)       ((a32) + (((b32) >> 16) * ((c32) >> 16)) + ((((b32) & 0x0000FFFF) * ((c32) >> 16)) >> 16))
44 
45 // (SKP_int32)((SKP_int16)(a3))) * (SKP_int32)((SKP_int16)(b32)) output have to be 32bit int
46 #define SKP_SMULBB(a32, b32)            ((SKP_int32)((SKP_int16)(a32)) * (SKP_int32)((SKP_int16)(b32)))
47 
48 // a32 + (SKP_int32)((SKP_int16)(b32)) * (SKP_int32)((SKP_int16)(c32)) output have to be 32bit int
49 #define SKP_SMLABB(a32, b32, c32)       ((a32) + ((SKP_int32)((SKP_int16)(b32))) * (SKP_int32)((SKP_int16)(c32)))
50 
51 // (SKP_int32)((SKP_int16)(a32)) * (b32 >> 16)
52 #define SKP_SMULBT(a32, b32)            ((SKP_int32)((SKP_int16)(a32)) * ((b32) >> 16))
53 
54 // a32 + (SKP_int32)((SKP_int16)(b32)) * (c32 >> 16)
55 #define SKP_SMLABT(a32, b32, c32)       ((a32) + ((SKP_int32)((SKP_int16)(b32))) * ((c32) >> 16))
56 
57 // a64 + (b32 * c32)
58 #define SKP_SMLAL(a64, b32, c32)        (SKP_ADD64((a64), ((SKP_int64)(b32) * (SKP_int64)(c32))))
59 
60 // (a32 * b32) >> 16
61 #define SKP_SMULWW(a32, b32)            SKP_MLA(SKP_SMULWB((a32), (b32)), (a32), SKP_RSHIFT_ROUND((b32), 16))
62 
63 // a32 + ((b32 * c32) >> 16)
64 #define SKP_SMLAWW(a32, b32, c32)       SKP_MLA(SKP_SMLAWB((a32), (b32), (c32)), (b32), SKP_RSHIFT_ROUND((c32), 16))
65 
66 /* add/subtract with output saturated */
67 #define SKP_ADD_SAT32(a, b)             ((((a) + (b)) & 0x80000000) == 0 ?                              \
68                                         ((((a) & (b)) & 0x80000000) != 0 ? SKP_int32_MIN : (a)+(b)) :   \
69                                         ((((a) | (b)) & 0x80000000) == 0 ? SKP_int32_MAX : (a)+(b)) )
70 
71 #define SKP_SUB_SAT32(a, b)             ((((a)-(b)) & 0x80000000) == 0 ?                                        \
72                                         (( (a) & ((b)^0x80000000) & 0x80000000) ? SKP_int32_MIN : (a)-(b)) :    \
73                                         ((((a)^0x80000000) & (b)  & 0x80000000) ? SKP_int32_MAX : (a)-(b)) )
74 
SKP_Silk_CLZ16(SKP_int16 in16)75 SKP_INLINE SKP_int32 SKP_Silk_CLZ16(SKP_int16 in16)
76 {
77     SKP_int32 out32 = 0;
78     if( in16 == 0 ) {
79         return 16;
80     }
81     /* test nibbles */
82     if( in16 & 0xFF00 ) {
83         if( in16 & 0xF000 ) {
84             in16 >>= 12;
85         } else {
86             out32 += 4;
87             in16 >>= 8;
88         }
89     } else {
90         if( in16 & 0xFFF0 ) {
91             out32 += 8;
92             in16 >>= 4;
93         } else {
94             out32 += 12;
95         }
96     }
97     /* test bits and return */
98     if( in16 & 0xC ) {
99         if( in16 & 0x8 )
100             return out32 + 0;
101         else
102             return out32 + 1;
103     } else {
104         if( in16 & 0xE )
105             return out32 + 2;
106         else
107             return out32 + 3;
108     }
109 }
110 
SKP_Silk_CLZ32(SKP_int32 in32)111 SKP_INLINE SKP_int32 SKP_Silk_CLZ32(SKP_int32 in32)
112 {
113     /* test highest 16 bits and convert to SKP_int16 */
114     if( in32 & 0xFFFF0000 ) {
115         return SKP_Silk_CLZ16((SKP_int16)(in32 >> 16));
116     } else {
117         return SKP_Silk_CLZ16((SKP_int16)in32) + 16;
118     }
119 }
120 
121 #endif //_SKP_SILK_API_C_H_
122 
123