/* Double-precision vector (SVE) log10 function

   Copyright (C) 2023-2026 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <https://www.gnu.org/licenses/>.  */

#include "sv_math.h"

#define N (1 << V_LOG10_TABLE_BITS)

static const struct data
{
  double two_2_52, log10_52;
  double c0, c2, c4;
  double c1, c3;
  double invln10, log10_2;
  uint64_t off, thresh;
} data = {
  .c0 = -0x1.bcb7b1526e506p-3,
  .c1 = 0x1.287a7636be1d1p-3,
  .c2 = -0x1.bcb7b158af938p-4,
  .c3 = 0x1.63c78734e6d07p-4,
  .c4 = -0x1.287461742fee4p-4,
  .invln10 = 0x1.bcb7b1526e50ep-2,
  .log10_2 = 0x1.34413509f79ffp-2,
  .two_2_52 = 0x1p52,		     /* 2^52.  */
  .log10_52 = -0x1.f4e9f6303263ep+3, /* log10(2) * 52 ~ 15.65.  */
  .off = 0x3fe6900900000000,
  /* The threshold is computed from the lower and upper bounds,
     respectively the smallest normalised number, min = 0x0010000000000000
     and infinity, 0x7ff0000000000000.  */
  .thresh = 0x7fe0000000000000, /* infinity - min.  */
};

static inline svfloat64_t
v_log10_inline (svuint64_t ix, const svbool_t pg, const struct data *d)
{
  /* x = 2^k z; where z is in range [Off,2*Off) and exact.
     The range is split into N subintervals.
     The ith subinterval contains z and c is near its center.  */
  svuint64_t tmp = svsub_x (pg, ix, d->off);
  svuint64_t i = svlsr_x (pg, tmp, 51 - V_LOG10_TABLE_BITS);
  i = svand_x (pg, i, (N - 1) << 1);
  svfloat64_t k = svcvt_f64_x (pg, svasr_x (pg, svreinterpret_s64 (tmp), 52));
  svfloat64_t z = svreinterpret_f64 (
      svsub_x (pg, ix, svand_x (pg, tmp, 0xfffULL << 52)));

  /* log(x) = k*log(2) + log(c) + log(z/c).  */
  svfloat64_t invc = svld1_gather_index (pg, &__v_log10_data.table[0].invc, i);
  svfloat64_t logc
      = svld1_gather_index (pg, &__v_log10_data.table[0].log10c, i);

  /* We approximate log(z/c) with a polynomial P(x) ~= log(x + 1):
     r = z/c - 1 (we look up precomputed 1/c)
     log(z/c) ~= P(r).  */
  svfloat64_t r = svmad_x (pg, invc, z, -1.0);

  /* hi = log(c) + k*log(2).  */
  svfloat64_t invln10_log10_2 = svld1rq_f64 (svptrue_b64 (), &d->invln10);
  svfloat64_t w = svmla_lane_f64 (logc, r, invln10_log10_2, 0);
  svfloat64_t hi = svmla_lane_f64 (w, k, invln10_log10_2, 1);

  /* y = r2*(A0 + r*A1 + r2*(A2 + r*A3 + r2*A4)) + hi.  */
  svfloat64_t odd_coeffs = svld1rq_f64 (svptrue_b64 (), &d->c1);
  svfloat64_t r2 = svmul_x (svptrue_b64 (), r, r);
  svfloat64_t y = svmla_lane_f64 (sv_f64 (d->c2), r, odd_coeffs, 1);
  svfloat64_t p = svmla_lane_f64 (sv_f64 (d->c0), r, odd_coeffs, 0);
  y = svmla_x (pg, y, r2, d->c4);
  y = svmla_x (pg, p, r2, y);

  return svmla_x (pg, hi, r2, y);
}

/* The special case is made up of a series of selects which chose the correct
   outcome of the special lanes from inf, -inf or nan or for subnormals a
   calculation of x * 2^52 (2^mantissa) to normalise the number at entry to
   the log function and then subtract log10(2) * 52 to re-subnormalise the
   output to the correct result.  */
static svfloat64_t NOINLINE
special_case (svfloat64_t x, svbool_t pg, svbool_t special)
{
  const struct data *d = ptr_barrier (&data);
  /* Check covers subnormal range. This is greater than the actual range but
     standard case lanes and +inf are handled seperately.  */
  svbool_t is_sub = svcmpgt_f64 (pg, x, sv_f64 (0));
  /* Check for 0 which = -Infinity.  */
  svbool_t is_minf = svcmpeq_f64 (pg, x, sv_f64 (0));
  svbool_t is_pinf = svcmpeq_f64 (pg, x, sv_f64 (INFINITY));

  /* Increase x for special cases to catch sub normals.  */
  x = svmul_m (special, x, d->two_2_52);
  svuint64_t ix = svreinterpret_u64 (x);

  /* Select correct special case correction depending on x.  */
  svfloat64_t special_log = svsel (is_sub, sv_f64 (d->log10_52), sv_f64 (NAN));
  special_log = svsel (is_minf, sv_f64 (-INFINITY), special_log);
  special_log = svsel (is_pinf, sv_f64 (INFINITY), special_log);

  /* Return log for both special after offset and none special cases.  */
  svfloat64_t log_sum = v_log10_inline (ix, pg, d);

  /* Reduce the output of log for special cases to complete the subnormals
     calculation or add inf, -inf or nan depending on special_log.
     Return log without correction for none special lanes.  */
  return svadd_m (special, log_sum, special_log);
}

/* Double-precision SVE log10 routine.
   Maximum measured error is 2.46 ulps.
   SV_NAME_D1 (log10)(0x1.131956cd4b627p+0) got 0x1.fffbdf6eaa669p-6
					   want 0x1.fffbdf6eaa667p-6.  */
svfloat64_t SV_NAME_D1 (log10) (svfloat64_t x, const svbool_t pg)
{
  const struct data *d = ptr_barrier (&data);

  svuint64_t ix = svreinterpret_u64 (x);
  /* Special cases: x is subnormal, x <= 0, x == inf, x == nan.  */
  svbool_t special
      = svcmpge (pg, svsub_x (pg, ix, 0x0010000000000000), d->thresh);
  if (__glibc_unlikely (svptest_any (special, special)))
    return special_case (x, pg, special);

  /* If no special cases just return log_10 function call.  */
  return v_log10_inline (ix, pg, d);
}
