Skip to content

Commit

Permalink
add regular loop to matrix arithmetic stuff
Browse files Browse the repository at this point in the history
apparently MSVC is just as shitty at dealing with those so manual unrolling is not needed
  • Loading branch information
petiaccja committed Aug 12, 2024
1 parent cd00de9 commit 4eeea08
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 119 deletions.
4 changes: 3 additions & 1 deletion .clang-format
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
---
---
AccessModifierOffset: '-4'
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: 'false'
Expand All @@ -7,6 +7,7 @@ AlignEscapedNewlinesLeft: 'false'
AlignOperands: 'true'
AlignTrailingComments: 'false'
AllowAllParametersOfDeclarationOnNextLine: 'false'
AllowAllArgumentsOnNextLine: 'true'
AllowShortBlocksOnASingleLine: 'true'
AllowShortCaseLabelsOnASingleLine: 'true'
AllowShortFunctionsOnASingleLine: 'true'
Expand Down Expand Up @@ -48,6 +49,7 @@ IncludeCategories:
IndentCaseLabels: 'true'
IndentWidth: '4'
IndentWrappedFunctionNames: 'false'
LambdaBodyIndentation: Signature
Language: Cpp
MaxEmptyLinesToKeep: '3'
NamespaceIndentation: Inner
Expand Down
10 changes: 1 addition & 9 deletions benchmark/Benchmark.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include <Mathter/Common/LoopUtil.hpp>
#include <Mathter/Common/OptimizationUtil.hpp>

#include <algorithm>
#include <catch2/catch_test_macros.hpp>
Expand All @@ -19,14 +19,6 @@
#define MATHTER_TSC_USES_CHRONO
#endif

#ifdef _MSC_VER
#define MATHTER_NOINLINE __declspec(noinline)
#define MATHTER_FORCEINLINE __forceinline
#else
#define MATHTER_NOINLINE __attribute__((noinline))
#define MATHTER_FORCEINLINE __attribute__((always_inline))
#endif


namespace impl {

Expand Down
2 changes: 1 addition & 1 deletion include/Mathter/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ target_sources(Mathter
# Common
"Common/DeterministicInitializer.hpp"
"Common/Functional.hpp"
"Common/LoopUtil.hpp"
"Common/OptimizationUtil.hpp"
"Common/MathUtil.hpp"
"Common/Range.hpp"
"Common/Types.hpp"
Expand Down
24 changes: 0 additions & 24 deletions include/Mathter/Common/LoopUtil.hpp

This file was deleted.

69 changes: 69 additions & 0 deletions include/Mathter/Common/OptimizationUtil.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#pragma once

#include <cstddef>
#include <utility>


namespace mathter {


#ifdef _MSC_VER
#define MATHTER_NOINLINE __declspec(noinline)
#define MATHTER_FORCEINLINE __forceinline
#define MATHTER_FLATTEN [[msvc::flatten]]
#else
#define MATHTER_NOINLINE __attribute__((noinline))
#define MATHTER_FORCEINLINE __attribute__((always_inline))
#define MATHTER_FLATTEN __attribute__((flatten))
#endif


namespace impl {

template <size_t... Indices, class Func, class... Args>
MATHTER_FORCEINLINE auto LoopUnrollHelper(std::index_sequence<Indices...>, Func&& func, Args&&... args) {
return func(std::forward<Args>(args)..., Indices...);
}

} // namespace impl


template <size_t Iterations, class Func, class... Args>
MATHTER_FORCEINLINE auto LoopUnroll(Func&& func, Args&&... args) {
return impl::LoopUnrollHelper(std::make_index_sequence<Iterations>{}, std::forward<Func>(func), std::forward<Args>(args)...);
}


template <ptrdiff_t First, ptrdiff_t Last, ptrdiff_t Step, ptrdiff_t Limit, class Fun, class... Args>
void ForUnrolled(std::integral_constant<ptrdiff_t, First>,
std::integral_constant<ptrdiff_t, Last> last,
std::integral_constant<ptrdiff_t, Step> step,
std::integral_constant<ptrdiff_t, Limit> limit,
Fun&& fun,
Args&&... args) {
constexpr auto Count = Last - First / Step;
if constexpr (Count <= Limit) {
if constexpr (0 < Step ? First < Last : First > Last) {
fun(First, args...);
ForUnrolled(std::integral_constant<ptrdiff_t, First + Step>{}, last, step, limit, std::move(fun), std::forward<Args>(args)...);
}
}
else {
for (auto i = First; i < Last; i += Step) {
fun(i, args...);
}
}
}


template <ptrdiff_t First, ptrdiff_t Last, ptrdiff_t Step, ptrdiff_t Limit, class Fun, class... Args>
auto ForUnrolled(Fun&& fun, Args&&... args) -> std::enable_if_t<!std::is_convertible_v<Fun, std::integral_constant<ptrdiff_t, First>>> {
ForUnrolled(std::integral_constant<ptrdiff_t, First>{},
std::integral_constant<ptrdiff_t, Last>{},
std::integral_constant<ptrdiff_t, Step>{},
std::integral_constant<ptrdiff_t, Limit>{},
std::forward<Fun>(fun),
std::forward<Args>(args)...);
}

} // namespace mathter
Loading

0 comments on commit 4eeea08

Please sign in to comment.