ex
Fork of mbed-os-example-mbed5-blinky by
DuerOS-Light-SDK-v1.1.0/duer-os-light/external/speex/libspeex/pseudofloat.h@47:9e361da97763, 2017-07-18 (annotated)
- Committer:
- TMBOY
- Date:
- Tue Jul 18 16:54:45 2017 +0800
- Revision:
- 47:9e361da97763
?
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| TMBOY | 47:9e361da97763 | 1 | /* Copyright (C) 2005 Jean-Marc Valin */ |
| TMBOY | 47:9e361da97763 | 2 | /** |
| TMBOY | 47:9e361da97763 | 3 | @file pseudofloat.h |
| TMBOY | 47:9e361da97763 | 4 | @brief Pseudo-floating point |
| TMBOY | 47:9e361da97763 | 5 | * This header file provides a lightweight floating point type for |
| TMBOY | 47:9e361da97763 | 6 | * use on fixed-point platforms when a large dynamic range is |
| TMBOY | 47:9e361da97763 | 7 | * required. The new type is not compatible with the 32-bit IEEE format, |
| TMBOY | 47:9e361da97763 | 8 | * it is not even remotely as accurate as 32-bit floats, and is not |
| TMBOY | 47:9e361da97763 | 9 | * even guaranteed to produce even remotely correct results for code |
| TMBOY | 47:9e361da97763 | 10 | * other than Speex. It makes all kinds of shortcuts that are acceptable |
| TMBOY | 47:9e361da97763 | 11 | * for Speex, but may not be acceptable for your application. You're |
| TMBOY | 47:9e361da97763 | 12 | * quite welcome to reuse this code and improve it, but don't assume |
| TMBOY | 47:9e361da97763 | 13 | * it works out of the box. Most likely, it doesn't. |
| TMBOY | 47:9e361da97763 | 14 | */ |
| TMBOY | 47:9e361da97763 | 15 | /* |
| TMBOY | 47:9e361da97763 | 16 | Redistribution and use in source and binary forms, with or without |
| TMBOY | 47:9e361da97763 | 17 | modification, are permitted provided that the following conditions |
| TMBOY | 47:9e361da97763 | 18 | are met: |
| TMBOY | 47:9e361da97763 | 19 | |
| TMBOY | 47:9e361da97763 | 20 | - Redistributions of source code must retain the above copyright |
| TMBOY | 47:9e361da97763 | 21 | notice, this list of conditions and the following disclaimer. |
| TMBOY | 47:9e361da97763 | 22 | |
| TMBOY | 47:9e361da97763 | 23 | - Redistributions in binary form must reproduce the above copyright |
| TMBOY | 47:9e361da97763 | 24 | notice, this list of conditions and the following disclaimer in the |
| TMBOY | 47:9e361da97763 | 25 | documentation and/or other materials provided with the distribution. |
| TMBOY | 47:9e361da97763 | 26 | |
| TMBOY | 47:9e361da97763 | 27 | - Neither the name of the Xiph.org Foundation nor the names of its |
| TMBOY | 47:9e361da97763 | 28 | contributors may be used to endorse or promote products derived from |
| TMBOY | 47:9e361da97763 | 29 | this software without specific prior written permission. |
| TMBOY | 47:9e361da97763 | 30 | |
| TMBOY | 47:9e361da97763 | 31 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| TMBOY | 47:9e361da97763 | 32 | ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| TMBOY | 47:9e361da97763 | 33 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| TMBOY | 47:9e361da97763 | 34 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR |
| TMBOY | 47:9e361da97763 | 35 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| TMBOY | 47:9e361da97763 | 36 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| TMBOY | 47:9e361da97763 | 37 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| TMBOY | 47:9e361da97763 | 38 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| TMBOY | 47:9e361da97763 | 39 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| TMBOY | 47:9e361da97763 | 40 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| TMBOY | 47:9e361da97763 | 41 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| TMBOY | 47:9e361da97763 | 42 | */ |
| TMBOY | 47:9e361da97763 | 43 | |
| TMBOY | 47:9e361da97763 | 44 | #ifndef PSEUDOFLOAT_H |
| TMBOY | 47:9e361da97763 | 45 | #define PSEUDOFLOAT_H |
| TMBOY | 47:9e361da97763 | 46 | |
| TMBOY | 47:9e361da97763 | 47 | #include "misc.h" |
| TMBOY | 47:9e361da97763 | 48 | #include "math_approx.h" |
| TMBOY | 47:9e361da97763 | 49 | #include <math.h> |
| TMBOY | 47:9e361da97763 | 50 | |
| TMBOY | 47:9e361da97763 | 51 | #ifdef FIXED_POINT |
| TMBOY | 47:9e361da97763 | 52 | |
| TMBOY | 47:9e361da97763 | 53 | typedef struct { |
| TMBOY | 47:9e361da97763 | 54 | spx_int16_t m; |
| TMBOY | 47:9e361da97763 | 55 | spx_int16_t e; |
| TMBOY | 47:9e361da97763 | 56 | } spx_float_t; |
| TMBOY | 47:9e361da97763 | 57 | |
| TMBOY | 47:9e361da97763 | 58 | static const spx_float_t FLOAT_ZERO = {0,0}; |
| TMBOY | 47:9e361da97763 | 59 | static const spx_float_t FLOAT_ONE = {16384,-14}; |
| TMBOY | 47:9e361da97763 | 60 | static const spx_float_t FLOAT_HALF = {16384,-15}; |
| TMBOY | 47:9e361da97763 | 61 | |
| TMBOY | 47:9e361da97763 | 62 | #define MIN(a,b) ((a)<(b)?(a):(b)) |
| TMBOY | 47:9e361da97763 | 63 | static inline spx_float_t PSEUDOFLOAT(spx_int32_t x) |
| TMBOY | 47:9e361da97763 | 64 | { |
| TMBOY | 47:9e361da97763 | 65 | int e=0; |
| TMBOY | 47:9e361da97763 | 66 | int sign=0; |
| TMBOY | 47:9e361da97763 | 67 | if (x<0) |
| TMBOY | 47:9e361da97763 | 68 | { |
| TMBOY | 47:9e361da97763 | 69 | sign = 1; |
| TMBOY | 47:9e361da97763 | 70 | x = -x; |
| TMBOY | 47:9e361da97763 | 71 | } |
| TMBOY | 47:9e361da97763 | 72 | if (x==0) |
| TMBOY | 47:9e361da97763 | 73 | { |
| TMBOY | 47:9e361da97763 | 74 | spx_float_t r = {0,0}; |
| TMBOY | 47:9e361da97763 | 75 | return r; |
| TMBOY | 47:9e361da97763 | 76 | } |
| TMBOY | 47:9e361da97763 | 77 | e = spx_ilog2(ABS32(x))-14; |
| TMBOY | 47:9e361da97763 | 78 | x = VSHR32(x, e); |
| TMBOY | 47:9e361da97763 | 79 | if (sign) |
| TMBOY | 47:9e361da97763 | 80 | { |
| TMBOY | 47:9e361da97763 | 81 | spx_float_t r; |
| TMBOY | 47:9e361da97763 | 82 | r.m = -x; |
| TMBOY | 47:9e361da97763 | 83 | r.e = e; |
| TMBOY | 47:9e361da97763 | 84 | return r; |
| TMBOY | 47:9e361da97763 | 85 | } |
| TMBOY | 47:9e361da97763 | 86 | else |
| TMBOY | 47:9e361da97763 | 87 | { |
| TMBOY | 47:9e361da97763 | 88 | spx_float_t r; |
| TMBOY | 47:9e361da97763 | 89 | r.m = x; |
| TMBOY | 47:9e361da97763 | 90 | r.e = e; |
| TMBOY | 47:9e361da97763 | 91 | return r; |
| TMBOY | 47:9e361da97763 | 92 | } |
| TMBOY | 47:9e361da97763 | 93 | } |
| TMBOY | 47:9e361da97763 | 94 | |
| TMBOY | 47:9e361da97763 | 95 | |
| TMBOY | 47:9e361da97763 | 96 | static inline spx_float_t FLOAT_ADD(spx_float_t a, spx_float_t b) |
| TMBOY | 47:9e361da97763 | 97 | { |
| TMBOY | 47:9e361da97763 | 98 | spx_float_t r; |
| TMBOY | 47:9e361da97763 | 99 | if (a.m==0) |
| TMBOY | 47:9e361da97763 | 100 | return b; |
| TMBOY | 47:9e361da97763 | 101 | else if (b.m==0) |
| TMBOY | 47:9e361da97763 | 102 | return a; |
| TMBOY | 47:9e361da97763 | 103 | if ((a).e > (b).e) |
| TMBOY | 47:9e361da97763 | 104 | { |
| TMBOY | 47:9e361da97763 | 105 | r.m = ((a).m>>1) + ((b).m>>MIN(15,(a).e-(b).e+1)); |
| TMBOY | 47:9e361da97763 | 106 | r.e = (a).e+1; |
| TMBOY | 47:9e361da97763 | 107 | } |
| TMBOY | 47:9e361da97763 | 108 | else |
| TMBOY | 47:9e361da97763 | 109 | { |
| TMBOY | 47:9e361da97763 | 110 | r.m = ((b).m>>1) + ((a).m>>MIN(15,(b).e-(a).e+1)); |
| TMBOY | 47:9e361da97763 | 111 | r.e = (b).e+1; |
| TMBOY | 47:9e361da97763 | 112 | } |
| TMBOY | 47:9e361da97763 | 113 | if (r.m>0) |
| TMBOY | 47:9e361da97763 | 114 | { |
| TMBOY | 47:9e361da97763 | 115 | if (r.m<16384) |
| TMBOY | 47:9e361da97763 | 116 | { |
| TMBOY | 47:9e361da97763 | 117 | r.m<<=1; |
| TMBOY | 47:9e361da97763 | 118 | r.e-=1; |
| TMBOY | 47:9e361da97763 | 119 | } |
| TMBOY | 47:9e361da97763 | 120 | } else { |
| TMBOY | 47:9e361da97763 | 121 | if (r.m>-16384) |
| TMBOY | 47:9e361da97763 | 122 | { |
| TMBOY | 47:9e361da97763 | 123 | r.m<<=1; |
| TMBOY | 47:9e361da97763 | 124 | r.e-=1; |
| TMBOY | 47:9e361da97763 | 125 | } |
| TMBOY | 47:9e361da97763 | 126 | } |
| TMBOY | 47:9e361da97763 | 127 | /*printf ("%f + %f = %f\n", REALFLOAT(a), REALFLOAT(b), REALFLOAT(r));*/ |
| TMBOY | 47:9e361da97763 | 128 | return r; |
| TMBOY | 47:9e361da97763 | 129 | } |
| TMBOY | 47:9e361da97763 | 130 | |
| TMBOY | 47:9e361da97763 | 131 | static inline spx_float_t FLOAT_SUB(spx_float_t a, spx_float_t b) |
| TMBOY | 47:9e361da97763 | 132 | { |
| TMBOY | 47:9e361da97763 | 133 | spx_float_t r; |
| TMBOY | 47:9e361da97763 | 134 | if (a.m==0) |
| TMBOY | 47:9e361da97763 | 135 | return b; |
| TMBOY | 47:9e361da97763 | 136 | else if (b.m==0) |
| TMBOY | 47:9e361da97763 | 137 | return a; |
| TMBOY | 47:9e361da97763 | 138 | if ((a).e > (b).e) |
| TMBOY | 47:9e361da97763 | 139 | { |
| TMBOY | 47:9e361da97763 | 140 | r.m = ((a).m>>1) - ((b).m>>MIN(15,(a).e-(b).e+1)); |
| TMBOY | 47:9e361da97763 | 141 | r.e = (a).e+1; |
| TMBOY | 47:9e361da97763 | 142 | } |
| TMBOY | 47:9e361da97763 | 143 | else |
| TMBOY | 47:9e361da97763 | 144 | { |
| TMBOY | 47:9e361da97763 | 145 | r.m = ((a).m>>MIN(15,(b).e-(a).e+1)) - ((b).m>>1); |
| TMBOY | 47:9e361da97763 | 146 | r.e = (b).e+1; |
| TMBOY | 47:9e361da97763 | 147 | } |
| TMBOY | 47:9e361da97763 | 148 | if (r.m>0) |
| TMBOY | 47:9e361da97763 | 149 | { |
| TMBOY | 47:9e361da97763 | 150 | if (r.m<16384) |
| TMBOY | 47:9e361da97763 | 151 | { |
| TMBOY | 47:9e361da97763 | 152 | r.m<<=1; |
| TMBOY | 47:9e361da97763 | 153 | r.e-=1; |
| TMBOY | 47:9e361da97763 | 154 | } |
| TMBOY | 47:9e361da97763 | 155 | } else { |
| TMBOY | 47:9e361da97763 | 156 | if (r.m>-16384) |
| TMBOY | 47:9e361da97763 | 157 | { |
| TMBOY | 47:9e361da97763 | 158 | r.m<<=1; |
| TMBOY | 47:9e361da97763 | 159 | r.e-=1; |
| TMBOY | 47:9e361da97763 | 160 | } |
| TMBOY | 47:9e361da97763 | 161 | } |
| TMBOY | 47:9e361da97763 | 162 | /*printf ("%f + %f = %f\n", REALFLOAT(a), REALFLOAT(b), REALFLOAT(r));*/ |
| TMBOY | 47:9e361da97763 | 163 | return r; |
| TMBOY | 47:9e361da97763 | 164 | } |
| TMBOY | 47:9e361da97763 | 165 | |
| TMBOY | 47:9e361da97763 | 166 | static inline int FLOAT_LT(spx_float_t a, spx_float_t b) |
| TMBOY | 47:9e361da97763 | 167 | { |
| TMBOY | 47:9e361da97763 | 168 | if (a.m==0) |
| TMBOY | 47:9e361da97763 | 169 | return b.m>0; |
| TMBOY | 47:9e361da97763 | 170 | else if (b.m==0) |
| TMBOY | 47:9e361da97763 | 171 | return a.m<0; |
| TMBOY | 47:9e361da97763 | 172 | if ((a).e > (b).e) |
| TMBOY | 47:9e361da97763 | 173 | return ((a).m>>1) < ((b).m>>MIN(15,(a).e-(b).e+1)); |
| TMBOY | 47:9e361da97763 | 174 | else |
| TMBOY | 47:9e361da97763 | 175 | return ((b).m>>1) > ((a).m>>MIN(15,(b).e-(a).e+1)); |
| TMBOY | 47:9e361da97763 | 176 | |
| TMBOY | 47:9e361da97763 | 177 | } |
| TMBOY | 47:9e361da97763 | 178 | |
| TMBOY | 47:9e361da97763 | 179 | static inline int FLOAT_GT(spx_float_t a, spx_float_t b) |
| TMBOY | 47:9e361da97763 | 180 | { |
| TMBOY | 47:9e361da97763 | 181 | return FLOAT_LT(b,a); |
| TMBOY | 47:9e361da97763 | 182 | } |
| TMBOY | 47:9e361da97763 | 183 | |
| TMBOY | 47:9e361da97763 | 184 | static inline spx_float_t FLOAT_MULT(spx_float_t a, spx_float_t b) |
| TMBOY | 47:9e361da97763 | 185 | { |
| TMBOY | 47:9e361da97763 | 186 | spx_float_t r; |
| TMBOY | 47:9e361da97763 | 187 | r.m = (spx_int16_t)((spx_int32_t)(a).m*(b).m>>15); |
| TMBOY | 47:9e361da97763 | 188 | r.e = (a).e+(b).e+15; |
| TMBOY | 47:9e361da97763 | 189 | if (r.m>0) |
| TMBOY | 47:9e361da97763 | 190 | { |
| TMBOY | 47:9e361da97763 | 191 | if (r.m<16384) |
| TMBOY | 47:9e361da97763 | 192 | { |
| TMBOY | 47:9e361da97763 | 193 | r.m<<=1; |
| TMBOY | 47:9e361da97763 | 194 | r.e-=1; |
| TMBOY | 47:9e361da97763 | 195 | } |
| TMBOY | 47:9e361da97763 | 196 | } else { |
| TMBOY | 47:9e361da97763 | 197 | if (r.m>-16384) |
| TMBOY | 47:9e361da97763 | 198 | { |
| TMBOY | 47:9e361da97763 | 199 | r.m<<=1; |
| TMBOY | 47:9e361da97763 | 200 | r.e-=1; |
| TMBOY | 47:9e361da97763 | 201 | } |
| TMBOY | 47:9e361da97763 | 202 | } |
| TMBOY | 47:9e361da97763 | 203 | /*printf ("%f * %f = %f\n", REALFLOAT(a), REALFLOAT(b), REALFLOAT(r));*/ |
| TMBOY | 47:9e361da97763 | 204 | return r; |
| TMBOY | 47:9e361da97763 | 205 | } |
| TMBOY | 47:9e361da97763 | 206 | |
| TMBOY | 47:9e361da97763 | 207 | static inline spx_float_t FLOAT_AMULT(spx_float_t a, spx_float_t b) |
| TMBOY | 47:9e361da97763 | 208 | { |
| TMBOY | 47:9e361da97763 | 209 | spx_float_t r; |
| TMBOY | 47:9e361da97763 | 210 | r.m = (spx_int16_t)((spx_int32_t)(a).m*(b).m>>15); |
| TMBOY | 47:9e361da97763 | 211 | r.e = (a).e+(b).e+15; |
| TMBOY | 47:9e361da97763 | 212 | return r; |
| TMBOY | 47:9e361da97763 | 213 | } |
| TMBOY | 47:9e361da97763 | 214 | |
| TMBOY | 47:9e361da97763 | 215 | |
| TMBOY | 47:9e361da97763 | 216 | static inline spx_float_t FLOAT_SHL(spx_float_t a, int b) |
| TMBOY | 47:9e361da97763 | 217 | { |
| TMBOY | 47:9e361da97763 | 218 | spx_float_t r; |
| TMBOY | 47:9e361da97763 | 219 | r.m = a.m; |
| TMBOY | 47:9e361da97763 | 220 | r.e = a.e+b; |
| TMBOY | 47:9e361da97763 | 221 | return r; |
| TMBOY | 47:9e361da97763 | 222 | } |
| TMBOY | 47:9e361da97763 | 223 | |
| TMBOY | 47:9e361da97763 | 224 | static inline spx_int16_t FLOAT_EXTRACT16(spx_float_t a) |
| TMBOY | 47:9e361da97763 | 225 | { |
| TMBOY | 47:9e361da97763 | 226 | if (a.e<0) |
| TMBOY | 47:9e361da97763 | 227 | return EXTRACT16((EXTEND32(a.m)+(EXTEND32(1)<<(-a.e-1)))>>-a.e); |
| TMBOY | 47:9e361da97763 | 228 | else |
| TMBOY | 47:9e361da97763 | 229 | return a.m<<a.e; |
| TMBOY | 47:9e361da97763 | 230 | } |
| TMBOY | 47:9e361da97763 | 231 | |
| TMBOY | 47:9e361da97763 | 232 | static inline spx_int32_t FLOAT_EXTRACT32(spx_float_t a) |
| TMBOY | 47:9e361da97763 | 233 | { |
| TMBOY | 47:9e361da97763 | 234 | if (a.e<0) |
| TMBOY | 47:9e361da97763 | 235 | return (EXTEND32(a.m)+(EXTEND32(1)<<(-a.e-1)))>>-a.e; |
| TMBOY | 47:9e361da97763 | 236 | else |
| TMBOY | 47:9e361da97763 | 237 | return EXTEND32(a.m)<<a.e; |
| TMBOY | 47:9e361da97763 | 238 | } |
| TMBOY | 47:9e361da97763 | 239 | |
| TMBOY | 47:9e361da97763 | 240 | static inline spx_int32_t FLOAT_MUL32(spx_float_t a, spx_word32_t b) |
| TMBOY | 47:9e361da97763 | 241 | { |
| TMBOY | 47:9e361da97763 | 242 | return VSHR32(MULT16_32_Q15(a.m, b),-a.e-15); |
| TMBOY | 47:9e361da97763 | 243 | } |
| TMBOY | 47:9e361da97763 | 244 | |
| TMBOY | 47:9e361da97763 | 245 | static inline spx_float_t FLOAT_MUL32U(spx_word32_t a, spx_word32_t b) |
| TMBOY | 47:9e361da97763 | 246 | { |
| TMBOY | 47:9e361da97763 | 247 | int e1, e2; |
| TMBOY | 47:9e361da97763 | 248 | spx_float_t r; |
| TMBOY | 47:9e361da97763 | 249 | if (a==0 || b==0) |
| TMBOY | 47:9e361da97763 | 250 | { |
| TMBOY | 47:9e361da97763 | 251 | return FLOAT_ZERO; |
| TMBOY | 47:9e361da97763 | 252 | } |
| TMBOY | 47:9e361da97763 | 253 | e1 = spx_ilog2(ABS32(a)); |
| TMBOY | 47:9e361da97763 | 254 | a = VSHR32(a, e1-14); |
| TMBOY | 47:9e361da97763 | 255 | e2 = spx_ilog2(ABS32(b)); |
| TMBOY | 47:9e361da97763 | 256 | b = VSHR32(b, e2-14); |
| TMBOY | 47:9e361da97763 | 257 | r.m = MULT16_16_Q15(a,b); |
| TMBOY | 47:9e361da97763 | 258 | r.e = e1+e2-13; |
| TMBOY | 47:9e361da97763 | 259 | return r; |
| TMBOY | 47:9e361da97763 | 260 | } |
| TMBOY | 47:9e361da97763 | 261 | |
| TMBOY | 47:9e361da97763 | 262 | /* Do NOT attempt to divide by a negative number */ |
| TMBOY | 47:9e361da97763 | 263 | static inline spx_float_t FLOAT_DIV32_FLOAT(spx_word32_t a, spx_float_t b) |
| TMBOY | 47:9e361da97763 | 264 | { |
| TMBOY | 47:9e361da97763 | 265 | int e=0; |
| TMBOY | 47:9e361da97763 | 266 | spx_float_t r; |
| TMBOY | 47:9e361da97763 | 267 | if (a==0) |
| TMBOY | 47:9e361da97763 | 268 | { |
| TMBOY | 47:9e361da97763 | 269 | return FLOAT_ZERO; |
| TMBOY | 47:9e361da97763 | 270 | } |
| TMBOY | 47:9e361da97763 | 271 | e = spx_ilog2(ABS32(a))-spx_ilog2(b.m-1)-15; |
| TMBOY | 47:9e361da97763 | 272 | a = VSHR32(a, e); |
| TMBOY | 47:9e361da97763 | 273 | if (ABS32(a)>=SHL32(EXTEND32(b.m-1),15)) |
| TMBOY | 47:9e361da97763 | 274 | { |
| TMBOY | 47:9e361da97763 | 275 | a >>= 1; |
| TMBOY | 47:9e361da97763 | 276 | e++; |
| TMBOY | 47:9e361da97763 | 277 | } |
| TMBOY | 47:9e361da97763 | 278 | r.m = DIV32_16(a,b.m); |
| TMBOY | 47:9e361da97763 | 279 | r.e = e-b.e; |
| TMBOY | 47:9e361da97763 | 280 | return r; |
| TMBOY | 47:9e361da97763 | 281 | } |
| TMBOY | 47:9e361da97763 | 282 | |
| TMBOY | 47:9e361da97763 | 283 | |
| TMBOY | 47:9e361da97763 | 284 | /* Do NOT attempt to divide by a negative number */ |
| TMBOY | 47:9e361da97763 | 285 | static inline spx_float_t FLOAT_DIV32(spx_word32_t a, spx_word32_t b) |
| TMBOY | 47:9e361da97763 | 286 | { |
| TMBOY | 47:9e361da97763 | 287 | int e0=0,e=0; |
| TMBOY | 47:9e361da97763 | 288 | spx_float_t r; |
| TMBOY | 47:9e361da97763 | 289 | if (a==0) |
| TMBOY | 47:9e361da97763 | 290 | { |
| TMBOY | 47:9e361da97763 | 291 | return FLOAT_ZERO; |
| TMBOY | 47:9e361da97763 | 292 | } |
| TMBOY | 47:9e361da97763 | 293 | if (b>32767) |
| TMBOY | 47:9e361da97763 | 294 | { |
| TMBOY | 47:9e361da97763 | 295 | e0 = spx_ilog2(b)-14; |
| TMBOY | 47:9e361da97763 | 296 | b = VSHR32(b, e0); |
| TMBOY | 47:9e361da97763 | 297 | e0 = -e0; |
| TMBOY | 47:9e361da97763 | 298 | } |
| TMBOY | 47:9e361da97763 | 299 | e = spx_ilog2(ABS32(a))-spx_ilog2(b-1)-15; |
| TMBOY | 47:9e361da97763 | 300 | a = VSHR32(a, e); |
| TMBOY | 47:9e361da97763 | 301 | if (ABS32(a)>=SHL32(EXTEND32(b-1),15)) |
| TMBOY | 47:9e361da97763 | 302 | { |
| TMBOY | 47:9e361da97763 | 303 | a >>= 1; |
| TMBOY | 47:9e361da97763 | 304 | e++; |
| TMBOY | 47:9e361da97763 | 305 | } |
| TMBOY | 47:9e361da97763 | 306 | e += e0; |
| TMBOY | 47:9e361da97763 | 307 | r.m = DIV32_16(a,b); |
| TMBOY | 47:9e361da97763 | 308 | r.e = e; |
| TMBOY | 47:9e361da97763 | 309 | return r; |
| TMBOY | 47:9e361da97763 | 310 | } |
| TMBOY | 47:9e361da97763 | 311 | |
| TMBOY | 47:9e361da97763 | 312 | /* Do NOT attempt to divide by a negative number */ |
| TMBOY | 47:9e361da97763 | 313 | static inline spx_float_t FLOAT_DIVU(spx_float_t a, spx_float_t b) |
| TMBOY | 47:9e361da97763 | 314 | { |
| TMBOY | 47:9e361da97763 | 315 | int e=0; |
| TMBOY | 47:9e361da97763 | 316 | spx_int32_t num; |
| TMBOY | 47:9e361da97763 | 317 | spx_float_t r; |
| TMBOY | 47:9e361da97763 | 318 | if (b.m<=0) |
| TMBOY | 47:9e361da97763 | 319 | { |
| TMBOY | 47:9e361da97763 | 320 | speex_warning_int("Attempted to divide by", b.m); |
| TMBOY | 47:9e361da97763 | 321 | return FLOAT_ONE; |
| TMBOY | 47:9e361da97763 | 322 | } |
| TMBOY | 47:9e361da97763 | 323 | num = a.m; |
| TMBOY | 47:9e361da97763 | 324 | a.m = ABS16(a.m); |
| TMBOY | 47:9e361da97763 | 325 | while (a.m >= b.m) |
| TMBOY | 47:9e361da97763 | 326 | { |
| TMBOY | 47:9e361da97763 | 327 | e++; |
| TMBOY | 47:9e361da97763 | 328 | a.m >>= 1; |
| TMBOY | 47:9e361da97763 | 329 | } |
| TMBOY | 47:9e361da97763 | 330 | num = num << (15-e); |
| TMBOY | 47:9e361da97763 | 331 | r.m = DIV32_16(num,b.m); |
| TMBOY | 47:9e361da97763 | 332 | r.e = a.e-b.e-15+e; |
| TMBOY | 47:9e361da97763 | 333 | return r; |
| TMBOY | 47:9e361da97763 | 334 | } |
| TMBOY | 47:9e361da97763 | 335 | |
| TMBOY | 47:9e361da97763 | 336 | static inline spx_float_t FLOAT_SQRT(spx_float_t a) |
| TMBOY | 47:9e361da97763 | 337 | { |
| TMBOY | 47:9e361da97763 | 338 | spx_float_t r; |
| TMBOY | 47:9e361da97763 | 339 | spx_int32_t m; |
| TMBOY | 47:9e361da97763 | 340 | m = SHL32(EXTEND32(a.m), 14); |
| TMBOY | 47:9e361da97763 | 341 | r.e = a.e - 14; |
| TMBOY | 47:9e361da97763 | 342 | if (r.e & 1) |
| TMBOY | 47:9e361da97763 | 343 | { |
| TMBOY | 47:9e361da97763 | 344 | r.e -= 1; |
| TMBOY | 47:9e361da97763 | 345 | m <<= 1; |
| TMBOY | 47:9e361da97763 | 346 | } |
| TMBOY | 47:9e361da97763 | 347 | r.e >>= 1; |
| TMBOY | 47:9e361da97763 | 348 | r.m = spx_sqrt(m); |
| TMBOY | 47:9e361da97763 | 349 | return r; |
| TMBOY | 47:9e361da97763 | 350 | } |
| TMBOY | 47:9e361da97763 | 351 | |
| TMBOY | 47:9e361da97763 | 352 | #else |
| TMBOY | 47:9e361da97763 | 353 | |
| TMBOY | 47:9e361da97763 | 354 | #define spx_float_t float |
| TMBOY | 47:9e361da97763 | 355 | #define FLOAT_ZERO 0.f |
| TMBOY | 47:9e361da97763 | 356 | #define FLOAT_ONE 1.f |
| TMBOY | 47:9e361da97763 | 357 | #define FLOAT_HALF 0.5f |
| TMBOY | 47:9e361da97763 | 358 | #define PSEUDOFLOAT(x) (x) |
| TMBOY | 47:9e361da97763 | 359 | #define FLOAT_MULT(a,b) ((a)*(b)) |
| TMBOY | 47:9e361da97763 | 360 | #define FLOAT_AMULT(a,b) ((a)*(b)) |
| TMBOY | 47:9e361da97763 | 361 | #define FLOAT_MUL32(a,b) ((a)*(b)) |
| TMBOY | 47:9e361da97763 | 362 | #define FLOAT_DIV32(a,b) ((a)/(b)) |
| TMBOY | 47:9e361da97763 | 363 | #define FLOAT_EXTRACT16(a) (a) |
| TMBOY | 47:9e361da97763 | 364 | #define FLOAT_EXTRACT32(a) (a) |
| TMBOY | 47:9e361da97763 | 365 | #define FLOAT_ADD(a,b) ((a)+(b)) |
| TMBOY | 47:9e361da97763 | 366 | #define FLOAT_SUB(a,b) ((a)-(b)) |
| TMBOY | 47:9e361da97763 | 367 | #define REALFLOAT(x) (x) |
| TMBOY | 47:9e361da97763 | 368 | #define FLOAT_DIV32_FLOAT(a,b) ((a)/(b)) |
| TMBOY | 47:9e361da97763 | 369 | #define FLOAT_MUL32U(a,b) ((a)*(b)) |
| TMBOY | 47:9e361da97763 | 370 | #define FLOAT_SHL(a,b) (a) |
| TMBOY | 47:9e361da97763 | 371 | #define FLOAT_LT(a,b) ((a)<(b)) |
| TMBOY | 47:9e361da97763 | 372 | #define FLOAT_GT(a,b) ((a)>(b)) |
| TMBOY | 47:9e361da97763 | 373 | #define FLOAT_DIVU(a,b) ((a)/(b)) |
| TMBOY | 47:9e361da97763 | 374 | #define FLOAT_SQRT(a) (spx_sqrt(a)) |
| TMBOY | 47:9e361da97763 | 375 | |
| TMBOY | 47:9e361da97763 | 376 | #endif |
| TMBOY | 47:9e361da97763 | 377 | |
| TMBOY | 47:9e361da97763 | 378 | #endif |
