aboutsummaryrefslogtreecommitdiff
path: root/pl/math/v_log1pf_inline.h
blob: e3048e667c262a05dbf1be2834bbfbd578d84f76 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
 * Helper for single-precision routines which calculate log(1 + x) and do not
 * need special-case handling
 *
 * Copyright (c) 2022-2023, Arm Limited.
 * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
 */

#ifndef PL_MATH_V_LOG1PF_INLINE_H
#define PL_MATH_V_LOG1PF_INLINE_H

#include "v_math.h"
#include "math_config.h"

#define Four 0x40800000
#define Ln2 v_f32 (0x1.62e43p-1f)

#define C(i) v_f32 (__log1pf_data.coeffs[i])

static inline v_f32_t
eval_poly (v_f32_t m)
{
  /* Approximate log(1+m) on [-0.25, 0.5] using Estrin scheme.  */
  v_f32_t p_12 = v_fma_f32 (m, C (1), C (0));
  v_f32_t p_34 = v_fma_f32 (m, C (3), C (2));
  v_f32_t p_56 = v_fma_f32 (m, C (5), C (4));
  v_f32_t p_78 = v_fma_f32 (m, C (7), C (6));

  v_f32_t m2 = m * m;
  v_f32_t p_02 = v_fma_f32 (m2, p_12, m);
  v_f32_t p_36 = v_fma_f32 (m2, p_56, p_34);
  v_f32_t p_79 = v_fma_f32 (m2, C (8), p_78);

  v_f32_t m4 = m2 * m2;
  v_f32_t p_06 = v_fma_f32 (m4, p_36, p_02);

  return v_fma_f32 (m4, m4 * p_79, p_06);
}

static inline v_f32_t
log1pf_inline (v_f32_t x)
{
  /* Helper for calculating log(x + 1). Copied from log1pf_2u1.c, with no
     special-case handling. See that file for details of the algorithm.  */
  v_f32_t m = x + 1.0f;
  v_u32_t k = (v_as_u32_f32 (m) - 0x3f400000) & 0xff800000;
  v_f32_t s = v_as_f32_u32 (v_u32 (Four) - k);
  v_f32_t m_scale = v_as_f32_u32 (v_as_u32_f32 (x) - k)
		    + v_fma_f32 (v_f32 (0.25f), s, v_f32 (-1.0f));
  v_f32_t p = eval_poly (m_scale);
  v_f32_t scale_back = v_to_f32_u32 (k) * 0x1.0p-23f;
  return v_fma_f32 (scale_back, Ln2, p);
}

#endif //  PL_MATH_V_LOG1PF_INLINE_H