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

[Base] Add user-literals for several memory sizes #1935

Merged
merged 1 commit into from
Jan 2, 2022

Conversation

Wunkolo
Copy link
Contributor

@Wunkolo Wunkolo commented Dec 31, 2021

Rather than using n * 1024 * 1024, this adds a convenient _MiB/_KiB user-literal to the new literals.h header to concisely describe units of memory in a much more readable way. Any other useful literals can be added to this header. These literals exist in the xe::literals namespace so they are opt-in, similar to std::chrono literals, and require a using namespace xe::literals statement to utilize it within the current scope.

I've done a pass through the codebase to replace trivial instances of 1024 * 1024 * ... expressions being used but avoided anything that added additional casting complexity from size_t to uint32_t and such to keep this commit concise.

@Wunkolo Wunkolo force-pushed the mem-literals branch 2 times, most recently from 7fe6c99 to 1d834e1 Compare December 31, 2021 01:57
Rather than using `n * 1024 * 1024`, this adds a convenient `_MiB`/`_KiB` user-literal to the new `literals.h` header to concisely describe units of memory in a much more readable way. Any other useful literals can be added to this header. These literals exist in the `xe::literals` namespace so they are opt-in, similar to `std::chrono` literals, and require a `using namespace xe::literals` statement to utilize it within the current scope.

I've done a pass through the codebase to replace trivial instances of `1024 * 1024 * ...` expressions being used but avoided anything that added additional casting complexity from `size_t` to `uint32_t` and such to keep this commit concise.
@Razzile
Copy link
Member

Razzile commented Jan 2, 2022

This is a cool feature of C++ i didn't know about. I vaguely remember seeing custom literals for strings but had no idea it could be applied to other types as well

@gibbed gibbed merged commit 1a8068b into xenia-project:master Jan 2, 2022
@Wunkolo Wunkolo deleted the mem-literals branch January 2, 2022 19:57

namespace xe::literals {

constexpr size_t operator""_KiB(unsigned long long int x) {
Copy link

Choose a reason for hiding this comment

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

I think you can also add floating points?

constexpr unsigned long long operator""_PiB(long double sz) {
  const long double res = sz * 1024ULL * 1024ULL * 1024ULL * 1024ULL * 1024ULL;
  return static_cast<unsigned long long>(res);
}
constexpr unsigned long long operator""_PB(long double sz) {
  const long double res = sz * 1000ULL * 1000ULL * 1000ULL * 1000ULL * 1000ULL;
  return static_cast<unsigned long long>(res);
}

constexpr unsigned long long operator""_TiB(long double sz) {
  const long double res = sz * 1024ULL * 1024ULL * 1024ULL * 1024ULL;
  return static_cast<unsigned long long>(res);
}
constexpr unsigned long long operator""_TB(long double sz) {
  const long double res = sz * 1000ULL * 1000ULL * 1000ULL * 1000ULL;
  return static_cast<unsigned long long>(res);
}

constexpr unsigned long long operator""_GiB(long double sz) {
  const long double res = sz * 1024ULL * 1024ULL * 1024ULL;
  return static_cast<unsigned long long>(res);
}
constexpr unsigned long long operator""_GB(long double sz) {
  const long double res = sz * 1000ULL * 1000ULL * 1000ULL;
  return static_cast<unsigned long long>(res);
}

constexpr unsigned long long operator""_MiB(long double sz) {
  const long double res = sz * 1024ULL * 1024ULL;
  return static_cast<unsigned long long>(res);
}
constexpr unsigned long long operator""_MB(long double sz) {
  const long double res = sz * 1000ULL * 1000ULL;
  return static_cast<unsigned long long>(res);
}

constexpr unsigned long long operator""_KiB(long double sz) {
  const long double res = sz * 1024ULL;
  return static_cast<unsigned long long>(res);
}
constexpr unsigned long long operator""_KB(long double sz) {
  const long double res = sz * 1000ULL;
  return static_cast<unsigned long long>(res);
}

constexpr unsigned long long operator""_B(unsigned long long sz) { return sz; }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants