Utils#
A collection of utility functions and bit and byte manipulation helpers that are broadly useful but do not belong to a more specific module. All are available on both POSIX and baremetal targets unless noted otherwise.
Checksums#
#include "rcsw/utils/checksum.h"
XOR-and-rotate and add-ignore-carry checksums in 8, 16 and 32-bit widths
(utils_xchks8() … utils_xchks32(), utils_achks8()
… utils_achks32()), each taking a seed so a checksum can be
continued across buffers. The 16- and 32-bit versions require an aligned
buffer and a length that is a multiple of the word size; otherwise they fail
the function precondition and return 0xFFFF / 0xFFFFFFFF, which is also a
valid checksum, so check errno (EINVAL) if that matters.
CRC-32 is the standard IEEE 802.3 (Ethernet, zlib, PNG) CRC, in two
interchangeable implementations: utils_crc32() uses a 1 KiB lookup
table, and utils_crc32_bitwise() computes the same value bit by bit
without one, for targets where flash is tighter than time. Pass 0 to start a
CRC, or a previous result to continue it over another buffer.
Hash Functions#
#include "rcsw/utils/hash.h"
Three hash functions over arbitrary byte buffers, each writing a uint32_t:
utils_hash_fnv1a() (FNV-1a), utils_hash_default() (Jenkins
one-at-a-time) and utils_hash_djb() (DJB2). Their signature matches
hashmap_config.hash.
Bit and Byte Manipulation#
Header |
Contents |
|---|---|
|
|
|
Byte swapping ( |
|
|
The byte swapping and alignment helpers are static inline and evaluate
each argument once.
Numeric and Memory Utilities#
#include "rcsw/utils/numeric.h": utils_permute() and
utils_zchk().
#include "rcsw/utils/mem.h": 32-bit register and memory access
(utils_mem_read32(), utils_mem_write32(),
utils_mem_rmwr32()), word-wise copy (utils_mem_cpy32()), byte
swapping in place (utils_mem_bswap16(), utils_mem_bswap32()),
and hex dumps in 8, 16 and 32-bit units (utils_mem_dump8() …,
with offsets: utils_mem_dump8v() …). The dumps print through
DPRINTF, so they print nothing in a build with LIBRA_ERL=NONE.
Time Utilities#
#include "rcsw/utils/time.h"
Note
Time utilities are POSIX-only. They are not available in baremetal
builds (RCSW_BUILD_FOR=BAREMETAL).
Comparison, addition, differencing, and conversion between struct
timespec and scalar monotonic counts in seconds or nanoseconds
(utils_ts_cmp(), utils_ts_add(), utils_ts_diff(),
utils_ts2mono(), utils_ts2monons(),
utils_monons2ts()). utils_ts_make_abs() and
utils_ts_make_rel() convert between relative timeouts and absolute
deadlines; see Timeouts.