forked from facebook/folly
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
folly::align_floor, folly::align_ceil (facebook#2269)
Summary: Pull Request resolved: facebook#2269 Simple functions to do alignment for sizes and pointers. I looked into aligning any unsinged integrals but I believe it's cleaner with just std::size_t. Differential Revision: D60591352
- Loading branch information
1 parent
6b0454f
commit 7d2e9e9
Showing
5 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* 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 <folly/lang/Align.h> | ||
|
||
#include <folly/portability/GTest.h> | ||
|
||
namespace folly { | ||
|
||
TEST(Align, AlignFloor) { | ||
static_assert(1 == folly::align_floor(1, 1)); | ||
static_assert(2 == folly::align_floor(3, 2)); | ||
static_assert(8 == folly::align_floor(9, 8)); | ||
static_assert(4096 * 2 == folly::align_floor(4096 * 3 - 1, 4096)); | ||
|
||
struct Free { | ||
void operator()(void* ptr) { std::free(ptr); } | ||
}; | ||
|
||
std::unique_ptr<void, Free> alignedUptr(std::aligned_alloc(64, 256)); | ||
|
||
{ | ||
auto* alignedPtr = static_cast<char*>(alignedUptr.get()); | ||
|
||
EXPECT_EQ(alignedPtr, folly::align_floor(alignedPtr + 15, 16)); | ||
EXPECT_EQ(alignedPtr + 8, folly::align_floor(alignedPtr + 15, 8)); | ||
EXPECT_EQ(alignedPtr, folly::align_floor(alignedPtr + 8, 64)); | ||
} | ||
|
||
// not char ptrs | ||
{ | ||
auto* alignedPtrInt = static_cast<const int*>(alignedUptr.get()); | ||
EXPECT_EQ(alignedPtrInt, folly::align_floor(alignedPtrInt + 2, 16)); | ||
EXPECT_EQ(alignedPtrInt + 2, folly::align_floor(alignedPtrInt + 2, 4)); | ||
} | ||
|
||
// void* ptrs | ||
{ | ||
auto* alignedPtr = static_cast<char*>(alignedUptr.get()); | ||
|
||
EXPECT_EQ( | ||
static_cast<const void*>(alignedPtr + 8), | ||
folly::align_floor(static_cast<const void*>(alignedPtr + 15), 8)); | ||
} | ||
} | ||
|
||
TEST(Align, AlignCeil) { | ||
static_assert(1 == folly::align_ceil(1, 1)); | ||
static_assert(2 == folly::align_ceil(2, 2)); | ||
static_assert(4 == folly::align_ceil(3, 2)); | ||
static_assert(8 == folly::align_ceil(8, 8)); | ||
static_assert(16 == folly::align_ceil(9, 8)); | ||
static_assert(4096 * 3 == folly::align_ceil(4096 * 3 - 1, 4096)); | ||
|
||
struct Free { | ||
void operator()(void* ptr) { std::free(ptr); } | ||
}; | ||
|
||
std::unique_ptr<void, Free> alignedUptr(std::aligned_alloc(64, 256)); | ||
|
||
{ | ||
auto* alignedPtr = static_cast<char*>(alignedUptr.get()); | ||
|
||
EXPECT_EQ(alignedPtr, folly::align_ceil(alignedPtr, 16)); | ||
EXPECT_EQ(alignedPtr + 16, folly::align_ceil(alignedPtr + 15, 16)); | ||
EXPECT_EQ(alignedPtr + 32, folly::align_ceil(alignedPtr + 7, 32)); | ||
} | ||
|
||
// not char ptrs | ||
{ | ||
auto* alignedPtrInt = static_cast<const int*>(alignedUptr.get()); | ||
EXPECT_EQ(alignedPtrInt + 4, folly::align_ceil(alignedPtrInt + 2, 16)); | ||
EXPECT_EQ(alignedPtrInt + 2, folly::align_ceil(alignedPtrInt + 2, 4)); | ||
} | ||
|
||
// void* ptrs | ||
{ | ||
auto* alignedPtr = static_cast<char*>(alignedUptr.get()); | ||
|
||
EXPECT_EQ( | ||
static_cast<const void*>(alignedPtr + 16), | ||
folly::align_ceil(static_cast<const void*>(alignedPtr + 15), 8)); | ||
} | ||
} | ||
|
||
} // namespace folly |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters