246 lines · c
1//===-- Single-precision atan2f float function ----------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef LIBC_SRC___SUPPORT_MATH_ATAN2F_FLOAT_H10#define LIBC_SRC___SUPPORT_MATH_ATAN2F_FLOAT_H11 12#include "src/__support/FPUtil/FPBits.h"13#include "src/__support/FPUtil/double_double.h"14#include "src/__support/FPUtil/multiply_add.h"15#include "src/__support/FPUtil/nearest_integer.h"16#include "src/__support/macros/config.h"17#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY18 19namespace LIBC_NAMESPACE_DECL {20 21namespace math {22 23namespace atan2f_internal {24 25using FloatFloat = fputil::FloatFloat;26 27// atan(i/64) with i = 0..16, generated by Sollya with:28// > for i from 0 to 16 do {29// a = round(atan(i/16), SG, RN);30// b = round(atan(i/16) - a, SG, RN);31// print("{", b, ",", a, "},");32// };33static constexpr FloatFloat ATAN_I[17] = {34 {0.0f, 0.0f},35 {-0x1.1a6042p-30f, 0x1.ff55bcp-5f},36 {-0x1.54f424p-30f, 0x1.fd5baap-4f},37 {0x1.79cb6p-28f, 0x1.7b97b4p-3f},38 {-0x1.b4dfc8p-29f, 0x1.f5b76p-3f},39 {-0x1.1f0286p-27f, 0x1.362774p-2f},40 {0x1.e4defp-30f, 0x1.6f6194p-2f},41 {0x1.e611fep-29f, 0x1.a64eecp-2f},42 {0x1.586ed4p-28f, 0x1.dac67p-2f},43 {-0x1.6499e6p-26f, 0x1.0657eap-1f},44 {0x1.7bdfd6p-26f, 0x1.1e00bap-1f},45 {-0x1.98e422p-28f, 0x1.345f02p-1f},46 {0x1.934f7p-28f, 0x1.4978fap-1f},47 {0x1.c5a6c6p-27f, 0x1.5d5898p-1f},48 {0x1.5e118cp-27f, 0x1.700a7cp-1f},49 {-0x1.1d4eb6p-26f, 0x1.819d0cp-1f},50 {-0x1.777a5cp-26f, 0x1.921fb6p-1f},51};52 53// Approximate atan(x) for |x| <= 2^-5.54// Using degree-3 Taylor polynomial:55// P = x - x^3/356// Then the absolute error is bounded by:57// |atan(x) - P(x)| < |x|^5/5 < 2^(-5*5) / 5 < 2^-27.58// And the relative error is bounded by:59// |(atan(x) - P(x))/atan(x)| < |x|^4 / 4 < 2^-22.60// For x = x_hi + x_lo, fully expand the polynomial and drop any terms less than61// ulp(x_hi^3 / 3) gives us:62// P(x) ~ x_hi - x_hi^3/3 + x_lo * (1 - x_hi^2)63LIBC_INLINE static constexpr FloatFloat atan_eval(const FloatFloat &x) {64 FloatFloat p;65 p.hi = x.hi;66 float x_hi_sq = x.hi * x.hi;67 // c0 ~ - x_hi^2 / 368 float c0 = -0x1.555556p-2f * x_hi_sq;69 // c1 ~ x_lo * (1 - x_hi^2)70 float c1 = fputil::multiply_add(x_hi_sq, -x.lo, x.lo);71 // p.lo ~ - x_hi^3 / 3 + x_lo * (1 - x_hi*2)72 p.lo = fputil::multiply_add(x.hi, c0, c1);73 return p;74}75 76} // namespace atan2f_internal77 78// There are several range reduction steps we can take for atan2(y, x) as79// follow:80 81// * Range reduction 1: signness82// atan2(y, x) will return a number between -PI and PI representing the angle83// forming by the 0x axis and the vector (x, y) on the 0xy-plane.84// In particular, we have that:85// atan2(y, x) = atan( y/x ) if x >= 0 and y >= 0 (I-quadrant)86// = pi + atan( y/x ) if x < 0 and y >= 0 (II-quadrant)87// = -pi + atan( y/x ) if x < 0 and y < 0 (III-quadrant)88// = atan( y/x ) if x >= 0 and y < 0 (IV-quadrant)89// Since atan function is odd, we can use the formula:90// atan(-u) = -atan(u)91// to adjust the above conditions a bit further:92// atan2(y, x) = atan( |y|/|x| ) if x >= 0 and y >= 0 (I-quadrant)93// = pi - atan( |y|/|x| ) if x < 0 and y >= 0 (II-quadrant)94// = -pi + atan( |y|/|x| ) if x < 0 and y < 0 (III-quadrant)95// = -atan( |y|/|x| ) if x >= 0 and y < 0 (IV-quadrant)96// Which can be simplified to:97// atan2(y, x) = sign(y) * atan( |y|/|x| ) if x >= 098// = sign(y) * (pi - atan( |y|/|x| )) if x < 099 100// * Range reduction 2: reciprocal101// Now that the argument inside atan is positive, we can use the formula:102// atan(1/x) = pi/2 - atan(x)103// to make the argument inside atan <= 1 as follow:104// atan2(y, x) = sign(y) * atan( |y|/|x|) if 0 <= |y| <= x105// = sign(y) * (pi/2 - atan( |x|/|y| ) if 0 <= x < |y|106// = sign(y) * (pi - atan( |y|/|x| )) if 0 <= |y| <= -x107// = sign(y) * (pi/2 + atan( |x|/|y| )) if 0 <= -x < |y|108 109// * Range reduction 3: look up table.110// After the previous two range reduction steps, we reduce the problem to111// compute atan(u) with 0 <= u <= 1, or to be precise:112// atan( n / d ) where n = min(|x|, |y|) and d = max(|x|, |y|).113// An accurate polynomial approximation for the whole [0, 1] input range will114// require a very large degree. To make it more efficient, we reduce the input115// range further by finding an integer idx such that:116// | n/d - idx/16 | <= 1/32.117// In particular,118// idx := 2^-4 * round(2^4 * n/d)119// Then for the fast pass, we find a polynomial approximation for:120// atan( n/d ) ~ atan( idx/16 ) + (n/d - idx/16) * Q(n/d - idx/16)121// with Q(x) = x - x^3/3 be the cubic Taylor polynomial of atan(x).122// It's error in float-float precision is estimated in Sollya to be:123// > P = x - x^3/3;124// > dirtyinfnorm(atan(x) - P, [-2^-5, 2^-5]);125// 0x1.995...p-28.126 127LIBC_INLINE static constexpr float atan2f(float y, float x) {128 using namespace atan2f_internal;129 using FPBits = typename fputil::FPBits<float>;130 constexpr float IS_NEG[2] = {1.0f, -1.0f};131 constexpr FloatFloat ZERO = {0.0f, 0.0f};132 constexpr FloatFloat MZERO = {-0.0f, -0.0f};133 constexpr FloatFloat PI = {-0x1.777a5cp-24f, 0x1.921fb6p1f};134 constexpr FloatFloat MPI = {0x1.777a5cp-24f, -0x1.921fb6p1f};135 constexpr FloatFloat PI_OVER_4 = {-0x1.777a5cp-26f, 0x1.921fb6p-1f};136 constexpr FloatFloat PI_OVER_2 = {-0x1.777a5cp-25f, 0x1.921fb6p0f};137 constexpr FloatFloat MPI_OVER_2 = {-0x1.777a5cp-25f, 0x1.921fb6p0f};138 constexpr FloatFloat THREE_PI_OVER_4 = {-0x1.99bc5cp-28f, 0x1.2d97c8p1f};139 // Adjustment for constant term:140 // CONST_ADJ[x_sign][y_sign][recip]141 constexpr FloatFloat CONST_ADJ[2][2][2] = {142 {{ZERO, MPI_OVER_2}, {MZERO, MPI_OVER_2}},143 {{MPI, PI_OVER_2}, {MPI, PI_OVER_2}}};144 145 FPBits x_bits(x), y_bits(y);146 bool x_sign = x_bits.sign().is_neg();147 bool y_sign = y_bits.sign().is_neg();148 x_bits = x_bits.abs();149 y_bits = y_bits.abs();150 uint32_t x_abs = x_bits.uintval();151 uint32_t y_abs = y_bits.uintval();152 bool recip = x_abs < y_abs;153 uint32_t min_abs = recip ? x_abs : y_abs;154 uint32_t max_abs = !recip ? x_abs : y_abs;155 auto min_exp = static_cast<unsigned>(min_abs >> FPBits::FRACTION_LEN);156 auto max_exp = static_cast<unsigned>(max_abs >> FPBits::FRACTION_LEN);157 158 float num = FPBits(min_abs).get_val();159 float den = FPBits(max_abs).get_val();160 161 // Check for exceptional cases, whether inputs are 0, inf, nan, or close to162 // overflow, or close to underflow.163 if (LIBC_UNLIKELY(max_exp > 0xffU - 64U || min_exp < 64U)) {164 if (x_bits.is_nan() || y_bits.is_nan())165 return FPBits::quiet_nan().get_val();166 unsigned x_except = x == 0.0f ? 0 : (FPBits(x_abs).is_inf() ? 2 : 1);167 unsigned y_except = y == 0.0f ? 0 : (FPBits(y_abs).is_inf() ? 2 : 1);168 169 // Exceptional cases:170 // EXCEPT[y_except][x_except][x_is_neg]171 // with x_except & y_except:172 // 0: zero173 // 1: finite, non-zero174 // 2: infinity175 constexpr FloatFloat EXCEPTS[3][3][2] = {176 {{ZERO, PI}, {ZERO, PI}, {ZERO, PI}},177 {{PI_OVER_2, PI_OVER_2}, {ZERO, ZERO}, {ZERO, PI}},178 {{PI_OVER_2, PI_OVER_2},179 {PI_OVER_2, PI_OVER_2},180 {PI_OVER_4, THREE_PI_OVER_4}},181 };182 183 if ((x_except != 1) || (y_except != 1)) {184 FloatFloat r = EXCEPTS[y_except][x_except][x_sign];185 return fputil::multiply_add(IS_NEG[y_sign], r.hi, IS_NEG[y_sign] * r.lo);186 }187 bool scale_up = min_exp < 64U;188 bool scale_down = max_exp > 0xffU - 64U;189 // At least one input is denormal, multiply both numerator and denominator190 // by some large enough power of 2 to normalize denormal inputs.191 if (scale_up) {192 num *= 0x1.0p32f;193 if (!scale_down)194 den *= 0x1.0p32f;195 } else if (scale_down) {196 den *= 0x1.0p-32f;197 num *= 0x1.0p-32f;198 }199 200 min_abs = FPBits(num).uintval();201 max_abs = FPBits(den).uintval();202 min_exp = static_cast<unsigned>(min_abs >> FPBits::FRACTION_LEN);203 max_exp = static_cast<unsigned>(max_abs >> FPBits::FRACTION_LEN);204 }205 206 float final_sign = IS_NEG[(x_sign != y_sign) != recip];207 FloatFloat const_term = CONST_ADJ[x_sign][y_sign][recip];208 unsigned exp_diff = max_exp - min_exp;209 // We have the following bound for normalized n and d:210 // 2^(-exp_diff - 1) < n/d < 2^(-exp_diff + 1).211 if (LIBC_UNLIKELY(exp_diff > 25))212 return fputil::multiply_add(final_sign, const_term.hi,213 final_sign * (const_term.lo + num / den));214 215 float k = fputil::nearest_integer(16.0f * num / den);216 unsigned idx = static_cast<unsigned>(k);217 // k = idx / 16218 k *= 0x1.0p-4f;219 220 // Range reduction:221 // atan(n/d) - atan(k/64) = atan((n/d - k/16) / (1 + (n/d) * (k/16)))222 // = atan((n - d * k/16)) / (d + n * k/16))223 FloatFloat num_k = fputil::exact_mult(num, k);224 FloatFloat den_k = fputil::exact_mult(den, k);225 226 // num_dd = n - d * k227 FloatFloat num_ff = fputil::exact_add(num - den_k.hi, -den_k.lo);228 // den_dd = d + n * k229 FloatFloat den_ff = fputil::exact_add(den, num_k.hi);230 den_ff.lo += num_k.lo;231 232 // q = (n - d * k) / (d + n * k)233 FloatFloat q = fputil::div(num_ff, den_ff);234 // p ~ atan(q)235 FloatFloat p = atan_eval(q);236 237 FloatFloat r = fputil::add(const_term, fputil::add(ATAN_I[idx], p));238 return final_sign * r.hi;239}240 241} // namespace math242 243} // namespace LIBC_NAMESPACE_DECL244 245#endif // LIBC_SRC___SUPPORT_MATH_ATAN2F_FLOAT_H246