xref: /aosp_15_r20/external/pigweed/pw_bytes/public/pw_bytes/units.h (revision 61c4878ac05f98d0ceed94b57d316916de578985)
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 
16 namespace pw::bytes {
17 
18 // Size constants for bytes in powers of two as defined by IEC 60027-2 A.2 and
19 // ISO/IEC 80000:13-2008:
20 
21 // Kibibytes (KiB): 1024^1 or 2^10
22 inline constexpr unsigned long long int kBytesInKibibyte = 1ull << 10;
23 
24 // Mebibytes (MiB): 1024^2 or 2^20
25 inline constexpr unsigned long long int kBytesInMebibyte = 1ull << 20;
26 
27 // Gibibytes (GiB): 1024^3 or 2^30
28 inline constexpr unsigned long long int kBytesInGibibyte = 1ull << 30;
29 
30 // Tebibytes (TiB): 1024^4 or 2^40
31 inline constexpr unsigned long long int kBytesInTebibyte = 1ull << 40;
32 
33 // Pebibytes (PiB): 1024^5 or 2^50
34 inline constexpr unsigned long long int kBytesInPebibyte = 1ull << 50;
35 
36 // Exbibytes (EiB): 1024^6 or 2^60
37 inline constexpr unsigned long long int kBytesInExbibyte = 1ull << 60;
38 
39 // Functions for specifying a number of bytes in powers of two, as defined by
40 // IEC 60027-2 A.2 and ISO/IEC 80000:13-2008.
41 //
42 // These are useful in headers when using user-defined literals are disallowed.
43 //
44 //   #include "pw_bytes/units.h"
45 //
46 //   constexpr size_t kBufferSizeBytes = pw::bytes::MiB(1) + pw::bytes::KiB(42);
B(unsigned long long int bytes)47 constexpr unsigned long long int B(unsigned long long int bytes) {
48   return bytes;
49 }
50 
KiB(unsigned long long int kibibytes)51 constexpr unsigned long long int KiB(unsigned long long int kibibytes) {
52   return kibibytes * kBytesInKibibyte;
53 }
54 
MiB(unsigned long long int mebibytes)55 constexpr unsigned long long int MiB(unsigned long long int mebibytes) {
56   return mebibytes * kBytesInMebibyte;
57 }
58 
GiB(unsigned long long int gibibytes)59 constexpr unsigned long long int GiB(unsigned long long int gibibytes) {
60   return gibibytes * kBytesInGibibyte;
61 }
62 
TiB(unsigned long long int tebibytes)63 constexpr unsigned long long int TiB(unsigned long long int tebibytes) {
64   return tebibytes * kBytesInTebibyte;
65 }
66 
PiB(unsigned long long int pebibytes)67 constexpr unsigned long long int PiB(unsigned long long int pebibytes) {
68   return pebibytes * kBytesInPebibyte;
69 }
70 
EiB(unsigned long long int exbibytes)71 constexpr unsigned long long int EiB(unsigned long long int exbibytes) {
72   return exbibytes * kBytesInExbibyte;
73 }
74 
75 namespace unit_literals {
76 
77 // User-defined literals for specifying a number of bytes in powers of two, as
78 // defined by IEC 60027-2 A.2 and ISO/IEC 80000:13-2008.
79 //
80 // The supported prefixes include:
81 // _B   for bytes     (1024^0)
82 // _KiB for kibibytes (1024^1)
83 // _MiB for mebibytes (1024^2)
84 // _GiB for gibibytes (1024^3)
85 // _TiB for tebibytes (1024^4)
86 // _PiB for pebibytes (1024^5)
87 // _EiB for exbibytes (1024^6)
88 //
89 // In order to use these you must use a using namespace directive, for example:
90 //
91 //  #include "pw_bytes/units.h"
92 //
93 //  using namespace pw::bytes::unit_literals;
94 //
95 //  constepxr size_t kRandomBufferSizeBytes = 1_MiB + 42_KiB;
96 constexpr unsigned long long int operator""_B(unsigned long long int bytes) {
97   return bytes;
98 }
99 
100 constexpr unsigned long long int operator""_KiB(
101     unsigned long long int kibibytes) {
102   return kibibytes * kBytesInKibibyte;
103 }
104 
105 constexpr unsigned long long int operator""_MiB(
106     unsigned long long int mebibytes) {
107   return mebibytes * kBytesInMebibyte;
108 }
109 
110 constexpr unsigned long long int operator""_GiB(
111     unsigned long long int gibibytes) {
112   return gibibytes * kBytesInGibibyte;
113 }
114 
115 constexpr unsigned long long int operator""_TiB(
116     unsigned long long int tebibytes) {
117   return tebibytes * kBytesInTebibyte;
118 }
119 
120 constexpr unsigned long long int operator""_PiB(
121     unsigned long long int pebibytes) {
122   return pebibytes * kBytesInPebibyte;
123 }
124 
125 constexpr unsigned long long int operator""_EiB(
126     unsigned long long int exbibytes) {
127   return exbibytes * kBytesInExbibyte;
128 }
129 
130 }  // namespace unit_literals
131 }  // namespace pw::bytes
132