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 _SQRTF4_H_
43 #define _SQRTF4_H_	1
44 
45 #include <spu_intrinsics.h>
46 
47 /*
48  * FUNCTION
49  *      vector float _sqrtf4(vector float in)
50  *
51  * DESCRIPTION
52  *      The _sqrtf4 function computes the square root of the vector input "in"
53  *and returns the result.
54  *
55  */
_sqrtf4(vector float in)56 static __inline vector float _sqrtf4(vector float in)
57 {
58   vec_uint4 exp, valid;
59   vec_uint4 mask = spu_splats((unsigned int)0xFF000000);
60   vec_uint4 half = spu_splats((unsigned int)0x00800000);
61   vec_float4 one = spu_splats(1.0f);
62   vec_float4 three = spu_splats(3.0f);
63   vec_float4 x, y0, y1, y1_n1, y1_p1, y1_p2, y1_p3;
64   vec_float4 mant, err, err_p1, err_p2, err_p3;
65   vec_float4 out;
66 
67   /* Compute the mantissa of the result seperately from
68    * the exponent to assure complete accuracy over the allowable
69    * input range. The mantissa is computed for inputs in the
70    * range [0.5, 2.0).
71    */
72   x = spu_sel(in, one, mask);
73   y0 = spu_rsqrte(x);
74 
75   /* Perform one iteration of the Newton-Raphsom method in single precision
76    * arithmetic.
77    */
78   y1 = spu_mul(spu_nmsub(x, spu_mul(y0, y0), three),
79 	       spu_mul(y0, (vec_float4)(spu_sub((vec_uint4)(x), half))));
80 
81   /* Correct the result for possible error. The range of error is -3 to +1.
82    * Identify the extent of the error and correct for it.
83    */
84   y1_p3 = (vec_float4)spu_add((vec_uint4)(y1), 3);
85   y1_p2 = (vec_float4)spu_add((vec_uint4)(y1), 2);
86   y1_p1 = (vec_float4)spu_add((vec_uint4)(y1), 1);
87   y1_n1 = (vec_float4)spu_add((vec_uint4)(y1), -1);
88 
89   err    = spu_nmsub(y1,    y1,    x);
90   err_p1 = spu_nmsub(y1_p1, y1_p1, x);
91   err_p2 = spu_nmsub(y1_p2, y1_p2, x);
92   err_p3 = spu_nmsub(y1_p3, y1_p3, x);
93 
94   mant = spu_sel(y1_n1, y1,    spu_cmpgt((vec_int4)(err),    -1));
95   mant = spu_sel(mant,  y1_p1, spu_cmpgt((vec_int4)(err_p1), -1));
96   mant = spu_sel(mant,  y1_p2, spu_cmpgt((vec_int4)(err_p2), -1));
97   mant = spu_sel(mant,  y1_p3, spu_cmpgt((vec_int4)(err_p3), -1));
98 
99   /* Compute the expected exponent. If the exponent is zero or the input is
100    * negative, then set the result to zero.
101    */
102   exp = spu_rlmask(spu_add((vec_uint4)(in), (vec_uint4)(one)), -1);
103 
104   valid = spu_cmpgt(spu_and((vec_int4)(in), (vec_int4)(mask)), 0);
105 
106   /* Merge the computed exponent and mantissa.
107    */
108   out = spu_and(spu_sel(mant, (vec_float4)(exp), spu_splats(0xFF800000)), (vec_float4)(valid));
109 
110 
111   return (out);
112 
113 }
114 
115 #endif /* _SQRTF4_H_ */
116 #endif /* __SPU__ */
117