1 /* -------------------------------------------------------------- */
2 /* (C)Copyright 2006,2008, */
3 /* International Business Machines Corporation */
4 /* All Rights Reserved. */
5 /* */
6 /* Redistribution and use in source and binary forms, with or */
7 /* without modification, are permitted provided that the */
8 /* following conditions are met: */
9 /* */
10 /* - Redistributions of source code must retain the above copyright*/
11 /* notice, this list of conditions and the following disclaimer. */
12 /* */
13 /* - Redistributions in binary form must reproduce the above */
14 /* copyright notice, this list of conditions and the following */
15 /* disclaimer in the documentation and/or other materials */
16 /* provided with the distribution. */
17 /* */
18 /* - Neither the name of IBM Corporation nor the names of its */
19 /* contributors may be used to endorse or promote products */
20 /* derived from this software without specific prior written */
21 /* permission. */
22 /* */
23 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND */
24 /* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, */
25 /* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */
26 /* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
27 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR */
28 /* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, */
29 /* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT */
30 /* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */
31 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) */
32 /* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN */
33 /* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR */
34 /* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, */
35 /* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
36 /* -------------------------------------------------------------- */
37 /* PROLOG END TAG zYx */
38 #ifdef __SPU__
39
40 #ifndef _HYPOTD2_H_
41 #define _HYPOTD2_H_ 1
42
43 #include <spu_intrinsics.h>
44 #include "sqrtd2.h"
45
46 /*
47 * FUNCTION
48 * vector double hypotd2(vector double x, vector double y)
49 *
50 * DESCRIPTION
51 * The function hypotd2 returns a double vector in which each element is
52 * the square root of the sum of the squares of the corresponding
53 * elements of x and y.
54 *
55 * The purpose of this function is to avoid overflow during
56 * intermediate calculations, and therefore it is slower than
57 * simply calcualting sqrt(x^2 + y^2).
58 *
59 * This function is performed by factoring out the larger of the 2
60 * input exponents and moving this factor outside of the sqrt calculation.
61 * This will minimize the possibility of over/underflow when the square
62 * of the values are calculated. Think of it as normalizing the larger
63 * input to the range [1,2).
64 *
65 * Special Cases:
66 * - hypot(x, +/-0) returns |x|
67 * - hypot(+/- infinity, y) returns +infinity
68 * - hypot(+/- infinity, NaN) returns +infinity
69 *
70 */
_hypotd2(vector double x,vector double y)71 static __inline vector double _hypotd2(vector double x, vector double y)
72 {
73 vector unsigned long long emask = spu_splats(0x7FF0000000000000ull);
74 vector unsigned long long mmask = spu_splats(0x000FFFFFFFFFFFFFull);
75 vector signed long long bias = spu_splats(0x3FF0000000000000ll);
76 vector double oned = spu_splats(1.0);
77 vector double sbit = spu_splats(-0.0);
78 vector double inf = (vector double)spu_splats(0x7FF0000000000000ull);
79 vector double max, max_e, max_m;
80 vector double min, min_e, min_m;
81 vector unsigned long long xgty;
82 vector double sum;
83 vector double result;
84
85 /* Only need absolute values for this function */
86 x = spu_andc(x, sbit);
87 y = spu_andc(y, sbit);
88 xgty = spu_cmpgt(x,y);
89
90 max = spu_sel(y,x,xgty);
91 min = spu_sel(x,y,xgty);
92
93 /* Extract the exponents and mantissas */
94 max_e = (vec_double2)spu_and((vec_ullong2)max, emask);
95 max_m = (vec_double2)spu_and((vec_ullong2)max, mmask);
96 min_e = (vec_double2)spu_and((vec_ullong2)min, emask);
97 min_m = (vec_double2)spu_and((vec_ullong2)min, mmask);
98
99 /* Factor-out max exponent here by subtracting from min exponent */
100 vec_llong2 min_e_int = (vec_llong2)spu_sub((vec_int4)min_e, (vec_int4)max_e);
101 min_e = (vec_double2)spu_add((vec_int4)min_e_int, (vec_int4)bias);
102
103 /* If the new min exponent is too small, just set it to 0. It
104 * wouldn't contribute to the final result in either case.
105 */
106 min_e = spu_sel(min_e, sbit, spu_cmpgt(sbit, min_e));
107
108 /* Combine new exponents with original mantissas */
109 max = spu_or(oned, max_m);
110 min = spu_or(min_e, min_m);
111
112 sum = _sqrtd2(spu_madd(max, max, spu_mul(min, min)));
113 sum = spu_mul(max_e, sum);
114
115 /* Special case: x = +/- infinity */
116 result = spu_sel(sum, inf, spu_cmpeq(x, inf));
117
118 return result;
119 }
120
121 #endif /* _HYPOTD2_H_ */
122 #endif /* __SPU__ */
123