Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
230 changes: 224 additions & 6 deletions src/impl/vamp/vector/interface.hh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

namespace vamp
{
template <typename SimdT, std::size_t num_rows, std::size_t num_scalars_per_row>
struct Vector;

template <typename S>
inline constexpr void print_vector(std::ostream &out, typename S::VectorT vec) noexcept
{
Expand Down Expand Up @@ -328,6 +331,17 @@ namespace vamp
return D(apply<S::template or_<0>>(d()->data, o));
}

inline constexpr auto xor_(D o) const noexcept -> D
{
return xor_(o.data);
}

template <typename T, typename allow_types<DataT, typename S::VectorT>::template check<T> = true>
inline constexpr auto xor_(T o) const noexcept -> D
{
return D(apply<S::template xor_<0>>(d()->data, o));
}

template <
typename T,
typename allow_types<DataT, typename S::VectorT, unsigned int>::template check<T> = true>
Expand Down Expand Up @@ -441,9 +455,49 @@ namespace vamp
return *d() + (other - *d()) * alpha;
}

inline constexpr auto log() const noexcept -> D
template <
typename ScalarT = typename S::ScalarT,
typename = std::enable_if_t<std::is_same_v<ScalarT, float>, bool>>
inline auto log() const noexcept -> D
{
return D(apply<S::template log<0>>(d()->data));
using Scalar = typename S::ScalarT;
using IntVec = Vector<typename S::IntT, num_rows, num_scalars_per_row>;
using IntScalar = typename IntVec::S::ScalarT;

D x = *d();
const D invalid_mask = x <= Scalar(0);

x = x.max(D(std::numeric_limits<Scalar>::min()));
IntVec emm0 = x.template as<IntVec>() >> 23U;

const D mantissa_mask = IntVec::fill(static_cast<IntScalar>(0x807FFFFF)).template as<D>();
x = x & mantissa_mask;
x = x | D(Scalar(0.5F));

emm0 = emm0 - IntVec::fill(0x7F);
D e = D::template from<IntVec>(emm0) + Scalar(1);

const D small = x < Scalar(0.707106781186547524F);
const D delta = x & small;
x = x - Scalar(1) + delta;
e = e - (D(Scalar(1)) & small);

const D z = x * x;
D y = D(Scalar(7.0376836292E-2F));
y = y * x + Scalar(-1.1514610310E-1F);
y = y * x + Scalar(+1.1676998740E-1F);
y = y * x + Scalar(-1.2420140846E-1F);
y = y * x + Scalar(+1.4249322787E-1F);
y = y * x + Scalar(-1.6668057665E-1F);
y = y * x + Scalar(+2.0000714765E-1F);
y = y * x + Scalar(-2.4999993993E-1F);
y = y * x + Scalar(+3.3333331174E-1F);
y = y * x * z;
y = y + e * Scalar(-2.12194440E-4F);
y = y - z * Scalar(0.5F);
x = x + y + e * Scalar(0.693359375F);

return x | invalid_mask;
}

