1 /*
2   (C) Copyright 2001,2006,
3   International Business Machines Corporation,
4   Sony Computer Entertainment, Incorporated,
5   Toshiba Corporation,
6 
7   All rights reserved.
8 
9   Redistribution and use in source and binary forms, with or without
10   modification, are permitted provided that the following conditions are met:
11 
12     * Redistributions of source code must retain the above copyright notice,
13   this list of conditions and the following disclaimer.
14     * Redistributions in binary form must reproduce the above copyright
15   notice, this list of conditions and the following disclaimer in the
16   documentation and/or other materials provided with the distribution.
17     * Neither the names of the copyright holders nor the names of their
18   contributors may be used to endorse or promote products derived from this
19   software without specific prior written permission.
20 
21   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
22   IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23   TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
24   PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
25   OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33 #ifndef _FMOD_H_
34 #define _FMOD_H_	1
35 
36 #include <spu_intrinsics.h>
37 #include <errno.h>
38 #include "headers/vec_literal.h"
39 
40 /* This implementation returns zero if y is a denorm or zero.
41  */
_fmod(double x,double y)42 static __inline double _fmod(double x, double y)
43 {
44   int n, shift;
45   vec_uchar16 swap_words = VEC_LITERAL(vec_uchar16, 4,5,6,7, 0,1,2,3, 12,13,14,15, 8,9,10,11);
46   vec_uchar16 propagate = VEC_LITERAL(vec_uchar16, 4,5,6,7, 192,192,192,192, 12,13,14,15, 192,192,192,192);
47   vec_uchar16 splat_hi = VEC_LITERAL(vec_uchar16, 0,1,2,3,0,1,2,3, 8,9,10,11, 8,9,10,11);
48   vec_uint4 vx, vy, z;
49   vec_uint4 x_hi, y_hi;
50   vec_uint4 abs_x, abs_y;
51   vec_uint4 exp_x, exp_y;
52   vec_uint4 zero_x, zero_y;
53   vec_uint4 logb_x, logb_y;
54   vec_uint4 mant_x, mant_y;
55   vec_uint4 normal, norm, denorm;
56   vec_uint4 result, result0, resultx, cnt, sign, borrow;
57   vec_uint4 lsb       = (vec_uint4)(VEC_SPLAT_U64(0x0000000000000001ULL));
58   vec_uint4 sign_mask = (vec_uint4)(VEC_SPLAT_U64(0x8000000000000000ULL));
59   vec_uint4 implied_1 = (vec_uint4)(VEC_SPLAT_U64(0x0010000000000000ULL));
60   vec_uint4 mant_mask = (vec_uint4)(VEC_SPLAT_U64(0x000FFFFFFFFFFFFFULL));
61   vec_ullong2 domain;
62   vec_int4 verrno;
63   vec_double2 vc = { 0.0, 0.0 };
64   vec_int4 fail = { EDOM, EDOM, EDOM, EDOM };
65 
66   vx = (vec_uint4)spu_promote(x, 0);
67   vy = (vec_uint4)spu_promote(y, 0);
68 
69   abs_x = spu_andc(vx, sign_mask);
70   abs_y = spu_andc(vy, sign_mask);
71 
72   sign = spu_and(vx, sign_mask);
73 
74   x_hi = spu_shuffle(abs_x, abs_x, splat_hi);
75   y_hi = spu_shuffle(abs_y, abs_y, splat_hi);
76 
77   /* Determine ilogb of abs_x and abs_y and
78    * extract the mantissas (mant_x, mant_y)
79    */
80   exp_x  = spu_rlmask(x_hi, -20);
81   exp_y  = spu_rlmask(y_hi, -20);
82 
83   resultx = spu_cmpgt(y_hi, x_hi);
84 
85   zero_x = spu_cmpeq(exp_x, 0);
86   zero_y = spu_cmpeq(exp_y, 0);
87 
88   logb_x = spu_add(exp_x, -1023);
89   logb_y = spu_add(exp_y, -1023);
90 
91   mant_x = spu_andc(spu_sel(implied_1, abs_x, mant_mask), zero_x);
92   mant_y = spu_andc(spu_sel(implied_1, abs_y, mant_mask), zero_y);
93 
94   /* Compute fixed point fmod of mant_x and mant_y. Set the flag,
95    * result0, to all ones if we detect that the final result is
96    * ever 0.
97    */
98   result0 = spu_or(zero_x, zero_y);
99 
100   n = spu_extract(spu_sub(logb_x, logb_y), 0);
101 
102   while (n-- > 0) {
103     borrow = spu_genb(mant_x, mant_y);
104     borrow = spu_shuffle(borrow, borrow, propagate);
105     z = spu_subx(mant_x, mant_y, borrow);
106 
107     result0 = spu_or(spu_cmpeq(spu_or(z, spu_shuffle(z, z, swap_words)), 0), result0);
108 
109     mant_x = spu_sel(spu_slqw(mant_x, 1), spu_andc(spu_slqw(z, 1), lsb),
110                      spu_cmpgt((vec_int4)spu_shuffle(z, z, splat_hi), -1));
111   }
112 
113   borrow = spu_genb(mant_x, mant_y);
114   borrow = spu_shuffle(borrow, borrow, propagate);
115   z = spu_subx(mant_x, mant_y, borrow);
116 
117   mant_x = spu_sel(mant_x, z,
118                    spu_cmpgt((vec_int4)spu_shuffle(z, z, splat_hi), -1));
119   mant_x = spu_andc(mant_x, VEC_LITERAL(vec_uint4, 0,0,-1,-1));
120 
121   result0 = spu_or(spu_cmpeq(spu_or(mant_x, spu_shuffle(mant_x, mant_x, swap_words)), 0), result0);
122 
123   /* Convert the result back to floating point and restore
124    * the sign. If we flagged the result to be zero (result0),
125    * zero it. If we flagged the result to equal its input x,
126    * (resultx) then return x.
127    *
128    * Double precision generates a denorm for an output.
129    */
130   cnt = spu_cntlz(mant_x);
131   cnt = spu_add(cnt, spu_and(spu_rlqwbyte(cnt, 4), spu_cmpeq(cnt, 32)));
132   cnt = spu_add(spu_shuffle(cnt, cnt, splat_hi), -11);
133 
134   shift = spu_extract(exp_y, 0) - 1;
135   denorm = spu_slqwbytebc(spu_slqw(mant_x, shift), shift);
136 
137   exp_y = spu_sub(exp_y, cnt);
138 
139   normal = spu_cmpgt((vec_int4)exp_y, 0);
140 
141   /* Normalize normal results, denormalize denorm results.
142    */
143   shift = spu_extract(cnt, 0);
144   norm = spu_slqwbytebc(spu_slqw(spu_andc(mant_x, VEC_LITERAL(vec_uint4, 0x00100000, 0, -1, -1)), shift), shift);
145 
146   mant_x = spu_sel(denorm, norm, normal);
147 
148   exp_y = spu_and(spu_rl(exp_y, 20), normal);
149 
150   result = spu_sel(exp_y, spu_or(sign, mant_x), VEC_LITERAL(vec_uint4, 0x800FFFFF, -1, 0x800FFFFF, -1));
151 
152   result = spu_sel(spu_andc(result, spu_rlmask(result0, -1)), vx,
153                    resultx);
154 
155 #ifndef _IEEE_LIBM
156   /*
157    * If y is zero, set errno to EDOM
158    */
159   domain = spu_cmpeq(vc, (vec_double2) vy);
160   verrno = spu_splats(errno);
161   errno = spu_extract(spu_sel(verrno, fail, (vector unsigned int) domain), 0);
162 #endif
163 
164   return (spu_extract((vec_double2)result, 0));
165 }
166 #endif /* _FMOD_H_ */
167