//===-- Implementation header for log1pf ------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_LOG1PF_H
#define LLVM_LIBC_SRC___SUPPORT_MATH_LOG1PF_H

#include "src/__support/FPUtil/FEnvImpl.h"
#include "src/__support/FPUtil/FMA.h"
#include "src/__support/FPUtil/FPBits.h"
#include "src/__support/FPUtil/PolyEval.h"
#include "src/__support/FPUtil/except_value_utils.h"
#include "src/__support/FPUtil/multiply_add.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
#include "src/__support/macros/properties/cpu_features.h"
#include "src/__support/math/acoshf_utils.h"

// This is an algorithm for log(1+x) in single precision which is
// correctly rounded for all rounding modes.
// - An exhaustive test show that when x >= 2^45, log1pf(x) == logf(x)
// for all rounding modes.
// - When 2^(-6) <= |x| < 2^45, the sum (double(x) + 1.0) is exact,
// so we can adapt the correctly rounded algorithm of logf to compute
// log(double(x) + 1.0) correctly.  For more information about the logf
// algorithm, see `libc/src/math/generic/logf.cpp`.
// - When |x| < 2^(-6), we use a degree-8 polynomial in double precision
// generated with Sollya using the following command:
//   fpminimax(log(1 + x)/x, 7, [|D...|], [-2^-6; 2^-6]);

namespace LIBC_NAMESPACE_DECL {

namespace math {

LIBC_INLINE float log1pf(float x) {
  using FPBits = typename fputil::FPBits<float>;
  FPBits xbits(x);
  uint32_t x_u = xbits.uintval();
  uint32_t x_a = x_u & 0x7fff'ffffU;
  double xd = static_cast<double>(x);

  if (x_a <= 0x3c80'0000U) {
    // |x| <= 2^-6.
#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
    // Hard-to round cases.
    switch (x_u) {
    case 0x3540'0003U: // x = 0x1.800006p-21f
      return fputil::round_result_slightly_down(0x1.7ffffep-21f);
    case 0x3710'001bU: // x = 0x1.200036p-17f
      return fputil::round_result_slightly_down(0x1.1fffe6p-17f);
    case 0xb53f'fffdU: // x = -0x1.7ffffap-21
      return fputil::round_result_slightly_down(-0x1.800002p-21f);
    case 0xb70f'ffe5U: // x = -0x1.1fffcap-17
      return fputil::round_result_slightly_down(-0x1.20001ap-17f);
    case 0xbb0e'c8c4U: // x = -0x1.1d9188p-9
      return fputil::round_result_slightly_up(-0x1.1de14ap-9f);
    }
#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS

    // Polynomial generated by Sollya with:
    // > P = fpminimax(log(1 + x)/x, 7, [|D...|], [-2^-6; 2^-6]);
    // > dirtyinfnorm((log(1 + x) - x*P)/log(1 + x), [-2^-6, 2^-6]);
    // 0x1.1447755e54a327941f7db7316f8dcd7cf33d15ffp-58
    constexpr double COEFFS[7] = {-0x1.0000000000000p-1, 0x1.5555555556aadp-2,
                                  -0x1.000000000181ap-2, 0x1.999998998124ep-3,
                                  -0x1.55555452e2a2bp-3, 0x1.24adb8cde4aa7p-3,
                                  -0x1.0019db915ef6fp-3};

    double xsq = xd * xd;
    double c0 = fputil::multiply_add(xd, COEFFS[1], COEFFS[0]);
    double c1 = fputil::multiply_add(xd, COEFFS[3], COEFFS[2]);
    double c2 = fputil::multiply_add(xd, COEFFS[5], COEFFS[4]);
    double x4 = xsq * xsq;
    double d0 = fputil::multiply_add(xsq, c1, c0);
    double d1 = fputil::multiply_add(xsq, COEFFS[6], c2);
    double d2 = fputil::multiply_add(x4, d1, d0);
    double r = fputil::multiply_add(xsq, d2, xd);

    return static_cast<float>(r);
  }

  // Use log1p(x) = log(1 + x) for |x| > 2^-6;

  // Check for exceptional cases.
  if (x_a >= 0x3f80'0000) {
    // |x| >= 1.
    if (LIBC_UNLIKELY(x_u >= 0x7f80'0000)) {
      // x is inf, nan, or x <= -1.
      if (x == -1.0f) {
        // x = -1
        fputil::set_errno_if_required(ERANGE);
        fputil::raise_except_if_required(FE_DIVBYZERO);
        return FPBits::inf(Sign::NEG).get_val();
      }
      if (xbits.is_signaling_nan() || x < 1.0f) {
        // x is signaling NaNs or x < -1
        if (x < 1.0f)
          fputil::set_errno_if_required(EDOM);
        fputil::raise_except_if_required(FE_INVALID);
        return fputil::FPBits<float>::quiet_nan().get_val();
      }
      // x is +inf or quiet NaN
      return x;
    }
#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
    // Filter hard-to-round cases:
    if (LIBC_UNLIKELY(x >= 0x1.30bf04p+43f)) {
      switch (x_u) {
      case 0x5518'5f82U: // x = 0x1.30bf04p+43f
        return fputil::round_result_slightly_up(0x1.dfac9p+4f);
      case 0x5cd6'9e88U: // x = 0x1.ad3d1p+58f
        return fputil::round_result_slightly_up(0x1.45c146p+5f);
      case 0x5ee8'984eU: // x = 0x1.d1309cp+62f
        return fputil::round_result_slightly_up(0x1.5c9442p+5f);
      case 0x65d8'90d3U: // x = 0x1.b121a6p+76f
        return fputil::round_result_slightly_down(0x1.a9a3f2p+5f);
      case 0x6f31'a8ecU: // x = 0x1.6351d8p+95f
        return fputil::round_result_slightly_down(0x1.08b512p+6f);
      case 0x7a17'f30aU: // x = 0x1.2fe614p+117f
        return fputil::round_result_slightly_up(0x1.451436p+6f);
#ifndef LIBC_TARGET_CPU_HAS_FMA_DOUBLE
      case 0x58f1'9e31U: // x = 0x1.e33c62p+50f
        return fputil::round_result_slightly_down(0x1.1a576cp+5f);
      case 0x665e'7ca6U: // x = 0x1.bcf94cp+77f
        return fputil::round_result_slightly_up(0x1.af66cp+5f);
      case 0x79e7'ec37U: // x = 0x1.cfd86ep+116f
        return fputil::round_result_slightly_up(0x1.43ff6ep+6f);
#endif // LIBC_TARGET_CPU_HAS_FMA_DOUBLE
      }
    }
  } else {
    if (LIBC_UNLIKELY(x_u == 0x3efd'81adU)) // x = 0x1.fb035ap-2f
      return fputil::round_result_slightly_up(0x1.9bddc2p-2f);
#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
  }

  double r = acoshf_internal::log_eval(xd + 1.0);
  return static_cast<float>(r);
}

} // namespace math
} // namespace LIBC_NAMESPACE_DECL

#endif // LLVM_LIBC_SRC___SUPPORT_MATH_LOG1PF_H
