Thu, 15 Jan 2015 15:59:08 +0100
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 | /* Copyright (c) 2008-2011 Octasic Inc. |
michael@0 | 2 | Written by Jean-Marc Valin */ |
michael@0 | 3 | /* |
michael@0 | 4 | Redistribution and use in source and binary forms, with or without |
michael@0 | 5 | modification, are permitted provided that the following conditions |
michael@0 | 6 | are met: |
michael@0 | 7 | |
michael@0 | 8 | - Redistributions of source code must retain the above copyright |
michael@0 | 9 | notice, this list of conditions and the following disclaimer. |
michael@0 | 10 | |
michael@0 | 11 | - Redistributions in binary form must reproduce the above copyright |
michael@0 | 12 | notice, this list of conditions and the following disclaimer in the |
michael@0 | 13 | documentation and/or other materials provided with the distribution. |
michael@0 | 14 | |
michael@0 | 15 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
michael@0 | 16 | ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
michael@0 | 17 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
michael@0 | 18 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR |
michael@0 | 19 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
michael@0 | 20 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
michael@0 | 21 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
michael@0 | 22 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
michael@0 | 23 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
michael@0 | 24 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
michael@0 | 25 | SOFTWARE, EVEN IF ADVISED OF THE 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 | #include "opus_types.h" |
michael@0 | 33 | #include "opus_defines.h" |
michael@0 | 34 | |
michael@0 | 35 | #include <math.h> |
michael@0 | 36 | #include "mlp.h" |
michael@0 | 37 | #include "arch.h" |
michael@0 | 38 | #include "tansig_table.h" |
michael@0 | 39 | #define MAX_NEURONS 100 |
michael@0 | 40 | |
michael@0 | 41 | #if 0 |
michael@0 | 42 | static OPUS_INLINE opus_val16 tansig_approx(opus_val32 _x) /* Q19 */ |
michael@0 | 43 | { |
michael@0 | 44 | int i; |
michael@0 | 45 | opus_val16 xx; /* Q11 */ |
michael@0 | 46 | /*double x, y;*/ |
michael@0 | 47 | opus_val16 dy, yy; /* Q14 */ |
michael@0 | 48 | /*x = 1.9073e-06*_x;*/ |
michael@0 | 49 | if (_x>=QCONST32(8,19)) |
michael@0 | 50 | return QCONST32(1.,14); |
michael@0 | 51 | if (_x<=-QCONST32(8,19)) |
michael@0 | 52 | return -QCONST32(1.,14); |
michael@0 | 53 | xx = EXTRACT16(SHR32(_x, 8)); |
michael@0 | 54 | /*i = lrint(25*x);*/ |
michael@0 | 55 | i = SHR32(ADD32(1024,MULT16_16(25, xx)),11); |
michael@0 | 56 | /*x -= .04*i;*/ |
michael@0 | 57 | xx -= EXTRACT16(SHR32(MULT16_16(20972,i),8)); |
michael@0 | 58 | /*x = xx*(1./2048);*/ |
michael@0 | 59 | /*y = tansig_table[250+i];*/ |
michael@0 | 60 | yy = tansig_table[250+i]; |
michael@0 | 61 | /*y = yy*(1./16384);*/ |
michael@0 | 62 | dy = 16384-MULT16_16_Q14(yy,yy); |
michael@0 | 63 | yy = yy + MULT16_16_Q14(MULT16_16_Q11(xx,dy),(16384 - MULT16_16_Q11(yy,xx))); |
michael@0 | 64 | return yy; |
michael@0 | 65 | } |
michael@0 | 66 | #else |
michael@0 | 67 | /*extern const float tansig_table[501];*/ |
michael@0 | 68 | static OPUS_INLINE float tansig_approx(float x) |
michael@0 | 69 | { |
michael@0 | 70 | int i; |
michael@0 | 71 | float y, dy; |
michael@0 | 72 | float sign=1; |
michael@0 | 73 | /* Tests are reversed to catch NaNs */ |
michael@0 | 74 | if (!(x<8)) |
michael@0 | 75 | return 1; |
michael@0 | 76 | if (!(x>-8)) |
michael@0 | 77 | return -1; |
michael@0 | 78 | if (x<0) |
michael@0 | 79 | { |
michael@0 | 80 | x=-x; |
michael@0 | 81 | sign=-1; |
michael@0 | 82 | } |
michael@0 | 83 | i = (int)floor(.5f+25*x); |
michael@0 | 84 | x -= .04f*i; |
michael@0 | 85 | y = tansig_table[i]; |
michael@0 | 86 | dy = 1-y*y; |
michael@0 | 87 | y = y + x*dy*(1 - y*x); |
michael@0 | 88 | return sign*y; |
michael@0 | 89 | } |
michael@0 | 90 | #endif |
michael@0 | 91 | |
michael@0 | 92 | #if 0 |
michael@0 | 93 | void mlp_process(const MLP *m, const opus_val16 *in, opus_val16 *out) |
michael@0 | 94 | { |
michael@0 | 95 | int j; |
michael@0 | 96 | opus_val16 hidden[MAX_NEURONS]; |
michael@0 | 97 | const opus_val16 *W = m->weights; |
michael@0 | 98 | /* Copy to tmp_in */ |
michael@0 | 99 | for (j=0;j<m->topo[1];j++) |
michael@0 | 100 | { |
michael@0 | 101 | int k; |
michael@0 | 102 | opus_val32 sum = SHL32(EXTEND32(*W++),8); |
michael@0 | 103 | for (k=0;k<m->topo[0];k++) |
michael@0 | 104 | sum = MAC16_16(sum, in[k],*W++); |
michael@0 | 105 | hidden[j] = tansig_approx(sum); |
michael@0 | 106 | } |
michael@0 | 107 | for (j=0;j<m->topo[2];j++) |
michael@0 | 108 | { |
michael@0 | 109 | int k; |
michael@0 | 110 | opus_val32 sum = SHL32(EXTEND32(*W++),14); |
michael@0 | 111 | for (k=0;k<m->topo[1];k++) |
michael@0 | 112 | sum = MAC16_16(sum, hidden[k], *W++); |
michael@0 | 113 | out[j] = tansig_approx(EXTRACT16(PSHR32(sum,17))); |
michael@0 | 114 | } |
michael@0 | 115 | } |
michael@0 | 116 | #else |
michael@0 | 117 | void mlp_process(const MLP *m, const float *in, float *out) |
michael@0 | 118 | { |
michael@0 | 119 | int j; |
michael@0 | 120 | float hidden[MAX_NEURONS]; |
michael@0 | 121 | const float *W = m->weights; |
michael@0 | 122 | /* Copy to tmp_in */ |
michael@0 | 123 | for (j=0;j<m->topo[1];j++) |
michael@0 | 124 | { |
michael@0 | 125 | int k; |
michael@0 | 126 | float sum = *W++; |
michael@0 | 127 | for (k=0;k<m->topo[0];k++) |
michael@0 | 128 | sum = sum + in[k]**W++; |
michael@0 | 129 | hidden[j] = tansig_approx(sum); |
michael@0 | 130 | } |
michael@0 | 131 | for (j=0;j<m->topo[2];j++) |
michael@0 | 132 | { |
michael@0 | 133 | int k; |
michael@0 | 134 | float sum = *W++; |
michael@0 | 135 | for (k=0;k<m->topo[1];k++) |
michael@0 | 136 | sum = sum + hidden[k]**W++; |
michael@0 | 137 | out[j] = tansig_approx(sum); |
michael@0 | 138 | } |
michael@0 | 139 | } |
michael@0 | 140 | #endif |