Add src/utils/math.h

Move the RoundUp() and IsPowerOfTwo() methods from Resolver.cc to this file.

Add tests

Change-Id: Ib7af53dfa5e69083ec4fc2484da92a84c9468818
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/44682
Auto-Submit: Ben Clayton <bclayton@google.com>
Commit-Queue: Antonio Maiorano <amaiorano@google.com>
Reviewed-by: Antonio Maiorano <amaiorano@google.com>
This commit is contained in:
Ben Clayton 2021-03-15 13:37:41 +00:00 committed by Commit Bot service account
parent d614dd5d12
commit a75205265e
5 changed files with 114 additions and 18 deletions

View File

@ -500,6 +500,7 @@ if(${TINT_BUILD_TESTS})
type/vector_type_test.cc type/vector_type_test.cc
utils/command_test.cc utils/command_test.cc
utils/command.h utils/command.h
utils/math_test.cc
utils/tmpfile_test.cc utils/tmpfile_test.cc
utils/tmpfile.h utils/tmpfile.h
utils/unique_vector_test.cc utils/unique_vector_test.cc

View File

@ -38,6 +38,7 @@
#include "src/semantic/struct.h" #include "src/semantic/struct.h"
#include "src/semantic/variable.h" #include "src/semantic/variable.h"
#include "src/type/access_control_type.h" #include "src/type/access_control_type.h"
#include "src/utils/math.h"
namespace tint { namespace tint {
namespace resolver { namespace resolver {
@ -62,20 +63,6 @@ class ScopedAssignment {
T old_value_; T old_value_;
}; };
/// Rounds `value` to the next multiple of `alignment`
/// Assumes `alignment` is positive.
template <typename T>
T RoundUp(T alignment, T value) {
return ((value + alignment - 1) / alignment) * alignment;
}
/// Returns true if `value` is a power-of-two.
/// Assumes `alignment` is positive.
template <typename T>
bool IsPowerOfTwo(T value) {
return (value & (value - 1)) == 0;
}
} // namespace } // namespace
Resolver::Resolver(ProgramBuilder* builder) Resolver::Resolver(ProgramBuilder* builder)
@ -1126,7 +1113,7 @@ const semantic::Array* Resolver::Array(type::Array* arr) {
return nullptr; return nullptr;
} }
return create_semantic(RoundUp(el_align, el_size)); return create_semantic(utils::RoundUp(el_align, el_size));
} }
const semantic::Struct* Resolver::Structure(type::Struct* str) { const semantic::Struct* Resolver::Structure(type::Struct* str) {
@ -1179,7 +1166,7 @@ const semantic::Struct* Resolver::Structure(type::Struct* str) {
offset = o->offset(); offset = o->offset();
align = 1; align = 1;
} else if (auto* a = deco->As<ast::StructMemberAlignDecoration>()) { } else if (auto* a = deco->As<ast::StructMemberAlignDecoration>()) {
if (a->align() <= 0 || !IsPowerOfTwo(a->align())) { if (a->align() <= 0 || !utils::IsPowerOfTwo(a->align())) {
diagnostics_.add_error( diagnostics_.add_error(
"align value must be a positive, power-of-two integer", "align value must be a positive, power-of-two integer",
a->source()); a->source());
@ -1198,7 +1185,7 @@ const semantic::Struct* Resolver::Structure(type::Struct* str) {
} }
} }
offset = RoundUp(align, offset); offset = utils::RoundUp(align, offset);
auto* sem_member = auto* sem_member =
builder_->create<semantic::StructMember>(member, offset, size); builder_->create<semantic::StructMember>(member, offset, size);
@ -1209,7 +1196,7 @@ const semantic::Struct* Resolver::Structure(type::Struct* str) {
struct_align = std::max(struct_align, align); struct_align = std::max(struct_align, align);
} }
struct_size = RoundUp(struct_align, struct_size); struct_size = utils::RoundUp(struct_align, struct_size);
auto* sem = builder_->create<semantic::Struct>(str, std::move(sem_members), auto* sem = builder_->create<semantic::Struct>(str, std::move(sem_members),
struct_align, struct_size); struct_align, struct_size);

44
src/utils/math.h Normal file
View File

@ -0,0 +1,44 @@
// Copyright 2021 The Tint Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef SRC_UTILS_MATH_H_
#define SRC_UTILS_MATH_H_
#include <sstream>
#include <string>
namespace tint {
namespace utils {
/// @param alignment the next multiple to round `value` to
/// @param value the value to round to the next multiple of `alignment`
/// @return `value` rounded to the next multiple of `alignment`
/// @note `alignment` must be positive. An alignment of zero will cause a DBZ.
template <typename T>
inline T RoundUp(T alignment, T value) {
return ((value + alignment - 1) / alignment) * alignment;
}
/// @param value the value to check whether it is a power-of-two
/// @returns true if `value` is a power-of-two
/// @note `value` must be positive if `T` is signed
template <typename T>
inline bool IsPowerOfTwo(T value) {
return (value & (value - 1)) == 0;
}
} // namespace utils
} // namespace tint
#endif // SRC_UTILS_MATH_H_

63
src/utils/math_test.cc Normal file
View File

@ -0,0 +1,63 @@
// Copyright 2021 The Tint Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "src/utils/math.h"
#include "gtest/gtest.h"
namespace tint {
namespace utils {
namespace {
TEST(MathTests, RoundUp) {
EXPECT_EQ(RoundUp(1, 0), 0);
EXPECT_EQ(RoundUp(1, 1), 1);
EXPECT_EQ(RoundUp(1, 2), 2);
EXPECT_EQ(RoundUp(1, 1), 1);
EXPECT_EQ(RoundUp(2, 1), 2);
EXPECT_EQ(RoundUp(3, 1), 3);
EXPECT_EQ(RoundUp(4, 1), 4);
EXPECT_EQ(RoundUp(1, 2), 2);
EXPECT_EQ(RoundUp(2, 2), 2);
EXPECT_EQ(RoundUp(3, 2), 3);
EXPECT_EQ(RoundUp(4, 2), 4);
EXPECT_EQ(RoundUp(1, 3), 3);
EXPECT_EQ(RoundUp(2, 3), 4);
EXPECT_EQ(RoundUp(3, 3), 3);
EXPECT_EQ(RoundUp(4, 3), 4);
EXPECT_EQ(RoundUp(1, 4), 4);
EXPECT_EQ(RoundUp(2, 4), 4);
EXPECT_EQ(RoundUp(3, 4), 6);
EXPECT_EQ(RoundUp(4, 4), 4);
}
TEST(MathTests, IsPowerOfTwo) {
EXPECT_EQ(IsPowerOfTwo(1), true);
EXPECT_EQ(IsPowerOfTwo(2), true);
EXPECT_EQ(IsPowerOfTwo(3), false);
EXPECT_EQ(IsPowerOfTwo(4), true);
EXPECT_EQ(IsPowerOfTwo(5), false);
EXPECT_EQ(IsPowerOfTwo(6), false);
EXPECT_EQ(IsPowerOfTwo(7), false);
EXPECT_EQ(IsPowerOfTwo(8), true);
EXPECT_EQ(IsPowerOfTwo(9), false);
}
} // namespace
} // namespace utils
} // namespace tint

View File

@ -207,6 +207,7 @@ source_set("tint_unittests_core_src") {
"../src/type/vector_type_test.cc", "../src/type/vector_type_test.cc",
"../src/utils/command.h", "../src/utils/command.h",
"../src/utils/command_test.cc", "../src/utils/command_test.cc",
"../src/utils/math_test.cc",
"../src/utils/tmpfile.h", "../src/utils/tmpfile.h",
"../src/utils/tmpfile_test.cc", "../src/utils/tmpfile_test.cc",
"../src/utils/unique_vector_test.cc", "../src/utils/unique_vector_test.cc",