147 lines · c
1//===-- Implementation header for asinf16 -----------------------*- C++ -*-===//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 LLVM_LIBC_SRC___SUPPORT_MATH_ASINF16_H10#define LLVM_LIBC_SRC___SUPPORT_MATH_ASINF16_H11 12#include "include/llvm-libc-macros/float16-macros.h"13 14#ifdef LIBC_TYPES_HAS_FLOAT1615 16#include "src/__support/FPUtil/FEnvImpl.h"17#include "src/__support/FPUtil/FPBits.h"18#include "src/__support/FPUtil/PolyEval.h"19#include "src/__support/FPUtil/cast.h"20#include "src/__support/FPUtil/multiply_add.h"21#include "src/__support/FPUtil/sqrt.h"22#include "src/__support/macros/optimization.h"23 24namespace LIBC_NAMESPACE_DECL {25 26namespace math {27 28LIBC_INLINE static constexpr float16 asinf16(float16 x) {29 30 // Generated by Sollya using the following command:31 // > round(pi/2, D, RN);32 constexpr float PI_2 = 0x1.921fb54442d18p0f;33 34 using FPBits = fputil::FPBits<float16>;35 FPBits xbits(x);36 37 uint16_t x_u = xbits.uintval();38 uint16_t x_abs = x_u & 0x7fff;39 float xf = x;40 41 // |x| > 0x1p0, |x| > 1, or x is NaN.42 if (LIBC_UNLIKELY(x_abs > 0x3c00)) {43 // asinf16(NaN) = NaN44 if (xbits.is_nan()) {45 if (xbits.is_signaling_nan()) {46 fputil::raise_except_if_required(FE_INVALID);47 return FPBits::quiet_nan().get_val();48 }49 50 return x;51 }52 53 // 1 < |x| <= +/-inf54 fputil::raise_except_if_required(FE_INVALID);55 fputil::set_errno_if_required(EDOM);56 57 return FPBits::quiet_nan().get_val();58 }59 60 float xsq = xf * xf;61 62 // |x| <= 0x1p-1, |x| <= 0.563 if (x_abs <= 0x3800) {64 // asinf16(+/-0) = +/-065 if (LIBC_UNLIKELY(x_abs == 0))66 return x;67 68 // Exhaustive tests show that,69 // for |x| <= 0x1.878p-9, when:70 // x > 0, and rounding upward, or71 // x < 0, and rounding downward, then,72 // asin(x) = x * 2^-11 + x73 // else, in other rounding modes,74 // asin(x) = x75 if (LIBC_UNLIKELY(x_abs <= 0x1a1e)) {76 int rounding = fputil::quick_get_round();77 78 if ((xbits.is_pos() && rounding == FE_UPWARD) ||79 (xbits.is_neg() && rounding == FE_DOWNWARD))80 return fputil::cast<float16>(fputil::multiply_add(xf, 0x1.0p-11f, xf));81 return x;82 }83 84 // Degree-6 minimax odd polynomial of asin(x) generated by Sollya with:85 // > P = fpminimax(asin(x)/x, [|0, 2, 4, 6, 8|], [|SG...|], [0, 0.5]);86 float result =87 fputil::polyeval(xsq, 0x1.000002p0f, 0x1.554c2ap-3f, 0x1.3541ccp-4f,88 0x1.43b2d6p-5f, 0x1.a0d73ep-5f);89 return fputil::cast<float16>(xf * result);90 }91 92 // When |x| > 0.5, assume that 0.5 < |x| <= 1,93 //94 // Step-by-step range-reduction proof:95 // 1: Let y = asin(x), such that, x = sin(y)96 // 2: From complimentary angle identity:97 // x = sin(y) = cos(pi/2 - y)98 // 3: Let z = pi/2 - y, such that x = cos(z)99 // 4: From double angle formula; cos(2A) = 1 - sin^2(A):100 // z = 2A, z/2 = A101 // cos(z) = 1 - 2 * sin^2(z/2)102 // 5: Make sin(z/2) subject of the formula:103 // sin(z/2) = sqrt((1 - cos(z))/2)104 // 6: Recall [3]; x = cos(z). Therefore:105 // sin(z/2) = sqrt((1 - x)/2)106 // 7: Let u = (1 - x)/2107 // 8: Therefore:108 // asin(sqrt(u)) = z/2109 // 2 * asin(sqrt(u)) = z110 // 9: Recall [3], z = pi/2 - y. Therefore:111 // y = pi/2 - z112 // y = pi/2 - 2 * asin(sqrt(u))113 // 10: Recall [1], y = asin(x). Therefore:114 // asin(x) = pi/2 - 2 * asin(sqrt(u))115 //116 // WHY?117 // 11: Recall [7], u = (1 - x)/2118 // 12: Since 0.5 < x <= 1, therefore:119 // 0 <= u <= 0.25 and 0 <= sqrt(u) <= 0.5120 //121 // Hence, we can reuse the same [0, 0.5] domain polynomial approximation for122 // Step [10] as `sqrt(u)` is in range.123 124 // 0x1p-1 < |x| <= 0x1p0, 0.5 < |x| <= 1.0125 float xf_abs = (xf < 0 ? -xf : xf);126 float sign = (xbits.uintval() >> 15 == 1 ? -1.0 : 1.0);127 float u = fputil::multiply_add(-0.5f, xf_abs, 0.5f);128 float u_sqrt = fputil::sqrt<float>(u);129 130 // Degree-6 minimax odd polynomial of asin(x) generated by Sollya with:131 // > P = fpminimax(asin(x)/x, [|0, 2, 4, 6, 8|], [|SG...|], [0, 0.5]);132 float asin_sqrt_u =133 u_sqrt * fputil::polyeval(u, 0x1.000002p0f, 0x1.554c2ap-3f,134 0x1.3541ccp-4f, 0x1.43b2d6p-5f, 0x1.a0d73ep-5f);135 136 return fputil::cast<float16>(sign *137 fputil::multiply_add(-2.0f, asin_sqrt_u, PI_2));138}139 140} // namespace math141 142} // namespace LIBC_NAMESPACE_DECL143 144#endif // LIBC_TYPES_HAS_FLOAT16145 146#endif // LLVM_LIBC_SRC___SUPPORT_MATH_ASINF16_H147