Benny Prijono | eb30bf5 | 2006-03-04 20:43:52 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2005 Jean-Marc Valin */ |
| 2 | /** |
| 3 | @file pseudofloat.h |
| 4 | @brief Pseudo-floating point |
| 5 | */ |
| 6 | /* |
| 7 | Redistribution and use in source and binary forms, with or without |
| 8 | modification, are permitted provided that the following conditions |
| 9 | are met: |
| 10 | |
| 11 | - Redistributions of source code must retain the above copyright |
| 12 | notice, this list of conditions and the following disclaimer. |
| 13 | |
| 14 | - Redistributions in binary form must reproduce the above copyright |
| 15 | notice, this list of conditions and the following disclaimer in the |
| 16 | documentation and/or other materials provided with the distribution. |
| 17 | |
| 18 | - Neither the name of the Xiph.org Foundation nor the names of its |
| 19 | contributors may be used to endorse or promote products derived from |
| 20 | this software without specific prior written permission. |
| 21 | |
| 22 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 23 | ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 24 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 25 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR |
| 26 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 27 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 28 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 29 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 30 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 31 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 32 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 33 | */ |
| 34 | |
| 35 | #ifndef PSEUDOFLOAT_H |
| 36 | #define PSEUDOFLOAT_H |
| 37 | |
| 38 | #include "misc.h" |
| 39 | #include <math.h> |
| 40 | |
| 41 | #ifdef FIXED_POINT |
| 42 | |
| 43 | typedef struct { |
| 44 | spx_int16_t m; |
| 45 | spx_int16_t e; |
| 46 | } spx_float_t; |
| 47 | |
| 48 | #define FLOAT_ZERO ((spx_float_t){0,0}) |
| 49 | #define FLOAT_ONE ((spx_float_t){16384,-14}) |
| 50 | #define FLOAT_HALF ((spx_float_t){16384,-15}) |
| 51 | |
| 52 | #define MIN(a,b) ((a)<(b)?(a):(b)) |
| 53 | static inline spx_float_t PSEUDOFLOAT(spx_int32_t x) |
| 54 | { |
| 55 | int e=0; |
| 56 | int sign=0; |
| 57 | if (x<0) |
| 58 | { |
| 59 | sign = 1; |
| 60 | x = -x; |
| 61 | } |
| 62 | if (x==0) |
| 63 | return (spx_float_t) {0,0}; |
| 64 | while (x>32767) |
| 65 | { |
| 66 | x >>= 1; |
| 67 | /*x *= .5;*/ |
| 68 | e++; |
| 69 | } |
| 70 | while (x<16383) |
| 71 | { |
| 72 | x <<= 1; |
| 73 | /*x *= 2;*/ |
| 74 | e--; |
| 75 | } |
| 76 | if (sign) |
| 77 | return (spx_float_t) {-x,e}; |
| 78 | else |
| 79 | return (spx_float_t) {x,e}; |
| 80 | } |
| 81 | |
| 82 | |
| 83 | static inline spx_float_t FLOAT_ADD(spx_float_t a, spx_float_t b) |
| 84 | { |
| 85 | spx_float_t r; |
| 86 | if (a.m==0) |
| 87 | return b; |
| 88 | else if (b.m==0) |
| 89 | return a; |
| 90 | r = (a).e > (b).e ? (spx_float_t) {((a).m>>1) + ((b).m>>MIN(15,(a).e-(b).e+1)),(a).e+1} : (spx_float_t) {((b).m>>1) + ((a).m>>MIN(15,(b).e-(a).e+1)),(b).e+1}; |
| 91 | if (r.m>0) |
| 92 | { |
| 93 | if (r.m<16384) |
| 94 | { |
| 95 | r.m<<=1; |
| 96 | r.e-=1; |
| 97 | } |
| 98 | } else { |
| 99 | if (r.m>-16384) |
| 100 | { |
| 101 | r.m<<=1; |
| 102 | r.e-=1; |
| 103 | } |
| 104 | } |
| 105 | /*printf ("%f + %f = %f\n", REALFLOAT(a), REALFLOAT(b), REALFLOAT(r));*/ |
| 106 | return r; |
| 107 | } |
| 108 | |
| 109 | static inline spx_float_t FLOAT_SUB(spx_float_t a, spx_float_t b) |
| 110 | { |
| 111 | spx_float_t r; |
| 112 | if (a.m==0) |
| 113 | return b; |
| 114 | else if (b.m==0) |
| 115 | return a; |
| 116 | r = (a).e > (b).e ? (spx_float_t) {((a).m>>1) - ((b).m>>MIN(15,(a).e-(b).e+1)),(a).e+1} : (spx_float_t) {((a).m>>MIN(15,(b).e-(a).e+1)) - ((b).m>>1) ,(b).e+1}; |
| 117 | if (r.m>0) |
| 118 | { |
| 119 | if (r.m<16384) |
| 120 | { |
| 121 | r.m<<=1; |
| 122 | r.e-=1; |
| 123 | } |
| 124 | } else { |
| 125 | if (r.m>-16384) |
| 126 | { |
| 127 | r.m<<=1; |
| 128 | r.e-=1; |
| 129 | } |
| 130 | } |
| 131 | /*printf ("%f + %f = %f\n", REALFLOAT(a), REALFLOAT(b), REALFLOAT(r));*/ |
| 132 | return r; |
| 133 | } |
| 134 | |
| 135 | static inline int FLOAT_LT(spx_float_t a, spx_float_t b) |
| 136 | { |
| 137 | if (a.m==0) |
| 138 | return b.m<0; |
| 139 | else if (b.m==0) |
| 140 | return a.m>0; |
| 141 | if ((a).e > (b).e) |
| 142 | return ((a).m>>1) < ((b).m>>MIN(15,(a).e-(b).e+1)); |
| 143 | else |
| 144 | return ((b).m>>1) > ((a).m>>MIN(15,(b).e-(a).e+1)); |
| 145 | |
| 146 | } |
| 147 | |
| 148 | static inline int FLOAT_GT(spx_float_t a, spx_float_t b) |
| 149 | { |
| 150 | return FLOAT_LT(b,a); |
| 151 | } |
| 152 | |
| 153 | static inline spx_float_t FLOAT_MULT(spx_float_t a, spx_float_t b) |
| 154 | { |
| 155 | spx_float_t r = (spx_float_t) {(spx_int16_t)((spx_int32_t)(a).m*(b).m>>15), (a).e+(b).e+15}; |
| 156 | if (r.m>0) |
| 157 | { |
| 158 | if (r.m<16384) |
| 159 | { |
| 160 | r.m<<=1; |
| 161 | r.e-=1; |
| 162 | } |
| 163 | } else { |
| 164 | if (r.m>-16384) |
| 165 | { |
| 166 | r.m<<=1; |
| 167 | r.e-=1; |
| 168 | } |
| 169 | } |
| 170 | /*printf ("%f * %f = %f\n", REALFLOAT(a), REALFLOAT(b), REALFLOAT(r));*/ |
| 171 | return r; |
| 172 | } |
| 173 | |
| 174 | |
| 175 | static inline spx_float_t FLOAT_SHL(spx_float_t a, int b) |
| 176 | { |
| 177 | return (spx_float_t) {a.m,a.e+b}; |
| 178 | } |
| 179 | |
| 180 | static inline spx_int16_t FLOAT_EXTRACT16(spx_float_t a) |
| 181 | { |
| 182 | if (a.e<0) |
| 183 | return (a.m+(1<<(-a.e-1)))>>-a.e; |
| 184 | else |
| 185 | return a.m<<a.e; |
| 186 | } |
| 187 | |
| 188 | static inline spx_int32_t FLOAT_MUL32(spx_float_t a, spx_word32_t b) |
| 189 | { |
| 190 | if (a.e<-15) |
| 191 | return SHR32(MULT16_32_Q15(a.m, b),-a.e-15); |
| 192 | else |
| 193 | return SHL32(MULT16_32_Q15(a.m, b),15+a.e); |
| 194 | } |
| 195 | |
| 196 | static inline spx_float_t FLOAT_MUL32U(spx_word32_t a, spx_word32_t b) |
| 197 | { |
| 198 | int e=0; |
| 199 | /* FIXME: Handle the sign */ |
| 200 | if (a==0) |
| 201 | return (spx_float_t) {0,0}; |
| 202 | while (a>32767) |
| 203 | { |
| 204 | a >>= 1; |
| 205 | e++; |
| 206 | } |
| 207 | while (a<16384) |
| 208 | { |
| 209 | a <<= 1; |
| 210 | e--; |
| 211 | } |
| 212 | while (b>32767) |
| 213 | { |
| 214 | b >>= 1; |
| 215 | e++; |
| 216 | } |
| 217 | while (b<16384) |
| 218 | { |
| 219 | b <<= 1; |
| 220 | e--; |
| 221 | } |
| 222 | return (spx_float_t) {MULT16_16_Q15(a,b),e+15}; |
| 223 | } |
| 224 | |
| 225 | static inline spx_float_t FLOAT_DIV32_FLOAT(spx_word32_t a, spx_float_t b) |
| 226 | { |
| 227 | int e=0; |
| 228 | /* FIXME: Handle the sign */ |
| 229 | if (a==0) |
| 230 | return (spx_float_t) {0,0}; |
| 231 | while (a<SHL32(b.m,14)) |
| 232 | { |
| 233 | a <<= 1; |
| 234 | e--; |
| 235 | } |
| 236 | while (a>=SHL32(b.m-1,15)) |
| 237 | { |
| 238 | a >>= 1; |
| 239 | e++; |
| 240 | } |
| 241 | return (spx_float_t) {DIV32_16(a,b.m),e-b.e}; |
| 242 | } |
| 243 | |
| 244 | |
| 245 | static inline spx_float_t FLOAT_DIV32(spx_word32_t a, spx_word32_t b) |
| 246 | { |
| 247 | int e=0; |
| 248 | /* FIXME: Handle the sign */ |
| 249 | if (a==0) |
| 250 | return (spx_float_t) {0,0}; |
| 251 | while (b>32767) |
| 252 | { |
| 253 | b >>= 1; |
| 254 | e--; |
| 255 | } |
| 256 | while (a<SHL32(b,14)) |
| 257 | { |
| 258 | a <<= 1; |
| 259 | e--; |
| 260 | } |
| 261 | while (a>=SHL32(b-1,15)) |
| 262 | { |
| 263 | a >>= 1; |
| 264 | e++; |
| 265 | } |
| 266 | return (spx_float_t) {DIV32_16(a,b),e}; |
| 267 | } |
| 268 | |
| 269 | static inline spx_float_t FLOAT_DIVU(spx_float_t a, spx_float_t b) |
| 270 | { |
| 271 | int e=0; |
| 272 | spx_int32_t num; |
| 273 | num = a.m; |
| 274 | while (a.m >= b.m) |
| 275 | { |
| 276 | e++; |
| 277 | a.m >>= 1; |
| 278 | } |
| 279 | num = num << (15-e); |
| 280 | return (spx_float_t) {DIV32_16(num,b.m),a.e-b.e-15+e}; |
| 281 | } |
| 282 | |
| 283 | #else |
| 284 | |
| 285 | #define spx_float_t float |
| 286 | #define FLOAT_ZERO 0.f |
| 287 | #define FLOAT_ONE 1.f |
| 288 | #define FLOAT_HALF 0.5f |
| 289 | #define PSEUDOFLOAT(x) (x) |
| 290 | #define FLOAT_MULT(a,b) ((a)*(b)) |
| 291 | #define FLOAT_MUL32(a,b) ((a)*(b)) |
| 292 | #define FLOAT_DIV32(a,b) ((a)/(b)) |
| 293 | #define FLOAT_EXTRACT16(a) (a) |
| 294 | #define FLOAT_ADD(a,b) ((a)+(b)) |
| 295 | #define FLOAT_SUB(a,b) ((a)-(b)) |
| 296 | #define REALFLOAT(x) (x) |
| 297 | #define FLOAT_DIV32_FLOAT(a,b) ((a)/(b)) |
| 298 | #define FLOAT_MUL32U(a,b) ((a)*(b)) |
| 299 | #define FLOAT_SHL(a,b) (a) |
| 300 | #define FLOAT_LT(a,b) ((a)<(b)) |
| 301 | #define FLOAT_GT(a,b) ((a)>(b)) |
| 302 | #define FLOAT_DIVU(a,b) ((a)/(b)) |
| 303 | |
| 304 | #endif |
| 305 | |
| 306 | #endif |