Skip to content
Merged
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
2 changes: 1 addition & 1 deletion CMakeModules/InternalUtils.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ macro(arrayfire_set_cmake_default_variables)
# PREFIX AF
# COMPILERS AppleClang Clang GNU Intel MSVC
# # NOTE: cxx_attribute_deprecated does not work well with C
# FEATURES cxx_rvalue_references cxx_noexcept cxx_variadic_templates cxx_alignas cxx_static_assert
# FEATURES cxx_rvalue_references cxx_noexcept cxx_variadic_templates cxx_alignas cxx_static_assert cxx_generalized_initializers
# ALLOW_UNKNOWN_COMPILERS
# #[VERSION <version>]
# #[PROLOG <prolog>]
Expand Down
30 changes: 30 additions & 0 deletions CMakeModules/compilers.h
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,12 @@
# define AF_COMPILER_CXX_STATIC_ASSERT 0
# endif

# if ((__clang_major__ * 100) + __clang_minor__) >= 400 && __has_feature(cxx_generalized_initializers)
# define AF_COMPILER_CXX_GENERALIZED_INITIALIZERS 1
# else
# define AF_COMPILER_CXX_GENERALIZED_INITIALIZERS 0
# endif

# elif AF_COMPILER_IS_Clang

# if !(((__clang_major__ * 100) + __clang_minor__) >= 301)
Expand Down Expand Up @@ -241,6 +247,12 @@
# define AF_COMPILER_CXX_STATIC_ASSERT 0
# endif

# if ((__clang_major__ * 100) + __clang_minor__) >= 301 && __has_feature(cxx_generalized_initializers)
# define AF_COMPILER_CXX_GENERALIZED_INITIALIZERS 1
# else
# define AF_COMPILER_CXX_GENERALIZED_INITIALIZERS 0
# endif

# elif AF_COMPILER_IS_GNU

# if !((__GNUC__ * 100 + __GNUC_MINOR__) >= 404)
Expand Down Expand Up @@ -289,6 +301,12 @@
# define AF_COMPILER_CXX_STATIC_ASSERT 0
# endif

# if (__GNUC__ * 100 + __GNUC_MINOR__) >= 404 && (__cplusplus >= 201103L || (defined(__GXX_EXPERIMENTAL_CXX0X__) && __GXX_EXPERIMENTAL_CXX0X__))
# define AF_COMPILER_CXX_GENERALIZED_INITIALIZERS 1
# else
# define AF_COMPILER_CXX_GENERALIZED_INITIALIZERS 0
# endif

# elif AF_COMPILER_IS_Intel

# if !(__INTEL_COMPILER >= 1210)
Expand Down Expand Up @@ -354,6 +372,12 @@
# define AF_COMPILER_CXX_STATIC_ASSERT 0
# endif

# if __INTEL_COMPILER >= 1400 && ((__cplusplus >= 201103L) || defined(__INTEL_CXX11_MODE__) || defined(__GXX_EXPERIMENTAL_CXX0X__))
# define AF_COMPILER_CXX_GENERALIZED_INITIALIZERS 1
# else
# define AF_COMPILER_CXX_GENERALIZED_INITIALIZERS 0
# endif

# elif AF_COMPILER_IS_MSVC

# if !(_MSC_VER >= 1600)
Expand Down Expand Up @@ -406,6 +430,12 @@
# define AF_COMPILER_CXX_STATIC_ASSERT 0
# endif

# if _MSC_FULL_VER >= 180030723
# define AF_COMPILER_CXX_GENERALIZED_INITIALIZERS 1
# else
# define AF_COMPILER_CXX_GENERALIZED_INITIALIZERS 0
# endif

# endif

# if defined(AF_COMPILER_CXX_NOEXCEPT) && AF_COMPILER_CXX_NOEXCEPT
Expand Down
15 changes: 15 additions & 0 deletions include/af/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
#ifdef __cplusplus
#include <af/traits.hpp>

#if AF_API_VERSION >= 38
#if AF_COMPILER_CXX_GENERALIZED_INITIALIZERS
#include <initializer_list>
#endif
#endif

namespace af
{

Expand Down Expand Up @@ -486,6 +492,15 @@ namespace af
array(const dim4& dims,
const T *pointer, af::source src=afHost);

#if AF_API_VERSION >= 38
#if AF_COMPILER_CXX_GENERALIZED_INITIALIZERS
template <typename T> array(std::initializer_list<T> list);

template <typename T>
array(const af::dim4 &dims, std::initializer_list<T> list);
#endif
#endif

/**
Adjust the dimensions of an N-D array (fast).

Expand Down
10 changes: 9 additions & 1 deletion src/api/cpp/array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,15 @@ struct dtype_traits<half_float::half> {
AFAPI array::array(dim_t dim0, dim_t dim1, dim_t dim2, dim_t dim3, \
const T *ptr, af::source src) \
: arr(initDataArray(ptr, dtype_traits<T>::af_type, src, dim0, dim1, \
dim2, dim3)) {}
dim2, dim3)) {} \
template<> \
AFAPI array::array(std::initializer_list<T> list) \
: arr(initDataArray(list.begin(), dtype_traits<T>::af_type, afHost, \
list.size(), 1, 1, 1)) {} \
template<> \
AFAPI array::array(const af::dim4 &dims, std::initializer_list<T> list) \
: arr(initDataArray(list.begin(), dtype_traits<T>::af_type, afHost, \
dims[0], dims[1], dims[2], dims[3])) {}

INSTANTIATE(cdouble)
INSTANTIATE(cfloat)
Expand Down
19 changes: 19 additions & 0 deletions test/array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <testHelpers.hpp>
#include <cstddef>
#include <cstdlib>
#include <initializer_list>

using namespace af;
using std::vector;
Expand Down Expand Up @@ -565,3 +566,21 @@ void deathTest() {
TEST(ArrayDeathTest, ProxyMoveAssignmentOperator) {
EXPECT_EXIT(deathTest(), ::testing::ExitedWithCode(0), "");
}

TEST(Array, InitializerList) {
int h_buffer[] = {23, 34, 18, 99, 34};

array A(5, h_buffer);
array B({23, 34, 18, 99, 34});

ASSERT_ARRAYS_EQ(A, B);
}

TEST(Array, InitializerListAndDim4) {
int h_buffer[] = {23, 34, 18, 99, 34, 44};

array A(2, 3, h_buffer);
array B(dim4(2, 3), {23, 34, 18, 99, 34, 44});

ASSERT_ARRAYS_EQ(A, B);
}