mirror of
https://github.com/encounter/dawn-cmake.git
synced 2025-12-11 14:41:50 +00:00
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:
committed by
Commit Bot service account
parent
d614dd5d12
commit
a75205265e
44
src/utils/math.h
Normal file
44
src/utils/math.h
Normal 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
63
src/utils/math_test.cc
Normal 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
|
||||
Reference in New Issue
Block a user