forked from ray-project/ray
-
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.
[core] Add a util for size literals (ray-project#48638)
As titled, I think having `MB` explicitly called out is more readable than `1024 * 1024` or `1<<20` Intended use case: ray-project#48262 (comment) Signed-off-by: dentiny <dentinyhao@gmail.com>
- Loading branch information
Showing
6 changed files
with
160 additions
and
19 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,95 @@ | ||
// Copyright 2024 The Ray 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. | ||
|
||
// These user-defined literals makes sizes. | ||
|
||
#pragma once | ||
|
||
#include <cstdint> | ||
|
||
namespace ray::literals { | ||
|
||
// Size literal for integer values. | ||
constexpr unsigned long long operator""_B(unsigned long long sz) { return sz; } | ||
|
||
constexpr unsigned long long operator""_KiB(unsigned long long sz) { return sz * 1024_B; } | ||
constexpr unsigned long long operator""_KB(unsigned long long sz) { return sz * 1000_B; } | ||
|
||
constexpr unsigned long long operator""_MiB(unsigned long long sz) { | ||
return sz * 1024_KiB; | ||
} | ||
constexpr unsigned long long operator""_MB(unsigned long long sz) { return sz * 1000_KB; } | ||
|
||
constexpr unsigned long long operator""_GiB(unsigned long long sz) { | ||
return sz * 1024_MiB; | ||
} | ||
constexpr unsigned long long operator""_GB(unsigned long long sz) { return sz * 1000_MB; } | ||
|
||
constexpr unsigned long long operator""_TiB(unsigned long long sz) { | ||
return sz * 1024_GiB; | ||
} | ||
constexpr unsigned long long operator""_TB(unsigned long long sz) { return sz * 1000_GB; } | ||
|
||
constexpr unsigned long long operator""_PiB(unsigned long long sz) { | ||
return sz * 1024_TiB; | ||
} | ||
constexpr unsigned long long operator""_PB(unsigned long long sz) { return sz * 1000_TB; } | ||
|
||
// Size literals for floating point values. | ||
constexpr unsigned long long operator""_KiB(long double sz) { | ||
const long double res = sz * 1_KiB; | ||
return static_cast<unsigned long long>(res); | ||
} | ||
constexpr unsigned long long operator""_KB(long double sz) { | ||
const long double res = sz * 1_KB; | ||
return static_cast<unsigned long long>(res); | ||
} | ||
|
||
constexpr unsigned long long operator""_MiB(long double sz) { | ||
const long double res = sz * 1_MiB; | ||
return static_cast<unsigned long long>(res); | ||
} | ||
constexpr unsigned long long operator""_MB(long double sz) { | ||
const long double res = sz * 1_MB; | ||
return static_cast<unsigned long long>(res); | ||
} | ||
|
||
constexpr unsigned long long operator""_GiB(long double sz) { | ||
const long double res = sz * 1_GiB; | ||
return static_cast<unsigned long long>(res); | ||
} | ||
constexpr unsigned long long operator""_GB(long double sz) { | ||
const long double res = sz * 1_GB; | ||
return static_cast<unsigned long long>(res); | ||
} | ||
|
||
constexpr unsigned long long operator""_TiB(long double sz) { | ||
const long double res = sz * 1_TiB; | ||
return static_cast<unsigned long long>(res); | ||
} | ||
constexpr unsigned long long operator""_TB(long double sz) { | ||
const long double res = sz * 1_TB; | ||
return static_cast<unsigned long long>(res); | ||
} | ||
|
||
constexpr unsigned long long operator""_PiB(long double sz) { | ||
const long double res = sz * 1_PiB; | ||
return static_cast<unsigned long long>(res); | ||
} | ||
constexpr unsigned long long operator""_PB(long double sz) { | ||
const long double res = sz * 1_PB; | ||
return static_cast<unsigned long long>(res); | ||
} | ||
|
||
} // namespace ray::literals |
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,31 @@ | ||
// Copyright 2024 The Ray 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/ray/util/size_literals.h" | ||
|
||
#include <gtest/gtest.h> | ||
|
||
namespace ray::literals { | ||
|
||
namespace { | ||
|
||
TEST(SizeLiteralsTest, BasicTest) { | ||
static_assert(2_MiB == 2 * 1024 * 1024); | ||
static_assert(2.5_KB == 2500); | ||
static_assert(4_GB == 4'000'000'000); | ||
} | ||
|
||
} // namespace | ||
|
||
} // namespace ray::literals |