1 /* -------------------------------------------------------------- */
2 /* (C)Copyright 2007,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 #ifndef _HYPOTF4_H_
40 #define _HYPOTF4_H_ 1
41
42 #include <spu_intrinsics.h>
43
44 #include "sqrtf4.h"
45
46 /*
47 * FUNCTION
48 * vector float _hypotf4(vector float x, vector float y)
49 *
50 * DESCRIPTION
51 * The function hypotf4 returns a float 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. In other words, each element is sqrt(x^2 + y^2).
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 *
66 * Special Cases:
67 * - hypot(x, +/-0) returns |x|
68 * - hypot(+/- infinity, y) returns +infinity
69 * - hypot(+/- infinity, NaN) returns +infinity
70 *
71 */
72
73
_hypotf4(vector float x,vector float y)74 static __inline vector float _hypotf4(vector float x, vector float y)
75 {
76 vector unsigned int emask = spu_splats(0x7F800000u);
77 vector unsigned int mmask = spu_splats(0x007FFFFFu);
78 vector signed int bias = spu_splats(0x3F800000);
79 vector float inf = (vec_float4)spu_splats(0x7F800000);
80 vector float onef = spu_splats(1.0f);
81 vector float sbit = spu_splats(-0.0f);
82 vector float max, max_e, max_m;
83 vector float min, min_e, min_m;
84 vector unsigned int xgty;
85 vector float sum;
86 vector float result;
87
88 /* Only need absolute values for this function */
89 x = spu_andc(x, sbit);
90 y = spu_andc(y, sbit);
91 xgty = spu_cmpgt(x,y);
92
93 max = spu_sel(y,x,xgty);
94 min = spu_sel(x,y,xgty);
95
96 /* Extract exponents and mantissas */
97 max_e = (vec_float4)spu_and((vec_uint4)max, emask);
98 max_m = (vec_float4)spu_and((vec_uint4)max, mmask);
99 min_e = (vec_float4)spu_and((vec_uint4)min, emask);
100 min_m = (vec_float4)spu_and((vec_uint4)min, mmask);
101
102 /* Adjust the exponent of the smaller of the 2 input values by
103 * subtracting max_exp from min_exp.
104 */
105 vec_int4 min_e_int = spu_sub((vec_int4)min_e, (vec_int4)max_e);
106 min_e = (vec_float4)spu_add(min_e_int, bias);
107
108 /* If the new min exponent is too small, just set it to 0. It
109 * wouldn't contribute to the final result in either case.
110 */
111 min_e = spu_sel(min_e, sbit, spu_cmpgt(sbit, min_e));
112
113 /* Combine new exponents with original mantissas */
114 max = spu_or(onef, max_m);
115 min = spu_or(min_e, min_m);
116
117 sum = _sqrtf4(spu_madd(max, max, spu_mul(min, min)));
118 sum = spu_mul(max_e, sum);
119
120 /* Special case: x = +/- infinity */
121 result = spu_sel(sum, inf, spu_cmpeq(x, inf));
122
123 return result;
124 }
125
126 #endif /* _HYPOTF4_H_ */
127 #endif /* __SPU__ */
128