Tizen 2.1 base
[platform/upstream/libbullet.git] / Extras / simdmathlibrary / ppu / simdmath / tanf4.h
1 /* tanf4 - 
2    Copyright (C) 2006, 2007 Sony Computer Entertainment Inc.
3    All rights reserved.
4
5    Redistribution and use in source and binary forms,
6    with or without modification, are permitted provided that the
7    following conditions are met:
8     * Redistributions of source code must retain the above copyright
9       notice, this list of conditions and the following disclaimer.
10     * Redistributions in binary form must reproduce the above copyright
11       notice, this list of conditions and the following disclaimer in the
12       documentation and/or other materials provided with the distribution.
13     * Neither the name of the Sony Computer Entertainment Inc nor the names
14       of its contributors may be used to endorse or promote products derived
15       from this software without specific prior written permission.
16
17    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20    ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21    LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27    POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #ifndef ___SIMD_MATH_TANF4_H___
31 #define ___SIMD_MATH_TANF4_H___
32
33 #include <simdmath.h>
34 #include <altivec.h>
35
36 #include <simdmath/_sincos.h>
37 #include <simdmath/divf4.h>
38
39 //
40 // Computes the tangent of all four slots of x by using a polynomia approximation. 
41 //
42 static inline vector float
43 _tanf4 (vector float x)
44 {
45   vector float xl,xl2,xl3,res;
46   vector signed int q;
47
48   // Range reduction using : xl = angle * TwoOverPi;
49   //  
50   xl = vec_madd(x, __vec_splatsf4(0.63661977236f),__vec_splatsf4(0.0f));
51
52   // Find the quadrant the angle falls in
53   // using:  q = (int) (ceil(abs(x))*sign(x))
54   //
55   xl = vec_add(xl,vec_sel(__vec_splatsf4(0.5f),xl,__vec_splatsu4(0x80000000)));
56   q = vec_cts(xl,0);
57
58      
59   // Remainder in range [-pi/4..pi/4]
60   //
61   vector float qf = vec_ctf(q,0);
62   vector float p1 = vec_nmsub(qf,__vec_splatsf4(__SINCOSF_KC1),x);
63   xl  = vec_nmsub(qf,__vec_splatsf4(__SINCOSF_KC2),p1);
64     
65   // Compute x^2 and x^3
66   //
67   xl2 = vec_madd(xl,xl,__vec_splatsf4(0.0f));
68   xl3 = vec_madd(xl2,xl,__vec_splatsf4(0.0f));
69  
70   // Compute both the sin and cos of the angles
71   // using a polynomial expression:
72   //   cx = 1.0f + x2 * (C0 * x2 + C1), and
73   //   sx = xl + x3 * S0
74   //
75   vector float ct2 = vec_madd(__vec_splatsf4( 0.0097099364f),xl2,__vec_splatsf4(-0.4291161787f));
76     
77   vector float cx = vec_madd(ct2,xl2,__vec_splatsf4(1.0f));
78   vector float sx = vec_madd(__vec_splatsf4(-0.0957822992f),xl3,xl);
79
80     
81   // Compute both cx/sx and sx/cx
82   //
83   vector float cxosx = _divf4(cx,sx);
84   vector float sxocx = _divf4(sx,cx);
85
86   vector float ncxosx = (vector float)vec_xor(__vec_splatsu4(0x80000000),(vector unsigned int)cxosx);
87
88   // For odd numbered quadrants return -cx/sx , otherwise return
89   // sx/cx
90   //
91   vector unsigned int mask =
92     (vector unsigned int)vec_cmpeq(vec_and(q,__vec_splatsi4(0x1)),__vec_splatsi4(0));
93   res = vec_sel(ncxosx,sxocx,mask);
94
95   return res;
96 }
97
98 #endif