/* * SPDX-FileCopyrightText: 2022 L. E. Segovia * * SPDX-License-Identifier: BSD-3-Clause */ #ifndef KIS_XSIMD_GENERIC_HPP #define KIS_XSIMD_GENERIC_HPP #include "xsimd_generic_details.hpp" #include #include namespace xsimd { /*********************** * Truncate-initialize * ***********************/ template inline batch truncate_to_type(xsimd::batch const &self) noexcept { return kernel::detail::apply_with_value( [](float i) -> float { if (std::numeric_limits::min() > i) { return 0; } else if (std::numeric_limits::max() < i) { return 0; } else { return static_cast(i); } }, self); } // Mask to 0 elements of a vector. template inline auto set_zero(const batch &src, const batch_bool &mask) noexcept { return xsimd::select(mask, xsimd::batch(0), src); } // Mask to 1 elements of a vector. template inline auto set_one(const batch &src, const batch_bool &mask) noexcept { return xsimd::select(mask, xsimd::batch(1), src); } /********************************** * Sign-extending unaligned loads * **********************************/ // Load `T::size` values from the array of `T2` elements. template inline T load_and_extend(const T2 *src) noexcept { return kernel::detail::apply_with_index_and_value( [&](size_t i, typename T::value_type) { return static_cast(src[i]); }, T{}); } /************************************************* * Type-inferred, auto-aligned memory allocation * *************************************************/ // Allocate size bytes of memory aligned to `batch::alignment()`. template inline T *aligned_malloc(size_t size) noexcept { using T_v = batch; return reinterpret_cast(xsimd::aligned_malloc(size, T_v::arch_type::alignment())); } // Return the maximum of a list of templated values at compile time. template constexpr typename std::enable_if::type max() { return value; } // Return the maximum of a list of templated values at compile time. template constexpr typename std::enable_if::type max() { return std::max(value, max()); } // Allocate memory for `sz` T items, aligned to the selected architecture's // alignment. template inline T *vector_aligned_malloc(size_t sz) noexcept { return static_cast(xsimd::aligned_malloc(sz * sizeof(T), A::alignment())); } // Free allocated memory, hiding the `const_cast` if necessary. template inline void vector_aligned_free(const T *ptr) noexcept { // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast) return xsimd::aligned_free(const_cast(ptr)); } /**************** * Interleaving * ****************/ // Return the tuple of interleaved batches `a` and `b`. // First element is the low half, second is the upper half. template inline std::pair interleave(const V &a, const V &b) noexcept { return {xsimd::zip_lo(a, b), xsimd::zip_hi(a, b)}; } /********************** * Quadratic function * **********************/ template inline batch pow2 (batch const& self) noexcept { return self * self; } #if XSIMD_VERSION_MAJOR <= 10 template inline batch bitwise_cast_compat(batch const& x) noexcept { return bitwise_cast>(x); } #else template inline batch bitwise_cast_compat(batch const& x) noexcept { return bitwise_cast(x); } #endif }; // namespace xsimd #endif