inline constexpr auto remove_corrupted() const noexcept -> D
Expand All @@ -456,9 +510,44 @@ namespace vamp
typename ScalarT = typename S::ScalarT,
typename =
std::enable_if_t<std::is_same_v<ScalarT, float> or std::is_same_v<ScalarT, double>, bool>>
inline constexpr auto sin() const noexcept -> D
inline auto sin() const noexcept -> D
{
return D(apply<S::template sin<0>>(d()->data));
using Scalar = typename S::ScalarT;
using IntVec = Vector<typename S::IntT, num_rows, num_scalars_per_row>;

D x = *d();
D sign_bit = x & Scalar(-0.0F);

x = x.abs();
D y = x * Scalar(1.27323954473516F);
IntVec emm2 = IntVec::template from<D>(y);
emm2 = (emm2 + IntVec::fill(1)) & IntVec::fill(~1);
y = D::template from<IntVec>(emm2);

const IntVec emm0 = (emm2 & IntVec::fill(4)) << 29U;
const IntVec poly_select = (emm2 & IntVec::fill(2)).equal(IntVec::fill(0));

const D swap_sign_bit = emm0.template as<D>();
const D poly_mask = poly_select.template as<D>();
sign_bit = sign_bit ^ swap_sign_bit;

x = x + y * Scalar(-0.78515625F);
x = x + y * Scalar(-2.4187564849853515625E-4F);
x = x + y * Scalar(-3.77489497744594108E-8F);

const D z = x * x;
D poly_a = D(Scalar(2.443315711809948E-5F));
poly_a = poly_a * z + Scalar(-1.388731625493765E-3F);
poly_a = poly_a * z + Scalar(4.166664568298827E-2F);
poly_a = poly_a * z * z - z * Scalar(0.5F) + Scalar(1.0F);

D poly_b = D(Scalar(-1.9515295891E-4F));
poly_b = poly_b * z + Scalar(8.3321608736E-3F);
poly_b = poly_b * z + Scalar(-1.6666654611E-1F);
poly_b = poly_b * z * x + x;

D y_out = poly_a.blend(poly_b, poly_mask);
return y_out ^ sign_bit;
}

template <
Expand All @@ -474,6 +563,126 @@ namespace vamp
return vsq_sq.sin();
}

template <
typename ScalarT = typename S::ScalarT,
typename =
std::enable_if_t<std::is_same_v<ScalarT, float> or std::is_same_v<ScalarT, double>, bool>>
inline auto asin() const noexcept -> D
{
using Scalar = typename S::ScalarT;
const D x = *d();
const D a = x.abs();
const auto gt_half = a > Scalar(0.5);

const D zz_high = (D(Scalar(1)) - a) * Scalar(0.5);
const D zz = (a * a).blend(zz_high, gt_half);
const D x_branch = a.blend(zz_high.sqrt(), gt_half);

D z = D(Scalar(4.2163199048E-2));
z = z * zz + Scalar(2.4181311049E-2);
z = z * zz + Scalar(4.5470025998E-2);
z = z * zz + Scalar(7.4953002686E-2);
z = z * zz + Scalar(1.6666752422E-1);
z = z * zz * x_branch + x_branch;

const D z_large = Scalar(1.5707963267948966) - (z + z);
const D res = z.blend(z_large, gt_half);

return res.blend(-res, x < Scalar(0));
}

template <
typename ScalarT = typename S::ScalarT,
typename =
std::enable_if_t<std::is_same_v<ScalarT, float> or std::is_same_v<ScalarT, double>, bool>>
inline auto acos() const noexcept -> D
{
using Scalar = typename S::ScalarT;
const D x = *d();
const auto gt_half = x > Scalar(0.5);

const D arg1 = Scalar(0.5) - x * Scalar(0.5);
const D z = arg1.sqrt().asin() * Scalar(2);

constexpr Scalar PI4 = 7.85398163397448309616E-1;
constexpr Scalar MOREBITS = 6.123233995736765886130E-17;
const D z2 = (Scalar(PI4) - x.asin()) + Scalar(MOREBITS) + Scalar(PI4);

return z2.blend(z, gt_half);
}

