1 /* ceild2 - for each of two doule slots, round up to smallest integer not less than the value.
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_CEILD2_H___
31 #define ___SIMD_MATH_CEILD2_H___
34 #include <spu_intrinsics.h>
36 static inline vector double
37 _ceild2(vector double in)
39 vec_uchar16 swap_words = ((vec_uchar16){4,5,6,7, 0,1,2,3, 12,13,14,15, 8,9,10,11});
40 vec_uchar16 splat_hi = ((vec_uchar16){0,1,2,3,0,1,2,3, 8,9,10,11, 8,9,10,11});
41 vec_uint4 one = ((vec_uint4){0, 1, 0, 1});
43 vec_uint4 mask, mask_1, frac_mask, addend, insert, pos, equal0, e_0, e_00, e_sign, exp_ge0;
44 vec_ullong2 sign = spu_splats(0x8000000000000000ULL);
45 vec_double2 in_hi, out;
46 vec_double2 one_d = spu_splats((double)1.0);
47 vec_uint4 zero = spu_splats((unsigned int)0x0);
49 /* This function generates the following component
50 * based upon the inputs.
52 * mask = bits of the input that need to be replaced.
53 * insert = value of the bits that need to be replaced
54 * addend = value to be added to perform function.
56 * These are applied as follows:.
58 * out = ((in & mask) | insert) + addend
61 in_hi = spu_shuffle(in, in, splat_hi);
62 exp = spu_and(spu_rlmask((vec_int4)in_hi, -20), 0x7FF);
63 shift = spu_sub(((vec_int4){1023, 1043, 1023, 1043}), exp);
65 /* clamp shift to the range 0 to -31.
67 shift = spu_sel(spu_splats((int)-32), spu_andc(shift, (vec_int4)spu_cmpgt(shift, 0)), spu_cmpgt(shift, -32));
68 frac_mask = spu_rlmask(((vec_uint4){0xFFFFF, -1, 0xFFFFF, -1}), shift);
69 exp_ge0 = spu_cmpgt(exp, 0x3FE);
70 mask = spu_orc(frac_mask, exp_ge0);
72 /* addend = ((in & mask) && (in >= 0)) ? mask+1 : 0
74 mask_1 = spu_addx(mask, one, spu_rlqwbyte(spu_genc(mask, one), 4));
75 pos = spu_cmpgt((vec_int4)in_hi, -1);
76 //pos = spu_cmpgt((vec_int4)in_hi, 0x0); //it is also work
77 equal0 = spu_cmpeq(spu_and((vec_uint4)in, mask), 0);
78 addend = spu_andc(spu_and(mask_1, pos), spu_and(equal0, spu_shuffle(equal0, equal0, swap_words)));
82 e_0 = spu_cmpeq(spu_andc((vec_uint4)in, (vec_uint4)sign), zero);
83 e_00 = spu_and(e_0, spu_shuffle(e_0, e_0, swap_words));
84 // e_sign = spu_sel(spu_splats((unsigned int)0x0), (vec_uint4)one_d, spu_cmpeq( spu_and((vec_uint4)in_hi, spu_splats((unsigned int)0x80000000)), zero));
85 e_sign = spu_and( (vec_uint4)one_d, spu_cmpeq( spu_and((vec_uint4)in_hi,spu_splats((unsigned int)0x80000000)), zero));
86 insert =spu_andc(spu_andc(e_sign, e_00), exp_ge0);
90 in = spu_sel(in, (vec_double2)insert, spu_andc((vec_ullong2)mask, sign));
94 out = (vec_double2)spu_addx((vec_uint4)in, addend, spu_rlqwbyte(spu_genc((vec_uint4)in, addend), 4));