Initialize libbullet git in 2.0_beta.
[platform/upstream/libbullet.git] / Extras / simdmathlibrary / spu / simdmath / ceild2.h
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.
3    All rights reserved.
4
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.
16
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.
28  */
29
30 #ifndef ___SIMD_MATH_CEILD2_H___
31 #define ___SIMD_MATH_CEILD2_H___
32
33 #include <simdmath.h>
34 #include <spu_intrinsics.h>
35
36 static inline vector double
37 _ceild2(vector double in)
38 {
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});
42   vec_int4 exp, shift;
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);
48
49   /* This function generates the following component
50    * based upon the inputs.
51    *
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.
55    *
56    * These are applied as follows:.
57    *
58    *   out = ((in & mask) | insert) + addend
59    */
60
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);
64
65   /* clamp shift to the range 0 to -31.
66    */
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);
71
72   /* addend = ((in & mask) && (in >= 0)) ? mask+1 : 0
73    */
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)));
79
80   /* insert
81    */
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);
87
88   /* replace insert
89    */
90   in = spu_sel(in, (vec_double2)insert, spu_andc((vec_ullong2)mask, sign));
91
92   /* in + addend
93    */
94   out = (vec_double2)spu_addx((vec_uint4)in, addend, spu_rlqwbyte(spu_genc((vec_uint4)in, addend), 4));
95
96   return (out);
97 }
98
99 #endif