Tizen 2.1 base
[platform/upstream/libbullet.git] / Extras / simdmathlibrary / spu / simdmath / sqrtd2.h
1 /* sqrtd2 - for each of two double slots, compute square root.
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_SQRTD2_H___
31 #define ___SIMD_MATH_SQRTD2_H___
32
33 #include <simdmath.h>
34 #include <spu_intrinsics.h>
35
36 #include <simdmath/isinfd2.h>
37 #include <simdmath/isnand2.h>
38 #include <simdmath/is0denormd2.h>
39
40 // 
41 // Handles exceptional values as follows:
42 // NaN -> NaN
43 // -Inf -> Nan
44 // -Finite -> Nan
45 // Denormal inputs are treated as zero.
46
47 static inline vector double
48 _sqrtd2 (vector double x)
49 {
50   vec_ullong2 expmask, onemask, signmask, evenexp;
51   vec_double2 half, one, man, exp, nexp, y1, y2, y3, zero, inf, nan, neg, result;
52   vec_float4  halff, onef, manf, y0f, y1f;
53
54   expmask = spu_splats(0x7ff0000000000000ull);
55   onemask = spu_splats(0x0010000000000000ull);
56   signmask = spu_splats(0x8000000000000000ull);
57   onef = spu_splats(1.0f);
58   one = spu_extend( onef );
59   halff = spu_splats(0.5f);
60   half = spu_extend( halff );
61               
62   // First compute reciprocal square root.
63   // Factor input ( mantissa x 2^exponent ) into ( mantissa x 2^(-i) ) and ( 2^(exponent+i) )
64   // where i = 0 when exponent is even and i = 1 when exponent is odd.
65   // 
66   // Compute reciprocal-square-root of second factor by finding -(exponent+i)/2:
67   // 
68   // biased_exp = 1023 + exponent
69   // new_biased_exp = 1023 - (exponent+i)/2 
70   //                = 1023 - (biased_exp-1023+i)/2
71   //                = (3069 - (biased_exp+i)) / 2
72
73   evenexp = spu_and( (vec_ullong2)x, onemask );
74   man = spu_sel( x, (vec_double2)spu_add( spu_splats(0x3fe00000u), (vec_uint4)evenexp ), expmask );
75
76   exp = spu_and( x, (vec_double2)expmask );
77   nexp = spu_or( exp, (vec_double2)onemask );
78   nexp = (vec_double2)spu_rlmask( spu_sub( (vec_uint4)spu_splats(0xbfd0000000000000ull), (vec_uint4)nexp ), -1 );
79
80   // Compute mantissa part in single precision.
81   // Convert back to double and multiply with 2^(-(exponent+i)/2), then
82   // do two Newton-Raphson steps for full precision.
83
84   manf = spu_roundtf( man );
85   y0f = spu_rsqrte( manf );
86   y1f = spu_madd( spu_mul( y0f, halff ), spu_nmsub( y0f, spu_mul( y0f, manf ), onef ), y0f );
87   y1 = spu_mul( spu_extend( y1f ), nexp );
88   y2 = spu_madd( spu_mul( y1, half ), spu_nmsub( y1, spu_mul( y1, x ), one ), y1 );
89   y3 = spu_madd( spu_mul( y2, half ), spu_nmsub( y2, spu_mul( y2, x ), one ), y2 );
90
91   // Multiply by input to get square root.
92
93   y3 = spu_mul( y3, x );
94
95   // Choose iterated result or special value.
96
97   zero = spu_and( x, (vec_double2)signmask );
98   inf = (vec_double2)expmask;
99   nan = (vec_double2)spu_splats(0x7ff8000000000000ull);
100
101   neg = spu_and(x, (vec_double2)spu_splats(0x8000000000000000ull));
102   neg = spu_shuffle(neg, neg, ((vec_uchar16){0,0,0,0,0,0,0,0,8,8,8,8,8,8,8,8}));
103   neg = (vec_double2)spu_rlmaska((vec_int4)neg, -31);
104
105   result = spu_sel( y3, inf, _isinfd2 ( x ) );
106   result = spu_sel( result, nan, _isnand2 ( x ) );
107   result = spu_sel( result, nan, (vec_ullong2)neg );
108   result = spu_sel( result, zero, _is0denormd2 ( x ) );
109
110   return result;
111 }
112
113 #endif