1/*
2 * Single-precision vector log(x + 1) function.
3 *
4 * Copyright (c) 2023, Arm Limited.
5 * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
6 */
7
8#include "sv_math.h"
9#include "pl_sig.h"
10#include "pl_test.h"
11#include "poly_sve_f32.h"
12
13static const struct data
14{
15  float poly[8];
16  float ln2, exp_bias;
17  uint32_t four, three_quarters;
18} data = {.poly = {/* Do not store first term of polynomial, which is -0.5, as
19                      this can be fmov-ed directly instead of including it in
20                      the main load-and-mla polynomial schedule.  */
21		   0x1.5555aap-2f, -0x1.000038p-2f, 0x1.99675cp-3f,
22		   -0x1.54ef78p-3f, 0x1.28a1f4p-3f, -0x1.0da91p-3f,
23		   0x1.abcb6p-4f, -0x1.6f0d5ep-5f},
24	  .ln2 = 0x1.62e43p-1f,
25	  .exp_bias = 0x1p-23f,
26	  .four = 0x40800000,
27	  .three_quarters = 0x3f400000};
28
29#define SignExponentMask 0xff800000
30
31static svfloat32_t NOINLINE
32special_case (svfloat32_t x, svfloat32_t y, svbool_t special)
33{
34  return sv_call_f32 (log1pf, x, y, special);
35}
36
37/* Vector log1pf approximation using polynomial on reduced interval. Worst-case
38   error is 1.27 ULP very close to 0.5.
39   _ZGVsMxv_log1pf(0x1.fffffep-2) got 0x1.9f324p-2
40				 want 0x1.9f323ep-2.  */
41svfloat32_t SV_NAME_F1 (log1p) (svfloat32_t x, svbool_t pg)
42{
43  const struct data *d = ptr_barrier (&data);
44  /* x < -1, Inf/Nan.  */
45  svbool_t special = svcmpeq (pg, svreinterpret_u32 (x), 0x7f800000);
46  special = svorn_z (pg, special, svcmpge (pg, x, -1));
47
48  /* With x + 1 = t * 2^k (where t = m + 1 and k is chosen such that m
49			   is in [-0.25, 0.5]):
50     log1p(x) = log(t) + log(2^k) = log1p(m) + k*log(2).
51
52     We approximate log1p(m) with a polynomial, then scale by
53     k*log(2). Instead of doing this directly, we use an intermediate
54     scale factor s = 4*k*log(2) to ensure the scale is representable
55     as a normalised fp32 number.  */
56  svfloat32_t m = svadd_x (pg, x, 1);
57
58  /* Choose k to scale x to the range [-1/4, 1/2].  */
59  svint32_t k
60      = svand_x (pg, svsub_x (pg, svreinterpret_s32 (m), d->three_quarters),
61		 sv_s32 (SignExponentMask));
62
63  /* Scale x by exponent manipulation.  */
64  svfloat32_t m_scale = svreinterpret_f32 (
65      svsub_x (pg, svreinterpret_u32 (x), svreinterpret_u32 (k)));
66
67  /* Scale up to ensure that the scale factor is representable as normalised
68     fp32 number, and scale m down accordingly.  */
69  svfloat32_t s = svreinterpret_f32 (svsubr_x (pg, k, d->four));
70  m_scale = svadd_x (pg, m_scale, svmla_x (pg, sv_f32 (-1), s, 0.25));
71
72  /* Evaluate polynomial on reduced interval.  */
73  svfloat32_t ms2 = svmul_x (pg, m_scale, m_scale),
74	      ms4 = svmul_x (pg, ms2, ms2);
75  svfloat32_t p = sv_estrin_7_f32_x (pg, m_scale, ms2, ms4, d->poly);
76  p = svmad_x (pg, m_scale, p, -0.5);
77  p = svmla_x (pg, m_scale, m_scale, svmul_x (pg, m_scale, p));
78
79  /* The scale factor to be applied back at the end - by multiplying float(k)
80     by 2^-23 we get the unbiased exponent of k.  */
81  svfloat32_t scale_back = svmul_x (pg, svcvt_f32_x (pg, k), d->exp_bias);
82
83  /* Apply the scaling back.  */
84  svfloat32_t y = svmla_x (pg, p, scale_back, d->ln2);
85
86  if (unlikely (svptest_any (pg, special)))
87    return special_case (x, y, special);
88
89  return y;
90}
91
92PL_SIG (SV, F, 1, log1p, -0.9, 10.0)
93PL_TEST_ULP (SV_NAME_F1 (log1p), 0.77)
94PL_TEST_SYM_INTERVAL (SV_NAME_F1 (log1p), 0, 0x1p-23, 5000)
95PL_TEST_SYM_INTERVAL (SV_NAME_F1 (log1p), 0x1p-23, 1, 5000)
96PL_TEST_INTERVAL (SV_NAME_F1 (log1p), 1, inf, 10000)
97PL_TEST_INTERVAL (SV_NAME_F1 (log1p), -1, -inf, 10)
98