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 if (x<0) 79 { 80 x=-x; 81 sign=-1; 82 } 83 i = (int)floor(.5f+25*x); 84 x -= .04f*i; 85 y = tansig_table[i]; 86 dy = 1-y*y; 87 y = y + x*dy*(1 - y*x); 88 return sign*y; 89 } 90 #endif 91 92 #if 0 93 void mlp_process(const MLP *m, const opus_val16 *in, opus_val16 *out) 94 { 95 int j; 96 opus_val16 hidden[MAX_NEURONS]; 97 const opus_val16 *W = m->weights; 98 /* Copy to tmp_in */ 99 for (j=0;j<m->topo[1];j++) 100 { 101 int k; 102 opus_val32 sum = SHL32(EXTEND32(*W++),8); 103 for (k=0;k<m->topo[0];k++) 104 sum = MAC16_16(sum, in[k],*W++); 105 hidden[j] = tansig_approx(sum); 106 } 107 for (j=0;j<m->topo[2];j++) 108 { 109 int k; 110 opus_val32 sum = SHL32(EXTEND32(*W++),14); 111 for (k=0;k<m->topo[1];k++) 112 sum = MAC16_16(sum, hidden[k], *W++); 113 out[j] = tansig_approx(EXTRACT16(PSHR32(sum,17))); 114 } 115 } 116 #else 117 void mlp_process(const MLP *m, const float *in, float *out) 118 { 119 int j; 120 float hidden[MAX_NEURONS]; 121 const float *W = m->weights; 122 /* Copy to tmp_in */ 123 for (j=0;j<m->topo[1];j++) 124 { 125 int k; 126 float sum = *W++; 127 for (k=0;k<m->topo[0];k++) 128 sum = sum + in[k]**W++; 129 hidden[j] = tansig_approx(sum); 130 } 131 for (j=0;j<m->topo[2];j++) 132 { 133 int k; 134 float sum = *W++; 135 for (k=0;k<m->topo[1];k++) 136 sum = sum + hidden[k]**W++; 137 out[j] = tansig_approx(sum); 138 } 139 } 140 #endif 141