xref: /aosp_15_r20/external/pigweed/pw_bytes/docs.rst (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1*61c4878aSAndroid Build Coastguard Worker.. _module-pw_bytes:
2*61c4878aSAndroid Build Coastguard Worker
3*61c4878aSAndroid Build Coastguard Worker========
4*61c4878aSAndroid Build Coastguard Workerpw_bytes
5*61c4878aSAndroid Build Coastguard Worker========
6*61c4878aSAndroid Build Coastguard Worker.. pigweed-module::
7*61c4878aSAndroid Build Coastguard Worker   :name: pw_bytes
8*61c4878aSAndroid Build Coastguard Worker
9*61c4878aSAndroid Build Coastguard Workerpw_bytes is a collection of utilities for manipulating binary data.
10*61c4878aSAndroid Build Coastguard Worker
11*61c4878aSAndroid Build Coastguard Worker------------
12*61c4878aSAndroid Build Coastguard WorkerDependencies
13*61c4878aSAndroid Build Coastguard Worker------------
14*61c4878aSAndroid Build Coastguard Worker* ``pw_preprocessor``
15*61c4878aSAndroid Build Coastguard Worker* ``pw_status``
16*61c4878aSAndroid Build Coastguard Worker* ``pw_span``
17*61c4878aSAndroid Build Coastguard Worker
18*61c4878aSAndroid Build Coastguard Worker--------
19*61c4878aSAndroid Build Coastguard WorkerFeatures
20*61c4878aSAndroid Build Coastguard Worker--------
21*61c4878aSAndroid Build Coastguard Worker
22*61c4878aSAndroid Build Coastguard Workerpw_bytes/packed_ptr.h
23*61c4878aSAndroid Build Coastguard Worker======================
24*61c4878aSAndroid Build Coastguard WorkerWrapper type that allows storing data in the least significant bits of a
25*61c4878aSAndroid Build Coastguard Workerpointer that would otherwise be unused.
26*61c4878aSAndroid Build Coastguard Worker
27*61c4878aSAndroid Build Coastguard Worker.. doxygenclass:: pw::PackedPtr
28*61c4878aSAndroid Build Coastguard Worker   :members:
29*61c4878aSAndroid Build Coastguard Worker
30*61c4878aSAndroid Build Coastguard Workerpw_bytes/alignment.h
31*61c4878aSAndroid Build Coastguard Worker====================
32*61c4878aSAndroid Build Coastguard WorkerFunctions for aligning sizes and addresses to memory alignment boundaries.
33*61c4878aSAndroid Build Coastguard Worker
34*61c4878aSAndroid Build Coastguard Worker .. doxygenfunction:: pw::IsAlignedAs(const void* ptr, size_t alignment)
35*61c4878aSAndroid Build Coastguard Worker
36*61c4878aSAndroid Build Coastguard Worker .. doxygenfunction:: pw::IsAlignedAs(const void* ptr)
37*61c4878aSAndroid Build Coastguard Worker
38*61c4878aSAndroid Build Coastguard Worker .. doxygenfunction:: pw::AlignDown(uintptr_t value, size_t alignment)
39*61c4878aSAndroid Build Coastguard Worker
40*61c4878aSAndroid Build Coastguard Worker .. doxygenfunction:: pw::AlignDown(T* value, size_t alignment)
41*61c4878aSAndroid Build Coastguard Worker
42*61c4878aSAndroid Build Coastguard Worker .. doxygenfunction:: pw::AlignUp(uintptr_t value, size_t alignment)
43*61c4878aSAndroid Build Coastguard Worker
44*61c4878aSAndroid Build Coastguard Worker .. doxygenfunction:: pw::AlignUp(T* value, size_t alignment)
45*61c4878aSAndroid Build Coastguard Worker
46*61c4878aSAndroid Build Coastguard Worker .. doxygenfunction:: pw::Padding
47*61c4878aSAndroid Build Coastguard Worker
48*61c4878aSAndroid Build Coastguard Worker .. doxygenfunction:: pw::GetAlignedSubspan
49*61c4878aSAndroid Build Coastguard Worker
50*61c4878aSAndroid Build Coastguard Workerpw_bytes/array.h
51*61c4878aSAndroid Build Coastguard Worker================
52*61c4878aSAndroid Build Coastguard WorkerFunctions for working with byte arrays, primarily for building fixed-size byte
53*61c4878aSAndroid Build Coastguard Workerarrays at compile time.
54*61c4878aSAndroid Build Coastguard Worker
55*61c4878aSAndroid Build Coastguard Workerpw_bytes/byte_builder.h
56*61c4878aSAndroid Build Coastguard Worker=======================
57*61c4878aSAndroid Build Coastguard Worker.. doxygenclass:: pw::ByteBuilder
58*61c4878aSAndroid Build Coastguard Worker   :members:
59*61c4878aSAndroid Build Coastguard Worker
60*61c4878aSAndroid Build Coastguard Worker.. doxygenclass:: pw::ByteBuffer
61*61c4878aSAndroid Build Coastguard Worker   :members:
62*61c4878aSAndroid Build Coastguard Worker
63*61c4878aSAndroid Build Coastguard WorkerSize report: using ByteBuffer
64*61c4878aSAndroid Build Coastguard Worker-----------------------------
65*61c4878aSAndroid Build Coastguard Worker.. include:: byte_builder_size_report
66*61c4878aSAndroid Build Coastguard Worker
67*61c4878aSAndroid Build Coastguard Workerpw_bytes/bit.h
68*61c4878aSAndroid Build Coastguard Worker================
69*61c4878aSAndroid Build Coastguard WorkerImplementation of features provided by C++20's ``<bit>`` header. Supported
70*61c4878aSAndroid Build Coastguard Workerfeatures:
71*61c4878aSAndroid Build Coastguard Worker
72*61c4878aSAndroid Build Coastguard Worker* ``pw::endian`` -- Implementation of the ``std::endian`` enum. If
73*61c4878aSAndroid Build Coastguard Worker  ``std::endian`` is available, ``pw::endian`` is an alias of it.
74*61c4878aSAndroid Build Coastguard Worker
75*61c4878aSAndroid Build Coastguard Worker* Additional functions for bit-level operations.
76*61c4878aSAndroid Build Coastguard Worker
77*61c4878aSAndroid Build Coastguard Worker  .. doxygenfunction:: pw::bytes::SignExtend
78*61c4878aSAndroid Build Coastguard Worker  .. doxygenfunction:: pw::bytes::ExtractBits
79*61c4878aSAndroid Build Coastguard Worker
80*61c4878aSAndroid Build Coastguard Workerpw_bytes/endian.h
81*61c4878aSAndroid Build Coastguard Worker=================
82*61c4878aSAndroid Build Coastguard WorkerFunctions for converting the endianness of integral values.
83*61c4878aSAndroid Build Coastguard Worker
84*61c4878aSAndroid Build Coastguard Workerpw_bytes/suffix.h
85*61c4878aSAndroid Build Coastguard Worker=================
86*61c4878aSAndroid Build Coastguard WorkerThis module exports a single ``_b`` literal, making it easier to create
87*61c4878aSAndroid Build Coastguard Worker``std::byte`` values for tests.
88*61c4878aSAndroid Build Coastguard Worker
89*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: constexpr std::byte operator""_b(unsigned long long value)
90*61c4878aSAndroid Build Coastguard Worker
91*61c4878aSAndroid Build Coastguard Worker.. note::
92*61c4878aSAndroid Build Coastguard Worker   This should not be used in header files, as it requires a ``using``
93*61c4878aSAndroid Build Coastguard Worker   declaration that will be publicly exported at whatever level it is
94*61c4878aSAndroid Build Coastguard Worker   used.
95*61c4878aSAndroid Build Coastguard Worker
96*61c4878aSAndroid Build Coastguard WorkerExample:
97*61c4878aSAndroid Build Coastguard Worker
98*61c4878aSAndroid Build Coastguard Worker.. code-block:: cpp
99*61c4878aSAndroid Build Coastguard Worker
100*61c4878aSAndroid Build Coastguard Worker   #include "pw_bytes/units.h"
101*61c4878aSAndroid Build Coastguard Worker
102*61c4878aSAndroid Build Coastguard Worker   using ::pw::operator""_b;
103*61c4878aSAndroid Build Coastguard Worker
104*61c4878aSAndroid Build Coastguard Worker   constexpr std::byte kValue = 5_b;
105*61c4878aSAndroid Build Coastguard Worker
106*61c4878aSAndroid Build Coastguard Workerpw_bytes/units.h
107*61c4878aSAndroid Build Coastguard Worker================
108*61c4878aSAndroid Build Coastguard WorkerConstants, functions and user-defined literals for specifying a number of bytes
109*61c4878aSAndroid Build Coastguard Workerin powers of two, as defined by IEC 60027-2 A.2 and ISO/IEC 80000:13-2008.
110*61c4878aSAndroid Build Coastguard Worker
111*61c4878aSAndroid Build Coastguard WorkerThe supported suffixes include:
112*61c4878aSAndroid Build Coastguard Worker
113*61c4878aSAndroid Build Coastguard Worker* ``_B``   for bytes     (1024\ :sup:`0`)
114*61c4878aSAndroid Build Coastguard Worker* ``_KiB`` for kibibytes (1024\ :sup:`1`)
115*61c4878aSAndroid Build Coastguard Worker* ``_MiB`` for mebibytes (1024\ :sup:`2`)
116*61c4878aSAndroid Build Coastguard Worker* ``_GiB`` for gibibytes (1024\ :sup:`3`)
117*61c4878aSAndroid Build Coastguard Worker* ``_TiB`` for tebibytes (1024\ :sup:`4`)
118*61c4878aSAndroid Build Coastguard Worker* ``_PiB`` for pebibytes (1024\ :sup:`5`)
119*61c4878aSAndroid Build Coastguard Worker* ``_EiB`` for exbibytes (1024\ :sup:`6`)
120*61c4878aSAndroid Build Coastguard Worker
121*61c4878aSAndroid Build Coastguard WorkerIn order to use these you must use a using namespace directive, for example:
122*61c4878aSAndroid Build Coastguard Worker
123*61c4878aSAndroid Build Coastguard Worker.. code-block:: cpp
124*61c4878aSAndroid Build Coastguard Worker
125*61c4878aSAndroid Build Coastguard Worker   #include "pw_bytes/units.h"
126*61c4878aSAndroid Build Coastguard Worker
127*61c4878aSAndroid Build Coastguard Worker   using namespace pw::bytes::unit_literals;
128*61c4878aSAndroid Build Coastguard Worker
129*61c4878aSAndroid Build Coastguard Worker   constexpr size_t kRandomBufferSizeBytes = 1_MiB + 42_KiB;
130*61c4878aSAndroid Build Coastguard Worker
131*61c4878aSAndroid Build Coastguard WorkerIn some cases, the use of user-defined literals is not permitted because of the
132*61c4878aSAndroid Build Coastguard Workerrequired using namespace directive. One example of this is in header files,
133*61c4878aSAndroid Build Coastguard Workerwhere it is undesirable to pollute the namespace. For this situation, there are
134*61c4878aSAndroid Build Coastguard Workeralso similar functions:
135*61c4878aSAndroid Build Coastguard Worker
136*61c4878aSAndroid Build Coastguard Worker.. code-block:: cpp
137*61c4878aSAndroid Build Coastguard Worker
138*61c4878aSAndroid Build Coastguard Worker   #include "pw_bytes/units.h"
139*61c4878aSAndroid Build Coastguard Worker
140*61c4878aSAndroid Build Coastguard Worker   constexpr size_t kBufferSizeBytes = pw::bytes::MiB(1) + pw::bytes::KiB(42);
141*61c4878aSAndroid Build Coastguard Worker
142*61c4878aSAndroid Build Coastguard Worker------
143*61c4878aSAndroid Build Coastguard WorkerZephyr
144*61c4878aSAndroid Build Coastguard Worker------
145*61c4878aSAndroid Build Coastguard WorkerTo enable ``pw_bytes`` for Zephyr add ``CONFIG_PIGWEED_BYTES=y`` to the
146*61c4878aSAndroid Build Coastguard Workerproject's configuration.
147*61c4878aSAndroid Build Coastguard Worker
148*61c4878aSAndroid Build Coastguard Worker--------
149*61c4878aSAndroid Build Coastguard WorkerRust API
150*61c4878aSAndroid Build Coastguard Worker--------
151*61c4878aSAndroid Build Coastguard Worker``pw_bytes``'s Rust API is documented in our
152*61c4878aSAndroid Build Coastguard Worker`rustdoc API docs </rustdoc/pw_bytes/>`_.
153