1 /* --------------------------------------------------------------  */
2 /* (C)Copyright 2001,2008,                                         */
3 /* International Business Machines Corporation,                    */
4 /* Sony Computer Entertainment, Incorporated,                      */
5 /* Toshiba Corporation,                                            */
6 /*                                                                 */
7 /* All Rights Reserved.                                            */
8 /*                                                                 */
9 /* Redistribution and use in source and binary forms, with or      */
10 /* without modification, are permitted provided that the           */
11 /* following conditions are met:                                   */
12 /*                                                                 */
13 /* - Redistributions of source code must retain the above copyright*/
14 /*   notice, this list of conditions and the following disclaimer. */
15 /*                                                                 */
16 /* - Redistributions in binary form must reproduce the above       */
17 /*   copyright notice, this list of conditions and the following   */
18 /*   disclaimer in the documentation and/or other materials        */
19 /*   provided with the distribution.                               */
20 /*                                                                 */
21 /* - Neither the name of IBM Corporation nor the names of its      */
22 /*   contributors may be used to endorse or promote products       */
23 /*   derived from this software without specific prior written     */
24 /*   permission.                                                   */
25 /*                                                                 */
26 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND          */
27 /* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,     */
28 /* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF        */
29 /* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE        */
30 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR            */
31 /* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,    */
32 /* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT    */
33 /* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;    */
34 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)        */
35 /* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN       */
36 /* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR    */
37 /* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,  */
38 /* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.              */
39 /* --------------------------------------------------------------  */
40 /* PROLOG END TAG zYx                                              */
41 #ifdef __SPU__
42 #ifndef _COS_SIN_H_
43 #define _COS_SIN_H_	1
44 
45 #define M_PI_OVER_4_HI_32 0x3fe921fb
46 
47 #define M_PI_OVER_4	0.78539816339744827900
48 #define M_FOUR_OVER_PI 	1.27323954478442180616
49 
50 #define M_PI_OVER_2	1.57079632679489655800
51 #define M_PI_OVER_2_HI 	1.57079632673412561417
52 #define M_PI_OVER_2_LO 	0.0000000000607710050650619224932
53 
54 #define M_PI_OVER_2F_HI   1.570312500000000000
55 #define M_PI_OVER_2F_LO	  0.000483826794896558
56 
57 /* The following coefficients correspond to the Taylor series
58  * coefficients for cos and sin.
59  */
60 #define COS_14 -0.00000000001138218794258068723867
61 #define COS_12  0.000000002087614008917893178252
62 #define COS_10 -0.0000002755731724204127572108
63 #define COS_08  0.00002480158729870839541888
64 #define COS_06 -0.001388888888888735934799
65 #define COS_04  0.04166666666666666534980
66 #define COS_02 -0.5000000000000000000000
67 #define COS_00  1.0
68 
69 #define SIN_15 -0.00000000000076471637318198164759
70 #define SIN_13  0.00000000016059043836821614599
71 #define SIN_11 -0.000000025052108385441718775
72 #define SIN_09  0.0000027557319223985890653
73 #define SIN_07 -0.0001984126984126984127
74 #define SIN_05  0.008333333333333333333
75 #define SIN_03 -0.16666666666666666666
76 #define SIN_01  1.0
77 
78 
79 /* Compute the following for each floating point element of x.
80  * 	x  = fmod(x, PI/4);
81  *  	ix = (int)x * PI/4;
82  * This allows one to compute cos / sin over the limited range
83  * and select the sign and correct result based upon the octant
84  * of the original angle (as defined by the ix result).
85  *
86  * Expected Inputs Types:
87  * 	x  = vec_float4
88  *	ix = vec_int4
89  */
90 #define MOD_PI_OVER_FOUR_F(_x, _ix) {					\
91     vec_float4 fx;							\
92 									\
93     _ix = spu_convts(spu_mul(_x, spu_splats((float)M_FOUR_OVER_PI)), 0); \
94     _ix = spu_add(_ix, spu_add(spu_rlmaska((vec_int4)_x, -31), 1));	\
95 									\
96     fx = spu_convtf(spu_rlmaska(_ix, -1), 0);				\
97     _x  = spu_nmsub(fx, spu_splats((float)M_PI_OVER_2F_HI), _x);	\
98     _x  = spu_nmsub(fx, spu_splats((float)M_PI_OVER_2F_LO), _x);	\
99   }
100 
101 /* Double precision MOD_PI_OVER_FOUR
102  *
103  * Expected Inputs Types:
104  * 	x  = vec_double2
105  *	ix = vec_int4
106  */
107 #define MOD_PI_OVER_FOUR(_x, _ix) {					\
108     vec_float4 fx;							\
109     vec_double2 dix;							\
110 									\
111     fx = spu_roundtf(spu_mul(_x, spu_splats(M_FOUR_OVER_PI)));	\
112     _ix = spu_convts(fx, 0);						\
113     _ix = spu_add(_ix, spu_add(spu_rlmaska((vec_int4)fx, -31), 1));	\
114 									\
115     dix = spu_extend(spu_convtf(spu_rlmaska(_ix, -1), 0));		\
116     _x  = spu_nmsub(spu_splats(M_PI_OVER_2_HI), dix, _x);		\
117     _x  = spu_nmsub(spu_splats(M_PI_OVER_2_LO), dix, _x);		\
118   }
119 
120 
121 /* Compute the cos(x) and sin(x) for the range reduced angle x.
122  * In order to compute these trig functions to full single precision
123  * accuracy, we solve the Taylor series.
124  *
125  *   c = cos(x) = 1 - x^2/2! + x^4/4! - x^6/6! + x^8/8! - x^10/10!
126  *   s = sin(x) = x - x^3/4! + x^5/5! - x^7/7! + x^9/9! - x^11/11!
127  *
128  * Expected Inputs Types:
129  * 	x = vec_float4
130  *	c = vec_float4
131  *	s = vec_float4
132  */
133 
134 #define COMPUTE_COS_SIN_F(_x, _c, _s) {					\
135     vec_float4 x2, x4, x6;						\
136     vec_float4 cos_hi, cos_lo;						\
137     vec_float4 sin_hi, sin_lo;						\
138 									\
139     x2 = spu_mul(_x, _x);						\
140     x4 = spu_mul(x2, x2);						\
141     x6 = spu_mul(x2, x4);						\
142 									\
143     cos_hi = spu_madd(spu_splats((float)COS_10), x2, spu_splats((float)COS_08)); \
144     cos_lo = spu_madd(spu_splats((float)COS_04), x2, spu_splats((float)COS_02)); \
145     cos_hi = spu_madd(cos_hi, x2, spu_splats((float)COS_06));		\
146     cos_lo = spu_madd(cos_lo, x2, spu_splats((float)COS_00));		\
147     _c     = spu_madd(cos_hi, x6, cos_lo);				\
148 									\
149     sin_hi = spu_madd(spu_splats((float)SIN_11), x2, spu_splats((float)SIN_09)); \
150     sin_lo = spu_madd(spu_splats((float)SIN_05), x2, spu_splats((float)SIN_03)); \
151     sin_hi = spu_madd(sin_hi, x2, spu_splats((float)SIN_07));		\
152     sin_lo = spu_madd(sin_lo, x2, spu_splats((float)SIN_01));		\
153     _s    = spu_madd(sin_hi, x6, sin_lo);				\
154     _s     = spu_mul(_s, _x);						\
155   }
156 
157 
158 /* Compute the cos(x) and sin(x) for the range reduced angle x.
159  * This version computes the cosine and sine to double precision
160  * accuracy using the Taylor series:
161  *
162  *   c = cos(x) = 1 - x^2/2! + x^4/4! - x^6/6! + x^8/8! - x^10/10! + x^12/12! - x^14/14!
163  *   s = sin(x) = x - x^3/4! + x^5/5! - x^7/7! + x^9/9! - x^11/11! + x^13/13! - x^15/15!
164  *
165  * Expected Inputs Types:
166  * 	x = vec_double2
167  *	c = vec_double2
168  *	s = vec_double2
169  */
170 
171 #define COMPUTE_COS_SIN(_x, _c, _s) {					\
172     vec_double2 x2, x4, x8;						\
173     vec_double2 cos_hi, cos_lo;						\
174     vec_double2 sin_hi, sin_lo;						\
175 									\
176     x2 = spu_mul(_x, _x);						\
177     x4 = spu_mul(x2, x2);						\
178     x8 = spu_mul(x4, x4);						\
179 									\
180     cos_hi = spu_madd(spu_splats(COS_14), x2, spu_splats(COS_12));	\
181     cos_lo = spu_madd(spu_splats(COS_06), x2, spu_splats(COS_04));	\
182     cos_hi = spu_madd(cos_hi, x2, spu_splats(COS_10));			\
183     cos_lo = spu_madd(cos_lo, x2, spu_splats(COS_02));			\
184     cos_hi = spu_madd(cos_hi, x2, spu_splats(COS_08));			\
185     cos_lo = spu_madd(cos_lo, x2, spu_splats(COS_00));			\
186     _c     = spu_madd(cos_hi, x8, cos_lo);				\
187 									\
188     sin_hi = spu_madd(spu_splats(SIN_15), x2, spu_splats(SIN_13));	\
189     sin_lo = spu_madd(spu_splats(SIN_07), x2, spu_splats(SIN_05));	\
190     sin_hi = spu_madd(sin_hi, x2, spu_splats(SIN_11));			\
191     sin_lo = spu_madd(sin_lo, x2, spu_splats(SIN_03));			\
192     sin_hi = spu_madd(sin_hi, x2, spu_splats(SIN_09));			\
193     sin_lo = spu_madd(sin_lo, x2, spu_splats(SIN_01));			\
194     _s     = spu_madd(sin_hi, x8, sin_lo);				\
195     _s     = spu_mul(_s, _x);						\
196   }
197 
198 
199 
200 
201 #endif /* _COS_SIN_H_ */
202 #endif /* __SPU__ */
203 
204 
205