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

[compute] Apply fold expressions #14677

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
21 changes: 11 additions & 10 deletions runtime/compute/cker/include/cker/Shape.h
Original file line number Diff line number Diff line change
Expand Up @@ -267,27 +267,28 @@ inline int FlatSizeSkipDim(const Shape &shape, int skip_dim)
// arrays.
template <typename... Ts> inline bool checkMatching(const Shape &shape, Ts... check_shapes)
{
const Shape check_shapes_array[sizeof...(Ts)] = {std::forward<Ts>(check_shapes)...};
for (const auto &check_shape : check_shapes_array)
{
// Check matching of shapes except the case of that two shapes can be scalar
if (shape.DimensionsCount() > 1 || check_shape.DimensionsCount() > 1 || shape.FlatSize() != 1 ||
check_shape.FlatSize() != 1)
auto match = [&shape](const Shape &s) -> bool {
// Check matching of shapes except the case that both shapes are scalars.
if (shape.DimensionsCount() > 1 || s.DimensionsCount() > 1 || shape.FlatSize() != 1 ||
s.FlatSize() != 1)
{
if (shape.DimensionsCount() != check_shape.DimensionsCount())
if (shape.DimensionsCount() != s.DimensionsCount())
{
return false;
}
for (int i = 0; i < shape.DimensionsCount(); ++i)
{
if (shape.Dims(i) != check_shape.Dims(i))
if (shape.Dims(i) != s.Dims(i))
{
return false;
}
}
}
}
return true;
return true;
};

// Apply the lambda to each check shape and combine with &&
return (match(check_shapes) && ...);
}

struct UNUSED_ALL
Copy link
Contributor Author

@ragmani ragmani Feb 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The structure UNUSED_ALL can be replaced with (void(check_shapes), ...);. But There are no overhead difference, readability, and conciseness.

Expand Down
21 changes: 11 additions & 10 deletions runtime/compute/ruy/include/ruy/Shape.h
Original file line number Diff line number Diff line change
Expand Up @@ -268,27 +268,28 @@ inline int FlatSizeSkipDim(const Shape &shape, int skip_dim)
// arrays.
template <typename... Ts> inline bool checkMatching(const Shape &shape, Ts... check_shapes)
{
const Shape check_shapes_array[sizeof...(Ts)] = {std::forward<Ts>(check_shapes)...};
for (const auto &check_shape : check_shapes_array)
{
// Check matching of shapes except the case of that two shapes can be scalar
if (shape.DimensionsCount() > 1 || check_shape.DimensionsCount() > 1 || shape.FlatSize() != 1 ||
check_shape.FlatSize() != 1)
auto match = [&shape](const Shape &s) -> bool {
// Check matching of shapes except the case that both shapes are scalars.
if (shape.DimensionsCount() > 1 || s.DimensionsCount() > 1 || shape.FlatSize() != 1 ||
s.FlatSize() != 1)
{
if (shape.DimensionsCount() != check_shape.DimensionsCount())
if (shape.DimensionsCount() != s.DimensionsCount())
{
return false;
}
for (int i = 0; i < shape.DimensionsCount(); ++i)
{
if (shape.Dims(i) != check_shape.Dims(i))
if (shape.Dims(i) != s.Dims(i))
{
return false;
}
}
}
}
return true;
return true;
};

// Apply the lambda to each check shape and combine with &&
return (match(check_shapes) && ...);
}

struct UNUSED_ALL
Expand Down
Loading