media/libopus/silk/fixed/corrMatrix_FIX.c

Thu, 15 Jan 2015 15:59:08 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:59:08 +0100
branch
TOR_BUG_9701
changeset 10
ac0c01689b40
permissions
-rw-r--r--

Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /***********************************************************************
michael@0 2 Copyright (c) 2006-2011, Skype Limited. All rights reserved.
michael@0 3 Redistribution and use in source and binary forms, with or without
michael@0 4 modification, are permitted provided that the following conditions
michael@0 5 are met:
michael@0 6 - Redistributions of source code must retain the above copyright notice,
michael@0 7 this list of conditions and the following disclaimer.
michael@0 8 - Redistributions in binary form must reproduce the above copyright
michael@0 9 notice, this list of conditions and the following disclaimer in the
michael@0 10 documentation and/or other materials provided with the distribution.
michael@0 11 - Neither the name of Internet Society, IETF or IETF Trust, nor the
michael@0 12 names of specific contributors, may be used to endorse or promote
michael@0 13 products derived from this software without specific prior written
michael@0 14 permission.
michael@0 15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
michael@0 16 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
michael@0 17 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
michael@0 18 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
michael@0 19 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
michael@0 20 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
michael@0 21 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
michael@0 22 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
michael@0 23 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
michael@0 24 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
michael@0 25 POSSIBILITY OF SUCH DAMAGE.
michael@0 26 ***********************************************************************/
michael@0 27
michael@0 28 #ifdef HAVE_CONFIG_H
michael@0 29 #include "config.h"
michael@0 30 #endif
michael@0 31
michael@0 32 /**********************************************************************
michael@0 33 * Correlation Matrix Computations for LS estimate.
michael@0 34 **********************************************************************/
michael@0 35
michael@0 36 #include "main_FIX.h"
michael@0 37
michael@0 38 /* Calculates correlation vector X'*t */
michael@0 39 void silk_corrVector_FIX(
michael@0 40 const opus_int16 *x, /* I x vector [L + order - 1] used to form data matrix X */
michael@0 41 const opus_int16 *t, /* I Target vector [L] */
michael@0 42 const opus_int L, /* I Length of vectors */
michael@0 43 const opus_int order, /* I Max lag for correlation */
michael@0 44 opus_int32 *Xt, /* O Pointer to X'*t correlation vector [order] */
michael@0 45 const opus_int rshifts /* I Right shifts of correlations */
michael@0 46 )
michael@0 47 {
michael@0 48 opus_int lag, i;
michael@0 49 const opus_int16 *ptr1, *ptr2;
michael@0 50 opus_int32 inner_prod;
michael@0 51
michael@0 52 ptr1 = &x[ order - 1 ]; /* Points to first sample of column 0 of X: X[:,0] */
michael@0 53 ptr2 = t;
michael@0 54 /* Calculate X'*t */
michael@0 55 if( rshifts > 0 ) {
michael@0 56 /* Right shifting used */
michael@0 57 for( lag = 0; lag < order; lag++ ) {
michael@0 58 inner_prod = 0;
michael@0 59 for( i = 0; i < L; i++ ) {
michael@0 60 inner_prod += silk_RSHIFT32( silk_SMULBB( ptr1[ i ], ptr2[i] ), rshifts );
michael@0 61 }
michael@0 62 Xt[ lag ] = inner_prod; /* X[:,lag]'*t */
michael@0 63 ptr1--; /* Go to next column of X */
michael@0 64 }
michael@0 65 } else {
michael@0 66 silk_assert( rshifts == 0 );
michael@0 67 for( lag = 0; lag < order; lag++ ) {
michael@0 68 Xt[ lag ] = silk_inner_prod_aligned( ptr1, ptr2, L ); /* X[:,lag]'*t */
michael@0 69 ptr1--; /* Go to next column of X */
michael@0 70 }
michael@0 71 }
michael@0 72 }
michael@0 73
michael@0 74 /* Calculates correlation matrix X'*X */
michael@0 75 void silk_corrMatrix_FIX(
michael@0 76 const opus_int16 *x, /* I x vector [L + order - 1] used to form data matrix X */
michael@0 77 const opus_int L, /* I Length of vectors */
michael@0 78 const opus_int order, /* I Max lag for correlation */
michael@0 79 const opus_int head_room, /* I Desired headroom */
michael@0 80 opus_int32 *XX, /* O Pointer to X'*X correlation matrix [ order x order ] */
michael@0 81 opus_int *rshifts /* I/O Right shifts of correlations */
michael@0 82 )
michael@0 83 {
michael@0 84 opus_int i, j, lag, rshifts_local, head_room_rshifts;
michael@0 85 opus_int32 energy;
michael@0 86 const opus_int16 *ptr1, *ptr2;
michael@0 87
michael@0 88 /* Calculate energy to find shift used to fit in 32 bits */
michael@0 89 silk_sum_sqr_shift( &energy, &rshifts_local, x, L + order - 1 );
michael@0 90 /* Add shifts to get the desired head room */
michael@0 91 head_room_rshifts = silk_max( head_room - silk_CLZ32( energy ), 0 );
michael@0 92
michael@0 93 energy = silk_RSHIFT32( energy, head_room_rshifts );
michael@0 94 rshifts_local += head_room_rshifts;
michael@0 95
michael@0 96 /* Calculate energy of first column (0) of X: X[:,0]'*X[:,0] */
michael@0 97 /* Remove contribution of first order - 1 samples */
michael@0 98 for( i = 0; i < order - 1; i++ ) {
michael@0 99 energy -= silk_RSHIFT32( silk_SMULBB( x[ i ], x[ i ] ), rshifts_local );
michael@0 100 }
michael@0 101 if( rshifts_local < *rshifts ) {
michael@0 102 /* Adjust energy */
michael@0 103 energy = silk_RSHIFT32( energy, *rshifts - rshifts_local );
michael@0 104 rshifts_local = *rshifts;
michael@0 105 }
michael@0 106
michael@0 107 /* Calculate energy of remaining columns of X: X[:,j]'*X[:,j] */
michael@0 108 /* Fill out the diagonal of the correlation matrix */
michael@0 109 matrix_ptr( XX, 0, 0, order ) = energy;
michael@0 110 ptr1 = &x[ order - 1 ]; /* First sample of column 0 of X */
michael@0 111 for( j = 1; j < order; j++ ) {
michael@0 112 energy = silk_SUB32( energy, silk_RSHIFT32( silk_SMULBB( ptr1[ L - j ], ptr1[ L - j ] ), rshifts_local ) );
michael@0 113 energy = silk_ADD32( energy, silk_RSHIFT32( silk_SMULBB( ptr1[ -j ], ptr1[ -j ] ), rshifts_local ) );
michael@0 114 matrix_ptr( XX, j, j, order ) = energy;
michael@0 115 }
michael@0 116
michael@0 117 ptr2 = &x[ order - 2 ]; /* First sample of column 1 of X */
michael@0 118 /* Calculate the remaining elements of the correlation matrix */
michael@0 119 if( rshifts_local > 0 ) {
michael@0 120 /* Right shifting used */
michael@0 121 for( lag = 1; lag < order; lag++ ) {
michael@0 122 /* Inner product of column 0 and column lag: X[:,0]'*X[:,lag] */
michael@0 123 energy = 0;
michael@0 124 for( i = 0; i < L; i++ ) {
michael@0 125 energy += silk_RSHIFT32( silk_SMULBB( ptr1[ i ], ptr2[i] ), rshifts_local );
michael@0 126 }
michael@0 127 /* Calculate remaining off diagonal: X[:,j]'*X[:,j + lag] */
michael@0 128 matrix_ptr( XX, lag, 0, order ) = energy;
michael@0 129 matrix_ptr( XX, 0, lag, order ) = energy;
michael@0 130 for( j = 1; j < ( order - lag ); j++ ) {
michael@0 131 energy = silk_SUB32( energy, silk_RSHIFT32( silk_SMULBB( ptr1[ L - j ], ptr2[ L - j ] ), rshifts_local ) );
michael@0 132 energy = silk_ADD32( energy, silk_RSHIFT32( silk_SMULBB( ptr1[ -j ], ptr2[ -j ] ), rshifts_local ) );
michael@0 133 matrix_ptr( XX, lag + j, j, order ) = energy;
michael@0 134 matrix_ptr( XX, j, lag + j, order ) = energy;
michael@0 135 }
michael@0 136 ptr2--; /* Update pointer to first sample of next column (lag) in X */
michael@0 137 }
michael@0 138 } else {
michael@0 139 for( lag = 1; lag < order; lag++ ) {
michael@0 140 /* Inner product of column 0 and column lag: X[:,0]'*X[:,lag] */
michael@0 141 energy = silk_inner_prod_aligned( ptr1, ptr2, L );
michael@0 142 matrix_ptr( XX, lag, 0, order ) = energy;
michael@0 143 matrix_ptr( XX, 0, lag, order ) = energy;
michael@0 144 /* Calculate remaining off diagonal: X[:,j]'*X[:,j + lag] */
michael@0 145 for( j = 1; j < ( order - lag ); j++ ) {
michael@0 146 energy = silk_SUB32( energy, silk_SMULBB( ptr1[ L - j ], ptr2[ L - j ] ) );
michael@0 147 energy = silk_SMLABB( energy, ptr1[ -j ], ptr2[ -j ] );
michael@0 148 matrix_ptr( XX, lag + j, j, order ) = energy;
michael@0 149 matrix_ptr( XX, j, lag + j, order ) = energy;
michael@0 150 }
michael@0 151 ptr2--;/* Update pointer to first sample of next column (lag) in X */
michael@0 152 }
michael@0 153 }
michael@0 154 *rshifts = rshifts_local;
michael@0 155 }
michael@0 156

mercurial