1 /* fmind2 - for each of two double slots, compute minimum of x and y
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_FMIND2_H___
31 #define ___SIMD_MATH_FMIND2_H___
34 #include <spu_intrinsics.h>
36 /* Return the minimum numeric value of their arguments. If one argument
37 * is a NaN, fmin returns the other value. If both are NaNs, then a NaN
41 static inline vector double
42 _fmind2 (vector double x, vector double y)
44 vec_ullong2 selector, denorm;
45 vec_double2 x_offset, y_offset, diff;
46 vec_uint4 nan_x, abs_x, gt, eq;
47 vec_uint4 sign = (vec_uint4){0x80000000, 0, 0x80000000, 0};
48 vec_uint4 infinity = (vec_uint4){0x7FF00000, 0, 0x7FF00000, 0};
49 vec_uint4 exp0 = (vec_uint4){0x3FF00000, 0, 0x3FF00000, 0};
51 /* If both x and y are denorm or zero, then set 0x3ff to exponent
53 denorm = (vec_ullong2)spu_cmpeq(spu_and((vec_uint4)spu_or(x, y), infinity), 0);
54 x_offset = spu_sel(x, spu_or(x, (vec_double2)exp0), denorm);
55 y_offset = spu_sel(y, spu_or(y, (vec_double2)exp0), denorm);
57 /* If x is a NaN, then select y as min
59 abs_x = spu_andc((vec_uint4)x, sign);
60 gt = spu_cmpgt(abs_x, infinity);
61 eq = spu_cmpeq(abs_x, infinity);
62 nan_x = spu_or(gt, spu_and(eq, spu_rlqwbyte(gt, 4)));
64 diff = spu_sub(y_offset, x_offset);
65 selector = (vec_ullong2)spu_orc(nan_x, spu_cmpgt((vec_int4)diff, -1));
66 selector = spu_shuffle(selector, selector, ((vec_uchar16){0,1,2,3, 0,1,2,3, 8,9,10,11, 8,9,10,11}));
68 return spu_sel(x, y, selector);