xref: /aosp_15_r20/external/pigweed/pw_checksum/docs.rst (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1*61c4878aSAndroid Build Coastguard Worker.. _module-pw_checksum:
2*61c4878aSAndroid Build Coastguard Worker
3*61c4878aSAndroid Build Coastguard Worker-----------
4*61c4878aSAndroid Build Coastguard Workerpw_checksum
5*61c4878aSAndroid Build Coastguard Worker-----------
6*61c4878aSAndroid Build Coastguard Worker.. pigweed-module::
7*61c4878aSAndroid Build Coastguard Worker   :name: pw_checksum
8*61c4878aSAndroid Build Coastguard Worker
9*61c4878aSAndroid Build Coastguard WorkerThe ``pw_checksum`` module provides functions for calculating checksums.
10*61c4878aSAndroid Build Coastguard Worker
11*61c4878aSAndroid Build Coastguard Workerpw_checksum/crc16_ccitt.h
12*61c4878aSAndroid Build Coastguard Worker=========================
13*61c4878aSAndroid Build Coastguard Worker
14*61c4878aSAndroid Build Coastguard Worker.. cpp:namespace:: pw::checksum
15*61c4878aSAndroid Build Coastguard Worker
16*61c4878aSAndroid Build Coastguard Worker.. cpp:var:: constexpr uint16_t kCcittCrc16DefaultInitialValue = 0xFFFF
17*61c4878aSAndroid Build Coastguard Worker
18*61c4878aSAndroid Build Coastguard Worker  The default initial value for the CRC16.
19*61c4878aSAndroid Build Coastguard Worker
20*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: uint16_t CcittCrc16(span<const std::byte> data, uint16_t initial_value = kCcittCrc16DefaultInitialValue)
21*61c4878aSAndroid Build Coastguard Worker
22*61c4878aSAndroid Build Coastguard Worker  Calculates the CRC16 of the provided data using polynomial 0x1021, with a
23*61c4878aSAndroid Build Coastguard Worker  default initial value of :cpp:expr:`0xFFFF`.
24*61c4878aSAndroid Build Coastguard Worker
25*61c4878aSAndroid Build Coastguard Worker  To incrementally calculate a CRC16, use the previous value as the initial
26*61c4878aSAndroid Build Coastguard Worker  value.
27*61c4878aSAndroid Build Coastguard Worker
28*61c4878aSAndroid Build Coastguard Worker  .. code-block:: cpp
29*61c4878aSAndroid Build Coastguard Worker
30*61c4878aSAndroid Build Coastguard Worker     uint16_t crc = CcittCrc16(my_data);
31*61c4878aSAndroid Build Coastguard Worker
32*61c4878aSAndroid Build Coastguard Worker     crc  = CcittCrc16(more_data, crc);
33*61c4878aSAndroid Build Coastguard Worker
34*61c4878aSAndroid Build Coastguard Workerpw_checksum/crc32.h
35*61c4878aSAndroid Build Coastguard Worker===================
36*61c4878aSAndroid Build Coastguard Worker
37*61c4878aSAndroid Build Coastguard Worker.. cpp:var:: constexpr uint32_t kCrc32InitialValue = 0xFFFFFFFF
38*61c4878aSAndroid Build Coastguard Worker
39*61c4878aSAndroid Build Coastguard Worker  The initial value for the CRC32.
40*61c4878aSAndroid Build Coastguard Worker
41*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: uint32_t Crc32(span<const std::byte> data)
42*61c4878aSAndroid Build Coastguard Worker
43*61c4878aSAndroid Build Coastguard Worker  Calculates the initial / one-time CRC32 of the provided data using polynomial
44*61c4878aSAndroid Build Coastguard Worker  0x4C11DB7, with an initial value of :cpp:expr:`0xFFFFFFFF`.
45*61c4878aSAndroid Build Coastguard Worker
46*61c4878aSAndroid Build Coastguard Worker  .. code-block:: cpp
47*61c4878aSAndroid Build Coastguard Worker
48*61c4878aSAndroid Build Coastguard Worker     uint32_t crc = Crc32(my_data);
49*61c4878aSAndroid Build Coastguard Worker
50*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: uint32_t Crc32(span<const std::byte> data, uint32_t previous_result)
51*61c4878aSAndroid Build Coastguard Worker
52*61c4878aSAndroid Build Coastguard Worker  Incrementally append calculation of a CRC32, need to pass in the previous
53*61c4878aSAndroid Build Coastguard Worker  result.
54*61c4878aSAndroid Build Coastguard Worker
55*61c4878aSAndroid Build Coastguard Worker  .. code-block:: cpp
56*61c4878aSAndroid Build Coastguard Worker
57*61c4878aSAndroid Build Coastguard Worker     uint32_t crc = Crc32(my_data);
58*61c4878aSAndroid Build Coastguard Worker     crc = Crc32(more_data, crc);
59*61c4878aSAndroid Build Coastguard Worker
60*61c4878aSAndroid Build Coastguard Worker.. _CRC32 Implementations:
61*61c4878aSAndroid Build Coastguard Worker
62*61c4878aSAndroid Build Coastguard WorkerImplementations
63*61c4878aSAndroid Build Coastguard Worker---------------
64*61c4878aSAndroid Build Coastguard WorkerPigweed provides 3 different CRC32 implementations with different size and
65*61c4878aSAndroid Build Coastguard Workerruntime tradeoffs.  The below table summarizes the variants.  For more detailed
66*61c4878aSAndroid Build Coastguard Workersize information see the :ref:`pw_checksum-size-report` below.  Instructions
67*61c4878aSAndroid Build Coastguard Workercounts were calculated by hand by analyzing the
68*61c4878aSAndroid Build Coastguard Worker`assembly <https://godbolt.org/z/nY1bbb5Pb>`_. Clock Cycle counts were measured
69*61c4878aSAndroid Build Coastguard Workerusing :ref:`module-pw_perf_test` on a STM32F429I-DISC1 development board.
70*61c4878aSAndroid Build Coastguard Worker
71*61c4878aSAndroid Build Coastguard Worker
72*61c4878aSAndroid Build Coastguard Worker.. list-table::
73*61c4878aSAndroid Build Coastguard Worker   :header-rows: 1
74*61c4878aSAndroid Build Coastguard Worker
75*61c4878aSAndroid Build Coastguard Worker   * - Variant
76*61c4878aSAndroid Build Coastguard Worker     - Relative size (see Size Report below)
77*61c4878aSAndroid Build Coastguard Worker     - Speed
78*61c4878aSAndroid Build Coastguard Worker     - Lookup table size (entries)
79*61c4878aSAndroid Build Coastguard Worker     - Instructions/byte (M33/-Os)
80*61c4878aSAndroid Build Coastguard Worker     - Clock Cycles (123 char string)
81*61c4878aSAndroid Build Coastguard Worker     - Clock Cycles (9 bytes)
82*61c4878aSAndroid Build Coastguard Worker   * - 8 bits per iteration (default)
83*61c4878aSAndroid Build Coastguard Worker     - large
84*61c4878aSAndroid Build Coastguard Worker     - fastest
85*61c4878aSAndroid Build Coastguard Worker     - 256
86*61c4878aSAndroid Build Coastguard Worker     - 8
87*61c4878aSAndroid Build Coastguard Worker     - 1538
88*61c4878aSAndroid Build Coastguard Worker     - 170
89*61c4878aSAndroid Build Coastguard Worker   * - 4 bits per iteration
90*61c4878aSAndroid Build Coastguard Worker     - small
91*61c4878aSAndroid Build Coastguard Worker     - fast
92*61c4878aSAndroid Build Coastguard Worker     - 16
93*61c4878aSAndroid Build Coastguard Worker     - 13
94*61c4878aSAndroid Build Coastguard Worker     - 2153
95*61c4878aSAndroid Build Coastguard Worker     - 215
96*61c4878aSAndroid Build Coastguard Worker   * - 1 bit per iteration
97*61c4878aSAndroid Build Coastguard Worker     - smallest
98*61c4878aSAndroid Build Coastguard Worker     - slow
99*61c4878aSAndroid Build Coastguard Worker     - 0
100*61c4878aSAndroid Build Coastguard Worker     - 43
101*61c4878aSAndroid Build Coastguard Worker     - 7690
102*61c4878aSAndroid Build Coastguard Worker     - 622
103*61c4878aSAndroid Build Coastguard Worker
104*61c4878aSAndroid Build Coastguard WorkerThe default implementation provided by the APIs above can be selected through
105*61c4878aSAndroid Build Coastguard Worker:ref:`Module Configuration Options`.  Additionally ``pw_checksum`` provides
106*61c4878aSAndroid Build Coastguard Workervariants of the C++ API to explicitly use each of the implementations.  These
107*61c4878aSAndroid Build Coastguard Workerclasses provide the same API as ``Crc32``:
108*61c4878aSAndroid Build Coastguard Worker
109*61c4878aSAndroid Build Coastguard Worker* ``Crc32EightBit``
110*61c4878aSAndroid Build Coastguard Worker* ``Crc32FourBit``
111*61c4878aSAndroid Build Coastguard Worker* ``Crc32OneBit``
112*61c4878aSAndroid Build Coastguard Worker
113*61c4878aSAndroid Build Coastguard Worker.. _pw_checksum-size-report:
114*61c4878aSAndroid Build Coastguard Worker
115*61c4878aSAndroid Build Coastguard WorkerSize report
116*61c4878aSAndroid Build Coastguard Worker===========
117*61c4878aSAndroid Build Coastguard WorkerThe CRC module currently optimizes for speed instead of binary size, by using
118*61c4878aSAndroid Build Coastguard Workerpre-computed 256-entry tables to reduce the CPU cycles per byte CRC
119*61c4878aSAndroid Build Coastguard Workercalculation.
120*61c4878aSAndroid Build Coastguard Worker
121*61c4878aSAndroid Build Coastguard Worker.. include:: size_report
122*61c4878aSAndroid Build Coastguard Worker
123*61c4878aSAndroid Build Coastguard WorkerCompatibility
124*61c4878aSAndroid Build Coastguard Worker=============
125*61c4878aSAndroid Build Coastguard Worker* C
126*61c4878aSAndroid Build Coastguard Worker* C++17
127*61c4878aSAndroid Build Coastguard Worker
128*61c4878aSAndroid Build Coastguard WorkerDependencies
129*61c4878aSAndroid Build Coastguard Worker============
130*61c4878aSAndroid Build Coastguard Worker- :ref:`module-pw_span`
131*61c4878aSAndroid Build Coastguard Worker
132*61c4878aSAndroid Build Coastguard Worker.. _Module Configuration Options:
133*61c4878aSAndroid Build Coastguard Worker
134*61c4878aSAndroid Build Coastguard WorkerModule Configuration Options
135*61c4878aSAndroid Build Coastguard Worker============================
136*61c4878aSAndroid Build Coastguard WorkerThe following configurations can be adjusted via compile-time configuration of
137*61c4878aSAndroid Build Coastguard Workerthis module, see the
138*61c4878aSAndroid Build Coastguard Worker:ref:`module documentation <module-structure-compile-time-configuration>` for
139*61c4878aSAndroid Build Coastguard Workermore details.
140*61c4878aSAndroid Build Coastguard Worker
141*61c4878aSAndroid Build Coastguard Worker.. c:macro:: PW_CHECKSUM_CRC32_DEFAULT_IMPL
142*61c4878aSAndroid Build Coastguard Worker
143*61c4878aSAndroid Build Coastguard Worker  Selects which of the :ref:`CRC32 Implementations` the default CRC32 APIs
144*61c4878aSAndroid Build Coastguard Worker  use.  Set to one of the following values:
145*61c4878aSAndroid Build Coastguard Worker
146*61c4878aSAndroid Build Coastguard Worker  * ``PW_CHECKSUM_CRC32_8BITS``
147*61c4878aSAndroid Build Coastguard Worker  * ``PW_CHECKSUM_CRC32_4BITS``
148*61c4878aSAndroid Build Coastguard Worker  * ``PW_CHECKSUM_CRC32_1BITS``
149*61c4878aSAndroid Build Coastguard Worker
150*61c4878aSAndroid Build Coastguard WorkerZephyr
151*61c4878aSAndroid Build Coastguard Worker======
152*61c4878aSAndroid Build Coastguard WorkerTo enable ``pw_checksum`` for Zephyr add ``CONFIG_PIGWEED_CHECKSUM=y`` to the
153*61c4878aSAndroid Build Coastguard Workerproject's configuration.
154