1 /* rsqrtd2 - for each of two double slots, compute reciprocal square root.
2 Copyright (C) 2006, 2007 Sony Computer Entertainment Inc.
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.
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.
30 #ifndef ___SIMD_MATH_RSQRTD2_H___
31 #define ___SIMD_MATH_RSQRTD2_H___
34 #include <spu_intrinsics.h>
36 #include <simdmath/isinfd2.h>
37 #include <simdmath/signbitd2.h>
38 #include <simdmath/is0denormd2.h>
41 // Handles exceptional values as follows:
47 // Denormal inputs are treated as zero.
49 static inline vector double
50 _rsqrtd2 (vector double x)
52 vec_ullong2 expmask, onemask, signmask, evenexp;
53 vec_double2 half, one, man, exp, nexp, y1, y2, y3, zero, inf, nan, result;
54 vec_float4 halff, onef, manf, y0f, y1f;
56 expmask = spu_splats(0x7ff0000000000000ull);
57 onemask = spu_splats(0x0010000000000000ull);
58 signmask = spu_splats(0x8000000000000000ull);
59 onef = spu_splats(1.0f);
60 one = spu_extend( onef );
61 halff = spu_splats(0.5f);
62 half = spu_extend( halff );
64 // Factor input ( mantissa x 2^exponent ) into ( mantissa x 2^(-i) ) and ( 2^(exponent+i) )
65 // where i = 0 when exponent is even and i = 1 when exponent is odd.
67 // Compute reciprocal-square-root of second factor by finding -(exponent+i)/2:
69 // biased_exp = 1023 + exponent
70 // new_biased_exp = 1023 - (exponent+i)/2
71 // = 1023 - (biased_exp-1023+i)/2
72 // = (3069 - (biased_exp+i)) / 2
74 evenexp = spu_and( (vec_ullong2)x, onemask );
75 man = spu_sel( x, (vec_double2)spu_add( spu_splats(0x3fe00000u), (vec_uint4)evenexp ), expmask );
77 exp = spu_and( x, (vec_double2)expmask );
78 nexp = spu_or( exp, (vec_double2)onemask );
79 nexp = (vec_double2)spu_rlmask( spu_sub( (vec_uint4)spu_splats(0xbfd0000000000000ull), (vec_uint4)nexp ), -1 );
81 // Compute mantissa part in single precision.
82 // Convert back to double and multiply with 2^(-(exponent+i)/2), then
83 // do two Newton-Raphson steps for full precision.
85 manf = spu_roundtf( man );
86 y0f = spu_rsqrte( manf );
87 y1f = spu_madd( spu_mul( y0f, halff ), spu_nmsub( y0f, spu_mul( y0f, manf ), onef ), y0f );
88 y1 = spu_mul( spu_extend( y1f ), nexp );
89 y2 = spu_madd( spu_mul( y1, half ), spu_nmsub( y1, spu_mul( y1, x ), one ), y1 );
90 y3 = spu_madd( spu_mul( y2, half ), spu_nmsub( y2, spu_mul( y2, x ), one ), y2 );
92 // Choose iterated result or special value.
94 zero = spu_and( x, (vec_double2)signmask );
95 inf = spu_sel( (vec_double2)expmask, x, signmask );
96 nan = (vec_double2)spu_splats(0x7ff8000000000000ull);
98 result = spu_sel( y3, zero, _isinfd2 ( x ) );
99 result = spu_sel( result, nan, _signbitd2 ( x ) );
100 result = spu_sel( result, inf, _is0denormd2 ( x ) );