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 _REMQUOF_H_
34 #define _REMQUOF_H_ 1
35
36 #include <spu_intrinsics.h>
37 #include "headers/vec_literal.h"
38
39
_remquof(float x,float y,int * quo)40 static __inline float _remquof(float x, float y, int *quo)
41 {
42 int n;
43 vec_int4 quotient;
44 vec_int4 four = { 4, 4, 4, 4 };
45 vec_uint4 vx, vy, z, y2, y4;
46 vec_uint4 abs_x, abs_y, abs_2x, abs_8y;
47 vec_uint4 exp_x, exp_y;
48 vec_uint4 zero_x, zero_y;
49 vec_uint4 logb_x, logb_y;
50 vec_uint4 mant_x, mant_y;
51 vec_uint4 not_ge, overflow, quo_pos;
52 vec_uint4 result, result0, resultx, cnt, sign, bias;
53 vec_uint4 sign_mask = VEC_SPLAT_U32(0x80000000);
54 vec_uint4 implied_1 = VEC_SPLAT_U32(0x00800000);
55 vec_uint4 mant_mask = VEC_SPLAT_U32(0x007FFFFF);
56
57 vx = (vec_uint4)spu_promote(x, 0);
58 vy = (vec_uint4)spu_promote(y, 0);
59
60 abs_x = spu_andc(vx, sign_mask);
61 abs_y = spu_andc(vy, sign_mask);
62
63 abs_8y = spu_add(abs_y, VEC_SPLAT_U32(0x01800000)); /* abs_2y = 8 * abs_y */
64
65 sign = spu_and(vx, sign_mask);
66
67 quo_pos = spu_cmpgt((vec_int4)spu_and(spu_xor(vx, vy), sign_mask), -1);
68
69 /* Compute abs_x = fmodf(abs_x, 8*abs_y). If y is greater than 0.125*SMAX
70 * (SMAX is the maximum representable float), then return abs_x.
71 */
72 {
73 /* Determine ilogb of abs_x and abs_8y and
74 * extract the mantissas (mant_x, mant_y)
75 */
76 exp_x = spu_rlmask(abs_x, -23);
77 exp_y = spu_rlmask(abs_8y, -23);
78
79 resultx = spu_or(spu_cmpgt(abs_8y, abs_x), spu_cmpgt(abs_y, VEC_SPLAT_U32(0x7E7FFFFF)));
80
81 zero_x = spu_cmpeq(exp_x, 0);
82 zero_y = spu_cmpeq(exp_y, 0);
83
84 logb_x = spu_add(exp_x, -127);
85 logb_y = spu_add(exp_y, -127);
86
87 mant_x = spu_andc(spu_sel(implied_1, abs_x, mant_mask), zero_x);
88 mant_y = spu_andc(spu_sel(implied_1, abs_8y, mant_mask), zero_y);
89
90 /* Compute fixed point fmod of mant_x and mant_y. Set the flag,
91 * result0, to all ones if we detect that the final result is
92 * ever 0.
93 */
94 result0 = spu_or(zero_x, zero_y);
95
96 n = spu_extract(spu_sub(logb_x, logb_y), 0);
97
98
99 while (n-- > 0) {
100 z = spu_sub(mant_x, mant_y);
101
102 result0 = spu_or(spu_cmpeq(z, 0), result0);
103
104 mant_x = spu_sel(spu_add(mant_x, mant_x), spu_add(z, z),
105 spu_cmpgt((vec_int4)z, -1));
106 }
107
108 z = spu_sub(mant_x, mant_y);
109 mant_x = spu_sel(mant_x, z, spu_cmpgt((vec_int4)z, -1));
110
111 result0 = spu_or(spu_cmpeq(mant_x, 0), result0);
112
113 /* Convert the result back to floating point and restore
114 * the sign. If we flagged the result to be zero (result0),
115 * zero it. If we flagged the result to equal its input x,
116 * (resultx) then return x.
117 */
118 cnt = spu_add(spu_cntlz(mant_x), -8);
119
120 mant_x = spu_rl(spu_andc(mant_x, implied_1), (vec_int4)cnt);
121
122 exp_y = spu_sub(exp_y, cnt);
123 result0 = spu_orc(result0, spu_cmpgt((vec_int4)exp_y, 0)); /* zero denorm results */
124 exp_y = spu_rl(exp_y, 23);
125
126 result = spu_sel(exp_y, mant_x, mant_mask);
127 abs_x = spu_sel(spu_andc(result, spu_rlmask(result0, -1)), abs_x, resultx);
128 }
129
130 /* if (x >= 4*y)
131 * x -= 4*y
132 * quotient = 4
133 * else
134 * quotient = 0
135 */
136 y4 = spu_andc(spu_add(abs_y, VEC_SPLAT_U32(0x01000000)), zero_y);
137
138 overflow = spu_cmpgt(abs_y, VEC_SPLAT_U32(0x7EFFFFFF));
139 not_ge = spu_or(spu_cmpgt(y4, abs_x), overflow);
140
141 abs_x = spu_sel((vec_uint4)spu_sub((vec_float4)abs_x, (vec_float4)y4), abs_x, not_ge);
142 quotient = spu_andc (four, (vec_int4)not_ge);
143
144 /* if (x >= 2*y
145 * x -= 2*y
146 * quotient += 2
147 */
148 y2 = spu_andc(spu_add(abs_y, implied_1), zero_y);
149 not_ge = spu_cmpgt(y2, abs_x);
150
151 abs_x = spu_sel((vec_uint4)spu_sub((vec_float4)abs_x, (vec_float4)y2), abs_x, not_ge);
152 quotient = spu_sel(spu_add(quotient, 2), quotient, not_ge);
153
154 /* if (2*x > y)
155 * x -= y
156 * if (2*x >= y) x -= y
157 */
158 abs_2x = spu_add(abs_x, implied_1);
159 bias = spu_cmpgt(abs_2x, abs_y);
160 abs_x = spu_sel(abs_x, (vec_uint4)spu_sub((vec_float4)abs_x, (vec_float4)abs_y), bias);
161 quotient = spu_sub(quotient, (vec_int4)bias);
162
163 bias = spu_andc(bias, spu_rlmaska((vec_uint4)spu_msub((vec_float4)abs_x, VEC_SPLAT_F32(2.0f), (vec_float4)abs_y), -31));
164 abs_x = spu_sel(abs_x, (vec_uint4)spu_sub((vec_float4)abs_x, (vec_float4)abs_y), bias);
165 quotient = spu_sub(quotient, (vec_int4)bias);
166
167 /* Generate a correct final sign
168 */
169 result = spu_xor(abs_x, sign);
170
171 quotient = spu_and(quotient, 7);
172 quotient = spu_sel(spu_sub(0, quotient), quotient, quo_pos);
173
174 *quo = spu_extract(quotient, 0);
175
176 return (spu_extract((vec_float4)result, 0));
177 }
178 #endif /* _REMQUOF_H_ */
179