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