C/C++ API Reference
Loading...
Searching...
No Matches
units.h
1// Copyright 2021 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14#pragma once
15
16namespace pw::bytes {
17
19
20// Size constants for bytes in powers of two as defined by IEC 60027-2 A.2 and
21// ISO/IEC 80000:13-2008:
22
23// Kibibytes (KiB): 1024^1 or 2^10
24inline constexpr unsigned long long int kBytesInKibibyte = 1ull << 10;
25
26// Mebibytes (MiB): 1024^2 or 2^20
27inline constexpr unsigned long long int kBytesInMebibyte = 1ull << 20;
28
29// Gibibytes (GiB): 1024^3 or 2^30
30inline constexpr unsigned long long int kBytesInGibibyte = 1ull << 30;
31
32// Tebibytes (TiB): 1024^4 or 2^40
33inline constexpr unsigned long long int kBytesInTebibyte = 1ull << 40;
34
35// Pebibytes (PiB): 1024^5 or 2^50
36inline constexpr unsigned long long int kBytesInPebibyte = 1ull << 50;
37
38// Exbibytes (EiB): 1024^6 or 2^60
39inline constexpr unsigned long long int kBytesInExbibyte = 1ull << 60;
40
41// Functions for specifying a number of bytes in powers of two, as defined by
42// IEC 60027-2 A.2 and ISO/IEC 80000:13-2008.
43//
44// These are useful in headers when using user-defined literals are disallowed.
45//
46// #include "pw_bytes/units.h"
47//
48// constexpr size_t kBufferSizeBytes = pw::bytes::MiB(1) + pw::bytes::KiB(42);
49constexpr unsigned long long int B(unsigned long long int bytes) {
50 return bytes;
51}
52
53constexpr unsigned long long int KiB(unsigned long long int kibibytes) {
54 return kibibytes * kBytesInKibibyte;
55}
56
57constexpr unsigned long long int MiB(unsigned long long int mebibytes) {
58 return mebibytes * kBytesInMebibyte;
59}
60
61constexpr unsigned long long int GiB(unsigned long long int gibibytes) {
62 return gibibytes * kBytesInGibibyte;
63}
64
65constexpr unsigned long long int TiB(unsigned long long int tebibytes) {
66 return tebibytes * kBytesInTebibyte;
67}
68
69constexpr unsigned long long int PiB(unsigned long long int pebibytes) {
70 return pebibytes * kBytesInPebibyte;
71}
72
73constexpr unsigned long long int EiB(unsigned long long int exbibytes) {
74 return exbibytes * kBytesInExbibyte;
75}
76
77namespace unit_literals {
78
79// User-defined literals for specifying a number of bytes in powers of two, as
80// defined by IEC 60027-2 A.2 and ISO/IEC 80000:13-2008.
81//
82// The supported prefixes include:
83// _B for bytes (1024^0)
84// _KiB for kibibytes (1024^1)
85// _MiB for mebibytes (1024^2)
86// _GiB for gibibytes (1024^3)
87// _TiB for tebibytes (1024^4)
88// _PiB for pebibytes (1024^5)
89// _EiB for exbibytes (1024^6)
90//
91// In order to use these you must use a using namespace directive, for example:
92//
93// #include "pw_bytes/units.h"
94//
95// using namespace pw::bytes::unit_literals;
96//
97// constepxr size_t kRandomBufferSizeBytes = 1_MiB + 42_KiB;
98constexpr unsigned long long int operator""_B(unsigned long long int bytes) {
99 return bytes;
100}
101
102constexpr unsigned long long int operator""_KiB(
103 unsigned long long int kibibytes) {
104 return kibibytes * kBytesInKibibyte;
105}
106
107constexpr unsigned long long int operator""_MiB(
108 unsigned long long int mebibytes) {
109 return mebibytes * kBytesInMebibyte;
110}
111
112constexpr unsigned long long int operator""_GiB(
113 unsigned long long int gibibytes) {
114 return gibibytes * kBytesInGibibyte;
115}
116
117constexpr unsigned long long int operator""_TiB(
118 unsigned long long int tebibytes) {
119 return tebibytes * kBytesInTebibyte;
120}
121
122constexpr unsigned long long int operator""_PiB(
123 unsigned long long int pebibytes) {
124 return pebibytes * kBytesInPebibyte;
125}
126
127constexpr unsigned long long int operator""_EiB(
128 unsigned long long int exbibytes) {
129 return exbibytes * kBytesInExbibyte;
130}
131
132} // namespace unit_literals
133
135
136} // namespace pw::bytes