Skip to content

Commit

Permalink
Implement experimental SIMD 4x4 matrix products with AVX2
Browse files Browse the repository at this point in the history
- The new functions are enabled only when compiling with MSVC and "/arch:AVX2".
  • Loading branch information
demianmnave committed Dec 1, 2024
1 parent b8721b6 commit 34b6e4f
Show file tree
Hide file tree
Showing 19 changed files with 1,003 additions and 362 deletions.
4 changes: 4 additions & 0 deletions cml/common/size_tags.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ template<class T> struct is_fixed_size
std::is_same<size_tag_of_t<T>, fixed_size_tag>::value;
};

/** Helper to detect fixed-size types. */
template<class T> constexpr auto is_fixed_size_v
= is_fixed_size<T>::value;

/** Wrapper for enable_if to detect types tagged with fixed_size_tag. */
template<class Sub, class T = void>
struct enable_if_fixed_size : std::enable_if<is_fixed_size<Sub>::value, T>
Expand Down
24 changes: 13 additions & 11 deletions cml/matrix/detail/get.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@

#pragma once

#include <cml/matrix/readable_matrix.h>
#include <cml/matrix/writable_matrix.h>

namespace cml::detail {

/** Helper to return the passed-in value in response to a matrix index @c
* (i,j).
*/
template<class Other>
template<class Other,
typename = std::enable_if_t<!cml::is_matrix<Other>::value>>
inline auto
get(const Other& v, int, int) -> const Other&
{
Expand All @@ -20,17 +22,16 @@ get(const Other& v, int, int) -> const Other&

/** Helper to return element @c (i,j) of @c array. */
template<class Other, int Rows, int Cols>
inline const Other&
get(Other const (&array)[Rows][Cols], int i, int j)
-> const Other&
inline auto
get(Other const (&array)[Rows][Cols], int i, int j) -> const Other&
{
return array[i][j];
}

/** Helper to return element @c (i,j) of @c array. */
template<class Other, int Rows, int Cols> inline auto
get(Other (&array)[Rows][Cols], int i, int j)
-> Other&
template<class Other, int Rows, int Cols>
inline auto
get(Other (&array)[Rows][Cols], int i, int j) -> Other&
{
return array[i][j];
}
Expand All @@ -44,11 +45,12 @@ get(const readable_matrix<Sub>& sub, int i, int j) ->
return sub.get(i, j);
}

template<class Sub> inline auto
get(writable_matrix<Sub>& sub, int i, int j)
-> typename matrix_traits<Sub>::mutable_value
template<class Sub>
inline auto
get(writable_matrix<Sub>& sub, int i, int j) ->
typename matrix_traits<Sub>::mutable_value
{
return sub.get(i, j);
}

}
} // namespace cml::detail
46 changes: 23 additions & 23 deletions cml/matrix/matrix_product.h
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
/*-------------------------------------------------------------------------
@@COPYRIGHT@@
*-----------------------------------------------------------------------*/

#pragma once

#include <cml/matrix/readable_matrix.h>
#include <cml/matrix/promotion.h>

namespace cml {

/** Multiply two matrices, and return the result as a temporary. */
template<class Sub1, class Sub2, enable_if_matrix_t<Sub1>* = nullptr,
enable_if_matrix_t<Sub2>* = nullptr>
auto operator*(Sub1&& sub1, Sub2&& sub2)
-> matrix_inner_product_promote_t<actual_operand_type_of_t<decltype(sub1)>,
actual_operand_type_of_t<decltype(sub2)>>;

} // namespace cml

#define __CML_MATRIX_MATRIX_PRODUCT_TPP
#include <cml/matrix/matrix_product.tpp>
#undef __CML_MATRIX_MATRIX_PRODUCT_TPP
/*-------------------------------------------------------------------------
@@COPYRIGHT@@
*-----------------------------------------------------------------------*/

#pragma once

#include <cml/matrix/readable_matrix.h>
#include <cml/matrix/promotion.h>
#include <cml/matrix/detail/resize.h>

namespace cml {

/** Multiply two matrices and return the result as a temporary. */
template<class Sub1, class Sub2, typename = enable_if_matrix_t<Sub1>,
typename = enable_if_matrix_t<Sub2>>
auto operator*(Sub1&& sub1, Sub2&& sub2) -> matrix_inner_product_promote_t<
actual_type_of_t<Sub1>, actual_type_of_t<Sub2>>;

} // namespace cml

#define __CML_MATRIX_MATRIX_PRODUCT_TPP
#include <cml/matrix/matrix_product.tpp>
#undef __CML_MATRIX_MATRIX_PRODUCT_TPP
Loading

0 comments on commit 34b6e4f

Please sign in to comment.