brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.9 KiB · d07236c Raw
100 lines · c
1//===-- Implementation header for cospif16 ----------------------*- 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_COSPIF16_H10#define LLVM_LIBC_SRC___SUPPORT_MATH_COSPIF16_H11 12#include "include/llvm-libc-macros/float16-macros.h"13 14#ifdef LIBC_TYPES_HAS_FLOAT1615 16#include "sincosf16_utils.h"17#include "src/__support/FPUtil/FEnvImpl.h"18#include "src/__support/FPUtil/FPBits.h"19#include "src/__support/FPUtil/cast.h"20#include "src/__support/FPUtil/multiply_add.h"21#include "src/__support/macros/optimization.h"22 23namespace LIBC_NAMESPACE_DECL {24 25namespace math {26 27LIBC_INLINE static constexpr float16 cospif16(float16 x) {28 29  using namespace sincosf16_internal;30  using FPBits = typename fputil::FPBits<float16>;31  FPBits xbits(x);32 33  uint16_t x_u = xbits.uintval();34  uint16_t x_abs = x_u & 0x7fff;35  float xf = x;36 37  // Range reduction:38  // For |x| > 1/32, we perform range reduction as follows:39  // Find k and y such that:40  //   x = (k + y) * 1/3241  //   k is an integer42  //   |y| < 0.543  //44  // This is done by performing:45  //   k = round(x * 32)46  //   y = x * 32 - k47  //48  // Once k and y are computed, we then deduce the answer by the cosine of sum49  // formula:50  //   cos(x * pi) = cos((k + y) * pi/32)51  //               = cos(k * pi/32) * cos(y * pi/32) +52  //                 sin(y * pi/32) * sin(k * pi/32)53 54  // For signed zeros55  if (LIBC_UNLIKELY(x_abs == 0U))56    return fputil::cast<float16>(1.0f);57 58  // Numbers greater or equal to 2^10 are integers, or infinity, or NaN59  if (LIBC_UNLIKELY(x_abs >= 0x6400)) {60    if (LIBC_UNLIKELY(x_abs <= 0x67FF))61      return fputil::cast<float16>((x_abs & 0x1) ? -1.0f : 1.0f);62 63    // Check for NaN or infintiy values64    if (LIBC_UNLIKELY(x_abs >= 0x7c00)) {65      if (xbits.is_signaling_nan()) {66        fputil::raise_except_if_required(FE_INVALID);67        return FPBits::quiet_nan().get_val();68      }69      // If value is equal to infinity70      if (x_abs == 0x7c00) {71        fputil::set_errno_if_required(EDOM);72        fputil::raise_except_if_required(FE_INVALID);73      }74 75      return x + FPBits::quiet_nan().get_val();76    }77 78    return fputil::cast<float16>(1.0f);79  }80 81  float sin_k = 0, cos_k = 0, sin_y = 0, cosm1_y = 0;82  sincospif16_eval(xf, sin_k, cos_k, sin_y, cosm1_y);83 84  if (LIBC_UNLIKELY(sin_y == 0 && cos_k == 0))85    return fputil::cast<float16>(0.0f);86 87  // Since, cosm1_y = cos_y - 1, therefore:88  //   cos(x * pi) = cos_k(cosm1_y) + cos_k - sin_k * sin_y89  return fputil::cast<float16>(fputil::multiply_add(90      cos_k, cosm1_y, fputil::multiply_add(-sin_k, sin_y, cos_k)));91}92 93} // namespace math94 95} // namespace LIBC_NAMESPACE_DECL96 97#endif // LIBC_TYPES_HAS_FLOAT1698 99#endif // LLVM_LIBC_SRC___SUPPORT_MATH_COSHF16_H100