Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add proposed alignment specifier #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
51 changes: 50 additions & 1 deletion experimental/bits/simd.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,53 @@ struct vector_aligned_tag
}
};

namespace __proposed
{
namespace {
enum _AlignmentType{
_Unaligned,
_Vector,
_Element
};

template<_AlignmentType at>
struct _alignment_tag_selector{
static_assert(at!=_Unaligned,"Unaligned reads are undefined behavior.");
};
template<>
struct _alignment_tag_selector<_Element>{
using type=element_aligned_tag;
};
template<>
struct _alignment_tag_selector<_Vector>{
using type=vector_aligned_tag;
};
}

template<size_t _Np>
struct aligned_tag
{
template <typename _Tp, typename _Up = typename _Tp::value_type>
static constexpr size_t _S_alignment = _Np;

template <typename _Tp, typename _Up>
_GLIBCXX_SIMD_INTRINSIC static constexpr _Up*
_S_apply(_Up* __ptr){
constexpr size_t _expected_vector_alignment=vector_aligned_tag::_S_alignment<_Tp,_Up>;
constexpr size_t _expected_element_alignment=element_aligned_tag::_S_alignment<_Tp,_Up>;
constexpr _AlignmentType _computed_at=
((_Np % _expected_vector_alignment)==0) ? _Vector :
((_Np % _expected_element_alignment)==0) ? _Element : _Unaligned;
using _result_tag=typename _alignment_tag_selector<_computed_at>::type;
return _result_tag::template _S_apply<_Tp,_Up>(__ptr);
}
};

template<size_t _Np>
inline constexpr aligned_tag<_Np> aligned = {};

}

template <size_t _Np> struct overaligned_tag
{
template <typename _Tp, typename _Up = typename _Tp::value_type>
Expand All @@ -193,7 +240,7 @@ template <size_t _Np> struct overaligned_tag
template <typename _Tp, typename _Up>
_GLIBCXX_SIMD_INTRINSIC static constexpr _Up*
_S_apply(_Up* __ptr)
{ return static_cast<_Up*>(__builtin_assume_aligned(__ptr, _Np)); }
{ return __proposed::aligned_tag<_Np>::_S_apply(__ptr); }
};

inline constexpr element_aligned_tag element_aligned = {};
Expand All @@ -203,6 +250,8 @@ inline constexpr vector_aligned_tag vector_aligned = {};
template <size_t _Np>
inline constexpr overaligned_tag<_Np> overaligned = {};



// }}}
template <size_t _X>
using _SizeConstant = integral_constant<size_t, _X>;
Expand Down
15 changes: 15 additions & 0 deletions tests/simple_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
cmake_minimum_required(VERSION 3.14)

add_executable(alignment_compute alignment_compute.cpp)
target_compile_options(alignment_compute PUBLIC
"$<$<CONFIG:Release>:-march=native>"
"$<$<CONFIG:Debug>:-march=native>"
"$<$<CONFIG:RelWithDebInfo>:-march=native>"
"$<$<CONFIG:MinSizeRel>:-march=native>"
)
set_target_properties(alignment_compute PROPERTIES
CXX_STANDARD 20
CXX_STANDARD_REQUIRED ON)

message(WARNING "${CMAKE_CURRENT_SOURCE_DIR}/../..")
target_include_directories(alignment_compute PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/../..")
28 changes: 28 additions & 0 deletions tests/simple_tests/alignment_compute.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include<experimental/simd>

#include<array>
#include<vector>
#include<cstdint>
#include<cstdlib>
#include<type_traits>

#include<experimental/simd>
namespace stdx = std::experimental;


using uint8v_t = stdx::native_simd<uint8_t>;
using uint8v_mask_t = stdx::native_simd_mask<uint8_t>;
//using mask_cpu_t = min_uint_t<stdx::native_simd_mask<uint8_t>::size()>;



int main()
{
std::array<uint8_t,128> data;
uint8v_t hi(&data[0],stdx::__proposed::aligned<3>);

std::array<float,128> dataf;
stdx::native_simd<float> vf(&dataf[0],stdx::__proposed::aligned<3>);
return 0;
}