aboutsummaryrefslogtreecommitdiff
path: root/pw_bytes/public/pw_bytes/units.h
blob: 67327e558c546f4e5d9831c7213730f13982f82c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright 2021 The Pigweed 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
//
//     https://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.
#pragma once

namespace pw::bytes {

// Size constants for bytes in powers of two as defined by IEC 60027-2 A.2 and
// ISO/IEC 80000:13-2008:

// Kibibytes (KiB): 1024^1 or 2^10
inline constexpr unsigned long long int kBytesInKibibyte = 1ull << 10;

// Mebibytes (MiB): 1024^2 or 2^20
inline constexpr unsigned long long int kBytesInMibibyte = 1ull << 20;

// Gibibytes (GiB): 1024^3 or 2^30
inline constexpr unsigned long long int kBytesInGibibyte = 1ull << 30;

// Tebibytes (TiB): 1024^4 or 2^40
inline constexpr unsigned long long int kBytesInTebibyte = 1ull << 40;

// Pebibytes (PiB): 1024^5 or 2^50
inline constexpr unsigned long long int kBytesInPebibyte = 1ull << 50;

// Exbibytes (EiB): 1024^6 or 2^60
inline constexpr unsigned long long int kBytesInExbibyte = 1ull << 60;

namespace unit_literals {

// Helper user-defined literals for specifying a number of bytes in powers of
// two, as defined by IEC 60027-2 A.2 and ISO/IEC 80000:13-2008.
//
// The supported prefixes include:
// _B   for bytes     (1024^0)
// _KiB for kibibytes (1024^1)
// _MiB for mibibytes (1024^2)
// _GiB for gibibytes (1024^3)
// _TiB for tebibytes (1024^4)
// _PiB for pebibytes (1024^5)
// _EiB for exbibytes (1024^6)
//
// In order to use these you must use a using namespace directive, for example:
//
//  #include "pw_bytes/units.h"
//
//  using namespace pw::bytes::unit_literals;
//
//  constepxr size_t kRandomBufferSizeBytes = 1_MiB + 42_KiB;
constexpr unsigned long long int operator""_B(unsigned long long int bytes) {
  return bytes;
}

constexpr unsigned long long int operator""_KiB(
    unsigned long long int kibibytes) {
  return kibibytes * kBytesInKibibyte;
}

constexpr unsigned long long int operator""_MiB(
    unsigned long long int mibibytes) {
  return mibibytes * kBytesInMibibyte;
}

constexpr unsigned long long int operator""_GiB(
    unsigned long long int gibibytes) {
  return gibibytes * kBytesInGibibyte;
}

constexpr unsigned long long int operator""_TiB(
    unsigned long long int tebibytes) {
  return tebibytes * kBytesInTebibyte;
}

constexpr unsigned long long int operator""_PiB(
    unsigned long long int pebibytes) {
  return pebibytes * kBytesInPebibyte;
}

constexpr unsigned long long int operator""_EiB(
    unsigned long long int exbibytes) {
  return exbibytes * kBytesInExbibyte;
}

}  // namespace unit_literals
}  // namespace pw::bytes