xref: /llvm-project/libc/src/math/generic/cospif16.cpp (revision 6a865b6d3d44e53adc9342fb2d80a604f5459707)
17395ef54Swldfngrs //===-- Half-precision cospif function ------------------------------------===//
27395ef54Swldfngrs //
37395ef54Swldfngrs // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47395ef54Swldfngrs // See https://llvm.org/LICENSE.txt for license information.
57395ef54Swldfngrs // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67395ef54Swldfngrs //
77395ef54Swldfngrs //===----------------------------------------------------------------------===//
87395ef54Swldfngrs 
97395ef54Swldfngrs #include "src/math/cospif16.h"
107395ef54Swldfngrs #include "hdr/errno_macros.h"
117395ef54Swldfngrs #include "hdr/fenv_macros.h"
127395ef54Swldfngrs #include "sincosf16_utils.h"
137395ef54Swldfngrs #include "src/__support/FPUtil/FEnvImpl.h"
147395ef54Swldfngrs #include "src/__support/FPUtil/FPBits.h"
157395ef54Swldfngrs #include "src/__support/FPUtil/cast.h"
167395ef54Swldfngrs #include "src/__support/FPUtil/multiply_add.h"
177395ef54Swldfngrs #include "src/__support/macros/optimization.h"
187395ef54Swldfngrs 
197395ef54Swldfngrs namespace LIBC_NAMESPACE_DECL {
207395ef54Swldfngrs 
217395ef54Swldfngrs LLVM_LIBC_FUNCTION(float16, cospif16, (float16 x)) {
227395ef54Swldfngrs   using FPBits = typename fputil::FPBits<float16>;
237395ef54Swldfngrs   FPBits xbits(x);
247395ef54Swldfngrs 
257395ef54Swldfngrs   uint16_t x_u = xbits.uintval();
267395ef54Swldfngrs   uint16_t x_abs = x_u & 0x7fff;
277395ef54Swldfngrs   float xf = x;
287395ef54Swldfngrs 
297395ef54Swldfngrs   // Range reduction:
307395ef54Swldfngrs   // For |x| > 1/32, we perform range reduction as follows:
317395ef54Swldfngrs   // Find k and y such that:
327395ef54Swldfngrs   //   x = (k + y) * 1/32
337395ef54Swldfngrs   //   k is an integer
347395ef54Swldfngrs   //   |y| < 0.5
357395ef54Swldfngrs   //
367395ef54Swldfngrs   // This is done by performing:
377395ef54Swldfngrs   //   k = round(x * 32)
387395ef54Swldfngrs   //   y = x * 32 - k
397395ef54Swldfngrs   //
40*f7bb1290Swldfngrs   // Once k and y are computed, we then deduce the answer by the cosine of sum
417395ef54Swldfngrs   // formula:
427395ef54Swldfngrs   //   cos(x * pi) = cos((k + y) * pi/32)
437395ef54Swldfngrs   //               = cos(k * pi/32) * cos(y * pi/32) +
447395ef54Swldfngrs   //                 sin(y * pi/32) * sin(k * pi/32)
457395ef54Swldfngrs 
467395ef54Swldfngrs   // For signed zeros
477395ef54Swldfngrs   if (LIBC_UNLIKELY(x_abs == 0U))
487395ef54Swldfngrs     return fputil::cast<float16>(1.0f);
497395ef54Swldfngrs 
507395ef54Swldfngrs   // Numbers greater or equal to 2^10 are integers, or infinity, or NaN
517395ef54Swldfngrs   if (LIBC_UNLIKELY(x_abs >= 0x6400)) {
527395ef54Swldfngrs     if (LIBC_UNLIKELY(x_abs <= 0x67FF))
537395ef54Swldfngrs       return fputil::cast<float16>((x_abs & 0x1) ? -1.0f : 1.0f);
547395ef54Swldfngrs 
557395ef54Swldfngrs     // Check for NaN or infintiy values
567395ef54Swldfngrs     if (LIBC_UNLIKELY(x_abs >= 0x7c00)) {
577395ef54Swldfngrs       // If value is equal to infinity
587395ef54Swldfngrs       if (x_abs == 0x7c00) {
597395ef54Swldfngrs         fputil::set_errno_if_required(EDOM);
607395ef54Swldfngrs         fputil::raise_except_if_required(FE_INVALID);
617395ef54Swldfngrs       }
627395ef54Swldfngrs 
637395ef54Swldfngrs       return x + FPBits::quiet_nan().get_val();
647395ef54Swldfngrs     }
657395ef54Swldfngrs 
667395ef54Swldfngrs     return fputil::cast<float16>(1.0f);
677395ef54Swldfngrs   }
687395ef54Swldfngrs 
697395ef54Swldfngrs   float sin_k, cos_k, sin_y, cosm1_y;
707395ef54Swldfngrs   sincospif16_eval(xf, sin_k, cos_k, sin_y, cosm1_y);
717395ef54Swldfngrs 
727395ef54Swldfngrs   if (LIBC_UNLIKELY(sin_y == 0 && cos_k == 0))
737395ef54Swldfngrs     return fputil::cast<float16>(0.0f);
747395ef54Swldfngrs 
757395ef54Swldfngrs   // Since, cosm1_y = cos_y - 1, therefore:
767395ef54Swldfngrs   //   cos(x * pi) = cos_k(cosm1_y) + cos_k - sin_k * sin_y
777395ef54Swldfngrs   return fputil::cast<float16>(fputil::multiply_add(
787395ef54Swldfngrs       cos_k, cosm1_y, fputil::multiply_add(-sin_k, sin_y, cos_k)));
797395ef54Swldfngrs }
807395ef54Swldfngrs 
817395ef54Swldfngrs } // namespace LIBC_NAMESPACE_DECL
82