Path: blob/main/contrib/arm-optimized-routines/math/aarch64/experimental/coshf_1u9.c
48375 views
/*1* Single-precision cosh(x) function.2*3* Copyright (c) 2022-2024, Arm Limited.4* SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception5*/67#include "mathlib.h"8#include "math_config.h"9#include "test_sig.h"10#include "test_defs.h"1112#define AbsMask 0x7fffffff13#define TinyBound 0x20000000 /* 0x1p-63: Round to 1 below this. */14/* 0x1.5a92d8p+6: expf overflows above this, so have to use special case. */15#define SpecialBound 0x42ad496c1617static NOINLINE float18specialcase (float x, uint32_t iax)19{20if (iax == 0x7f800000)21return INFINITY;22if (iax > 0x7f800000)23return __math_invalidf (x);24if (iax <= TinyBound)25/* For tiny x, avoid underflow by just returning 1. */26return 1;27/* Otherwise SpecialBound <= |x| < Inf. x is too large to calculate exp(x)28without overflow, so use exp(|x|/2) instead. For large x cosh(x) is29dominated by exp(x), so return:30cosh(x) ~= (exp(|x|/2))^2 / 2. */31float t = expf (asfloat (iax) / 2);32return (0.5 * t) * t;33}3435/* Approximation for single-precision cosh(x) using exp.36cosh(x) = (exp(x) + exp(-x)) / 2.37The maximum error is 1.89 ULP, observed for |x| > SpecialBound:38coshf(0x1.65898cp+6) got 0x1.f00aep+127 want 0x1.f00adcp+127.39The maximum error observed for TinyBound < |x| < SpecialBound is 1.02 ULP:40coshf(0x1.50a3cp+0) got 0x1.ff21dcp+0 want 0x1.ff21dap+0. */41float42coshf (float x)43{44uint32_t ix = asuint (x);45uint32_t iax = ix & AbsMask;46float ax = asfloat (iax);4748if (unlikely (iax <= TinyBound || iax >= SpecialBound))49{50/* x is tiny, large or special. */51return specialcase (x, iax);52}5354/* Compute cosh using the definition:55coshf(x) = exp(x) / 2 + exp(-x) / 2. */56float t = expf (ax);57return 0.5f * t + 0.5f / t;58}5960TEST_SIG (S, F, 1, cosh, -10.0, 10.0)61TEST_ULP (coshf, 1.89)62TEST_SYM_INTERVAL (coshf, 0, 0x1p-63, 100)63TEST_SYM_INTERVAL (coshf, 0, 0x1.5a92d8p+6, 80000)64TEST_SYM_INTERVAL (coshf, 0x1.5a92d8p+6, inf, 2000)656667