OLD | NEW |
1 /* Copyright (c) 2008-2011 Octasic Inc. | 1 /* Copyright (c) 2008-2011 Octasic Inc. |
2 Written by Jean-Marc Valin */ | 2 Written by Jean-Marc Valin */ |
3 /* | 3 /* |
4 Redistribution and use in source and binary forms, with or without | 4 Redistribution and use in source and binary forms, with or without |
5 modification, are permitted provided that the following conditions | 5 modification, are permitted provided that the following conditions |
6 are met: | 6 are met: |
7 | 7 |
8 - Redistributions of source code must retain the above copyright | 8 - Redistributions of source code must retain the above copyright |
9 notice, this list of conditions and the following disclaimer. | 9 notice, this list of conditions and the following disclaimer. |
10 | 10 |
(...skipping 23 matching lines...) Expand all Loading... |
34 | 34 |
35 #include <math.h> | 35 #include <math.h> |
36 #include "mlp.h" | 36 #include "mlp.h" |
37 #include "arch.h" | 37 #include "arch.h" |
38 #include "tansig_table.h" | 38 #include "tansig_table.h" |
39 #define MAX_NEURONS 100 | 39 #define MAX_NEURONS 100 |
40 | 40 |
41 #if 0 | 41 #if 0 |
42 static OPUS_INLINE opus_val16 tansig_approx(opus_val32 _x) /* Q19 */ | 42 static OPUS_INLINE opus_val16 tansig_approx(opus_val32 _x) /* Q19 */ |
43 { | 43 { |
44 » int i; | 44 int i; |
45 » opus_val16 xx; /* Q11 */ | 45 opus_val16 xx; /* Q11 */ |
46 » /*double x, y;*/ | 46 /*double x, y;*/ |
47 » opus_val16 dy, yy; /* Q14 */ | 47 opus_val16 dy, yy; /* Q14 */ |
48 » /*x = 1.9073e-06*_x;*/ | 48 /*x = 1.9073e-06*_x;*/ |
49 » if (_x>=QCONST32(8,19)) | 49 if (_x>=QCONST32(8,19)) |
50 » » return QCONST32(1.,14); | 50 return QCONST32(1.,14); |
51 » if (_x<=-QCONST32(8,19)) | 51 if (_x<=-QCONST32(8,19)) |
52 » » return -QCONST32(1.,14); | 52 return -QCONST32(1.,14); |
53 » xx = EXTRACT16(SHR32(_x, 8)); | 53 xx = EXTRACT16(SHR32(_x, 8)); |
54 » /*i = lrint(25*x);*/ | 54 /*i = lrint(25*x);*/ |
55 » i = SHR32(ADD32(1024,MULT16_16(25, xx)),11); | 55 i = SHR32(ADD32(1024,MULT16_16(25, xx)),11); |
56 » /*x -= .04*i;*/ | 56 /*x -= .04*i;*/ |
57 » xx -= EXTRACT16(SHR32(MULT16_16(20972,i),8)); | 57 xx -= EXTRACT16(SHR32(MULT16_16(20972,i),8)); |
58 » /*x = xx*(1./2048);*/ | 58 /*x = xx*(1./2048);*/ |
59 » /*y = tansig_table[250+i];*/ | 59 /*y = tansig_table[250+i];*/ |
60 » yy = tansig_table[250+i]; | 60 yy = tansig_table[250+i]; |
61 » /*y = yy*(1./16384);*/ | 61 /*y = yy*(1./16384);*/ |
62 » dy = 16384-MULT16_16_Q14(yy,yy); | 62 dy = 16384-MULT16_16_Q14(yy,yy); |
63 » yy = yy + MULT16_16_Q14(MULT16_16_Q11(xx,dy),(16384 - MULT16_16_Q11(yy,x
x))); | 63 yy = yy + MULT16_16_Q14(MULT16_16_Q11(xx,dy),(16384 - MULT16_16_Q11(yy,xx)))
; |
64 » return yy; | 64 return yy; |
65 } | 65 } |
66 #else | 66 #else |
67 /*extern const float tansig_table[501];*/ | 67 /*extern const float tansig_table[501];*/ |
68 static OPUS_INLINE float tansig_approx(float x) | 68 static OPUS_INLINE float tansig_approx(float x) |
69 { | 69 { |
70 » int i; | 70 int i; |
71 » float y, dy; | 71 float y, dy; |
72 » float sign=1; | 72 float sign=1; |
73 » /* Tests are reversed to catch NaNs */ | 73 /* Tests are reversed to catch NaNs */ |
74 if (!(x<8)) | 74 if (!(x<8)) |
75 return 1; | 75 return 1; |
76 if (!(x>-8)) | 76 if (!(x>-8)) |
77 return -1; | 77 return -1; |
78 » if (x<0) | 78 #ifndef FIXED_POINT |
79 » { | 79 /* Another check in case of -ffast-math */ |
80 » x=-x; | 80 if (celt_isnan(x)) |
81 » sign=-1; | 81 return 0; |
82 » } | 82 #endif |
83 » i = (int)floor(.5f+25*x); | 83 if (x<0) |
84 » x -= .04f*i; | 84 { |
85 » y = tansig_table[i]; | 85 x=-x; |
86 » dy = 1-y*y; | 86 sign=-1; |
87 » y = y + x*dy*(1 - y*x); | 87 } |
88 » return sign*y; | 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; |
89 } | 94 } |
90 #endif | 95 #endif |
91 | 96 |
92 #if 0 | 97 #if 0 |
93 void mlp_process(const MLP *m, const opus_val16 *in, opus_val16 *out) | 98 void mlp_process(const MLP *m, const opus_val16 *in, opus_val16 *out) |
94 { | 99 { |
95 » int j; | 100 int j; |
96 » opus_val16 hidden[MAX_NEURONS]; | 101 opus_val16 hidden[MAX_NEURONS]; |
97 » const opus_val16 *W = m->weights; | 102 const opus_val16 *W = m->weights; |
98 » /* Copy to tmp_in */ | 103 /* Copy to tmp_in */ |
99 » for (j=0;j<m->topo[1];j++) | 104 for (j=0;j<m->topo[1];j++) |
100 » { | 105 { |
101 » » int k; | 106 int k; |
102 » » opus_val32 sum = SHL32(EXTEND32(*W++),8); | 107 opus_val32 sum = SHL32(EXTEND32(*W++),8); |
103 » » for (k=0;k<m->topo[0];k++) | 108 for (k=0;k<m->topo[0];k++) |
104 » » » sum = MAC16_16(sum, in[k],*W++); | 109 sum = MAC16_16(sum, in[k],*W++); |
105 » » hidden[j] = tansig_approx(sum); | 110 hidden[j] = tansig_approx(sum); |
106 » } | 111 } |
107 » for (j=0;j<m->topo[2];j++) | 112 for (j=0;j<m->topo[2];j++) |
108 » { | 113 { |
109 » » int k; | 114 int k; |
110 » » opus_val32 sum = SHL32(EXTEND32(*W++),14); | 115 opus_val32 sum = SHL32(EXTEND32(*W++),14); |
111 » » for (k=0;k<m->topo[1];k++) | 116 for (k=0;k<m->topo[1];k++) |
112 » » » sum = MAC16_16(sum, hidden[k], *W++); | 117 sum = MAC16_16(sum, hidden[k], *W++); |
113 » » out[j] = tansig_approx(EXTRACT16(PSHR32(sum,17))); | 118 out[j] = tansig_approx(EXTRACT16(PSHR32(sum,17))); |
114 » } | 119 } |
115 } | 120 } |
116 #else | 121 #else |
117 void mlp_process(const MLP *m, const float *in, float *out) | 122 void mlp_process(const MLP *m, const float *in, float *out) |
118 { | 123 { |
119 int j; | 124 int j; |
120 float hidden[MAX_NEURONS]; | 125 float hidden[MAX_NEURONS]; |
121 const float *W = m->weights; | 126 const float *W = m->weights; |
122 /* Copy to tmp_in */ | 127 /* Copy to tmp_in */ |
123 for (j=0;j<m->topo[1];j++) | 128 for (j=0;j<m->topo[1];j++) |
124 { | 129 { |
125 int k; | 130 int k; |
126 float sum = *W++; | 131 float sum = *W++; |
127 for (k=0;k<m->topo[0];k++) | 132 for (k=0;k<m->topo[0];k++) |
128 sum = sum + in[k]**W++; | 133 sum = sum + in[k]**W++; |
129 hidden[j] = tansig_approx(sum); | 134 hidden[j] = tansig_approx(sum); |
130 } | 135 } |
131 for (j=0;j<m->topo[2];j++) | 136 for (j=0;j<m->topo[2];j++) |
132 { | 137 { |
133 int k; | 138 int k; |
134 float sum = *W++; | 139 float sum = *W++; |
135 for (k=0;k<m->topo[1];k++) | 140 for (k=0;k<m->topo[1];k++) |
136 sum = sum + hidden[k]**W++; | 141 sum = sum + hidden[k]**W++; |
137 out[j] = tansig_approx(sum); | 142 out[j] = tansig_approx(sum); |
138 } | 143 } |
139 } | 144 } |
140 #endif | 145 #endif |
OLD | NEW |