diff --git a/src/impl/vamp/vector/interface.hh b/src/impl/vamp/vector/interface.hh index 402f07c6..8c251ff3 100644 --- a/src/impl/vamp/vector/interface.hh +++ b/src/impl/vamp/vector/interface.hh @@ -16,6 +16,9 @@ namespace vamp { + template + struct Vector; + template inline constexpr void print_vector(std::ostream &out, typename S::VectorT vec) noexcept { @@ -328,6 +331,17 @@ namespace vamp return D(apply>(d()->data, o)); } + inline constexpr auto xor_(D o) const noexcept -> D + { + return xor_(o.data); + } + + template ::template check = true> + inline constexpr auto xor_(T o) const noexcept -> D + { + return D(apply>(d()->data, o)); + } + template < typename T, typename allow_types::template check = true> @@ -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, bool>> + inline auto log() const noexcept -> D { - return D(apply>(d()->data)); + using Scalar = typename S::ScalarT; + using IntVec = Vector; + using IntScalar = typename IntVec::S::ScalarT; + + D x = *d(); + const D invalid_mask = x <= Scalar(0); + + x = x.max(D(std::numeric_limits::min())); + IntVec emm0 = x.template as() >> 23U; + + const D mantissa_mask = IntVec::fill(static_cast(0x807FFFFF)).template as(); + x = x & mantissa_mask; + x = x | D(Scalar(0.5F)); + + emm0 = emm0 - IntVec::fill(0x7F); + D e = D::template from(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 @@ -456,9 +510,44 @@ namespace vamp typename ScalarT = typename S::ScalarT, typename = std::enable_if_t or std::is_same_v, bool>> - inline constexpr auto sin() const noexcept -> D + inline auto sin() const noexcept -> D { - return D(apply>(d()->data)); + using Scalar = typename S::ScalarT; + using IntVec = Vector; + + D x = *d(); + D sign_bit = x & Scalar(-0.0F); + + x = x.abs(); + D y = x * Scalar(1.27323954473516F); + IntVec emm2 = IntVec::template from(y); + emm2 = (emm2 + IntVec::fill(1)) & IntVec::fill(~1); + y = D::template from(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(); + const D poly_mask = poly_select.template as(); + 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 < @@ -474,6 +563,126 @@ namespace vamp return vsq_sq.sin(); } + template < + typename ScalarT = typename S::ScalarT, + typename = + std::enable_if_t or std::is_same_v, 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 or std::is_same_v, 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 or std::is_same_v, 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 or std::is_same_v, 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 inline static constexpr auto map_to_range(OtherT v, BoundsT min_v, BoundsT max_v) noexcept -> D { @@ -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); @@ -661,9 +875,9 @@ namespace vamp } template , 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>(v.data)); + return D(apply, typename OtherT::DataT>(v.data)); } template , bool> = true> @@ -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( diff --git a/src/impl/vamp/vector/isa/avx.hh b/src/impl/vamp/vector/isa/avx.hh index 6dfa3a8a..e7bcd08e 100644 --- a/src/impl/vamp/vector/isa/avx.hh +++ b/src/impl/vamp/vector/isa/avx.hh @@ -232,6 +232,7 @@ namespace vamp { using VectorT = __m256; using ScalarT = float; + using IntT = SIMDVector<__m256i>; static constexpr std::size_t VectorWidth = 8; static constexpr std::size_t Alignment = 32; @@ -290,6 +291,12 @@ namespace vamp l, _mm256_castsi256_ps(_mm256_cmpeq_epi32(_mm256_castps_si256(l), _mm256_castps_si256(l)))); } + template + inline static constexpr auto xor_(VectorT l, VectorT r) noexcept -> VectorT + { + return _mm256_xor_ps(l, r); + } + // NOTE: Dummy parameter because otherwise we get constexpr errors with set1_ps... template inline static constexpr auto neg(VectorT l) noexcept -> VectorT @@ -411,7 +418,7 @@ namespace vamp template inline static constexpr auto sqrt(VectorT v) noexcept -> VectorT { - return _mm256_mul_ps(v, _mm256_rsqrt_ps(v)); + return _mm256_sqrt_ps(v); } template @@ -457,164 +464,6 @@ namespace vamp return _mm_cvtss_f32(sum_3); } - // converted from http://gruntthepeon.free.fr/ssemath/ - template - inline static constexpr auto sin(VectorT x) noexcept -> VectorT - { - using IntVector = SIMDVector<__m256i>; - - const auto ps_sign_mask = constant_int(0x80000000); - const auto ps_cephes_FOPI = constant(1.27323954473516f); // 4/Pi - const auto pi32_1 = IntVector::constant(1); - const auto pi32_inv1 = IntVector::constant(~1); - const auto pi32_4 = IntVector::constant(4); - const auto pi32_2 = IntVector::constant(2); - const auto ps_minus_cephes_DP1 = constant(-0.78515625f); - const auto ps_minus_cephes_DP2 = constant(-2.4187564849853515625e-4f); - const auto ps_minus_cephes_DP3 = constant(-3.77489497744594108e-8f); - const auto ps_coscof_p0 = constant(2.443315711809948E-005f); - const auto ps_coscof_p1 = constant(-1.388731625493765E-003f); - const auto ps_coscof_p2 = constant(4.166664568298827E-002f); - const auto ps_0p5 = constant(0.5f); - const auto ps_1 = constant(1.0f); - const auto ps_sincof_p0 = constant(-1.9515295891E-4f); - const auto ps_sincof_p1 = constant(8.3321608736E-3f); - const auto ps_sincof_p2 = constant(-1.6666654611E-1f); - - auto sign_bit = x; - - x = abs(x); - sign_bit = and_(sign_bit, ps_sign_mask); - auto y = mul(x, ps_cephes_FOPI); - - auto emm2 = IntVector::from(y); - - // j=(j+1) & (~1) (see the cephes sources) - emm2 = IntVector::add(emm2, pi32_1); - emm2 = IntVector::and_(emm2, pi32_inv1); - y = from(emm2); - - // Get the swap sign flag - auto emm0 = IntVector::and_(emm2, pi32_4); - emm0 = IntVector::shift_left(emm0, 29); - - // Get the polynom selection mask - // There is one polynom for 0 <= x <= Pi/4 - // and another one for Pi/4 < x <= Pi/2 - // Both branches will be computed. - emm2 = IntVector::and_(emm2, pi32_2); - emm2 = IntVector::cmp_equal(emm2, IntVector::zero_vector()); - - auto swap_sign_bit = IntVector::template as(emm0); - auto poly_mask = IntVector::template as(emm2); - sign_bit = _mm256_xor_ps(sign_bit, swap_sign_bit); - - // The magic pass: "Extended precision modular arithmetic" - // x = ((x - y * DP1) - y * DP2) - y * DP3; - auto xmm1 = mul(y, ps_minus_cephes_DP1); - auto xmm2 = mul(y, ps_minus_cephes_DP2); - auto xmm3 = mul(y, ps_minus_cephes_DP3); - x = add(x, xmm1); - x = add(x, xmm2); - x = add(x, xmm3); - - // Evaluate the first polynom (0 <= x <= Pi/4) - y = ps_coscof_p0; - auto z = mul(x, x); - y = mul(y, z); - y = add(y, ps_coscof_p1); - y = mul(y, z); - y = add(y, ps_coscof_p2); - y = mul(y, z); - y = mul(y, z); - auto tmp = mul(z, ps_0p5); - y = sub(y, tmp); - y = add(y, ps_1); - - // Evaluate the second polynom (Pi/4 <= x <= 0) - auto y2 = ps_sincof_p0; - y2 = mul(y2, z); - y2 = add(y2, ps_sincof_p1); - y2 = mul(y2, z); - y2 = add(y2, ps_sincof_p2); - y2 = mul(y2, z); - y2 = mul(y2, x); - y2 = add(y2, x); - - // Select the correct result from the two polynoms - xmm3 = poly_mask; - y2 = and_(xmm3, y2); - y = _mm256_andnot_ps(xmm3, y); - y = add(y, y2); - - // Update the sign - y = _mm256_xor_ps(y, sign_bit); - - return y; - } - - // NOTE: Dummy parameter because otherwise we get constexpr errors with set1_ps... - template - inline static constexpr auto log(VectorT x) noexcept -> VectorT - { - using IntVector = SIMDVector<__m256i>; - - const auto half = constant(0.5F); - const auto one = constant(1.0F); - auto invalid_mask = cmp_less_equal(x, zero_vector()); - - // cut off denormalized values - x = max(x, constant_int(0x00800000)); - - auto emm0 = IntVector::shift_right(as(x), 23); - - x = and_(x, constant_int(~0x7f800000)); - x = or_(x, half); - - // keep only the fractional part - emm0 = IntVector::sub(emm0, IntVector::constant(0x7f)); - auto e = from(emm0); - - e = add(e, one); - - // compute approx - auto mask = cmp_less_than(x, constant(0.707106781186547524f)); - auto tmp = and_(x, mask); - x = sub(x, one); - e = sub(e, and_(one, mask)); - x = add(x, tmp); - - auto z = mul(x, x); - - auto y = constant(7.0376836292E-2f); - y = mul(y, x); - y = add(y, constant(-1.1514610310E-1f)); - y = mul(y, x); - y = add(y, constant(1.1676998740E-1f)); - y = mul(y, x); - y = add(y, constant(-1.2420140846E-1f)); - y = mul(y, x); - y = add(y, constant(+1.4249322787E-1f)); - y = mul(y, x); - y = add(y, constant(-1.6668057665E-1f)); - y = mul(y, x); - y = add(y, constant(+2.0000714765E-1f)); - y = mul(y, x); - y = add(y, constant(-2.4999993993E-1f)); - y = mul(y, x); - y = add(y, constant(+3.3333331174E-1f)); - y = mul(y, mul(x, z)); - tmp = mul(e, constant(-2.12194440e-4f)); - y = add(y, tmp); - tmp = mul(z, half); - y = sub(y, tmp); - tmp = mul(e, constant(0.693359375f)); - x = add(x, add(y, tmp)); - - x = or_(x, invalid_mask); // negative arg will be NAN - return x; - } - template inline static constexpr auto blend(VectorT a, VectorT b, VectorT blend_mask) noexcept -> VectorT { diff --git a/src/impl/vamp/vector/isa/neon.hh b/src/impl/vamp/vector/isa/neon.hh index b0587123..d4080edd 100644 --- a/src/impl/vamp/vector/isa/neon.hh +++ b/src/impl/vamp/vector/isa/neon.hh @@ -88,6 +88,12 @@ namespace vamp return vreinterpretq_s32_u32(vorrq_u32(vreinterpretq_u32_s32(l), vreinterpretq_u32_s32(r))); } + template + inline static constexpr auto xor_(VectorT l, VectorT r) noexcept -> VectorT + { + return vreinterpretq_s32_u32(veorq_u32(vreinterpretq_u32_s32(l), vreinterpretq_u32_s32(r))); + } + template inline static constexpr auto lshift_lookup(VectorT v, ScalarT shift, std::index_sequence) noexcept -> VectorT @@ -258,6 +264,7 @@ namespace vamp { using VectorT = float32x4_t; using ScalarT = float32_t; + using IntT = SIMDVector; static constexpr std::size_t VectorWidth = 4; static constexpr std::size_t Alignment = 16; @@ -464,6 +471,12 @@ namespace vamp return vreinterpretq_f32_u32(vorrq_u32(vreinterpretq_u32_f32(l), vreinterpretq_u32_f32(r))); } + template + inline static constexpr auto xor_(VectorT l, VectorT r) noexcept -> VectorT + { + return vreinterpretq_f32_u32(veorq_u32(vreinterpretq_u32_f32(l), vreinterpretq_u32_f32(r))); + } + template inline static constexpr auto lshift_lookup(VectorT v, ScalarT shift, std::index_sequence) noexcept -> VectorT @@ -540,139 +553,6 @@ namespace vamp return vaddvq_f32(v); } - // converted from http://gruntthepeon.free.fr/ssemath/neon_mathfun.html - template - inline static constexpr auto sin(VectorT x) noexcept -> VectorT - { - using IntVector = SIMDVector; - - // Constants - const auto c_cephes_FOPI = constant(1.27323954473516f); // 4 / M_PI - const auto c_minus_cephes_DP1 = constant(-0.78515625f); - const auto c_minus_cephes_DP2 = constant(-2.4187564849853515625e-4f); - const auto c_minus_cephes_DP3 = constant(-3.77489497744594108e-8f); - const auto c_sincof_p0 = constant(-1.9515295891E-4f); - const auto c_sincof_p1 = constant(8.3321608736E-3f); - const auto c_sincof_p2 = constant(-1.6666654611E-1f); - const auto one = constant(1.0f); - const auto half = constant(0.5f); - - auto sign_mask_sin = cmp_less_than(x, zero_vector()); - x = abs(x); - - auto y = mul(x, c_cephes_FOPI); - - auto emm2 = to(y); - emm2 = IntVector::add(emm2, IntVector::constant(1)); - emm2 = IntVector::and_(emm2, IntVector::constant(~1)); - y = from(emm2); - - auto poly_mask = IntVector::and_(emm2, IntVector::constant(2)); - poly_mask = IntVector::cmp_not_equal(poly_mask, IntVector::zero_vector()); - - auto xmm1 = mul(y, c_minus_cephes_DP1); - auto xmm2 = mul(y, c_minus_cephes_DP2); - auto xmm3 = mul(y, c_minus_cephes_DP3); - x = add(x, xmm1); - x = add(x, xmm2); - x = add(x, xmm3); - - // Update sign mask - auto temp_mask = IntVector::and_(emm2, IntVector::constant(4)); - temp_mask = IntVector::cmp_not_equal(temp_mask, IntVector::zero_vector()); - sign_mask_sin = vreinterpretq_f32_u32(veorq_u32( - vreinterpretq_u32_f32(sign_mask_sin), - vreinterpretq_u32_f32(IntVector::template as(temp_mask)))); - - // Evaluate polynomials - auto z = mul(x, x); - - // First polynomial (cosine) - auto y1 = mul(z, constant(2.443315711809948E-005f)); - y1 = add(y1, constant(-1.388731625493765E-003f)); - y1 = mul(y1, z); - y1 = add(y1, constant(4.166664568298827E-002f)); - y1 = mul(y1, z); - y1 = mul(y1, z); - y1 = sub(y1, mul(z, half)); - y1 = add(y1, one); - - // Second polynomial (sine) - auto y2 = mul(z, c_sincof_p0); - y2 = add(y2, c_sincof_p1); - y2 = mul(y2, z); - y2 = add(y2, c_sincof_p2); - y2 = mul(y2, z); - y2 = mul(y2, x); - y2 = add(y2, x); - - auto poly_mask_f = IntVector::template as(poly_mask); - auto ys = blend(y2, y1, poly_mask_f); - return blend(ys, neg(ys), sign_mask_sin); - } - - // NOTE: Dummy parameter because otherwise we get constexpr errors with set1_ps... - template - inline static constexpr auto log(VectorT x) noexcept -> VectorT - { - using IntVector = SIMDVector; - - const auto half = constant(0.5F); - const auto one = constant(1.0F); - auto invalid_mask = cmp_less_equal(x, zero_vector()); - - // cut off denormalized values - x = max(x, vreinterpretq_f32_u32(vdupq_n_u32(0x00800000u))); - - auto emm0 = IntVector::shift_right(as(x), 23); - - x = and_(x, vreinterpretq_f32_u32(vdupq_n_u32(~0x7f800000u))); - x = or_(x, half); - - // keep only the fractional part - emm0 = IntVector::sub(emm0, IntVector::constant(0x7f)); - auto e = from(emm0); - - e = add(e, one); - - // compute approx - auto mask = cmp_less_than(x, constant(0.707106781186547524f)); - auto tmp = and_(x, mask); - x = sub(x, one); - e = sub(e, and_(one, mask)); - x = add(x, tmp); - - auto z = mul(x, x); - - auto y = constant(7.0376836292E-2f); - y = mul(y, x); - y = add(y, constant(-1.1514610310E-1f)); - y = mul(y, x); - y = add(y, constant(1.1676998740E-1f)); - y = mul(y, x); - y = add(y, constant(-1.2420140846E-1f)); - y = mul(y, x); - y = add(y, constant(+1.4249322787E-1f)); - y = mul(y, x); - y = add(y, constant(-1.6668057665E-1f)); - y = mul(y, x); - y = add(y, constant(+2.0000714765E-1f)); - y = mul(y, x); - y = add(y, constant(-2.4999993993E-1f)); - y = mul(y, x); - y = add(y, constant(+3.3333331174E-1f)); - y = mul(y, mul(x, z)); - tmp = mul(e, constant(-2.12194440e-4f)); - y = add(y, tmp); - tmp = mul(z, half); - y = sub(y, tmp); - tmp = mul(e, constant(0.693359375f)); - x = add(x, add(y, tmp)); - - x = or_(x, invalid_mask); // negative arg will be NAN - return x; - } - template inline static constexpr auto blend(VectorT a, VectorT b, VectorT blend_mask) noexcept -> VectorT { diff --git a/src/impl/vamp/vector/isa/wasm.hh b/src/impl/vamp/vector/isa/wasm.hh index d82d603e..f75e4a54 100644 --- a/src/impl/vamp/vector/isa/wasm.hh +++ b/src/impl/vamp/vector/isa/wasm.hh @@ -298,6 +298,7 @@ namespace vamp { using VectorT = WasmFloatVec; using ScalarT = float; + using IntT = SIMDVector; static constexpr std::size_t VectorWidth = 4; static constexpr std::size_t Alignment = 16; @@ -373,6 +374,12 @@ namespace vamp return {wasm_v128_not(l.v)}; } + template + inline static constexpr auto xor_(VectorT l, VectorT r) noexcept -> VectorT + { + return {wasm_v128_xor(l.v, r.v)}; + } + template inline static constexpr auto neg(VectorT l) noexcept -> VectorT { @@ -569,136 +576,6 @@ namespace vamp return arr[0] + arr[1] + arr[2] + arr[3]; } - // Converted from http://gruntthepeon.free.fr/ssemath/neon_mathfun.html - template - inline static constexpr auto sin(VectorT x) noexcept -> VectorT - { - using IntVector = SIMDVector; - - // Constants - const auto c_cephes_FOPI = constant(1.27323954473516f); // 4 / M_PI - const auto c_minus_cephes_DP1 = constant(-0.78515625f); - const auto c_minus_cephes_DP2 = constant(-2.4187564849853515625e-4f); - const auto c_minus_cephes_DP3 = constant(-3.77489497744594108e-8f); - const auto c_sincof_p0 = constant(-1.9515295891E-4f); - const auto c_sincof_p1 = constant(8.3321608736E-3f); - const auto c_sincof_p2 = constant(-1.6666654611E-1f); - const auto one = constant(1.0f); - const auto half = constant(0.5f); - - auto sign_mask_sin = cmp_less_than(x, zero_vector()); - x = abs(x); - - auto y = mul(x, c_cephes_FOPI); - - auto emm2 = to(y); - emm2 = IntVector::add(emm2, IntVector::constant(1)); - emm2 = IntVector::and_(emm2, IntVector::constant(~1)); - y = from(emm2); - - auto poly_mask = IntVector::and_(emm2, IntVector::constant(2)); - poly_mask = IntVector::cmp_not_equal(poly_mask, IntVector::zero_vector()); - - auto xmm1 = mul(y, c_minus_cephes_DP1); - auto xmm2 = mul(y, c_minus_cephes_DP2); - auto xmm3 = mul(y, c_minus_cephes_DP3); - x = add(x, xmm1); - x = add(x, xmm2); - x = add(x, xmm3); - - // Update sign mask - auto temp_mask = IntVector::and_(emm2, IntVector::constant(4)); - temp_mask = IntVector::cmp_not_equal(temp_mask, IntVector::zero_vector()); - sign_mask_sin.v = wasm_v128_xor(sign_mask_sin.v, IntVector::template as(temp_mask).v); - - // Evaluate polynomials - auto z = mul(x, x); - - // First polynomial (cosine) - auto y1 = mul(z, constant(2.443315711809948E-005f)); - y1 = add(y1, constant(-1.388731625493765E-003f)); - y1 = mul(y1, z); - y1 = add(y1, constant(4.166664568298827E-002f)); - y1 = mul(y1, z); - y1 = mul(y1, z); - y1 = sub(y1, mul(z, half)); - y1 = add(y1, one); - - // Second polynomial (sine) - auto y2 = mul(z, c_sincof_p0); - y2 = add(y2, c_sincof_p1); - y2 = mul(y2, z); - y2 = add(y2, c_sincof_p2); - y2 = mul(y2, z); - y2 = mul(y2, x); - y2 = add(y2, x); - - auto poly_mask_f = IntVector::template as(poly_mask); - auto ys = blend(y2, y1, poly_mask_f); - return blend(ys, neg(ys), sign_mask_sin); - } - - template - inline static constexpr auto log(VectorT x) noexcept -> VectorT - { - using IntVector = SIMDVector; - - const auto half = constant(0.5F); - const auto one = constant(1.0F); - auto invalid_mask = cmp_less_equal(x, zero_vector()); - - // Cut off denormalized values - x.v = wasm_f32x4_max(x.v, wasm_f32x4_convert_i32x4(wasm_i32x4_splat(0x00800000))); - - auto emm0 = IntVector::shift_right(as(x), 23); - - x = and_(x, VectorT{wasm_f32x4_convert_i32x4(wasm_i32x4_splat(~0x7f800000))}); - x = or_(x, half); - - // Keep only the fractional part - emm0 = IntVector::sub(emm0, IntVector::constant(0x7f)); - auto e = from(emm0); - - e = add(e, one); - - // Compute approx - auto mask_cmp = cmp_less_than(x, constant(0.707106781186547524f)); - auto tmp = and_(x, mask_cmp); - x = sub(x, one); - e = sub(e, and_(one, mask_cmp)); - x = add(x, tmp); - - auto z = mul(x, x); - - auto y_val = constant(7.0376836292E-2f); - y_val = mul(y_val, x); - y_val = add(y_val, constant(-1.1514610310E-1f)); - y_val = mul(y_val, x); - y_val = add(y_val, constant(1.1676998740E-1f)); - y_val = mul(y_val, x); - y_val = add(y_val, constant(-1.2420140846E-1f)); - y_val = mul(y_val, x); - y_val = add(y_val, constant(+1.4249322787E-1f)); - y_val = mul(y_val, x); - y_val = add(y_val, constant(-1.6668057665E-1f)); - y_val = mul(y_val, x); - y_val = add(y_val, constant(+2.0000714765E-1f)); - y_val = mul(y_val, x); - y_val = add(y_val, constant(-2.4999993993E-1f)); - y_val = mul(y_val, x); - y_val = add(y_val, constant(+3.3333331174E-1f)); - y_val = mul(y_val, mul(x, z)); - tmp = mul(e, constant(-2.12194440e-4f)); - y_val = add(y_val, tmp); - tmp = mul(z, half); - y_val = sub(y_val, tmp); - tmp = mul(e, constant(0.693359375f)); - x = add(x, add(y_val, tmp)); - - x = or_(x, invalid_mask); // Negative arg will be NAN - return x; - } - template inline static constexpr auto blend(VectorT a, VectorT b, VectorT blend_mask) noexcept -> VectorT { diff --git a/src/impl/vamp/vector/math.hh b/src/impl/vamp/vector/math.hh index 43c95d47..965d136e 100644 --- a/src/impl/vamp/vector/math.hh +++ b/src/impl/vamp/vector/math.hh @@ -33,7 +33,106 @@ inline constexpr auto cos(const DataT &v) -> DataT template inline constexpr auto sqrt(const DataT &v) -> DataT { - return v.sqrt(); + if constexpr (std::is_arithmetic_v) + { + return std::sqrt(v); + } + else + { + return v.sqrt(); + } +} + +template +inline constexpr auto fabs(const DataT &v) -> DataT +{ + if constexpr (std::is_arithmetic_v) + { + return std::fabs(v); + } + else + { + return v.abs(); + } +} + +template +inline constexpr auto max(const DataT &x, const DataTB &y) -> DataT +{ + return x.max(y); +} + +template +inline constexpr auto min(const DataT &x, const DataTB &y) -> DataT +{ + return x.min(y); +} + +template +inline constexpr auto blend(const DataA &a, const DataB &b, const DataC &mask) -> DataA +{ + if constexpr (std::is_arithmetic_v) + { + return (mask >= 0) ? a : b; + } + else + { + DataA b_vec(b); + DataC mask_vec(mask); + return a.blend(b_vec, mask_vec); + } +} + +template +inline constexpr auto asin(const DataT &v) -> DataT +{ + if constexpr (std::is_arithmetic_v) + { + return std::asin(v); + } + else + { + return v.asin(); + } +} + +template +inline constexpr auto acos(const DataT &v) -> DataT +{ + if constexpr (std::is_arithmetic_v) + { + return std::acos(v); + } + else + { + return v.acos(); + } +} + +template +inline constexpr auto atan(const DataT &v) -> DataT +{ + if constexpr (std::is_arithmetic_v) + { + return std::atan(v); + } + else + { + return v.atan(); + } +} + +template +inline constexpr auto atan2(const DataT &y, const DataT &x) -> DataT +{ + if constexpr (std::is_arithmetic_v) + { + return std::atan2(y, x); + } + else + { + return y.atan2(x); + } } template