1 /*
2   (C) Copyright 2001,2006,
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 without
10   modification, are permitted provided that the following conditions are met:
11 
12     * Redistributions of source code must retain the above copyright notice,
13   this list of conditions and the following disclaimer.
14     * Redistributions in binary form must reproduce the above copyright
15   notice, this list of conditions and the following disclaimer in the
16   documentation and/or other materials provided with the distribution.
17     * Neither the names of the copyright holders nor the names of their
18   contributors may be used to endorse or promote products derived from this
19   software without specific prior written permission.
20 
21   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
22   IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23   TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
24   PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
25   OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33 #ifndef _SQRT_H_
34 #define _SQRT_H_	1
35 
36 /*
37  * FUNCTION
38  *   double _sqrt(double in)
39  *
40  * DESCRIPTION
41  *   _sqrt computes the square root of the input "in" and returns the
42  *   result.
43  */
44 #include <spu_intrinsics.h>
45 #include "headers/vec_literal.h"
46 #include "headers/dom_chkd_less_than.h"
47 
_sqrt(double in)48 static __inline double _sqrt(double in)
49 {
50   vec_int4 bias_exp;
51   vec_uint4 exp;
52   vec_float4 fx, fg, fy, fd, fe, fy2, fhalf;
53   vec_ullong2 nochange;
54   vec_ullong2 mask = VEC_SPLAT_U64(0x7FE0000000000000ULL);
55   vec_double2 x, dx, de, dd, dy, dg, dy2, dhalf;
56   vec_double2 denorm, neg;
57   vec_double2 vc = { 0.0, 0.0 };
58 
59   fhalf = VEC_SPLAT_F32(0.5f);
60   dhalf = VEC_SPLAT_F64(0.5);
61 
62   /* Coerce the input, in, into the argument reduced space [0.5, 2.0).
63    */
64   x = spu_promote(in, 0);
65   dx = spu_sel(x, dhalf, mask);
66 
67   /* Compute an initial single precision guess for the square root (fg)
68    * and half reciprocal (fy2).
69    */
70   fx = spu_roundtf(dx);
71 
72   fy2 = spu_rsqrte(fx);
73   fy = spu_mul(fy2, fhalf);
74   fg = spu_mul(fy2, fx); /* 12-bit approximation to sqrt(cx) */
75 
76   /* Perform one single precision Newton-Raphson iteration to improve
77    * accuracy to about 22 bits.
78    */
79   fe = spu_nmsub(fy, fg, fhalf);
80   fd = spu_nmsub(fg, fg, fx);
81 
82   fy = spu_madd(fy2, fe, fy);
83   fg = spu_madd(fy, fd, fg); /* 22-bit approximation */
84 
85   dy = spu_extend(fy);
86   dg = spu_extend(fg);
87 
88   /* Perform two double precision Newton-Raphson iteration to improve
89    * accuracy to about 44 and 88 bits repectively.
90    */
91   dy2 = spu_add(dy, dy);
92   de = spu_nmsub(dy, dg, dhalf);
93   dd = spu_nmsub(dg, dg, dx);
94   dy = spu_madd(dy2, de, dy);
95   dg = spu_madd(dy, dd, dg); /* 44 bit approximation */
96 
97   dd = spu_nmsub(dg, dg, dx);
98   dg = spu_madd(dy, dd, dg); /* full double precision approximation */
99 
100 
101   /* Compute the expected exponent assuming that it is not a special value.
102    * See special value handling below.
103    */
104   bias_exp = spu_rlmaska(spu_sub((vec_int4)spu_and((vec_ullong2)x, mask),
105                                  (vec_int4)VEC_SPLAT_U64(0x3FE0000000000000ULL)), -1);
106   dg = (vec_double2)spu_add((vec_int4)dg, bias_exp);
107 
108 
109   /* Handle special inputs. These include:
110    *
111    *   input      output
112    * =========    =========
113    *    -0           -0
114    * +infinity    +infinity
115    *    NaN         NaN
116    *    <0          NaN
117    *   denorm       zero
118    */
119   exp = (vec_uint4)spu_and((vec_ullong2)x, VEC_SPLAT_U64(0xFFF0000000000000ULL));
120   exp = spu_shuffle(exp, exp, VEC_LITERAL(vec_uchar16, 0,1,2,3,0,1,2,3, 8,9,10,11,8,9,10,11));
121 
122   neg = (vec_double2)spu_rlmaska((vec_int4)exp, -31);
123   denorm = (vec_double2)spu_rlmask(spu_cmpeq(spu_sl(exp, 1), 0), VEC_LITERAL(vec_int4, -1,0,-1,0));
124 
125   nochange = (vec_ullong2)spu_cmpeq(exp, 0x7FF00000);
126 
127   dg = spu_sel(spu_andc(spu_or(dg, neg), denorm), x, nochange);
128 
129 #ifndef _IEEE_LIBM
130   dom_chkd_less_than(spu_splats(in), vc);
131 #endif
132   return (spu_extract(dg, 0));
133 }
134 #endif /* _SQRT_H_ */
135