1 /*
2 Copyright (c) 2010, Warmux Team
3 
4 Large parts of the code are from the fixed point library of Markus Trenkwalder
5 Copyright (c) 2007, Markus Trenkwalder
6 
7 Portions taken from the Vicent 3D rendering library
8 Copyright (c) 2004, David Blythe, Hans Martin Will
9 
10 All rights reserved.
11 
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions are met:
14 
15 * Redistributions of source code must retain the above copyright notice,
16   this list of conditions and the following disclaimer.
17 
18 * Redistributions in binary form must reproduce the above copyright notice,
19   this list of conditions and the following disclaimer in the documentation
20   and/or other materials provided with the distribution.
21 
22 * Neither the name of the library's copyright owner nor the names of its
23   contributors may be used to endorse or promote products derived from this
24   software without specific prior written permission.
25 
26 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
30 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 */
38 
39 #include <stdio.h>
40 #include "fixed_func.h"
41 
42 namespace fp {
43 
44 static const int32_t FIX16_2PI     = float2fix<16>(2*M_PI);
45 static const int32_t FIX16_HALF_PI = float2fix<16>(0.5*M_PI);
46 static const int32_t FIX16_R2PI    = float2fix<16>(1/(2*M_PI));
47 
48 static const uint16_t sin_tab[] = {
49 #include "fixsintab.h"
50 };
51 
52 static const int32_t asin_tab[] = {
53 #include "fixasintab.h"
54 };
55 
56 static const uint16_t atan_tab[] = {
57 #include "fixatantab.h"
58 };
59 
60 
fixcos16(fint_t a)61 fint_t fixcos16(fint_t a)
62 {
63   fint_t v;
64   /* reduce to [0,1) */
65   while (a < 0) a += FIX16_2PI;
66   a = fixmul<16>(a, FIX16_R2PI);
67   a += 0x4000;
68 
69   /* now in the range [0, 0xffff], reduce to [0, 0xfff] */
70   a >>= 4;
71 
72   v = (a & 0x400) ? sin_tab[0x3ff - (a & 0x3ff)] : sin_tab[a & 0x3ff];
73   v = fixmul<16>(v, 1 << 16);
74   return (a & 0x800) ? -v : v;
75 }
76 
fixsin16(fint_t a)77 fint_t fixsin16(fint_t a)
78 {
79   fint_t v;
80 
81   /* reduce to [0,1) */
82   while (a < 0) a += FIX16_2PI;
83   a = fixmul<16>(a, FIX16_R2PI);
84 
85   /* now in the range [0, 0xffff], reduce to [0, 0xfff] */
86   a >>= 4;
87 
88   v = (a & 0x400) ? sin_tab[0x3ff - (a & 0x3ff)] : sin_tab[a & 0x3ff];
89   v = fixmul<16>(v, 1 << 16);
90   return (a & 0x800) ? -v : v;
91 }
92 
fixacos16(fint_t a)93 fint_t fixacos16(fint_t a)
94 {
95   return FIX16_HALF_PI - fixasin16(a);
96 }
97 
fixasin16(fint_t a)98 fint_t fixasin16(fint_t a)
99 {
100   if (a > 0) {
101     if (a >= 0x10000)
102       a = 0x10000;
103     return asin_tab[a];;
104   } else {
105     if (a <= -0x10000)
106       a = -0x10000;
107     return -asin_tab[-a];
108   }
109 }
110 
fixatan16(fint_t a)111 fint_t fixatan16(fint_t a)
112 {
113   if (a > 0) {
114     if (a <= 0x10000) {
115       return atan_tab[a];
116     } else {
117       return FIX16_HALF_PI - atan_tab[fixinv<16>(a)];;
118     }
119   } else {
120     if (a >= -0x10000) {
121       return - atan_tab[-a];
122     } else {
123       return -FIX16_HALF_PI + atan_tab[-fixinv<16>(a)];
124     }
125   }
126 }
127 
fixrsqrt16(fint_t a)128 fint_t fixrsqrt16(fint_t a)
129 {
130   fint_t x;
131 
132   static const uint16_t rsq_tab[] = { /* domain 0.5 .. 1.0-1/16 */
133     0xb504, 0xaaaa, 0xa1e8, 0x9a5f, 0x93cd, 0x8e00, 0x88d6, 0x8432,
134   };
135 
136   fint_t i, exp;
137   if (a == 0) return 0x7fffffff;
138   if (a == (1<<16)) return a;
139 
140   exp = detail::CountLeadingZeros(a);
141   x = rsq_tab[(a>>(28-exp))&0x7]<<1;
142 
143   exp -= 16;
144   if (exp <= 0)
145     x >>= -exp>>1;
146   else
147     x <<= (exp>>1)+(exp&1);
148   if (exp&1) x = fixmul<16>(x, rsq_tab[0]);
149 
150 
151   /* newton-raphson */
152   /* x = x/2*(3-(a*x)*x) */
153    i = 0;
154   do {
155     x = fixmul<16>((x>>1),((1<<16)*3 - fixmul<16>(fixmul<16>(a,x),x)));
156   } while(++i < 3);
157 
158   return x;
159 }
160 
fast_div16(fint_t a,fint_t b)161 static inline fint_t fast_div16(fint_t a, fint_t b)
162 {
163   if ((b >> 24) && (b >> 24) + 1) {
164     return fixmul<16>(a >> 8, fixinv<16>(b >> 8));
165   } else {
166     return fixmul<16>(a, fixinv<16>(b));
167   }
168 }
169 
fixsqrt16(fint_t a)170 fint_t fixsqrt16(fint_t a)
171 {
172   if (a < 1<<7) {
173     return 0;
174   }
175 
176 #if 0
177   static fint_t max = 0;
178   if (max < a) {
179     printf("max: %"PRIi64"\n", a);
180     max = a;
181   }
182 #endif
183 
184   fint_t s = (a + (1<<16)) >> 1;
185   /* 14 iterations to find exact value for max 948015267840 */
186   for (int i = 0; i < 14; i++) {
187     s = (s + (a<<16) / s) >> 1;
188   }
189   return s;
190 }
191 
fixsqrt16_approx(fint_t a)192 fint_t fixsqrt16_approx(fint_t a)
193 {
194   if (a < 1<<7) {
195     return 0;
196   }
197 
198 #if 0
199   static fint_t max = 0;
200   if (max < a) {
201     printf("max: %"PRIi64"\n", a);
202     max = a;
203   }
204 #endif
205 
206   fint_t s = (a + (1<<16)) >> 1;
207   /* 6 iterations to find exact value for max 34668544 */
208   for (int i = 0; i < 6; i++) {
209     s = (s + (a<<16) / s) >> 1;
210   }
211   return s;
212 }
213 
214 } // end namespace fp
215