Path: blob/main/contrib/arm-optimized-routines/math/aarch64/experimental/atan2_2u5.c
48375 views
/*1* Double-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 "atan_common.h"10#include "math_config.h"11#include "test_sig.h"12#include "test_defs.h"1314#define Pi (0x1.921fb54442d18p+1)15#define PiOver2 (0x1.921fb54442d18p+0)16#define PiOver4 (0x1.921fb54442d18p-1)17#define SignMask (0x8000000000000000)18#define ExpMask (0x7ff0000000000000)1920/* We calculate atan2 by P(n/d), where n and d are similar to the input21arguments, and P is a polynomial. Evaluating P(x) requires calculating x^8,22which may underflow if n and d have very different magnitude.23POW8_EXP_UFLOW_BOUND is the lower bound of the difference in exponents of n24and d for which P underflows, and is used to special-case such inputs. */25#define POW8_EXP_UFLOW_BOUND 622627static inline int64_t28biased_exponent (double f)29{30uint64_t fi = asuint64 (f);31return (fi & ExpMask) >> 52;32}3334/* Fast implementation of scalar atan2. Largest errors are when y and x are35close together. The greatest observed error is 2.28 ULP:36atan2(-0x1.5915b1498e82fp+732, 0x1.54d11ef838826p+732)37got -0x1.954f42f1fa841p-1 want -0x1.954f42f1fa843p-1. */38double39atan2 (double y, double x)40{41uint64_t ix = asuint64 (x);42uint64_t iy = asuint64 (y);4344uint64_t sign_x = ix & SignMask;45uint64_t sign_y = iy & SignMask;4647uint64_t iax = ix & ~SignMask;48uint64_t iay = iy & ~SignMask;4950bool xisnan = isnan (x);51if (unlikely (isnan (y) && !xisnan))52return __math_invalid (y);53if (unlikely (xisnan))54return __math_invalid (x);5556/* m = 2 * sign(x) + sign(y). */57uint32_t m = ((iy >> 63) & 1) | ((ix >> 62) & 2);5859int64_t exp_diff = biased_exponent (x) - biased_exponent (y);6061/* y = 0. */62if (iay == 0)63{64switch (m)65{66case 0:67case 1:68return y; /* atan(+-0,+anything)=+-0. */69case 2:70return Pi; /* atan(+0,-anything) = pi. */71case 3:72return -Pi; /* atan(-0,-anything) =-pi. */73}74}75/* Special case for (x, y) either on or very close to the y axis. Either x =760, or y is much larger than x (difference in exponents >=77POW8_EXP_UFLOW_BOUND). */78if (unlikely (iax == 0 || exp_diff <= -POW8_EXP_UFLOW_BOUND))79return sign_y ? -PiOver2 : PiOver2;8081/* Special case for either x is INF or (x, y) is very close to x axis and x82is negative. */83if (unlikely (iax == 0x7ff000000000000084|| (exp_diff >= POW8_EXP_UFLOW_BOUND && m >= 2)))85{86if (iay == 0x7ff0000000000000)87{88switch (m)89{90case 0:91return PiOver4; /* atan(+INF,+INF). */92case 1:93return -PiOver4; /* atan(-INF,+INF). */94case 2:95return 3.0 * PiOver4; /* atan(+INF,-INF). */96case 3:97return -3.0 * PiOver4; /* atan(-INF,-INF). */98}99}100else101{102switch (m)103{104case 0:105return 0.0; /* atan(+...,+INF). */106case 1:107return -0.0; /* atan(-...,+INF). */108case 2:109return Pi; /* atan(+...,-INF). */110case 3:111return -Pi; /* atan(-...,-INF). */112}113}114}115/* y is INF. */116if (iay == 0x7ff0000000000000)117return sign_y ? -PiOver2 : PiOver2;118119uint64_t sign_xy = sign_x ^ sign_y;120121double ax = asdouble (iax);122double ay = asdouble (iay);123uint64_t pred_aygtax = (ay > ax);124125/* Set up z for call to atan. */126double n = pred_aygtax ? -ax : ay;127double d = pred_aygtax ? ay : ax;128double z = n / d;129130double ret;131if (unlikely (m < 2 && exp_diff >= POW8_EXP_UFLOW_BOUND))132{133/* If (x, y) is very close to x axis and x is positive, the polynomial134will underflow and evaluate to z. */135ret = z;136}137else138{139/* Work out the correct shift. */140double shift = sign_x ? -2.0 : 0.0;141shift = pred_aygtax ? shift + 1.0 : shift;142shift *= PiOver2;143144ret = eval_poly (z, z, shift);145}146147/* Account for the sign of x and y. */148return asdouble (asuint64 (ret) ^ sign_xy);149}150151/* Arity of 2 means no mathbench entry emitted. See test/mathbench_funcs.h. */152TEST_SIG (S, D, 2, atan2)153TEST_ULP (atan2, 1.78)154TEST_INTERVAL (atan2, -10.0, 10.0, 50000)155TEST_INTERVAL (atan2, -1.0, 1.0, 40000)156TEST_INTERVAL (atan2, 0.0, 1.0, 40000)157TEST_INTERVAL (atan2, 1.0, 100.0, 40000)158TEST_INTERVAL (atan2, 1e6, 1e32, 40000)159160161