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 _NEXTAFTERF4_H_
40 #define _NEXTAFTERF4_H_	1
41 
42 #include <spu_intrinsics.h>
43 
44 /*
45  * FUNCTION
46  *  vector float _nextafterf4(vector float x, vector float y)
47  *
48  * DESCRIPTION
49  *  The nextafterf4 function returns a vector containing the next representable
50  *  floating-point number after the element of x, in the direction of the
51  *  corresponding element y.
52  *
53  *  Special Cases:
54  *	- Infinity and NaN are not supported in single-precision on SPU. They are treated
55  *	  as normal numbers.
56  *	- x != y, and result = 0 is considered an underflow.
57  *
58  *
59  */
60 
_nextafterf4(vector float x,vector float y)61 static __inline vector float _nextafterf4(vector float x, vector float y)
62 {
63     vec_float4 n1ulp = (vec_float4)spu_splats(0x80000001);
64     vec_float4 zerof = spu_splats(0.0f);
65     vec_int4  one    = spu_splats(1);
66     vec_uint4 xlt0, xgty, xeqy, xeq0;
67     vec_int4  xint;
68     vec_int4  delta, deltap1;
69     vec_float4 result;
70 
71     /*
72      * The idea here is to treat x as a signed int value, which allows us to
73      * add or subtact one to/from it to get the next representable value.
74      */
75 
76     xeq0 = spu_cmpeq(x, zerof);
77     xlt0 = spu_cmpgt(zerof, x);
78     xeqy = spu_cmpeq(x, y);
79     xgty = spu_cmpgt(x, y);
80 
81     /* If x = -0.0, set x = 0.0 */
82     x = spu_andc(x, (vec_float4)xeq0);
83 
84     xint = (vec_int4)x;
85 
86     /* Determine value to add to x */
87     delta = (vec_int4)spu_xor(xgty, xlt0);
88     deltap1 = spu_add(delta,one);
89     delta = spu_sel(deltap1, delta, (vec_uint4)delta);
90 
91     xint = spu_add(xint, delta);
92 
93     /* Fix the case of x = 0, and answer should be -1 ulp */
94     result = spu_sel((vec_float4)xint, n1ulp, spu_and((vec_uint4)delta, xeq0));
95 
96     /*
97      * Special Cases
98      */
99 
100     /* x = y */
101     result = spu_sel(result, y, xeqy);
102 
103     return result;
104 
105 }
106 
107 #endif /* _NEXTAFTERF4_H_ */
108 #endif /* __SPU__ */
109