Initialize libbullet git in 2.0_beta.
[platform/upstream/libbullet.git] / Extras / simdmathlibrary / spu / simdmath / remainderd2.h
1 /* A vector double is returned that contains the remainder xi REM yi,
2         for the corresponding elements of vector double x and vector double y.
3    Copyright (C) 2006, 2007 Sony Computer Entertainment Inc.
4    All rights reserved.
5
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.
17
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.
29  */
30
31 #ifndef ___SIMD_MATH_REMAINDERD2_H___
32 #define ___SIMD_MATH_REMAINDERD2_H___
33
34 #include <simdmath.h>
35 #include <spu_intrinsics.h>
36
37 #include <simdmath/_remainder.h>
38 #include <simdmath/fmodd2.h>
39
40
41 static inline vector double
42 _remainderd2(vector double x, vector double yy)
43 {
44   vec_uchar16 splat_hi   = ((vec_uchar16){ 0,1,2,3,0,1,2,3, 8,9,10,11, 8,9,10,11});
45   vec_uint4 y_hi;
46   vec_uint4 abs_x, abs_yy, abs_2x, abs_2y;
47   vec_uint4 bias;
48   vec_uint4 nan_out, overflow;
49   vec_uint4 result;
50   vec_uint4 half_smax = spu_splats((unsigned int)0x7FEFFFFF);
51   vec_uint4 sign_mask = (vec_uint4)(spu_splats(0x8000000000000000ULL));
52   vec_uint4 exp_mask  = (vec_uint4)(spu_splats(0x7FF0000000000000ULL));
53   vec_uint4 val_nan   = (vec_uint4)(spu_splats(0x7FF8000000000000ULL));
54   vec_uint4 vec_zero = spu_splats((unsigned int)0);
55   vec_uint4 is_zeroy;
56
57   // cut sign
58   abs_x = spu_andc((vec_uint4)x, sign_mask);
59   abs_yy = spu_andc((vec_uint4)yy, sign_mask);
60   y_hi = spu_shuffle(abs_yy, abs_yy, splat_hi);
61
62
63   // check nan out
64   is_zeroy = spu_cmpeq(abs_yy, vec_zero);
65   is_zeroy = spu_and(is_zeroy, spu_rlqwbyte(is_zeroy, 4));
66   nan_out = __vec_gt64_half(abs_yy, exp_mask);  // y > 7FF00000
67   nan_out = spu_or(nan_out, spu_cmpgt(abs_x, half_smax)); // x >= 7FF0000000000000
68   nan_out = spu_or(nan_out, is_zeroy);                    // y = 0
69   nan_out = spu_shuffle(nan_out, nan_out, splat_hi);
70
71
72   // make y x2
73   abs_2y = __rem_twice_d(abs_yy); // 2 x y
74
75   result = (vec_uint4)_fmodd2((vec_double2)abs_x, (vec_double2)abs_2y);
76
77   //  abs_x = spu_sel(spu_andc(result, sign_mask), abs_x, spu_cmpgt(y_hi, spu_splats((unsigned int)0x7FBFFFFF)));
78   abs_x = spu_sel(result, abs_x, spu_cmpgt(y_hi, spu_splats((unsigned int)0x7FEFFFFF)));
79
80   /* if (2*x > y)
81    *     x -= y
82    *     if (2*x >= y) x -= y
83    */
84   overflow = spu_cmpgt(y_hi, spu_splats((unsigned int)0x7FEFFFFF));
85   // make x2
86   abs_2x = __rem_twice_d(abs_x);  // 2 x x
87
88   bias = __vec_gt64(abs_2x, abs_yy);  // abs_2x > abs_yy
89   bias = spu_andc(bias, overflow);
90
91   abs_x = spu_sel(abs_x, __rem_sub_d(abs_x, abs_yy), bias);
92
93
94   overflow = spu_or(overflow, spu_shuffle(spu_rlmaska(abs_x, -31), vec_zero, splat_hi)); // minous
95
96   // make x2
97   abs_2x = __rem_twice_d(spu_andc(abs_x, sign_mask));  // 2 x x  unsupport minous 
98   bias = spu_andc(bias, spu_rlmaska(__rem_sub_d(abs_2x, abs_yy), -31));
99   bias = spu_andc(spu_shuffle(bias, bias, splat_hi), overflow);
100   abs_x = spu_sel(abs_x, __rem_sub_d(abs_x, abs_yy), bias);
101
102   /* select final answer 
103    */
104   result = spu_xor(abs_x, spu_and((vec_uint4)x, sign_mask)); // set sign
105   result = spu_sel(result, val_nan, nan_out); // if nan
106
107   return ((vec_double2)result);
108 }
109
110 #endif