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
34 #ifndef _CBRT_H_
35 #define _CBRT_H_ 1
36
37 #include <spu_intrinsics.h>
38 #include "headers/vec_literal.h"
39
40 static double cbrt_factors[5] = {
41 0.629960524947436484311, /* 2^(-2/3) */
42 0.793700525984099680699, /* 2^(-1/3) */
43 1.0, /* 2^(0) */
44 1.259921049894873164666, /* 2^(1/3) */
45 1.587401051968199583441 /* 2^(2/3) */
46 };
47
48 /* Compute the cube root of x to double precision.
49 */
50
_cbrt(double x)51 static __inline double _cbrt(double x)
52 {
53 vec_int4 exp, bias;
54 vec_uint4 e_div_3, e_mod_3;
55 vec_float4 bf, inv_bf;
56 vec_float4 onef = VEC_SPLAT_F32(1.0f);
57 vec_ullong2 mask;
58 vec_ullong2 mant_mask = VEC_SPLAT_U64(0xFFFFFFFFFFFFFULL);
59 vec_double2 one = VEC_SPLAT_F64(1.0);
60 vec_double2 two = VEC_SPLAT_F64(2.0);
61 vec_double2 half = VEC_SPLAT_F64(0.5);
62 /* Polynomial coefficients */
63 vec_double2 c0 = VEC_SPLAT_F64(0.354895765043919860);
64 vec_double2 c1 = VEC_SPLAT_F64(1.50819193781584896);
65 vec_double2 c2 = VEC_SPLAT_F64(-2.11499494167371287);
66 vec_double2 c3 = VEC_SPLAT_F64(2.44693122563534430);
67 vec_double2 c4 = VEC_SPLAT_F64(-1.83469277483613086);
68 vec_double2 c5 = VEC_SPLAT_F64(0.784932344976639262);
69 vec_double2 c6 = VEC_SPLAT_F64(0.145263899385486377);
70 vec_double2 in, out, mant, u, u3, ym, a, b, factor, inv_b;
71
72 in = spu_promote(x, 0);
73
74 /* Normalize the mantissa (fraction part) into the range [0.5, 1.0) and
75 * extract the exponent.
76 */
77 mant = spu_sel(half, in, mant_mask);
78 exp = spu_and(spu_rlmask((vec_int4)in, -20), 0x7FF);
79
80 /* Generate mask used to zero result if the exponent is zero (ie, <in> is
81 * either zero or a denorm
82 */
83 mask = (vec_ullong2)spu_cmpeq(exp, 0);
84 mask = spu_shuffle(mask, mask, VEC_LITERAL(vec_uchar16, 0,1,2,3,0,1,2,3,8,9,10,11,8,9,10,11));
85 exp = spu_add(exp, -1022);
86
87 u = spu_madd(mant, spu_madd(mant, spu_madd(mant, spu_madd(mant, spu_madd(mant, spu_nmsub(mant, c6, c5), c4), c3), c2), c1), c0);
88 u3 = spu_mul(spu_mul(u, u), u);
89
90 /* Compute: e_div_3 = exp/3
91 *
92 * Fetch: factor = factor[2+exp%3]
93 *
94 * The factors array contains 5 values: 2^(-2/3), 2^(-1/3), 2^0, 2^(1/3),
95 * 2^(2/3), 2^1.
96 * The fetch is done using shuffle bytes so that is can easily be extended
97 * to support SIMD compution.
98 */
99 bias = spu_rlmask(spu_rlmaska(exp, -15), -16);
100 e_div_3 = (vec_uint4)spu_rlmaska(spu_madd((vec_short8)exp, VEC_SPLAT_S16(0x5556), bias), -16);
101
102 e_mod_3 = (vec_uint4)spu_sub((vec_int4)(exp), spu_mulo((vec_short8)e_div_3, VEC_SPLAT_S16(3)));
103
104 factor = spu_promote(cbrt_factors[2+spu_extract(e_mod_3, 0)], 0);
105
106 /* Compute the estimated mantissa cube root (ym) equals:
107 * ym = (u * factor * (2.0 * mant + u3)) / (2.0 * u3 + mant);
108 */
109 a = spu_mul(spu_mul(factor, u), spu_madd(two, mant, u3));
110 b = spu_madd(two, u3, mant);
111
112 bf = spu_roundtf(b);
113 inv_bf = spu_re(bf);
114 inv_bf = spu_madd(spu_nmsub(bf, inv_bf, onef), inv_bf, inv_bf);
115
116 inv_b = spu_extend(inv_bf);
117 inv_b = spu_madd(spu_nmsub(b, inv_b, one), inv_b, inv_b);
118
119 ym = spu_mul(a, inv_b);
120 ym = spu_madd(spu_nmsub(b, ym, a), inv_b, ym);
121
122 /* Merge sign, computed exponent, and computed mantissa.
123 */
124 exp = spu_rl(spu_add((vec_int4)e_div_3, 1023), 20);
125 exp = spu_andc(exp, (vec_int4)mant_mask);
126 out = spu_sel((vec_double2)exp, in, VEC_SPLAT_U64(0x8000000000000000ULL));
127 out = spu_mul(out, ym);
128
129 out = spu_andc(out, (vec_double2)mask);
130
131 return (spu_extract(out, 0));
132 }
133
134 #endif /* _CBRT_H_ */
135