Path: blob/main/contrib/arm-optimized-routines/math/aarch64/experimental/atan2f_3u.c
48378 views
/*1* Single-precision scalar atan2(x) function.2*3* Copyright (c) 2021-2024, Arm Limited.4* SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception5*/67#include <stdbool.h>89#include "atanf_common.h"10#include "math_config.h"11#include "test_sig.h"12#include "test_defs.h"1314#define Pi (0x1.921fb6p+1f)15#define PiOver2 (0x1.921fb6p+0f)16#define PiOver4 (0x1.921fb6p-1f)17#define SignMask (0x80000000)1819/* We calculate atan2f by P(n/d), where n and d are similar to the input20arguments, and P is a polynomial. The polynomial may underflow.21POLY_UFLOW_BOUND is the lower bound of the difference in exponents of n and22d for which P underflows, and is used to special-case such inputs. */23#define POLY_UFLOW_BOUND 242425static inline int32_t26biased_exponent (float f)27{28uint32_t fi = asuint (f);29int32_t ex = (int32_t) ((fi & 0x7f800000) >> 23);30if (unlikely (ex == 0))31{32/* Subnormal case - we still need to get the exponent right for subnormal33numbers as division may take us back inside the normal range. */34return ex - __builtin_clz (fi << 9);35}36return ex;37}3839/* Fast implementation of scalar atan2f. Largest observed error is402.88ulps in [99.0, 101.0] x [99.0, 101.0]:41atan2f(0x1.9332d8p+6, 0x1.8cb6c4p+6) got 0x1.964646p-142want 0x1.964640p-1. */43float44atan2f (float y, float x)45{46uint32_t ix = asuint (x);47uint32_t iy = asuint (y);4849uint32_t sign_x = ix & SignMask;50uint32_t sign_y = iy & SignMask;5152uint32_t iax = ix & ~SignMask;53uint32_t iay = iy & ~SignMask;5455/* x or y is NaN. */56if ((iax > 0x7f800000) || (iay > 0x7f800000))57return x + y;5859/* m = 2 * sign(x) + sign(y). */60uint32_t m = ((iy >> 31) & 1) | ((ix >> 30) & 2);6162/* The following follows glibc ieee754 implementation, except63that we do not use +-tiny shifts (non-nearest rounding mode). */6465int32_t exp_diff = biased_exponent (x) - biased_exponent (y);6667/* Special case for (x, y) either on or very close to the x axis. Either y =680, or y is tiny and x is huge (difference in exponents >=69POLY_UFLOW_BOUND). In the second case, we only want to use this special70case when x is negative (i.e. quadrants 2 or 3). */71if (unlikely (iay == 0 || (exp_diff >= POLY_UFLOW_BOUND && m >= 2)))72{73switch (m)74{75case 0:76case 1:77return y; /* atan(+-0,+anything)=+-0. */78case 2:79return Pi; /* atan(+0,-anything) = pi. */80case 3:81return -Pi; /* atan(-0,-anything) =-pi. */82}83}84/* Special case for (x, y) either on or very close to the y axis. Either x =850, or x is tiny and y is huge (difference in exponents >=86POLY_UFLOW_BOUND). */87if (unlikely (iax == 0 || exp_diff <= -POLY_UFLOW_BOUND))88return sign_y ? -PiOver2 : PiOver2;8990/* x is INF. */91if (iax == 0x7f800000)92{93if (iay == 0x7f800000)94{95switch (m)96{97case 0:98return PiOver4; /* atan(+INF,+INF). */99case 1:100return -PiOver4; /* atan(-INF,+INF). */101case 2:102return 3.0f * PiOver4; /* atan(+INF,-INF). */103case 3:104return -3.0f * PiOver4; /* atan(-INF,-INF). */105}106}107else108{109switch (m)110{111case 0:112return 0.0f; /* atan(+...,+INF). */113case 1:114return -0.0f; /* atan(-...,+INF). */115case 2:116return Pi; /* atan(+...,-INF). */117case 3:118return -Pi; /* atan(-...,-INF). */119}120}121}122/* y is INF. */123if (iay == 0x7f800000)124return sign_y ? -PiOver2 : PiOver2;125126uint32_t sign_xy = sign_x ^ sign_y;127128float ax = asfloat (iax);129float ay = asfloat (iay);130131bool pred_aygtax = (ay > ax);132133/* Set up z for call to atanf. */134float n = pred_aygtax ? -ax : ay;135float d = pred_aygtax ? ay : ax;136float z = n / d;137138float ret;139if (unlikely (m < 2 && exp_diff >= POLY_UFLOW_BOUND))140{141/* If (x, y) is very close to x axis and x is positive, the polynomial142will underflow and evaluate to z. */143ret = z;144}145else146{147/* Work out the correct shift. */148float shift = sign_x ? -2.0f : 0.0f;149shift = pred_aygtax ? shift + 1.0f : shift;150shift *= PiOver2;151152ret = eval_poly (z, z, shift);153}154155/* Account for the sign of x and y. */156return asfloat (asuint (ret) ^ sign_xy);157}158159/* Arity of 2 means no mathbench entry emitted. See test/mathbench_funcs.h. */160TEST_SIG (S, F, 2, atan2)161TEST_ULP (atan2f, 2.4)162TEST_INTERVAL (atan2f, -10.0, 10.0, 50000)163TEST_INTERVAL (atan2f, -1.0, 1.0, 40000)164TEST_INTERVAL (atan2f, 0.0, 1.0, 40000)165TEST_INTERVAL (atan2f, 1.0, 100.0, 40000)166TEST_INTERVAL (atan2f, 1e6, 1e32, 40000)167168169