| 1 | /* Copyright (C) 2002 Jean-Marc Valin |
| 2 | File: math_approx.c |
| 3 | Various math approximation functions for Speex |
| 4 | |
| 5 | Redistribution and use in source and binary forms, with or without |
| 6 | modification, are permitted provided that the following conditions |
| 7 | are met: |
| 8 | |
| 9 | - Redistributions of source code must retain the above copyright |
| 10 | notice, this list of conditions and the following disclaimer. |
| 11 | |
| 12 | - Redistributions in binary form must reproduce the above copyright |
| 13 | notice, this list of conditions and the following disclaimer in the |
| 14 | documentation and/or other materials provided with the distribution. |
| 15 | |
| 16 | - Neither the name of the Xiph.org Foundation nor the names of its |
| 17 | contributors may be used to endorse or promote products derived from |
| 18 | this software without specific prior written permission. |
| 19 | |
| 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR |
| 24 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 25 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 26 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 27 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 28 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 29 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 30 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | */ |
| 32 | |
| 33 | #ifdef HAVE_CONFIG_H |
| 34 | #include "config.h" |
| 35 | #endif |
| 36 | |
| 37 | #include "math_approx.h" |
| 38 | #include "misc.h" |
| 39 | |
| 40 | #ifdef FIXED_POINT |
| 41 | |
| 42 | /* sqrt(x) ~= 0.22178 + 1.29227*x - 0.77070*x^2 + 0.25723*x^3 (for .25 < x < 1) */ |
| 43 | #define C0 3634 |
| 44 | #define C1 21173 |
| 45 | #define C2 -12627 |
| 46 | #define C3 4215 |
| 47 | |
| 48 | spx_word16_t spx_sqrt(spx_word32_t x) |
| 49 | { |
| 50 | int k=0; |
| 51 | spx_word32_t rt; |
| 52 | |
| 53 | if (x==0) |
| 54 | return 0; |
| 55 | #if 1 |
| 56 | if (x>16777216) |
| 57 | { |
| 58 | x>>=10; |
| 59 | k+=5; |
| 60 | } |
| 61 | if (x>1048576) |
| 62 | { |
| 63 | x>>=6; |
| 64 | k+=3; |
| 65 | } |
| 66 | if (x>262144) |
| 67 | { |
| 68 | x>>=4; |
| 69 | k+=2; |
| 70 | } |
| 71 | if (x>32768) |
| 72 | { |
| 73 | x>>=2; |
| 74 | k+=1; |
| 75 | } |
| 76 | if (x>16384) |
| 77 | { |
| 78 | x>>=2; |
| 79 | k+=1; |
| 80 | } |
| 81 | #else |
| 82 | while (x>16384) |
| 83 | { |
| 84 | x>>=2; |
| 85 | k++; |
| 86 | } |
| 87 | #endif |
| 88 | while (x<4096) |
| 89 | { |
| 90 | x<<=2; |
| 91 | k--; |
| 92 | } |
| 93 | rt = ADD16(C0, MULT16_16_Q14(x, ADD16(C1, MULT16_16_Q14(x, ADD16(C2, MULT16_16_Q14(x, (C3))))))); |
| 94 | if (k>0) |
| 95 | rt <<= k; |
| 96 | else |
| 97 | rt >>= -k; |
| 98 | rt >>=7; |
| 99 | return rt; |
| 100 | } |
| 101 | |
| 102 | /* log(x) ~= -2.18151 + 4.20592*x - 2.88938*x^2 + 0.86535*x^3 (for .5 < x < 1) */ |
| 103 | |
| 104 | |
| 105 | #define A1 16469 |
| 106 | #define A2 2242 |
| 107 | #define A3 1486 |
| 108 | |
| 109 | spx_word16_t spx_acos(spx_word16_t x) |
| 110 | { |
| 111 | int s=0; |
| 112 | spx_word16_t ret; |
| 113 | spx_word16_t sq; |
| 114 | if (x<0) |
| 115 | { |
| 116 | s=1; |
| 117 | x = NEG16(x); |
| 118 | } |
| 119 | x = SUB16(16384,x); |
| 120 | |
| 121 | x = x >> 1; |
| 122 | sq = MULT16_16_Q13(x, ADD16(A1, MULT16_16_Q13(x, ADD16(A2, MULT16_16_Q13(x, (A3)))))); |
| 123 | ret = spx_sqrt(SHL32(EXTEND32(sq),13)); |
| 124 | |
| 125 | /*ret = spx_sqrt(67108864*(-1.6129e-04 + 2.0104e+00*f + 2.7373e-01*f*f + 1.8136e-01*f*f*f));*/ |
| 126 | if (s) |
| 127 | ret = SUB16(25736,ret); |
| 128 | return ret; |
| 129 | } |
| 130 | |
| 131 | #endif |