Coverage Report

Created: 2026-03-31 06:56

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libjxl/lib/jxl/base/fast_math-inl.h
Line
Count
Source
1
// Copyright (c) the JPEG XL Project Authors. All rights reserved.
2
//
3
// Use of this source code is governed by a BSD-style
4
// license that can be found in the LICENSE file.
5
6
// Fast SIMD math ops (log2, encoder only, cos, erf for splines)
7
8
#include <cstdint>
9
10
#if defined(LIB_JXL_BASE_FAST_MATH_INL_H_) == defined(HWY_TARGET_TOGGLE)
11
#ifdef LIB_JXL_BASE_FAST_MATH_INL_H_
12
#undef LIB_JXL_BASE_FAST_MATH_INL_H_
13
#else
14
#define LIB_JXL_BASE_FAST_MATH_INL_H_
15
#endif
16
17
#include <hwy/highway.h>
18
19
#include "lib/jxl/base/common.h"
20
#include "lib/jxl/base/rational_polynomial-inl.h"
21
HWY_BEFORE_NAMESPACE();
22
namespace jxl {
23
namespace HWY_NAMESPACE {
24
25
// These templates are not found via ADL.
26
using hwy::HWY_NAMESPACE::Abs;
27
using hwy::HWY_NAMESPACE::Add;
28
using hwy::HWY_NAMESPACE::Eq;
29
using hwy::HWY_NAMESPACE::Floor;
30
using hwy::HWY_NAMESPACE::Ge;
31
using hwy::HWY_NAMESPACE::GetLane;
32
using hwy::HWY_NAMESPACE::IfThenElse;
33
using hwy::HWY_NAMESPACE::IfThenZeroElse;
34
using hwy::HWY_NAMESPACE::Le;
35
using hwy::HWY_NAMESPACE::Min;
36
using hwy::HWY_NAMESPACE::Mul;
37
using hwy::HWY_NAMESPACE::MulAdd;
38
using hwy::HWY_NAMESPACE::NegMulAdd;
39
using hwy::HWY_NAMESPACE::Rebind;
40
using hwy::HWY_NAMESPACE::ShiftLeft;
41
using hwy::HWY_NAMESPACE::ShiftRight;
42
using hwy::HWY_NAMESPACE::Sub;
43
using hwy::HWY_NAMESPACE::Xor;
44
45
// Computes base-2 logarithm like std::log2. Undefined if negative / NaN.
46
// L1 error ~3.9E-6
47
template <class DF, class V>
48
869M
V FastLog2f(const DF df, V x) {
49
  // 2,2 rational polynomial approximation of std::log1p(x) / std::log(2).
50
869M
  HWY_ALIGN const float p[4 * (2 + 1)] = {HWY_REP4(-1.8503833400518310E-06f),
51
869M
                                          HWY_REP4(1.4287160470083755E+00f),
52
869M
                                          HWY_REP4(7.4245873327820566E-01f)};
53
869M
  HWY_ALIGN const float q[4 * (2 + 1)] = {HWY_REP4(9.9032814277590719E-01f),
54
869M
                                          HWY_REP4(1.0096718572241148E+00f),
55
869M
                                          HWY_REP4(1.7409343003366853E-01f)};
56
57
869M
  const Rebind<int32_t, DF> di;
58
869M
  const auto x_bits = BitCast(di, x);
59
60
  // Range reduction to [-1/3, 1/3] - 3 integer, 2 float ops
61
869M
  const auto exp_bits = Sub(x_bits, Set(di, 0x3f2aaaab));  // = 2/3
62
  // Shifted exponent = log2; also used to clear mantissa.
63
869M
  const auto exp_shifted = ShiftRight<23>(exp_bits);
64
869M
  const auto mantissa = BitCast(df, Sub(x_bits, ShiftLeft<23>(exp_shifted)));
65
869M
  const auto exp_val = ConvertTo(df, exp_shifted);
66
869M
  return Add(EvalRationalPolynomial(df, Sub(mantissa, Set(df, 1.0f)), p, q),
67
869M
             exp_val);
68
869M
}
Unexecuted instantiation: hwy::N_SSE4::Vec128<float, 4ul> jxl::N_SSE4::FastLog2f<hwy::N_SSE4::Simd<float, 4ul, 0>, hwy::N_SSE4::Vec128<float, 4ul> >(hwy::N_SSE4::Simd<float, 4ul, 0>, hwy::N_SSE4::Vec128<float, 4ul>)
hwy::N_AVX2::Vec256<float> jxl::N_AVX2::FastLog2f<hwy::N_AVX2::Simd<float, 8ul, 0>, hwy::N_AVX2::Vec256<float> >(hwy::N_AVX2::Simd<float, 8ul, 0>, hwy::N_AVX2::Vec256<float>)
Line
Count
Source
48
847M
V FastLog2f(const DF df, V x) {
49
  // 2,2 rational polynomial approximation of std::log1p(x) / std::log(2).
50
847M
  HWY_ALIGN const float p[4 * (2 + 1)] = {HWY_REP4(-1.8503833400518310E-06f),
51
847M
                                          HWY_REP4(1.4287160470083755E+00f),
52
847M
                                          HWY_REP4(7.4245873327820566E-01f)};
53
847M
  HWY_ALIGN const float q[4 * (2 + 1)] = {HWY_REP4(9.9032814277590719E-01f),
54
847M
                                          HWY_REP4(1.0096718572241148E+00f),
55
847M
                                          HWY_REP4(1.7409343003366853E-01f)};
56
57
847M
  const Rebind<int32_t, DF> di;
58
847M
  const auto x_bits = BitCast(di, x);
59
60
  // Range reduction to [-1/3, 1/3] - 3 integer, 2 float ops
61
847M
  const auto exp_bits = Sub(x_bits, Set(di, 0x3f2aaaab));  // = 2/3
62
  // Shifted exponent = log2; also used to clear mantissa.
63
847M
  const auto exp_shifted = ShiftRight<23>(exp_bits);
64
847M
  const auto mantissa = BitCast(df, Sub(x_bits, ShiftLeft<23>(exp_shifted)));
65
847M
  const auto exp_val = ConvertTo(df, exp_shifted);
66
847M
  return Add(EvalRationalPolynomial(df, Sub(mantissa, Set(df, 1.0f)), p, q),
67
847M
             exp_val);
68
847M
}
Unexecuted instantiation: hwy::N_SSE2::Vec128<float, 4ul> jxl::N_SSE2::FastLog2f<hwy::N_SSE2::Simd<float, 4ul, 0>, hwy::N_SSE2::Vec128<float, 4ul> >(hwy::N_SSE2::Simd<float, 4ul, 0>, hwy::N_SSE2::Vec128<float, 4ul>)
Unexecuted instantiation: hwy::N_SSE4::Vec128<float, 1ul> jxl::N_SSE4::FastLog2f<hwy::N_SSE4::Simd<float, 1ul, 0>, hwy::N_SSE4::Vec128<float, 1ul> >(hwy::N_SSE4::Simd<float, 1ul, 0>, hwy::N_SSE4::Vec128<float, 1ul>)
hwy::N_AVX2::Vec128<float, 1ul> jxl::N_AVX2::FastLog2f<hwy::N_AVX2::Simd<float, 1ul, 0>, hwy::N_AVX2::Vec128<float, 1ul> >(hwy::N_AVX2::Simd<float, 1ul, 0>, hwy::N_AVX2::Vec128<float, 1ul>)
Line
Count
Source
48
3.81M
V FastLog2f(const DF df, V x) {
49
  // 2,2 rational polynomial approximation of std::log1p(x) / std::log(2).
50
3.81M
  HWY_ALIGN const float p[4 * (2 + 1)] = {HWY_REP4(-1.8503833400518310E-06f),
51
3.81M
                                          HWY_REP4(1.4287160470083755E+00f),
52
3.81M
                                          HWY_REP4(7.4245873327820566E-01f)};
53
3.81M
  HWY_ALIGN const float q[4 * (2 + 1)] = {HWY_REP4(9.9032814277590719E-01f),
54
3.81M
                                          HWY_REP4(1.0096718572241148E+00f),
55
3.81M
                                          HWY_REP4(1.7409343003366853E-01f)};
56
57
3.81M
  const Rebind<int32_t, DF> di;
58
3.81M
  const auto x_bits = BitCast(di, x);
59
60
  // Range reduction to [-1/3, 1/3] - 3 integer, 2 float ops
61
3.81M
  const auto exp_bits = Sub(x_bits, Set(di, 0x3f2aaaab));  // = 2/3
62
  // Shifted exponent = log2; also used to clear mantissa.
63
3.81M
  const auto exp_shifted = ShiftRight<23>(exp_bits);
64
3.81M
  const auto mantissa = BitCast(df, Sub(x_bits, ShiftLeft<23>(exp_shifted)));
65
3.81M
  const auto exp_val = ConvertTo(df, exp_shifted);
66
3.81M
  return Add(EvalRationalPolynomial(df, Sub(mantissa, Set(df, 1.0f)), p, q),
67
3.81M
             exp_val);
68
3.81M
}
hwy::N_SSE2::Vec128<float, 1ul> jxl::N_SSE2::FastLog2f<hwy::N_SSE2::Simd<float, 1ul, 0>, hwy::N_SSE2::Vec128<float, 1ul> >(hwy::N_SSE2::Simd<float, 1ul, 0>, hwy::N_SSE2::Vec128<float, 1ul>)
Line
Count
Source
48
1.03M
V FastLog2f(const DF df, V x) {
49
  // 2,2 rational polynomial approximation of std::log1p(x) / std::log(2).
50
1.03M
  HWY_ALIGN const float p[4 * (2 + 1)] = {HWY_REP4(-1.8503833400518310E-06f),
51
1.03M
                                          HWY_REP4(1.4287160470083755E+00f),
52
1.03M
                                          HWY_REP4(7.4245873327820566E-01f)};
53
1.03M
  HWY_ALIGN const float q[4 * (2 + 1)] = {HWY_REP4(9.9032814277590719E-01f),
54
1.03M
                                          HWY_REP4(1.0096718572241148E+00f),
55
1.03M
                                          HWY_REP4(1.7409343003366853E-01f)};
56
57
1.03M
  const Rebind<int32_t, DF> di;
58
1.03M
  const auto x_bits = BitCast(di, x);
59
60
  // Range reduction to [-1/3, 1/3] - 3 integer, 2 float ops
61
1.03M
  const auto exp_bits = Sub(x_bits, Set(di, 0x3f2aaaab));  // = 2/3
62
  // Shifted exponent = log2; also used to clear mantissa.
63
1.03M
  const auto exp_shifted = ShiftRight<23>(exp_bits);
64
1.03M
  const auto mantissa = BitCast(df, Sub(x_bits, ShiftLeft<23>(exp_shifted)));
65
1.03M
  const auto exp_val = ConvertTo(df, exp_shifted);
66
1.03M
  return Add(EvalRationalPolynomial(df, Sub(mantissa, Set(df, 1.0f)), p, q),
67
1.03M
             exp_val);
68
1.03M
}
hwy::N_AVX2::Vec128<float, 4ul> jxl::N_AVX2::FastLog2f<hwy::N_AVX2::Simd<float, 4ul, 0>, hwy::N_AVX2::Vec128<float, 4ul> >(hwy::N_AVX2::Simd<float, 4ul, 0>, hwy::N_AVX2::Vec128<float, 4ul>)
Line
Count
Source
48
17.4M
V FastLog2f(const DF df, V x) {
49
  // 2,2 rational polynomial approximation of std::log1p(x) / std::log(2).
50
17.4M
  HWY_ALIGN const float p[4 * (2 + 1)] = {HWY_REP4(-1.8503833400518310E-06f),
51
17.4M
                                          HWY_REP4(1.4287160470083755E+00f),
52
17.4M
                                          HWY_REP4(7.4245873327820566E-01f)};
53
17.4M
  HWY_ALIGN const float q[4 * (2 + 1)] = {HWY_REP4(9.9032814277590719E-01f),
54
17.4M
                                          HWY_REP4(1.0096718572241148E+00f),
55
17.4M
                                          HWY_REP4(1.7409343003366853E-01f)};
56
57
17.4M
  const Rebind<int32_t, DF> di;
58
17.4M
  const auto x_bits = BitCast(di, x);
59
60
  // Range reduction to [-1/3, 1/3] - 3 integer, 2 float ops
61
17.4M
  const auto exp_bits = Sub(x_bits, Set(di, 0x3f2aaaab));  // = 2/3
62
  // Shifted exponent = log2; also used to clear mantissa.
63
17.4M
  const auto exp_shifted = ShiftRight<23>(exp_bits);
64
17.4M
  const auto mantissa = BitCast(df, Sub(x_bits, ShiftLeft<23>(exp_shifted)));
65
17.4M
  const auto exp_val = ConvertTo(df, exp_shifted);
66
17.4M
  return Add(EvalRationalPolynomial(df, Sub(mantissa, Set(df, 1.0f)), p, q),
67
17.4M
             exp_val);
68
17.4M
}
69
70
// max relative error ~3e-7
71
template <class DF, class V>
72
26.7M
V FastPow2f(const DF df, V x) {
73
26.7M
  const Rebind<int32_t, DF> di;
74
26.7M
  auto floorx = Floor(x);
75
26.7M
  auto exp =
76
26.7M
      BitCast(df, ShiftLeft<23>(Add(ConvertTo(di, floorx), Set(di, 127))));
77
26.7M
  auto frac = Sub(x, floorx);
78
26.7M
  auto num = Add(frac, Set(df, 1.01749063e+01));
79
26.7M
  num = MulAdd(num, frac, Set(df, 4.88687798e+01));
80
26.7M
  num = MulAdd(num, frac, Set(df, 9.85506591e+01));
81
26.7M
  num = Mul(num, exp);
82
26.7M
  auto den = MulAdd(frac, Set(df, 2.10242958e-01), Set(df, -2.22328856e-02));
83
26.7M
  den = MulAdd(den, frac, Set(df, -1.94414990e+01));
84
26.7M
  den = MulAdd(den, frac, Set(df, 9.85506633e+01));
85
26.7M
  return Div(num, den);
86
26.7M
}
Unexecuted instantiation: hwy::N_SSE4::Vec128<float, 1ul> jxl::N_SSE4::FastPow2f<hwy::N_SSE4::Simd<float, 1ul, 0>, hwy::N_SSE4::Vec128<float, 1ul> >(hwy::N_SSE4::Simd<float, 1ul, 0>, hwy::N_SSE4::Vec128<float, 1ul>)
hwy::N_AVX2::Vec128<float, 1ul> jxl::N_AVX2::FastPow2f<hwy::N_AVX2::Simd<float, 1ul, 0>, hwy::N_AVX2::Vec128<float, 1ul> >(hwy::N_AVX2::Simd<float, 1ul, 0>, hwy::N_AVX2::Vec128<float, 1ul>)
Line
Count
Source
72
7.94M
V FastPow2f(const DF df, V x) {
73
7.94M
  const Rebind<int32_t, DF> di;
74
7.94M
  auto floorx = Floor(x);
75
7.94M
  auto exp =
76
7.94M
      BitCast(df, ShiftLeft<23>(Add(ConvertTo(di, floorx), Set(di, 127))));
77
7.94M
  auto frac = Sub(x, floorx);
78
7.94M
  auto num = Add(frac, Set(df, 1.01749063e+01));
79
7.94M
  num = MulAdd(num, frac, Set(df, 4.88687798e+01));
80
7.94M
  num = MulAdd(num, frac, Set(df, 9.85506591e+01));
81
7.94M
  num = Mul(num, exp);
82
7.94M
  auto den = MulAdd(frac, Set(df, 2.10242958e-01), Set(df, -2.22328856e-02));
83
7.94M
  den = MulAdd(den, frac, Set(df, -1.94414990e+01));
84
7.94M
  den = MulAdd(den, frac, Set(df, 9.85506633e+01));
85
7.94M
  return Div(num, den);
86
7.94M
}
Unexecuted instantiation: hwy::N_SSE2::Vec128<float, 1ul> jxl::N_SSE2::FastPow2f<hwy::N_SSE2::Simd<float, 1ul, 0>, hwy::N_SSE2::Vec128<float, 1ul> >(hwy::N_SSE2::Simd<float, 1ul, 0>, hwy::N_SSE2::Vec128<float, 1ul>)
Unexecuted instantiation: hwy::N_SSE4::Vec128<float, 4ul> jxl::N_SSE4::FastPow2f<hwy::N_SSE4::Simd<float, 4ul, 0>, hwy::N_SSE4::Vec128<float, 4ul> >(hwy::N_SSE4::Simd<float, 4ul, 0>, hwy::N_SSE4::Vec128<float, 4ul>)
hwy::N_AVX2::Vec128<float, 4ul> jxl::N_AVX2::FastPow2f<hwy::N_AVX2::Simd<float, 4ul, 0>, hwy::N_AVX2::Vec128<float, 4ul> >(hwy::N_AVX2::Simd<float, 4ul, 0>, hwy::N_AVX2::Vec128<float, 4ul>)
Line
Count
Source
72
17.4M
V FastPow2f(const DF df, V x) {
73
17.4M
  const Rebind<int32_t, DF> di;
74
17.4M
  auto floorx = Floor(x);
75
17.4M
  auto exp =
76
17.4M
      BitCast(df, ShiftLeft<23>(Add(ConvertTo(di, floorx), Set(di, 127))));
77
17.4M
  auto frac = Sub(x, floorx);
78
17.4M
  auto num = Add(frac, Set(df, 1.01749063e+01));
79
17.4M
  num = MulAdd(num, frac, Set(df, 4.88687798e+01));
80
17.4M
  num = MulAdd(num, frac, Set(df, 9.85506591e+01));
81
17.4M
  num = Mul(num, exp);
82
17.4M
  auto den = MulAdd(frac, Set(df, 2.10242958e-01), Set(df, -2.22328856e-02));
83
17.4M
  den = MulAdd(den, frac, Set(df, -1.94414990e+01));
84
17.4M
  den = MulAdd(den, frac, Set(df, 9.85506633e+01));
85
17.4M
  return Div(num, den);
86
17.4M
}
Unexecuted instantiation: hwy::N_SSE2::Vec128<float, 4ul> jxl::N_SSE2::FastPow2f<hwy::N_SSE2::Simd<float, 4ul, 0>, hwy::N_SSE2::Vec128<float, 4ul> >(hwy::N_SSE2::Simd<float, 4ul, 0>, hwy::N_SSE2::Vec128<float, 4ul>)
hwy::N_AVX2::Vec256<float> jxl::N_AVX2::FastPow2f<hwy::N_AVX2::Simd<float, 8ul, 0>, hwy::N_AVX2::Vec256<float> >(hwy::N_AVX2::Simd<float, 8ul, 0>, hwy::N_AVX2::Vec256<float>)
Line
Count
Source
72
1.36M
V FastPow2f(const DF df, V x) {
73
1.36M
  const Rebind<int32_t, DF> di;
74
1.36M
  auto floorx = Floor(x);
75
1.36M
  auto exp =
76
1.36M
      BitCast(df, ShiftLeft<23>(Add(ConvertTo(di, floorx), Set(di, 127))));
77
1.36M
  auto frac = Sub(x, floorx);
78
1.36M
  auto num = Add(frac, Set(df, 1.01749063e+01));
79
1.36M
  num = MulAdd(num, frac, Set(df, 4.88687798e+01));
80
1.36M
  num = MulAdd(num, frac, Set(df, 9.85506591e+01));
81
1.36M
  num = Mul(num, exp);
82
1.36M
  auto den = MulAdd(frac, Set(df, 2.10242958e-01), Set(df, -2.22328856e-02));
83
1.36M
  den = MulAdd(den, frac, Set(df, -1.94414990e+01));
84
1.36M
  den = MulAdd(den, frac, Set(df, 9.85506633e+01));
85
1.36M
  return Div(num, den);
86
1.36M
}
87
88
// max relative error ~3e-5
89
template <class DF, class V>
90
22.6M
V FastPowf(const DF df, V base, V exponent) {
91
22.6M
  return FastPow2f(df, Mul(FastLog2f(df, base), exponent));
92
22.6M
}
Unexecuted instantiation: hwy::N_SSE4::Vec128<float, 1ul> jxl::N_SSE4::FastPowf<hwy::N_SSE4::Simd<float, 1ul, 0>, hwy::N_SSE4::Vec128<float, 1ul> >(hwy::N_SSE4::Simd<float, 1ul, 0>, hwy::N_SSE4::Vec128<float, 1ul>, hwy::N_SSE4::Vec128<float, 1ul>)
hwy::N_AVX2::Vec128<float, 1ul> jxl::N_AVX2::FastPowf<hwy::N_AVX2::Simd<float, 1ul, 0>, hwy::N_AVX2::Vec128<float, 1ul> >(hwy::N_AVX2::Simd<float, 1ul, 0>, hwy::N_AVX2::Vec128<float, 1ul>, hwy::N_AVX2::Vec128<float, 1ul>)
Line
Count
Source
90
3.81M
V FastPowf(const DF df, V base, V exponent) {
91
3.81M
  return FastPow2f(df, Mul(FastLog2f(df, base), exponent));
92
3.81M
}
Unexecuted instantiation: hwy::N_SSE2::Vec128<float, 1ul> jxl::N_SSE2::FastPowf<hwy::N_SSE2::Simd<float, 1ul, 0>, hwy::N_SSE2::Vec128<float, 1ul> >(hwy::N_SSE2::Simd<float, 1ul, 0>, hwy::N_SSE2::Vec128<float, 1ul>, hwy::N_SSE2::Vec128<float, 1ul>)
Unexecuted instantiation: hwy::N_SSE4::Vec128<float, 4ul> jxl::N_SSE4::FastPowf<hwy::N_SSE4::Simd<float, 4ul, 0>, hwy::N_SSE4::Vec128<float, 4ul> >(hwy::N_SSE4::Simd<float, 4ul, 0>, hwy::N_SSE4::Vec128<float, 4ul>, hwy::N_SSE4::Vec128<float, 4ul>)
hwy::N_AVX2::Vec128<float, 4ul> jxl::N_AVX2::FastPowf<hwy::N_AVX2::Simd<float, 4ul, 0>, hwy::N_AVX2::Vec128<float, 4ul> >(hwy::N_AVX2::Simd<float, 4ul, 0>, hwy::N_AVX2::Vec128<float, 4ul>, hwy::N_AVX2::Vec128<float, 4ul>)
Line
Count
Source
90
17.4M
V FastPowf(const DF df, V base, V exponent) {
91
17.4M
  return FastPow2f(df, Mul(FastLog2f(df, base), exponent));
92
17.4M
}
Unexecuted instantiation: hwy::N_SSE2::Vec128<float, 4ul> jxl::N_SSE2::FastPowf<hwy::N_SSE2::Simd<float, 4ul, 0>, hwy::N_SSE2::Vec128<float, 4ul> >(hwy::N_SSE2::Simd<float, 4ul, 0>, hwy::N_SSE2::Vec128<float, 4ul>, hwy::N_SSE2::Vec128<float, 4ul>)
hwy::N_AVX2::Vec256<float> jxl::N_AVX2::FastPowf<hwy::N_AVX2::Simd<float, 8ul, 0>, hwy::N_AVX2::Vec256<float> >(hwy::N_AVX2::Simd<float, 8ul, 0>, hwy::N_AVX2::Vec256<float>, hwy::N_AVX2::Vec256<float>)
Line
Count
Source
90
1.36M
V FastPowf(const DF df, V base, V exponent) {
91
1.36M
  return FastPow2f(df, Mul(FastLog2f(df, base), exponent));
92
1.36M
}
93
94
// Computes cosine like std::cos.
95
// L1 error 7e-5.
96
template <class DF, class V>
97
1.01G
V FastCosf(const DF df, V x) {
98
  // Step 1: range reduction to [0, 2pi)
99
1.01G
  const auto pi2 = Set(df, kPi * 2.0f);
100
1.01G
  const auto pi2_inv = Set(df, 0.5f / kPi);
101
1.01G
  const auto npi2 = Mul(Floor(Mul(x, pi2_inv)), pi2);
102
1.01G
  const auto xmodpi2 = Sub(x, npi2);
103
  // Step 2: range reduction to [0, pi]
104
1.01G
  const auto x_pi = Min(xmodpi2, Sub(pi2, xmodpi2));
105
  // Step 3: range reduction to [0, pi/2]
106
1.01G
  const auto above_pihalf = Ge(x_pi, Set(df, kPi / 2.0f));
107
1.01G
  const auto x_pihalf = IfThenElse(above_pihalf, Sub(Set(df, kPi), x_pi), x_pi);
108
  // Step 4: Taylor-like approximation, scaled by 2**0.75 to make angle
109
  // duplication steps faster, on x/4.
110
1.01G
  const auto xs = Mul(x_pihalf, Set(df, 0.25f));
111
1.01G
  const auto x2 = Mul(xs, xs);
112
1.01G
  const auto x4 = Mul(x2, x2);
113
1.01G
  const auto cosx_prescaling =
114
1.01G
      MulAdd(x4, Set(df, 0.06960438),
115
1.01G
             MulAdd(x2, Set(df, -0.84087373), Set(df, 1.68179268)));
116
  // Step 5: angle duplication.
117
1.01G
  const auto cosx_scale1 =
118
1.01G
      MulAdd(cosx_prescaling, cosx_prescaling, Set(df, -1.414213562));
119
1.01G
  const auto cosx_scale2 = MulAdd(cosx_scale1, cosx_scale1, Set(df, -1));
120
  // Step 6: change sign if needed.
121
1.01G
  const Rebind<uint32_t, DF> du;
122
1.01G
  auto signbit = ShiftLeft<31>(BitCast(du, VecFromMask(df, above_pihalf)));
123
1.01G
  return BitCast(df, Xor(signbit, BitCast(du, cosx_scale2)));
124
1.01G
}
Unexecuted instantiation: hwy::N_SSE4::Vec128<float, 1ul> jxl::N_SSE4::FastCosf<hwy::N_SSE4::Simd<float, 1ul, 0>, hwy::N_SSE4::Vec128<float, 1ul> >(hwy::N_SSE4::Simd<float, 1ul, 0>, hwy::N_SSE4::Vec128<float, 1ul>)
Unexecuted instantiation: hwy::N_AVX2::Vec128<float, 1ul> jxl::N_AVX2::FastCosf<hwy::N_AVX2::Simd<float, 1ul, 0>, hwy::N_AVX2::Vec128<float, 1ul> >(hwy::N_AVX2::Simd<float, 1ul, 0>, hwy::N_AVX2::Vec128<float, 1ul>)
Unexecuted instantiation: hwy::N_SSE2::Vec128<float, 1ul> jxl::N_SSE2::FastCosf<hwy::N_SSE2::Simd<float, 1ul, 0>, hwy::N_SSE2::Vec128<float, 1ul> >(hwy::N_SSE2::Simd<float, 1ul, 0>, hwy::N_SSE2::Vec128<float, 1ul>)
hwy::N_AVX2::Vec256<float> jxl::N_AVX2::FastCosf<hwy::N_AVX2::Simd<float, 8ul, 0>, hwy::N_AVX2::Vec256<float> >(hwy::N_AVX2::Simd<float, 8ul, 0>, hwy::N_AVX2::Vec256<float>)
Line
Count
Source
97
1.01G
V FastCosf(const DF df, V x) {
98
  // Step 1: range reduction to [0, 2pi)
99
1.01G
  const auto pi2 = Set(df, kPi * 2.0f);
100
1.01G
  const auto pi2_inv = Set(df, 0.5f / kPi);
101
1.01G
  const auto npi2 = Mul(Floor(Mul(x, pi2_inv)), pi2);
102
1.01G
  const auto xmodpi2 = Sub(x, npi2);
103
  // Step 2: range reduction to [0, pi]
104
1.01G
  const auto x_pi = Min(xmodpi2, Sub(pi2, xmodpi2));
105
  // Step 3: range reduction to [0, pi/2]
106
1.01G
  const auto above_pihalf = Ge(x_pi, Set(df, kPi / 2.0f));
107
1.01G
  const auto x_pihalf = IfThenElse(above_pihalf, Sub(Set(df, kPi), x_pi), x_pi);
108
  // Step 4: Taylor-like approximation, scaled by 2**0.75 to make angle
109
  // duplication steps faster, on x/4.
110
1.01G
  const auto xs = Mul(x_pihalf, Set(df, 0.25f));
111
1.01G
  const auto x2 = Mul(xs, xs);
112
1.01G
  const auto x4 = Mul(x2, x2);
113
1.01G
  const auto cosx_prescaling =
114
1.01G
      MulAdd(x4, Set(df, 0.06960438),
115
1.01G
             MulAdd(x2, Set(df, -0.84087373), Set(df, 1.68179268)));
116
  // Step 5: angle duplication.
117
1.01G
  const auto cosx_scale1 =
118
1.01G
      MulAdd(cosx_prescaling, cosx_prescaling, Set(df, -1.414213562));
119
1.01G
  const auto cosx_scale2 = MulAdd(cosx_scale1, cosx_scale1, Set(df, -1));
120
  // Step 6: change sign if needed.
121
1.01G
  const Rebind<uint32_t, DF> du;
122
1.01G
  auto signbit = ShiftLeft<31>(BitCast(du, VecFromMask(df, above_pihalf)));
123
1.01G
  return BitCast(df, Xor(signbit, BitCast(du, cosx_scale2)));
124
1.01G
}
Unexecuted instantiation: hwy::N_SSE4::Vec128<float, 4ul> jxl::N_SSE4::FastCosf<hwy::N_SSE4::Simd<float, 4ul, 0>, hwy::N_SSE4::Vec128<float, 4ul> >(hwy::N_SSE4::Simd<float, 4ul, 0>, hwy::N_SSE4::Vec128<float, 4ul>)
Unexecuted instantiation: hwy::N_SSE2::Vec128<float, 4ul> jxl::N_SSE2::FastCosf<hwy::N_SSE2::Simd<float, 4ul, 0>, hwy::N_SSE2::Vec128<float, 4ul> >(hwy::N_SSE2::Simd<float, 4ul, 0>, hwy::N_SSE2::Vec128<float, 4ul>)
125
126
// Computes the error function like std::erf.
127
// L1 error 7e-4.
128
template <class DF, class V>
129
4.79M
V FastErff(const DF df, V x) {
130
  // Formula from
131
  // https://en.wikipedia.org/wiki/Error_function#Numerical_approximations
132
  // but constants have been recomputed.
133
4.79M
  const auto xle0 = Le(x, Zero(df));
134
4.79M
  const auto absx = Abs(x);
135
  // Compute 1 - 1 / ((((x * a + b) * x + c) * x + d) * x + 1)**4
136
4.79M
  const auto denom1 =
137
4.79M
      MulAdd(absx, Set(df, 7.77394369e-02), Set(df, 2.05260015e-04));
138
4.79M
  const auto denom2 = MulAdd(denom1, absx, Set(df, 2.32120216e-01));
139
4.79M
  const auto denom3 = MulAdd(denom2, absx, Set(df, 2.77820801e-01));
140
4.79M
  const auto denom4 = MulAdd(denom3, absx, Set(df, 1.0f));
141
4.79M
  const auto denom5 = Mul(denom4, denom4);
142
4.79M
  const auto inv_denom5 = Div(Set(df, 1.0f), denom5);
143
4.79M
  const auto result = NegMulAdd(inv_denom5, inv_denom5, Set(df, 1.0f));
144
  // Change sign if needed.
145
4.79M
  const Rebind<uint32_t, DF> du;
146
4.79M
  auto signbit = ShiftLeft<31>(BitCast(du, VecFromMask(df, xle0)));
147
4.79M
  return BitCast(df, Xor(signbit, BitCast(du, result)));
148
4.79M
}
Unexecuted instantiation: hwy::N_SSE4::Vec128<float, 1ul> jxl::N_SSE4::FastErff<hwy::N_SSE4::Simd<float, 1ul, 0>, hwy::N_SSE4::Vec128<float, 1ul> >(hwy::N_SSE4::Simd<float, 1ul, 0>, hwy::N_SSE4::Vec128<float, 1ul>)
hwy::N_AVX2::Vec128<float, 1ul> jxl::N_AVX2::FastErff<hwy::N_AVX2::Simd<float, 1ul, 0>, hwy::N_AVX2::Vec128<float, 1ul> >(hwy::N_AVX2::Simd<float, 1ul, 0>, hwy::N_AVX2::Vec128<float, 1ul>)
Line
Count
Source
129
2.48M
V FastErff(const DF df, V x) {
130
  // Formula from
131
  // https://en.wikipedia.org/wiki/Error_function#Numerical_approximations
132
  // but constants have been recomputed.
133
2.48M
  const auto xle0 = Le(x, Zero(df));
134
2.48M
  const auto absx = Abs(x);
135
  // Compute 1 - 1 / ((((x * a + b) * x + c) * x + d) * x + 1)**4
136
2.48M
  const auto denom1 =
137
2.48M
      MulAdd(absx, Set(df, 7.77394369e-02), Set(df, 2.05260015e-04));
138
2.48M
  const auto denom2 = MulAdd(denom1, absx, Set(df, 2.32120216e-01));
139
2.48M
  const auto denom3 = MulAdd(denom2, absx, Set(df, 2.77820801e-01));
140
2.48M
  const auto denom4 = MulAdd(denom3, absx, Set(df, 1.0f));
141
2.48M
  const auto denom5 = Mul(denom4, denom4);
142
2.48M
  const auto inv_denom5 = Div(Set(df, 1.0f), denom5);
143
2.48M
  const auto result = NegMulAdd(inv_denom5, inv_denom5, Set(df, 1.0f));
144
  // Change sign if needed.
145
2.48M
  const Rebind<uint32_t, DF> du;
146
2.48M
  auto signbit = ShiftLeft<31>(BitCast(du, VecFromMask(df, xle0)));
147
2.48M
  return BitCast(df, Xor(signbit, BitCast(du, result)));
148
2.48M
}
Unexecuted instantiation: hwy::N_SSE2::Vec128<float, 1ul> jxl::N_SSE2::FastErff<hwy::N_SSE2::Simd<float, 1ul, 0>, hwy::N_SSE2::Vec128<float, 1ul> >(hwy::N_SSE2::Simd<float, 1ul, 0>, hwy::N_SSE2::Vec128<float, 1ul>)
hwy::N_AVX2::Vec256<float> jxl::N_AVX2::FastErff<hwy::N_AVX2::Simd<float, 8ul, 0>, hwy::N_AVX2::Vec256<float> >(hwy::N_AVX2::Simd<float, 8ul, 0>, hwy::N_AVX2::Vec256<float>)
Line
Count
Source
129
2.30M
V FastErff(const DF df, V x) {
130
  // Formula from
131
  // https://en.wikipedia.org/wiki/Error_function#Numerical_approximations
132
  // but constants have been recomputed.
133
2.30M
  const auto xle0 = Le(x, Zero(df));
134
2.30M
  const auto absx = Abs(x);
135
  // Compute 1 - 1 / ((((x * a + b) * x + c) * x + d) * x + 1)**4
136
2.30M
  const auto denom1 =
137
2.30M
      MulAdd(absx, Set(df, 7.77394369e-02), Set(df, 2.05260015e-04));
138
2.30M
  const auto denom2 = MulAdd(denom1, absx, Set(df, 2.32120216e-01));
139
2.30M
  const auto denom3 = MulAdd(denom2, absx, Set(df, 2.77820801e-01));
140
2.30M
  const auto denom4 = MulAdd(denom3, absx, Set(df, 1.0f));
141
2.30M
  const auto denom5 = Mul(denom4, denom4);
142
2.30M
  const auto inv_denom5 = Div(Set(df, 1.0f), denom5);
143
2.30M
  const auto result = NegMulAdd(inv_denom5, inv_denom5, Set(df, 1.0f));
144
  // Change sign if needed.
145
2.30M
  const Rebind<uint32_t, DF> du;
146
2.30M
  auto signbit = ShiftLeft<31>(BitCast(du, VecFromMask(df, xle0)));
147
2.30M
  return BitCast(df, Xor(signbit, BitCast(du, result)));
148
2.30M
}
Unexecuted instantiation: hwy::N_SSE4::Vec128<float, 4ul> jxl::N_SSE4::FastErff<hwy::N_SSE4::Simd<float, 4ul, 0>, hwy::N_SSE4::Vec128<float, 4ul> >(hwy::N_SSE4::Simd<float, 4ul, 0>, hwy::N_SSE4::Vec128<float, 4ul>)
Unexecuted instantiation: hwy::N_SSE2::Vec128<float, 4ul> jxl::N_SSE2::FastErff<hwy::N_SSE2::Simd<float, 4ul, 0>, hwy::N_SSE2::Vec128<float, 4ul> >(hwy::N_SSE2::Simd<float, 4ul, 0>, hwy::N_SSE2::Vec128<float, 4ul>)
149
150
1.03M
inline float FastLog2f(float f) {
151
1.03M
  HWY_CAPPED(float, 1) D;
152
1.03M
  return GetLane(FastLog2f(D, Set(D, f)));
153
1.03M
}
Unexecuted instantiation: jxl::N_SSE4::FastLog2f(float)
Unexecuted instantiation: jxl::N_AVX2::FastLog2f(float)
jxl::N_SSE2::FastLog2f(float)
Line
Count
Source
150
1.03M
inline float FastLog2f(float f) {
151
1.03M
  HWY_CAPPED(float, 1) D;
152
1.03M
  return GetLane(FastLog2f(D, Set(D, f)));
153
1.03M
}
154
155
4.12M
inline float FastPow2f(float f) {
156
4.12M
  HWY_CAPPED(float, 1) D;
157
4.12M
  return GetLane(FastPow2f(D, Set(D, f)));
158
4.12M
}
Unexecuted instantiation: jxl::N_SSE4::FastPow2f(float)
jxl::N_AVX2::FastPow2f(float)
Line
Count
Source
155
4.12M
inline float FastPow2f(float f) {
156
4.12M
  HWY_CAPPED(float, 1) D;
157
4.12M
  return GetLane(FastPow2f(D, Set(D, f)));
158
4.12M
}
Unexecuted instantiation: jxl::N_SSE2::FastPow2f(float)
159
160
3.81M
inline float FastPowf(float b, float e) {
161
3.81M
  HWY_CAPPED(float, 1) D;
162
3.81M
  return GetLane(FastPowf(D, Set(D, b), Set(D, e)));
163
3.81M
}
Unexecuted instantiation: jxl::N_SSE4::FastPowf(float, float)
jxl::N_AVX2::FastPowf(float, float)
Line
Count
Source
160
3.81M
inline float FastPowf(float b, float e) {
161
3.81M
  HWY_CAPPED(float, 1) D;
162
3.81M
  return GetLane(FastPowf(D, Set(D, b), Set(D, e)));
163
3.81M
}
Unexecuted instantiation: jxl::N_SSE2::FastPowf(float, float)
164
165
0
inline float FastCosf(float f) {
166
0
  HWY_CAPPED(float, 1) D;
167
0
  return GetLane(FastCosf(D, Set(D, f)));
168
0
}
Unexecuted instantiation: jxl::N_SSE4::FastCosf(float)
Unexecuted instantiation: jxl::N_AVX2::FastCosf(float)
Unexecuted instantiation: jxl::N_SSE2::FastCosf(float)
169
170
0
inline float FastErff(float f) {
171
0
  HWY_CAPPED(float, 1) D;
172
0
  return GetLane(FastErff(D, Set(D, f)));
173
0
}
Unexecuted instantiation: jxl::N_SSE4::FastErff(float)
Unexecuted instantiation: jxl::N_AVX2::FastErff(float)
Unexecuted instantiation: jxl::N_SSE2::FastErff(float)
174
175
// Returns cbrt(x) + add with 6 ulp max error.
176
// Modified from vectormath_exp.h, Apache 2 license.
177
// https://www.agner.org/optimize/vectorclass.zip
178
template <class V>
179
98.6M
V CubeRootAndAdd(const V x, const V add) {
180
98.6M
  const HWY_FULL(float) df;
181
98.6M
  const HWY_FULL(int32_t) di;
182
183
98.6M
  const auto kExpBias = Set(di, 0x54800000);  // cast(1.) + cast(1.) / 3
184
98.6M
  const auto kExpMul = Set(di, 0x002AAAAA);   // shifted 1/3
185
98.6M
  const auto k1_3 = Set(df, 1.0f / 3);
186
98.6M
  const auto k4_3 = Set(df, 4.0f / 3);
187
188
98.6M
  const auto xa = x;  // assume inputs never negative
189
98.6M
  const auto xa_3 = Mul(k1_3, xa);
190
191
  // Multiply exponent by -1/3
192
98.6M
  const auto m1 = BitCast(di, xa);
193
  // Special case for 0. 0 is represented with an exponent of 0, so the
194
  // "kExpBias - 1/3 * exp" below gives the wrong result. The IfThenZeroElse()
195
  // sets those values as 0, which prevents having NaNs in the computations
196
  // below.
197
  // TODO(eustas): use fused op
198
98.6M
  const auto m2 = IfThenZeroElse(
199
98.6M
      Eq(m1, Zero(di)), Sub(kExpBias, Mul((ShiftRight<23>(m1)), kExpMul)));
200
98.6M
  auto r = BitCast(df, m2);
201
202
  // Newton-Raphson iterations
203
394M
  for (int i = 0; i < 3; i++) {
204
295M
    const auto r2 = Mul(r, r);
205
295M
    r = NegMulAdd(xa_3, Mul(r2, r2), Mul(k4_3, r));
206
295M
  }
207
  // Final iteration
208
98.6M
  auto r2 = Mul(r, r);
209
98.6M
  r = MulAdd(k1_3, NegMulAdd(xa, Mul(r2, r2), r), r);
210
98.6M
  r2 = Mul(r, r);
211
98.6M
  r = MulAdd(r2, x, add);
212
213
98.6M
  return r;
214
98.6M
}
Unexecuted instantiation: hwy::N_SSE4::Vec128<float, 4ul> jxl::N_SSE4::CubeRootAndAdd<hwy::N_SSE4::Vec128<float, 4ul> >(hwy::N_SSE4::Vec128<float, 4ul>, hwy::N_SSE4::Vec128<float, 4ul>)
hwy::N_AVX2::Vec256<float> jxl::N_AVX2::CubeRootAndAdd<hwy::N_AVX2::Vec256<float> >(hwy::N_AVX2::Vec256<float>, hwy::N_AVX2::Vec256<float>)
Line
Count
Source
179
98.6M
V CubeRootAndAdd(const V x, const V add) {
180
98.6M
  const HWY_FULL(float) df;
181
98.6M
  const HWY_FULL(int32_t) di;
182
183
98.6M
  const auto kExpBias = Set(di, 0x54800000);  // cast(1.) + cast(1.) / 3
184
98.6M
  const auto kExpMul = Set(di, 0x002AAAAA);   // shifted 1/3
185
98.6M
  const auto k1_3 = Set(df, 1.0f / 3);
186
98.6M
  const auto k4_3 = Set(df, 4.0f / 3);
187
188
98.6M
  const auto xa = x;  // assume inputs never negative
189
98.6M
  const auto xa_3 = Mul(k1_3, xa);
190
191
  // Multiply exponent by -1/3
192
98.6M
  const auto m1 = BitCast(di, xa);
193
  // Special case for 0. 0 is represented with an exponent of 0, so the
194
  // "kExpBias - 1/3 * exp" below gives the wrong result. The IfThenZeroElse()
195
  // sets those values as 0, which prevents having NaNs in the computations
196
  // below.
197
  // TODO(eustas): use fused op
198
98.6M
  const auto m2 = IfThenZeroElse(
199
98.6M
      Eq(m1, Zero(di)), Sub(kExpBias, Mul((ShiftRight<23>(m1)), kExpMul)));
200
98.6M
  auto r = BitCast(df, m2);
201
202
  // Newton-Raphson iterations
203
394M
  for (int i = 0; i < 3; i++) {
204
295M
    const auto r2 = Mul(r, r);
205
295M
    r = NegMulAdd(xa_3, Mul(r2, r2), Mul(k4_3, r));
206
295M
  }
207
  // Final iteration
208
98.6M
  auto r2 = Mul(r, r);
209
98.6M
  r = MulAdd(k1_3, NegMulAdd(xa, Mul(r2, r2), r), r);
210
98.6M
  r2 = Mul(r, r);
211
98.6M
  r = MulAdd(r2, x, add);
212
213
98.6M
  return r;
214
98.6M
}
Unexecuted instantiation: hwy::N_SSE2::Vec128<float, 4ul> jxl::N_SSE2::CubeRootAndAdd<hwy::N_SSE2::Vec128<float, 4ul> >(hwy::N_SSE2::Vec128<float, 4ul>, hwy::N_SSE2::Vec128<float, 4ul>)
215
216
// NOLINTNEXTLINE(google-readability-namespace-comments)
217
}  // namespace HWY_NAMESPACE
218
}  // namespace jxl
219
HWY_AFTER_NAMESPACE();
220
221
#endif  // LIB_JXL_BASE_FAST_MATH_INL_H_
222
223
#if HWY_ONCE
224
#ifndef LIB_JXL_BASE_FAST_MATH_ONCE
225
#define LIB_JXL_BASE_FAST_MATH_ONCE
226
227
namespace jxl {
228
1.03M
inline float FastLog2f(float f) { return HWY_STATIC_DISPATCH(FastLog2f)(f); }
229
0
inline float FastPow2f(float f) { return HWY_STATIC_DISPATCH(FastPow2f)(f); }
230
0
inline float FastPowf(float b, float e) {
231
0
  return HWY_STATIC_DISPATCH(FastPowf)(b, e);
232
0
}
233
0
inline float FastCosf(float f) { return HWY_STATIC_DISPATCH(FastCosf)(f); }
234
0
inline float FastErff(float f) { return HWY_STATIC_DISPATCH(FastErff)(f); }
235
}  // namespace jxl
236
237
#endif  // LIB_JXL_BASE_FAST_MATH_ONCE
238
#endif  // HWY_ONCE