1*a58d3d2aSXin Li /***********************************************************************
2*a58d3d2aSXin Li Copyright (c) 2006-2011, Skype Limited. All rights reserved.
3*a58d3d2aSXin Li Redistribution and use in source and binary forms, with or without
4*a58d3d2aSXin Li modification, are permitted provided that the following conditions
5*a58d3d2aSXin Li are met:
6*a58d3d2aSXin Li - Redistributions of source code must retain the above copyright notice,
7*a58d3d2aSXin Li this list of conditions and the following disclaimer.
8*a58d3d2aSXin Li - Redistributions in binary form must reproduce the above copyright
9*a58d3d2aSXin Li notice, this list of conditions and the following disclaimer in the
10*a58d3d2aSXin Li documentation and/or other materials provided with the distribution.
11*a58d3d2aSXin Li - Neither the name of Internet Society, IETF or IETF Trust, nor the
12*a58d3d2aSXin Li names of specific contributors, may be used to endorse or promote
13*a58d3d2aSXin Li products derived from this software without specific prior written
14*a58d3d2aSXin Li permission.
15*a58d3d2aSXin Li THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16*a58d3d2aSXin Li AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17*a58d3d2aSXin Li IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18*a58d3d2aSXin Li ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19*a58d3d2aSXin Li LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20*a58d3d2aSXin Li CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21*a58d3d2aSXin Li SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22*a58d3d2aSXin Li INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23*a58d3d2aSXin Li CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24*a58d3d2aSXin Li ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25*a58d3d2aSXin Li POSSIBILITY OF SUCH DAMAGE.
26*a58d3d2aSXin Li ***********************************************************************/
27*a58d3d2aSXin Li
28*a58d3d2aSXin Li #ifdef HAVE_CONFIG_H
29*a58d3d2aSXin Li #include "config.h"
30*a58d3d2aSXin Li #endif
31*a58d3d2aSXin Li
32*a58d3d2aSXin Li /* Insertion sort (fast for already almost sorted arrays): */
33*a58d3d2aSXin Li /* Best case: O(n) for an already sorted array */
34*a58d3d2aSXin Li /* Worst case: O(n^2) for an inversely sorted array */
35*a58d3d2aSXin Li /* */
36*a58d3d2aSXin Li /* Shell short: https://en.wikipedia.org/wiki/Shell_sort */
37*a58d3d2aSXin Li
38*a58d3d2aSXin Li #include "SigProc_FIX.h"
39*a58d3d2aSXin Li
silk_insertion_sort_increasing(opus_int32 * a,opus_int * idx,const opus_int L,const opus_int K)40*a58d3d2aSXin Li void silk_insertion_sort_increasing(
41*a58d3d2aSXin Li opus_int32 *a, /* I/O Unsorted / Sorted vector */
42*a58d3d2aSXin Li opus_int *idx, /* O Index vector for the sorted elements */
43*a58d3d2aSXin Li const opus_int L, /* I Vector length */
44*a58d3d2aSXin Li const opus_int K /* I Number of correctly sorted positions */
45*a58d3d2aSXin Li )
46*a58d3d2aSXin Li {
47*a58d3d2aSXin Li opus_int32 value;
48*a58d3d2aSXin Li opus_int i, j;
49*a58d3d2aSXin Li
50*a58d3d2aSXin Li /* Safety checks */
51*a58d3d2aSXin Li celt_assert( K > 0 );
52*a58d3d2aSXin Li celt_assert( L > 0 );
53*a58d3d2aSXin Li celt_assert( L >= K );
54*a58d3d2aSXin Li
55*a58d3d2aSXin Li /* Write start indices in index vector */
56*a58d3d2aSXin Li for( i = 0; i < K; i++ ) {
57*a58d3d2aSXin Li idx[ i ] = i;
58*a58d3d2aSXin Li }
59*a58d3d2aSXin Li
60*a58d3d2aSXin Li /* Sort vector elements by value, increasing order */
61*a58d3d2aSXin Li for( i = 1; i < K; i++ ) {
62*a58d3d2aSXin Li value = a[ i ];
63*a58d3d2aSXin Li for( j = i - 1; ( j >= 0 ) && ( value < a[ j ] ); j-- ) {
64*a58d3d2aSXin Li a[ j + 1 ] = a[ j ]; /* Shift value */
65*a58d3d2aSXin Li idx[ j + 1 ] = idx[ j ]; /* Shift index */
66*a58d3d2aSXin Li }
67*a58d3d2aSXin Li a[ j + 1 ] = value; /* Write value */
68*a58d3d2aSXin Li idx[ j + 1 ] = i; /* Write index */
69*a58d3d2aSXin Li }
70*a58d3d2aSXin Li
71*a58d3d2aSXin Li /* If less than L values are asked for, check the remaining values, */
72*a58d3d2aSXin Li /* but only spend CPU to ensure that the K first values are correct */
73*a58d3d2aSXin Li for( i = K; i < L; i++ ) {
74*a58d3d2aSXin Li value = a[ i ];
75*a58d3d2aSXin Li if( value < a[ K - 1 ] ) {
76*a58d3d2aSXin Li for( j = K - 2; ( j >= 0 ) && ( value < a[ j ] ); j-- ) {
77*a58d3d2aSXin Li a[ j + 1 ] = a[ j ]; /* Shift value */
78*a58d3d2aSXin Li idx[ j + 1 ] = idx[ j ]; /* Shift index */
79*a58d3d2aSXin Li }
80*a58d3d2aSXin Li a[ j + 1 ] = value; /* Write value */
81*a58d3d2aSXin Li idx[ j + 1 ] = i; /* Write index */
82*a58d3d2aSXin Li }
83*a58d3d2aSXin Li }
84*a58d3d2aSXin Li }
85*a58d3d2aSXin Li
86*a58d3d2aSXin Li #ifdef FIXED_POINT
87*a58d3d2aSXin Li /* This function is only used by the fixed-point build */
silk_insertion_sort_decreasing_int16(opus_int16 * a,opus_int * idx,const opus_int L,const opus_int K)88*a58d3d2aSXin Li void silk_insertion_sort_decreasing_int16(
89*a58d3d2aSXin Li opus_int16 *a, /* I/O Unsorted / Sorted vector */
90*a58d3d2aSXin Li opus_int *idx, /* O Index vector for the sorted elements */
91*a58d3d2aSXin Li const opus_int L, /* I Vector length */
92*a58d3d2aSXin Li const opus_int K /* I Number of correctly sorted positions */
93*a58d3d2aSXin Li )
94*a58d3d2aSXin Li {
95*a58d3d2aSXin Li opus_int i, j;
96*a58d3d2aSXin Li opus_int value;
97*a58d3d2aSXin Li
98*a58d3d2aSXin Li /* Safety checks */
99*a58d3d2aSXin Li celt_assert( K > 0 );
100*a58d3d2aSXin Li celt_assert( L > 0 );
101*a58d3d2aSXin Li celt_assert( L >= K );
102*a58d3d2aSXin Li
103*a58d3d2aSXin Li /* Write start indices in index vector */
104*a58d3d2aSXin Li for( i = 0; i < K; i++ ) {
105*a58d3d2aSXin Li idx[ i ] = i;
106*a58d3d2aSXin Li }
107*a58d3d2aSXin Li
108*a58d3d2aSXin Li /* Sort vector elements by value, decreasing order */
109*a58d3d2aSXin Li for( i = 1; i < K; i++ ) {
110*a58d3d2aSXin Li value = a[ i ];
111*a58d3d2aSXin Li for( j = i - 1; ( j >= 0 ) && ( value > a[ j ] ); j-- ) {
112*a58d3d2aSXin Li a[ j + 1 ] = a[ j ]; /* Shift value */
113*a58d3d2aSXin Li idx[ j + 1 ] = idx[ j ]; /* Shift index */
114*a58d3d2aSXin Li }
115*a58d3d2aSXin Li a[ j + 1 ] = value; /* Write value */
116*a58d3d2aSXin Li idx[ j + 1 ] = i; /* Write index */
117*a58d3d2aSXin Li }
118*a58d3d2aSXin Li
119*a58d3d2aSXin Li /* If less than L values are asked for, check the remaining values, */
120*a58d3d2aSXin Li /* but only spend CPU to ensure that the K first values are correct */
121*a58d3d2aSXin Li for( i = K; i < L; i++ ) {
122*a58d3d2aSXin Li value = a[ i ];
123*a58d3d2aSXin Li if( value > a[ K - 1 ] ) {
124*a58d3d2aSXin Li for( j = K - 2; ( j >= 0 ) && ( value > a[ j ] ); j-- ) {
125*a58d3d2aSXin Li a[ j + 1 ] = a[ j ]; /* Shift value */
126*a58d3d2aSXin Li idx[ j + 1 ] = idx[ j ]; /* Shift index */
127*a58d3d2aSXin Li }
128*a58d3d2aSXin Li a[ j + 1 ] = value; /* Write value */
129*a58d3d2aSXin Li idx[ j + 1 ] = i; /* Write index */
130*a58d3d2aSXin Li }
131*a58d3d2aSXin Li }
132*a58d3d2aSXin Li }
133*a58d3d2aSXin Li #endif
134*a58d3d2aSXin Li
silk_insertion_sort_increasing_all_values_int16(opus_int16 * a,const opus_int L)135*a58d3d2aSXin Li void silk_insertion_sort_increasing_all_values_int16(
136*a58d3d2aSXin Li opus_int16 *a, /* I/O Unsorted / Sorted vector */
137*a58d3d2aSXin Li const opus_int L /* I Vector length */
138*a58d3d2aSXin Li )
139*a58d3d2aSXin Li {
140*a58d3d2aSXin Li opus_int value;
141*a58d3d2aSXin Li opus_int i, j;
142*a58d3d2aSXin Li
143*a58d3d2aSXin Li /* Safety checks */
144*a58d3d2aSXin Li celt_assert( L > 0 );
145*a58d3d2aSXin Li
146*a58d3d2aSXin Li /* Sort vector elements by value, increasing order */
147*a58d3d2aSXin Li for( i = 1; i < L; i++ ) {
148*a58d3d2aSXin Li value = a[ i ];
149*a58d3d2aSXin Li for( j = i - 1; ( j >= 0 ) && ( value < a[ j ] ); j-- ) {
150*a58d3d2aSXin Li a[ j + 1 ] = a[ j ]; /* Shift value */
151*a58d3d2aSXin Li }
152*a58d3d2aSXin Li a[ j + 1 ] = value; /* Write value */
153*a58d3d2aSXin Li }
154*a58d3d2aSXin Li }
155