aboutsummaryrefslogtreecommitdiff
path: root/pl/math/v_log1pf_inline.h
diff options
context:
space:
mode:
Diffstat (limited to 'pl/math/v_log1pf_inline.h')
-rw-r--r--pl/math/v_log1pf_inline.h74
1 files changed, 43 insertions, 31 deletions
diff --git a/pl/math/v_log1pf_inline.h b/pl/math/v_log1pf_inline.h
index e3048e667c26..c654c6bad08f 100644
--- a/pl/math/v_log1pf_inline.h
+++ b/pl/math/v_log1pf_inline.h
@@ -10,46 +10,58 @@
#define PL_MATH_V_LOG1PF_INLINE_H
#include "v_math.h"
-#include "math_config.h"
+#include "poly_advsimd_f32.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)
+struct v_log1pf_data
{
- /* 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));
+ float32x4_t poly[8], ln2;
+ uint32x4_t four;
+ int32x4_t three_quarters;
+};
- 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);
+/* Polynomial generated using FPMinimax in [-0.25, 0.5]. First two coefficients
+ (1, -0.5) are not stored as they can be generated more efficiently. */
+#define V_LOG1PF_CONSTANTS_TABLE \
+ { \
+ .poly \
+ = { V4 (0x1.5555aap-2f), V4 (-0x1.000038p-2f), V4 (0x1.99675cp-3f), \
+ V4 (-0x1.54ef78p-3f), V4 (0x1.28a1f4p-3f), V4 (-0x1.0da91p-3f), \
+ V4 (0x1.abcb6p-4f), V4 (-0x1.6f0d5ep-5f) }, \
+ .ln2 = V4 (0x1.62e43p-1f), .four = V4 (0x40800000), \
+ .three_quarters = V4 (0x3f400000) \
+ }
- 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 float32x4_t
+eval_poly (float32x4_t m, const float32x4_t *c)
+{
+ /* Approximate log(1+m) on [-0.25, 0.5] using pairwise Horner (main routine
+ uses split Estrin, but this way reduces register pressure in the calling
+ routine). */
+ float32x4_t q = vfmaq_f32 (v_f32 (-0.5), m, c[0]);
+ float32x4_t m2 = vmulq_f32 (m, m);
+ q = vfmaq_f32 (m, m2, q);
+ float32x4_t p = v_pw_horner_6_f32 (m, m2, c + 1);
+ p = vmulq_f32 (m2, p);
+ return vfmaq_f32 (q, m2, p);
}
-static inline v_f32_t
-log1pf_inline (v_f32_t x)
+static inline float32x4_t
+log1pf_inline (float32x4_t x, const struct v_log1pf_data d)
{
/* 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);
+ float32x4_t m = vaddq_f32 (x, v_f32 (1.0f));
+ int32x4_t k
+ = vandq_s32 (vsubq_s32 (vreinterpretq_s32_f32 (m), d.three_quarters),
+ v_s32 (0xff800000));
+ uint32x4_t ku = vreinterpretq_u32_s32 (k);
+ float32x4_t s = vreinterpretq_f32_u32 (vsubq_u32 (d.four, ku));
+ float32x4_t m_scale
+ = vreinterpretq_f32_u32 (vsubq_u32 (vreinterpretq_u32_f32 (x), ku));
+ m_scale = vaddq_f32 (m_scale, vfmaq_f32 (v_f32 (-1.0f), v_f32 (0.25f), s));
+ float32x4_t p = eval_poly (m_scale, d.poly);
+ float32x4_t scale_back = vmulq_f32 (vcvtq_f32_s32 (k), v_f32 (0x1.0p-23f));
+ return vfmaq_f32 (p, scale_back, d.ln2);
}
#endif // PL_MATH_V_LOG1PF_INLINE_H