Skip to content

Commit 2a5b880

Browse files
authored
Merge pull request #436 from JohanMabille/from_to
Added broadcast / from_aligned / form_unaligned static methods
2 parents 220bae8 + a200693 commit 2a5b880

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

include/xsimd/types/xsimd_base.hpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,14 @@ namespace xsimd
153153
using reverse_iterator = std::reverse_iterator<iterator>;
154154
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
155155

156+
static X broadcast(value_type v);
157+
158+
template <class T>
159+
static X from_unaligned(T* src);
160+
161+
template <class T>
162+
static X from_aligned(T* src);
163+
156164
X& operator+=(const X& rhs);
157165
X& operator+=(const value_type& rhs);
158166

@@ -676,6 +684,52 @@ namespace xsimd
676684
{
677685
}
678686

687+
/**
688+
* @name Static builders
689+
*/
690+
//@{
691+
/**
692+
* Creates a batch from the single value \c v.
693+
* @param v the value used to initialize the batch
694+
* @return a new batch instance
695+
*/
696+
template <class X>
697+
inline X simd_batch<X>::broadcast(value_type v)
698+
{
699+
return X(v);
700+
}
701+
702+
/**
703+
* Creates a batch from the buffer \c src. The
704+
* memory does not need to be aligned.
705+
* @param src the memory buffer to read
706+
* @return a new batch instance
707+
*/
708+
template <class X>
709+
template <class T>
710+
inline X simd_batch<X>::from_unaligned(T* src)
711+
{
712+
X res;
713+
res.load_unaligned(src);
714+
return res;
715+
}
716+
717+
/**
718+
* Creates a batch from the buffer \c src. The
719+
* memory needs to be aligned.
720+
* @param src the memory buffer to read
721+
* @return a new batch instance
722+
*/
723+
template <class X>
724+
template <class T>
725+
inline X simd_batch<X>::from_aligned(T* src)
726+
{
727+
X res;
728+
res.load_aligned(src);
729+
return res;
730+
}
731+
//@}
732+
679733
/**
680734
* @name Arithmetic computed assignment
681735
*/

test/test_batch.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,30 @@ class batch_test : public testing::Test
6262
EXPECT_EQ(b1, lhs) << print_function_name("batch(value_type*)");
6363
}
6464

65+
void test_static_builders() const
66+
{
67+
{
68+
array_type expected;
69+
std::fill(expected.begin(), expected.end(), value_type(2));
70+
71+
auto res = batch_type::broadcast(value_type(2));
72+
EXPECT_EQ(res, expected) << print_function_name("batch::broadcast");
73+
}
74+
{
75+
array_type res;
76+
auto b = batch_type::from_unaligned(lhs.data());
77+
b.store_unaligned(res.data());
78+
EXPECT_EQ(res, lhs) << print_function_name("batch::from_unaligned");
79+
}
80+
{
81+
alignas(XSIMD_DEFAULT_ALIGNMENT) array_type arhs(this->rhs);
82+
alignas(XSIMD_DEFAULT_ALIGNMENT) array_type ares;
83+
auto b = batch_type::from_aligned(arhs.data());
84+
b.store_aligned(ares.data());
85+
EXPECT_EQ(ares, rhs) << print_function_name("batch::from_aligned");
86+
}
87+
}
88+
6589
void test_access_operator() const
6690
{
6791
batch_type res = batch_lhs();
@@ -549,6 +573,11 @@ TYPED_TEST(batch_test, constructors)
549573
this->test_constructors();
550574
}
551575

576+
TYPED_TEST(batch_test, static_builders)
577+
{
578+
this->test_static_builders();
579+
}
580+
552581
TYPED_TEST(batch_test, access_operator)
553582
{
554583
this->test_access_operator();

0 commit comments

Comments
 (0)