1 /* llrintd2 - rounds two doubles in to two nearest 64bit integer.
2 consistent with the current rounding mode.
3 Copyright (C) 2006, 2007 Sony Computer Entertainment Inc.
6 Redistribution and use in source and binary forms,
7 with or without modification, are permitted provided that the
8 following conditions are met:
9 * Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11 * Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution.
14 * Neither the name of the Sony Computer Entertainment Inc nor the names
15 of its contributors may be used to endorse or promote products derived
16 from this software without specific prior written permission.
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 POSSIBILITY OF SUCH DAMAGE.
31 #ifndef ___SIMD_MATH_LLRINTD2_H___
32 #define ___SIMD_MATH_LLRINTD2_H___
35 #include <spu_intrinsics.h>
38 // Handles no exception
39 // over flow will return unspecified data
41 static inline vector signed long long
42 _llrintd2 (vector double in)
45 vec_uchar16 splat_msb = ((vec_uchar16){0,0,0,0,0,0,0,0, 8,8,8,8,8,8,8,8});
47 vec_uint4 mant, mant0, mant1, sign, mask, borrow;
48 vec_uint4 implied_one = ((vec_uint4){ 0, 0, 0x00100000, 0});
49 vec_uint4 exp_mask = ((vec_uint4){-1,-1, 0xFFF00000, 0});
52 vec_uint4 vec_zero = ((vec_uint4){0,0,0,0});
54 vec_uint4 exp_in = spu_and( (vec_uint4)in, 0x7FF00000 );
55 vec_uint4 is_denorm = spu_cmpeq( exp_in, 0 );
56 vec_uint4 ofs = spu_and( ((vec_uint4){0x00100000,0,0x00100000,0}), is_denorm);
59 vec_uint4 abs_x = spu_and((vec_uint4)in, ((vec_uint4){0x7FFFFFFF,-1,0x7FFFFFFF,-1}));
60 vec_uint4 is_zerox = spu_cmpeq( abs_x, vec_zero);
61 is_zerox = spu_and( is_zerox, spu_shuffle(is_zerox,is_zerox, ((vec_uchar16){4,5,6,7,0,1,2,3,12,13,14,15,8,9,10,11})));
62 ofs = spu_sel( ofs, vec_zero, is_zerox);
64 vec_double2 xx = (vec_double2)spu_or( (vec_uint4)in, ofs );
66 /* Round the input according to the current rounding mode.
68 vec_uint4 is_large = spu_cmpgt( exp_in, 0x43200000 );
69 is_large = spu_shuffle(is_large,is_large,((vec_uchar16){0,0,0,0,0,0,0,0,8,8,8,8,8,8,8,8}));
70 bias = spu_sel((vec_double2)((vec_ullong2){0x4330000000000000ULL,0x4330000000000000ULL}), ((vec_double2){0.0,0.0}), (vec_ullong2)is_large);
71 bias = spu_sel(bias, xx, (vec_ullong2)spu_splats(0x8000000000000000ULL));
73 // bias = spu_sel((vec_double2)((vec_ullong2)spu_splats(0x4330000000000000ULL)), xx,
74 // (vec_ullong2)spu_splats(0x8000000000000000ULL));
75 mant = (vec_uint4)(spu_sub(spu_add(xx, bias), bias));
77 /* Determine how many bits to shift the mantissa to correctly
78 * align it into long long element 0.
80 exp = spu_and(spu_rlmask((vec_int4)mant, -20), 0x7FF);
81 exp = spu_add(exp, -1011);
82 shift0 = spu_extract(exp, 0);
83 shift1 = spu_extract(exp, 2);
85 mask = spu_cmpgt(exp, 0);
86 mask = spu_shuffle(mask, mask, splat_msb);
90 mant0 = spu_sel(spu_rlmaskqwbyte(mant, -8), implied_one, exp_mask);
91 mant1 = spu_sel(mant, implied_one, exp_mask);
93 mant0 = spu_slqwbytebc(spu_slqw(mant0, shift0), shift0);
94 mant1 = spu_slqwbytebc(spu_slqw(mant1, shift1), shift1);
96 mant = spu_shuffle(mant0, mant1, ((vec_uchar16){0,1,2,3,4,5,6,7, 16,17,18,19,20,21,22,23}));
97 mant = spu_and(mant, mask);
99 /* Compute the two's complement of the mantissa if the
102 sign = (vec_uint4)spu_rlmaska((vec_int4)xx, -31);
103 sign = spu_shuffle(sign, sign, splat_msb);
105 mant = spu_xor(mant, sign);
106 borrow = spu_genb(mant, sign);
107 borrow = spu_shuffle(borrow, borrow, ((vec_uchar16){
108 4,5,6,7, 192,192,192,192,
109 12,13,14,15, 192,192,192,192}));
110 mant = spu_subx(mant, sign, borrow);
112 return ((vec_llong2)(mant));