template <
typename ScalarT = typename S::ScalarT,
typename =
std::enable_if_t<std::is_same_v<ScalarT, float> or std::is_same_v<ScalarT, double>, bool>>
inline auto atan() const noexcept -> D
{
using Scalar = typename S::ScalarT;
const D x = *d();
D a = x.abs();

const auto gt_3pi8 = a > Scalar(2.41421356237309504880);
const auto gt_pi8 = a > Scalar(0.41421356237309504880);
const auto mid_mask = gt_pi8 & ~gt_3pi8;

const D reduced_big = D(Scalar(-1)) / a;
const D reduced_mid = (a - Scalar(1)) / (a + Scalar(1));

a = a.blend(reduced_big, gt_3pi8).blend(reduced_mid, mid_mask);

D y = D(Scalar(0));
y = y.blend(D(Scalar(1.57079632679489661923)), gt_3pi8);
y = y.blend(D(Scalar(0.785398163397448309616)), mid_mask);

const D zz = a * a;
D p = D(Scalar(-8.750608600031904122785E-1));
p = p * zz + Scalar(-1.615753718733365076637E1);
p = p * zz + Scalar(-7.500855792314704667340E1);
p = p * zz + Scalar(-1.228866684490136173410E2);
p = p * zz + Scalar(-6.485021904942025371773E1);
p = p * zz * a;

D q = zz + Scalar(2.485846490142306297962E1);
q = q * zz + Scalar(1.650270098316988542046E2);
q = q * zz + Scalar(4.328810604912902668951E2);
q = q * zz + Scalar(4.853903996359136964868E2);
q = q * zz + Scalar(1.945506571482613964425E2);

const D z = p / q + a + y;
return z.blend(-z, x < Scalar(0));
}

template <
typename ScalarT = typename S::ScalarT,
typename =
std::enable_if_t<std::is_same_v<ScalarT, float> or std::is_same_v<ScalarT, double>, bool>>
inline auto atan2(D x) const noexcept -> D
{
using Scalar = typename S::ScalarT;
constexpr Scalar PI = 3.14159265358979323846;
constexpr Scalar PI_2 = 1.57079632679489661923;

const D y = *d();
D z = (y / x).atan();

const auto x_lt_0 = x < Scalar(0);
const auto y_lt_0 = y < Scalar(0);
const auto y_ge_0 = y >= Scalar(0);

// x < 0, y >= 0 -> +pi; x < 0, y < 0 -> -pi
z = z.blend(z + Scalar(PI), x_lt_0 & y_ge_0);
z = z.blend(z - Scalar(PI), x_lt_0 & y_lt_0);

// x == 0: +/- pi/2 depending on sign of y
const auto x_eq_0 = x == Scalar(0);
z = z.blend(D(Scalar(PI_2)), x_eq_0 & (y > Scalar(0)));
z = z.blend(D(Scalar(-PI_2)), x_eq_0 & (y < Scalar(0)));

// x == 0 && y == 0 -> 0
z = z.blend(D(Scalar(0)), x_eq_0 & (y == Scalar(0)));
return z;
}

template <typename OtherT, typename BoundsT>
inline static constexpr auto map_to_range(OtherT v, BoundsT min_v, BoundsT max_v) noexcept -> D
{
Expand Down Expand Up @@ -543,6 +752,11 @@ namespace vamp
return and_(o.data);
}

inline constexpr auto operator^(D o) const noexcept -> D
{
return xor_(o.data);
}

inline constexpr auto operator==(D o) const noexcept -> D
{
return equal(o.data);
Expand Down Expand Up @@ -661,9 +875,9 @@ namespace vamp
}

template <typename OtherT, typename std::enable_if_t<not std::is_same_v<OtherT, D>, bool> = true>
inline static constexpr auto from(typename OtherT::DataT v) noexcept -> D
inline static constexpr auto from(OtherT v) noexcept -> D
{
return D(apply<S::template from<typename OtherT::S::VectorT>>(v.data));
return D(apply<S::template from<typename OtherT::S::VectorT>, typename OtherT::DataT>(v.data));
}

template <typename OtherT, typename std::enable_if_t<not std::is_same_v<OtherT, D>, bool> = true>
Expand Down Expand Up @@ -953,6 +1167,10 @@ namespace vamp
data.data() + idx));
}

// NOTE: Viewing rows in place as a distinct Vector type violates strict aliasing;
// GCC 16 at -O2 dead-store-eliminates whole-Vector writes whose only later reads are
// through these row references. VAMP must be compiled with -fno-strict-aliasing
// (applied via the CMake interface target).
[[nodiscard]] inline constexpr auto row(std::size_t idx) noexcept -> RowT &
{
return *reinterpret_cast<RowT *>(
Expand Down
Loading
Loading