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 _CBRTF_H_
34 #define _CBRTF_H_ 1
35
36 #include <spu_intrinsics.h>
37 #include "headers/vec_literal.h"
38
39 static double cbrt_factors[5] = {
40 0.629960524947436484311, /* 2^(-2/3) */
41 0.793700525984099680699, /* 2^(-1/3) */
42 1.0, /* 2^(0) */
43 1.259921049894873164666, /* 2^(1/3) */
44 1.587401051968199583441 /* 2^(2/3) */
45 };
46
47 /* Compute the cube root of the floating point input x.
48 */
49
_cbrtf(float x)50 static __inline float _cbrtf(float x)
51 {
52 vec_int4 exp, bias;
53 vec_uint4 mask, e_div_3, e_mod_3;
54 vec_uint4 mant_mask = VEC_SPLAT_U32(0x7FFFFF);
55 vec_float4 in;
56 vec_float4 half = VEC_SPLAT_F32(0.5f);
57 vec_float4 onef = VEC_SPLAT_F32(1.0f);
58 vec_float4 out, mant, ym, bf, inv_bf;
59 vec_double2 two = VEC_SPLAT_F64(2.0);
60 /* Polynomial coefficients */
61 vec_double2 c2 = VEC_SPLAT_F64(0.191502161678719066);
62 vec_double2 c1 = VEC_SPLAT_F64(0.697570460207922770);
63 vec_double2 c0 = VEC_SPLAT_F64(0.492659620528969547);
64 vec_double2 a0, b0, inv_b0, ym0;
65 vec_double2 mant0, u0, u0_3, factor0;
66
67 in = spu_promote(x, 0);
68
69 /* Normalize the mantissa (fraction part) into the range [0.5, 1.0) and
70 * extract the exponent.
71 */
72 mant = spu_sel(half, in, mant_mask);
73 exp = spu_and(spu_rlmask((vec_int4)in, -23), 0xFF);
74
75 /* Generate mask used to zero result if the exponent is zero (ie, in is either
76 * zero or a denorm
77 */
78 mask = spu_cmpeq(exp, 0);
79 exp = spu_add(exp, -126);
80
81 mant0 = spu_extend(mant);
82
83 u0 = spu_madd(mant0, spu_nmsub(mant0, c2, c1), c0);
84 u0_3 = spu_mul(spu_mul(u0, u0), u0);
85
86 /* Compute: e_div_3 = exp/3
87 *
88 * Fetch: factor = factor[2+exp%3]
89 *
90 * The factors array contains 5 values: 2^(-2/3), 2^(-1/3), 2^0, 2^(1/3), 2^(2/3), 2^1.
91 */
92 bias = spu_rlmask(spu_rlmaska(exp, -15), -16);
93 e_div_3 = (vec_uint4)spu_rlmaska(spu_madd((vec_short8)exp, VEC_SPLAT_S16(0x5556), bias), -16);
94
95 e_mod_3 = (vec_uint4)spu_sub((vec_int4)(exp), spu_mulo((vec_short8)e_div_3, VEC_SPLAT_S16(3)));
96
97 e_mod_3 = spu_add(e_mod_3, 2);
98
99 factor0 = spu_promote(cbrt_factors[spu_extract(e_mod_3, 0)], 0);
100
101 /* Compute the estimated mantissa cube root (ym) equals:
102 * ym = (u * factor * (2.0 * mant + u3)) / (2.0 * u3 + mant);
103 */
104 a0 = spu_mul(spu_mul(factor0, u0), spu_madd(two, mant0, u0_3));
105 b0 = spu_madd(two, u0_3, mant0);
106
107 bf = spu_roundtf(b0);
108
109 inv_bf = spu_re(bf);
110 inv_bf = spu_madd(spu_nmsub(bf, inv_bf, onef), inv_bf, inv_bf);
111
112 inv_b0 = spu_extend(inv_bf);
113
114 ym0 = spu_mul(a0, inv_b0);
115 ym0 = spu_madd(spu_nmsub(b0, ym0, a0), inv_b0, ym0);
116
117 ym = spu_roundtf(ym0);
118
119 /* Merge sign, computed exponent, and computed mantissa.
120 */
121 exp = spu_rl(spu_add((vec_int4)e_div_3, 127), 23);
122 out = spu_sel((vec_float4)exp, in, VEC_SPLAT_U32(0x80000000));
123 out = spu_mul(out, ym);
124
125 out = spu_andc(out, (vec_float4)mask);
126
127 return (spu_extract(out, 0));
128 }
129
130 #endif /* _CBRTF_H_ */
131