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