// This file is part of Eigen, a lightweight C++ template library // for linear algebra. // // Copyright (C) 2007 Julien Pommier // Copyright (C) 2014 Pedro Gonnet (pedro.gonnet@gmail.com) // Copyright (C) 2016 Gael Guennebaud <gael.guennebaud@inria.fr> // // Copyright (C) 2018 Wave Computing, Inc. // Written by: // Chris Larsen // Alexey Frunze (afrunze@wavecomp.com) // // This Source Code Form is subject to the terms of the Mozilla // Public License v. 2.0. If a copy of the MPL was not distributed // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
// Convert negative argument into NAN (quiet negative, to be specific).
Packet4f zero = (Packet4f)__builtin_msa_ldi_w(0);
Packet4i neg_mask = __builtin_msa_fclt_w(_x, zero);
Packet4i zero_mask = __builtin_msa_fceq_w(_x, zero);
Packet4f non_neg_x_or_nan = padd(_x, (Packet4f)neg_mask); // Add 0.0 or NAN.
Packet4f x = non_neg_x_or_nan;
// Extract exponent from x = mantissa * 2**exponent, where 1.0 <= mantissa < 2.0. // N.B. the exponent is one less of what frexpf() would return.
Packet4i e_int = __builtin_msa_ftint_s_w(__builtin_msa_flog2_w(x)); // Multiply x by 2**(-exponent-1) to get 0.5 <= x < 1.0 as from frexpf().
x = __builtin_msa_fexp2_w(x, (Packet4i)__builtin_msa_nori_b((v16u8)e_int, 0));
/* if (x < SQRTHF) { x = x + x - 1.0; } else { e += 1; x = x - 1.0; }
*/
Packet4f xx = padd(x, x);
Packet4i ge_mask = __builtin_msa_fcle_w(p4f_cephes_SQRTHF, x);
e_int = psub(e_int, ge_mask);
x = (Packet4f)__builtin_msa_bsel_v((v16u8)ge_mask, (v16u8)xx, (v16u8)x);
x = psub(x, p4f_1);
Packet4f e = __builtin_msa_ffint_s_w(e_int);
Packet4f y, y1, y2;
y = pmadd(p4f_cephes_log_p0, x, p4f_cephes_log_p1);
y1 = pmadd(p4f_cephes_log_p3, x, p4f_cephes_log_p4);
y2 = pmadd(p4f_cephes_log_p6, x, p4f_cephes_log_p7);
y = pmadd(y, x, p4f_cephes_log_p2);
y1 = pmadd(y1, x, p4f_cephes_log_p5);
y2 = pmadd(y2, x, p4f_cephes_log_p8);
y = pmadd(y, x3, y1);
y = pmadd(y, x3, y2);
y = pmul(y, x3);
y = pmadd(e, p4f_cephes_log_q1, y);
x = __builtin_msa_fmsub_w(x, x2, p4f_half);
x = padd(x, y);
x = pmadd(e, p4f_cephes_log_q2, x);
// x is now the logarithm result candidate. We still need to handle the // extreme arguments of zero and positive infinity, though. // N.B. if the argument is +INFINITY, x is NAN because the polynomial terms // contain infinities of both signs (see the coefficients and code above). // INFINITY - INFINITY is NAN.
// If the argument is +INFINITY, make it the new result candidate. // To achieve that we choose the smaller of the result candidate and the // argument. // This is correct for all finite pairs of values (the logarithm is smaller // than the argument). // This is also correct in the special case when the argument is +INFINITY // and the result candidate is NAN. This is because the fmin.df instruction // prefers non-NANs to NANs.
x = __builtin_msa_fmin_w(x, non_neg_x_or_nan);
// If the argument is zero (including -0.0), the result becomes -INFINITY.
Packet4i neg_infs = __builtin_msa_slli_w(zero_mask, 23);
x = (Packet4f)__builtin_msa_bsel_v((v16u8)zero_mask, (v16u8)x, (v16u8)neg_infs);
x = __builtin_msa_fmsub_w(x, x2_int_f, p4f_cephes_exp_C1);
x = __builtin_msa_fmsub_w(x, x2_int_f, p4f_cephes_exp_C2);
Packet4f z = pmul(x, x);
Packet4f y = p4f_cephes_exp_p0;
y = pmadd(y, x, p4f_cephes_exp_p1);
y = pmadd(y, x, p4f_cephes_exp_p2);
y = pmadd(y, x, p4f_cephes_exp_p3);
y = pmadd(y, x, p4f_cephes_exp_p4);
y = pmadd(y, x, p4f_cephes_exp_p5);
y = pmadd(y, z, x);
y = padd(y, p4f_1);
// y *= 2**exponent.
y = __builtin_msa_fexp2_w(y, x2_int);
Packet4f x = pabs(_x);
Packet4i tiny_mask = __builtin_msa_fclt_w(x, p4f_tanh_tiny);
// Clamp the inputs to the range [-9, 9] since anything outside // this range is -/+1.0f in single-precision.
x = (Packet4f)__builtin_msa_bsel_v((v16u8)__builtin_msa_fclt_w(p4f_tanh_hi, x), (v16u8)x,
(v16u8)p4f_tanh_hi);
// Since the polynomials are odd/even, we need x**2.
Packet4f x2 = pmul(x, x);
// Evaluate the numerator polynomial p.
Packet4f p = pmadd(x2, p4f_alpha_13, p4f_alpha_11);
p = pmadd(x2, p, p4f_alpha_9);
p = pmadd(x2, p, p4f_alpha_7);
p = pmadd(x2, p, p4f_alpha_5);
p = pmadd(x2, p, p4f_alpha_3);
p = pmadd(x2, p, p4f_alpha_1);
p = pmul(x, p);
// Evaluate the denominator polynomial q.
Packet4f q = pmadd(x2, p4f_beta_6, p4f_beta_4);
q = pmadd(x2, q, p4f_beta_2);
q = pmadd(x2, q, p4f_beta_0);
// Divide the numerator by the denominator.
p = pdiv(p, q);
// Reinstate the sign.
p = (Packet4f)__builtin_msa_binsli_w((v4u32)p, (v4u32)_x, 0);
// When the argument is very small in magnitude it's more accurate to just return it.
p = (Packet4f)__builtin_msa_bsel_v((v16u8)tiny_mask, (v16u8)p, (v16u8)_x);
// Translate infinite arguments into NANs.
Packet4f zero_or_nan_if_inf = psub(_x, _x);
x = padd(x, zero_or_nan_if_inf); // Prevent sin/cos from generating values larger than 1.0 in magnitude // for very large arguments by setting x to 0.0.
Packet4i small_or_nan_mask = __builtin_msa_fcult_w(x, p4f_sincos_max_arg);
x = pand(x, (Packet4f)small_or_nan_mask);
// Scale x by 4/Pi to find x's octant.
Packet4f y = pmul(x, p4f_cephes_FOPI); // Get the octant. We'll reduce x by this number of octants or by one more than it.
Packet4i y_int = __builtin_msa_ftrunc_s_w(y); // x's from even-numbered octants will translate to octant 0: [0, +Pi/4]. // x's from odd-numbered octants will translate to octant -1: [-Pi/4, 0]. // Adjustment for odd-numbered octants: octant = (octant + 1) & (~1).
Packet4i y_int1 = __builtin_msa_addvi_w(y_int, 1);
Packet4i y_int2 = (Packet4i)__builtin_msa_bclri_w((Packet4ui)y_int1, 0); // bclri = bit-clear
y = __builtin_msa_ffint_s_w(y_int2);
// Compute the sign to apply to the polynomial.
Packet4i sign_mask = sine ? pxor(__builtin_msa_slli_w(y_int1, 29), (Packet4i)_x)
: __builtin_msa_slli_w(__builtin_msa_addvi_w(y_int, 3), 29);
// Get the polynomial selection mask. // We'll calculate both (sin and cos) polynomials and then select from the two.
Packet4i poly_mask = __builtin_msa_ceqi_w(__builtin_msa_slli_w(y_int2, 30), 0);
// Reduce x by y octants to get: -Pi/4 <= x <= +Pi/4. // The magic pass: "Extended precision modular arithmetic" // x = ((x - y * DP1) - y * DP2) - y * DP3
Packet4f tmp1 = pmul(y, p4f_minus_cephes_DP1);
Packet4f tmp2 = pmul(y, p4f_minus_cephes_DP2);
Packet4f tmp3 = pmul(y, p4f_minus_cephes_DP3);
x = padd(x, tmp1);
x = padd(x, tmp2);
x = padd(x, tmp3);
// Evaluate the cos(x) polynomial.
y = p4f_coscof_p0;
Packet4f z = pmul(x, x);
y = pmadd(y, z, p4f_coscof_p1);
y = pmadd(y, z, p4f_coscof_p2);
y = pmul(y, z);
y = pmul(y, z);
y = __builtin_msa_fmsub_w(y, z, p4f_half);
y = padd(y, p4f_1);
// Select the correct result from the two polynomials.
y = sine ? (Packet4f)__builtin_msa_bsel_v((v16u8)poly_mask, (v16u8)y, (v16u8)y2)
: (Packet4f)__builtin_msa_bsel_v((v16u8)poly_mask, (v16u8)y2, (v16u8)y);
// Update the sign.
sign_mask = pxor(sign_mask, (Packet4i)y);
y = (Packet4f)__builtin_msa_binsli_w((v4u32)y, (v4u32)sign_mask, 0); // binsli = bit-insert-left return y;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.