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 _REMAINDER_H_
34 #define _REMAINDER_H_ 1
35
36 #include <spu_intrinsics.h>
37 #include "headers/vec_literal.h"
38
_remainder(double x,double y)39 static __inline double _remainder(double x, double y)
40 {
41 int n, shift;
42 vec_uchar16 swap_words = VEC_LITERAL(vec_uchar16, 4,5,6,7, 0,1,2,3, 12,13,14,15, 8,9,10,11);
43 vec_uchar16 propagate = VEC_LITERAL(vec_uchar16, 4,5,6,7, 192,192,192,192, 12,13,14,15, 192,192,192,192);
44 vec_uchar16 splat_hi = VEC_LITERAL(vec_uchar16, 0,1,2,3,0,1,2,3, 8,9,10,11, 8,9,10,11);
45 vec_uchar16 splat_lo = VEC_LITERAL(vec_uchar16, 4,5,6,7,4,5,6,7, 12,13,14,15, 12,13,14,15);
46 vec_uint4 vx, vy, z;
47 vec_uint4 x_hi, y_hi, y_lo;
48 vec_uint4 abs_x, abs_y, abs_2x, abs_2y;
49 vec_uint4 exp_x, exp_y;
50 vec_uint4 zero_x, zero_y;
51 vec_uint4 logb_x, logb_y;
52 vec_uint4 mant_x, mant_y;
53 vec_uint4 normal, norm, denorm;
54 vec_uint4 gt, eq, bias, y2_hi;
55 vec_uint4 nan_out;
56 vec_uint4 result, result0, resultx, cnt, sign, borrow;
57 vec_uint4 exp_special = VEC_SPLAT_U32(0x7FF00000);
58 vec_uint4 half_smax = VEC_SPLAT_U32(0x7FEFFFFF);
59 vec_uint4 lsb = (vec_uint4)(VEC_SPLAT_U64(0x0000000000000001ULL));
60 vec_uint4 sign_mask = (vec_uint4)(VEC_SPLAT_U64(0x8000000000000000ULL));
61 vec_uint4 implied_1 = (vec_uint4)(VEC_SPLAT_U64(0x0010000000000000ULL));
62 vec_uint4 mant_mask = (vec_uint4)(VEC_SPLAT_U64(0x000FFFFFFFFFFFFFULL));
63
64 vx = (vec_uint4)spu_promote(x, 0);
65 vy = (vec_uint4)spu_promote(y, 0);
66
67 abs_x = spu_andc(vx, sign_mask);
68 abs_y = spu_andc(vy, sign_mask);
69
70 abs_2y = spu_add(abs_y, implied_1);
71
72 sign = spu_and(vx, sign_mask);
73
74
75 /* Compute abs_x = fmodf(abs_x, 2*abs_y). If y is greater than 0.5*SMAX (SMAX is the maximum
76 * representable float), then return abs_x.
77 */
78 {
79 x_hi = spu_shuffle(abs_x, abs_x, splat_hi);
80 y_lo = spu_shuffle(abs_y, abs_y, splat_lo);
81 y_hi = spu_shuffle(abs_y, abs_y, splat_hi);
82 y2_hi = spu_shuffle(abs_2y, abs_2y, splat_hi);
83
84 /* Force a NaN output if (1) abs_x is infinity or NaN or (2)
85 * abs_y is a NaN.
86 */
87 nan_out = spu_or(spu_cmpgt(x_hi, half_smax),
88 spu_or(spu_cmpgt(y_hi, exp_special),
89 spu_and(spu_cmpeq(y_hi, exp_special),
90 spu_cmpgt(y_lo, 0))));
91
92 /* Determine ilogb of abs_x and abs_y and
93 * extract the mantissas (mant_x, mant_y)
94 */
95 exp_x = spu_rlmask(x_hi, -20);
96 exp_y = spu_rlmask(y2_hi, -20);
97
98 resultx = spu_or(spu_cmpgt(y2_hi, x_hi), spu_cmpgt(y_hi, half_smax));
99
100 zero_x = spu_cmpeq(exp_x, 0);
101 zero_y = spu_cmpeq(exp_y, 0);
102
103 logb_x = spu_add(exp_x, -1023);
104 logb_y = spu_add(exp_y, -1023);
105
106 mant_x = spu_andc(spu_sel(implied_1, abs_x, mant_mask), zero_x);
107 mant_y = spu_andc(spu_sel(implied_1, abs_2y, mant_mask), zero_y);
108
109 /* Compute fixed point fmod of mant_x and mant_y. Set the flag,
110 * result0, to all ones if we detect that the final result is
111 * ever 0.
112 */
113 result0 = spu_or(zero_x, zero_y);
114
115 n = spu_extract(spu_sub(logb_x, logb_y), 0);
116
117 while (n-- > 0) {
118 borrow = spu_genb(mant_x, mant_y);
119 borrow = spu_shuffle(borrow, borrow, propagate);
120 z = spu_subx(mant_x, mant_y, borrow);
121
122 result0 = spu_or(spu_cmpeq(spu_or(z, spu_shuffle(z, z, swap_words)), 0), result0);
123
124 mant_x = spu_sel(spu_slqw(mant_x, 1), spu_andc(spu_slqw(z, 1), lsb), spu_cmpgt((vec_int4)spu_shuffle(z, z, splat_hi), -1));
125 }
126
127
128 borrow = spu_genb(mant_x, mant_y);
129 borrow = spu_shuffle(borrow, borrow, propagate);
130 z = spu_subx(mant_x, mant_y, borrow);
131
132 mant_x = spu_sel(mant_x, z, spu_cmpgt((vec_int4)spu_shuffle(z, z, splat_hi), -1));
133 mant_x = spu_andc(mant_x, VEC_LITERAL(vec_uint4, 0,0,-1,-1));
134
135 result0 = spu_or(spu_cmpeq(spu_or(mant_x, spu_shuffle(mant_x, mant_x, swap_words)), 0), result0);
136
137 /* Convert the result back to floating point and restore
138 * the sign. If we flagged the result to be zero (result0),
139 * zero it. If we flagged the result to equal its input x,
140 * (resultx) then return x.
141 *
142 * Double precision generates a denorm for an output.
143 */
144 cnt = spu_cntlz(mant_x);
145 cnt = spu_add(cnt, spu_and(spu_rlqwbyte(cnt, 4), spu_cmpeq(cnt, 32)));
146 cnt = spu_add(spu_shuffle(cnt, cnt, splat_hi), -11);
147
148 shift = spu_extract(exp_y, 0) - 1;
149 denorm = spu_slqwbytebc(spu_slqw(mant_x, shift), shift);
150
151 exp_y = spu_sub(exp_y, cnt);
152
153 normal = spu_cmpgt((vec_int4)exp_y, 0);
154
155 /* Normalize normal results, denormalize denorm results.
156 */
157 shift = spu_extract(cnt, 0);
158 norm = spu_slqwbytebc(spu_slqw(spu_andc(mant_x, VEC_LITERAL(vec_uint4, 0x00100000, 0, -1, -1)), shift), shift);
159
160 mant_x = spu_sel(denorm, norm, normal);
161
162 exp_y = spu_and(spu_rl(exp_y, 20), normal);
163
164 result = spu_sel(exp_y, mant_x, mant_mask);
165
166 abs_x = spu_sel(spu_andc(result, spu_rlmask(result0, -1)), abs_x, resultx);
167
168 }
169
170 /* if (2*x > y)
171 * x -= y
172 * if (2*x >= y) x -= y
173 */
174 abs_2x = spu_and(spu_add(abs_x, implied_1), normal);
175
176 gt = spu_cmpgt(abs_2x, abs_y);
177 eq = spu_cmpeq(abs_2x, abs_y);
178 bias = spu_or(gt, spu_and(eq, spu_rlqwbyte(gt, 4)));
179 bias = spu_shuffle(bias, bias, splat_hi);
180 abs_x = spu_sel(abs_x, (vec_uint4)spu_sub((vec_double2)abs_x, (vec_double2)abs_y), bias);
181
182 bias = spu_andc(bias, spu_rlmaska((vec_uint4)spu_msub((vec_double2)abs_x, VEC_SPLAT_F64(2.0), (vec_double2)abs_y), -31));
183 bias = spu_shuffle(bias, bias, splat_hi);
184 abs_x = spu_sel(abs_x, (vec_uint4)spu_sub((vec_double2)abs_x, (vec_double2)abs_y), bias);
185
186 /* Generate a correct final sign
187 */
188 result = spu_sel(spu_xor(abs_x, sign), exp_special, nan_out);
189
190 return (spu_extract((vec_double2)result, 0));
191 }
192 #endif /* _REMAINDER_H_ */
193