Better handle span sources from iterators

This commit is contained in:
Chris Robinson 2022-12-14 17:39:47 -08:00
parent 42090e9371
commit c40e1bc1ce
5 changed files with 75 additions and 28 deletions

View File

@ -93,7 +93,7 @@ constexpr std::array<al::span<const ushort2>,11> gBitReverses{{
template<typename Real>
std::enable_if_t<std::is_floating_point<Real>::value>
complex_fft(const al::span<std::complex<Real>> buffer, const Real sign)
complex_fft(const al::span<std::complex<Real>> buffer, const al::type_identity_t<Real> sign)
{
const size_t fftsize{buffer.size()};
/* Get the number of bits used for indexing. Simplifies bit-reversal and

View File

@ -13,7 +13,7 @@
*/
template<typename Real>
std::enable_if_t<std::is_floating_point<Real>::value>
complex_fft(const al::span<std::complex<Real>> buffer, const Real sign);
complex_fft(const al::span<std::complex<Real>> buffer, const al::type_identity_t<Real> sign);
/**
* Calculate the frequency-domain response of the time-domain signal in the
@ -22,7 +22,7 @@ complex_fft(const al::span<std::complex<Real>> buffer, const Real sign);
template<typename Real, size_t N>
std::enable_if_t<std::is_floating_point<Real>::value>
forward_fft(const al::span<std::complex<Real>,N> buffer)
{ complex_fft(buffer.subspan(0), Real{-1}); }
{ complex_fft(buffer.subspan(0), -1); }
/**
* Calculate the time-domain signal of the frequency-domain response in the
@ -31,7 +31,7 @@ forward_fft(const al::span<std::complex<Real>,N> buffer)
template<typename Real, size_t N>
std::enable_if_t<std::is_floating_point<Real>::value>
inverse_fft(const al::span<std::complex<Real>,N> buffer)
{ complex_fft(buffer.subspan(0), Real{1}); }
{ complex_fft(buffer.subspan(0), 1); }
/**
* Calculate the complex helical sequence (discrete-time analytical signal) of

View File

@ -106,6 +106,18 @@ template<typename T, std::size_t N, typename U, std::size_t M>
constexpr bool operator!=(const allocator<T,N>&, const allocator<U,M>&) noexcept { return false; }
template<typename T>
constexpr T *to_address(T *p) noexcept
{
static_assert(!std::is_function<T>::value, "Can't be a function type");
return p;
}
template<typename T>
constexpr auto to_address(const T& p) noexcept
{ return to_address(p.operator->()); }
template<typename T, typename ...Args>
constexpr T* construct_at(T *ptr, Args&& ...args)
noexcept(std::is_nothrow_constructible<T, Args...>::value)

View File

@ -7,6 +7,8 @@
#include <iterator>
#include <type_traits>
#include "almalloc.h"
namespace al {
template<typename T>
@ -35,6 +37,13 @@ constexpr const T* data(std::initializer_list<T> list) noexcept
{ return list.begin(); }
template<typename T>
struct type_identity { using type = T; };
template<typename T>
using type_identity_t = typename type_identity<T>::type;
constexpr size_t dynamic_extent{static_cast<size_t>(-1)};
template<typename T, size_t E=dynamic_extent>
@ -66,13 +75,16 @@ namespace detail_ {
constexpr bool has_size_and_data<T,
void_t<decltype(al::size(std::declval<T>())), decltype(al::data(std::declval<T>()))>>
= true;
template<typename T, typename U>
constexpr bool is_convertible_v = std::is_convertible<T(*)[],U(*)[]>::value;
} // namespace detail_
#define REQUIRES(...) std::enable_if_t<(__VA_ARGS__),bool> = true
#define IS_VALID_CONTAINER(C) \
#define IS_VALID_CONTAINER(C, T) \
!detail_::is_span_v<C> && !detail_::is_std_array_v<C> && \
!std::is_array<C>::value && detail_::has_size_and_data<C> && \
std::is_convertible<std::remove_pointer_t<decltype(al::data(std::declval<C&>()))>(*)[],element_type(*)[]>::value
detail_::is_convertible_v<std::remove_pointer_t<decltype(al::data(std::declval<C&>()))>,T>
template<typename T, size_t E>
class span {
@ -96,21 +108,31 @@ public:
template<bool is0=(extent == 0), REQUIRES(is0)>
constexpr span() noexcept { }
constexpr span(pointer ptr, index_type /*count*/) : mData{ptr} { }
constexpr span(pointer first, pointer /*last*/) : mData{first} { }
constexpr span(element_type (&arr)[E]) noexcept : span{al::data(arr), al::size(arr)} { }
template<typename U>
constexpr explicit span(U iter, index_type) : mData{to_address(iter)} { }
template<typename U, typename V, REQUIRES(!std::is_integral<V>::value)>
constexpr explicit span(U first, V) : mData{to_address(first)} { }
constexpr span(type_identity_t<element_type> (&arr)[E]) noexcept
: span{al::data(arr), al::size(arr)}
{ }
constexpr span(std::array<value_type,E> &arr) noexcept : span{al::data(arr), al::size(arr)} { }
template<typename U=T, REQUIRES(std::is_const<U>::value)>
constexpr span(const std::array<value_type,E> &arr) noexcept
: span{al::data(arr), al::size(arr)}
{ }
template<typename U, REQUIRES(IS_VALID_CONTAINER(U))>
constexpr span(U &cont) : span{al::data(cont), al::size(cont)} { }
template<typename U, REQUIRES(IS_VALID_CONTAINER(const U))>
constexpr span(const U &cont) : span{al::data(cont), al::size(cont)} { }
template<typename U, REQUIRES(!std::is_same<element_type,U>::value
&& std::is_convertible<U(*)[],element_type(*)[]>::value)>
constexpr span(const span<U,E> &span_) noexcept : span{al::data(span_), al::size(span_)} { }
template<typename U, REQUIRES(IS_VALID_CONTAINER(U, element_type))>
constexpr explicit span(U&& cont) : span{al::data(cont), al::size(cont)} { }
template<typename U, index_type N, REQUIRES(!std::is_same<element_type,U>::value
&& detail_::is_convertible_v<U,element_type> && N == dynamic_extent)>
constexpr explicit span(const span<U,N> &span_) noexcept
: span{al::data(span_), al::size(span_)}
{ }
template<typename U, index_type N, REQUIRES(!std::is_same<element_type,U>::value
&& detail_::is_convertible_v<U,element_type> && N == extent)>
constexpr span(const span<U,N> &span_) noexcept : span{al::data(span_), al::size(span_)} { }
constexpr span(const span&) noexcept = default;
constexpr span& operator=(const span &rhs) noexcept = default;
@ -199,22 +221,31 @@ public:
static constexpr size_t extent{dynamic_extent};
constexpr span() noexcept = default;
constexpr span(pointer ptr, index_type count) : mData{ptr}, mDataEnd{ptr+count} { }
constexpr span(pointer first, pointer last) : mData{first}, mDataEnd{last} { }
template<typename U>
constexpr span(U iter, index_type count)
: mData{to_address(iter)}, mDataEnd{to_address(iter)+count}
{ }
template<typename U, typename V, REQUIRES(!std::is_integral<V>::value)>
constexpr span(U first, V last)
: mData{to_address(first)}, mDataEnd{to_address(last)}
{ }
template<size_t N>
constexpr span(element_type (&arr)[N]) noexcept : span{al::data(arr), al::size(arr)} { }
constexpr span(type_identity_t<element_type> (&arr)[N]) noexcept
: span{al::data(arr), al::size(arr)}
{ }
template<size_t N>
constexpr span(std::array<value_type,N> &arr) noexcept : span{al::data(arr), al::size(arr)} { }
template<size_t N, typename U=T, REQUIRES(std::is_const<U>::value)>
constexpr span(const std::array<value_type,N> &arr) noexcept
: span{al::data(arr), al::size(arr)}
{ }
template<typename U, REQUIRES(IS_VALID_CONTAINER(U))>
constexpr span(U &cont) : span{al::data(cont), al::size(cont)} { }
template<typename U, REQUIRES(IS_VALID_CONTAINER(const U))>
constexpr span(const U &cont) : span{al::data(cont), al::size(cont)} { }
template<typename U, REQUIRES(IS_VALID_CONTAINER(U, element_type))>
constexpr span(U&& cont) : span{al::data(cont), al::size(cont)} { }
template<typename U, size_t N, REQUIRES((!std::is_same<element_type,U>::value || extent != N)
&& std::is_convertible<U(*)[],element_type(*)[]>::value)>
&& detail_::is_convertible_v<U,element_type>)>
constexpr span(const span<U,N> &span_) noexcept : span{al::data(span_), al::size(span_)} { }
constexpr span(const span&) noexcept = default;
@ -301,7 +332,11 @@ constexpr inline auto span<T,E>::subspan(size_t offset, size_t count) const
/* Helpers to deal with the lack of user-defined deduction guides (C++17). */
template<typename T, typename U>
constexpr auto as_span(T *ptr, U count_or_end) { return span<T>{ptr, count_or_end}; }
constexpr auto as_span(T ptr, U count_or_end)
{
using value_type = typename std::pointer_traits<T>::element_type;
return span<value_type>{ptr, count_or_end};
}
template<typename T, size_t N>
constexpr auto as_span(T (&arr)[N]) noexcept { return span<T,N>{al::data(arr), al::size(arr)}; }
template<typename T, size_t N>
@ -312,7 +347,7 @@ constexpr auto as_span(const std::array<T,N> &arr) noexcept
{ return span<std::add_const_t<T>,N>{al::data(arr), al::size(arr)}; }
template<typename U, REQUIRES(!detail_::is_span_v<U> && !detail_::is_std_array_v<U>
&& !std::is_array<U>::value && detail_::has_size_and_data<U>)>
constexpr auto as_span(U &cont)
constexpr auto as_span(U&& cont)
{
using value_type = std::remove_pointer_t<decltype(al::data(std::declval<U&>()))>;
return span<value_type>{al::data(cont), al::size(cont)};

View File

@ -128,7 +128,7 @@ struct UhjDecoder {
void decode(const float *RESTRICT InSamples, const size_t InChannels,
const al::span<FloatBufferLine> OutSamples, const size_t SamplesToDo);
void decode2(const float *RESTRICT InSamples, const al::span<FloatBufferLine,3> OutSamples,
void decode2(const float *RESTRICT InSamples, const al::span<FloatBufferLine> OutSamples,
const size_t SamplesToDo);
DEF_NEWDEL(UhjDecoder)
@ -305,7 +305,7 @@ void UhjDecoder::decode(const float *RESTRICT InSamples, const size_t InChannels
* halving here is merely a -6dB reduction in output, but it's still incorrect.
*/
void UhjDecoder::decode2(const float *RESTRICT InSamples,
const al::span<FloatBufferLine,3> OutSamples, const size_t SamplesToDo)
const al::span<FloatBufferLine> OutSamples, const size_t SamplesToDo)
{
ASSUME(SamplesToDo > 0);