1 /***********************************************************************
2 Copyright (c) 2017 Google Inc., Jean-Marc Valin
3 Redistribution and use in source and binary forms, with or without
4 modification, are permitted provided that the following conditions
5 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 Internet Society, IETF or IETF Trust, nor the
12 names of specific contributors, may be used to endorse or promote
13 products derived from this software without specific prior written
14 permission.
15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 POSSIBILITY OF SUCH DAMAGE.
26 ***********************************************************************/
27 
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31 
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include "celt/stack_alloc.h"
35 #include "cpu_support.h"
36 #include "SigProc_FIX.h"
37 
38 /* Computes the impulse response of the filter so we
39    can catch filters that are definitely unstable. Some
40    unstable filters may be classified as stable, but not
41    the other way around. */
check_stability(opus_int16 * A_Q12,int order)42 int check_stability(opus_int16 *A_Q12, int order) {
43     int i;
44     int j;
45     int sum_a, sum_abs_a;
46     sum_a = sum_abs_a = 0;
47     for( j = 0; j < order; j++ ) {
48         sum_a += A_Q12[ j ];
49         sum_abs_a += silk_abs( A_Q12[ j ] );
50     }
51     /* Check DC stability. */
52     if( sum_a >= 4096 ) {
53         return 0;
54     }
55     /* If the sum of absolute values is less than 1, the filter
56        has to be stable. */
57     if( sum_abs_a < 4096 ) {
58         return 1;
59     }
60     double y[SILK_MAX_ORDER_LPC] = {0};
61     y[0] = 1;
62     for( i = 0; i < 10000; i++ ) {
63         double sum = 0;
64         for( j = 0; j < order; j++ ) {
65             sum += y[ j ]*A_Q12[ j ];
66         }
67         for( j = order - 1; j > 0; j-- ) {
68             y[ j ] = y[ j - 1 ];
69         }
70         y[ 0 ] = sum*(1./4096);
71         /* If impulse response reaches +/- 10000, the filter
72            is definitely unstable. */
73         if( !(y[ 0 ] < 10000 && y[ 0 ] > -10000) ) {
74             return 0;
75         }
76         /* Test every 8 sample for low amplitude. */
77         if( ( i & 0x7 ) == 0 ) {
78             double amp = 0;
79             for( j = 0; j < order; j++ ) {
80                 amp += fabs(y[j]);
81             }
82             if( amp < 0.00001 ) {
83                 return 1;
84             }
85         }
86     }
87     return 1;
88 }
89 
main(void)90 int main(void) {
91     const int arch = opus_select_arch();
92     /* Set to 10000 so all branches in C function are triggered */
93     const int loop_num = 10000;
94     int count = 0;
95     ALLOC_STACK;
96 
97     /* FIXME: Make the seed random (with option to set it explicitly)
98        so we get wider coverage. */
99     srand(0);
100 
101     printf("Testing silk_LPC_inverse_pred_gain() optimization ...\n");
102     for( count = 0; count < loop_num; count++ ) {
103         unsigned int i;
104         opus_int     order;
105         unsigned int shift;
106         opus_int16   A_Q12[ SILK_MAX_ORDER_LPC ];
107         opus_int32 gain;
108 
109         for( order = 2; order <= SILK_MAX_ORDER_LPC; order += 2 ) { /* order must be even. */
110             for( shift = 0; shift < 16; shift++ ) { /* Different dynamic range. */
111                 for( i = 0; i < SILK_MAX_ORDER_LPC; i++ ) {
112                     A_Q12[i] = ((opus_int16)rand()) >> shift;
113                 }
114                 gain = silk_LPC_inverse_pred_gain(A_Q12, order, arch);
115                 /* Look for filters that silk_LPC_inverse_pred_gain() thinks are
116                    stable but definitely aren't. */
117                 if( gain != 0 && !check_stability(A_Q12, order) ) {
118                     fprintf(stderr, "**Loop %4d failed!**\n", count);
119                     return 1;
120                 }
121             }
122         }
123         if( !(count % 500) ) {
124             printf("Loop %4d passed\n", count);
125         }
126     }
127     printf("silk_LPC_inverse_pred_gain() optimization passed\n");
128     return 0;
129 }
130