OpenLcbCLib 1.0 Alpha
OpenSource C Library to create OpenLcb/Lcc Nodes
Loading...
Searching...
No Matches
mustangpeak_endian_helper.h File Reference

Byte-order swap helpers for 8-, 16-, 32-, and 64-bit unsigned integers. More...

Go to the source code of this file.

Functions

uint8_t swap_endian8 (uint8_t x)
 Returns the input byte unchanged (no-op for API symmetry).
 
uint16_t swap_endian16 (uint16_t x)
 Reverses the byte order of a 16-bit unsigned integer.
 
uint32_t swap_endian32 (uint32_t x)
 Reverses the byte order of a 32-bit unsigned integer.
 
uint64_t swap_endian64 (uint64_t x)
 Reverses the byte order of a 64-bit unsigned integer.
 

Detailed Description

Byte-order swap helpers for 8-, 16-, 32-, and 64-bit unsigned integers.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Provides portable byte-swap functions used throughout the library wherever multi-byte values must be converted between host and network (big-endian) byte order. On GCC/Clang the implementations use compiler built-ins for optimal code generation; on other compilers a portable bit-shift fallback is used.

Author
Jim Kueneman
Date
28 Feb 2026

Function Documentation

◆ swap_endian8()

uint8_t swap_endian8 ( uint8_t x)
extern

Returns the input byte unchanged (no-op for API symmetry).

Provided so that generic code can call swap_endianN() for any width without special-casing the single-byte case.

Parameters
xByte value to "swap".
Returns
The same byte value, unchanged.
* @param x  Byte value to "swap".
* 
Returns
The same byte value, unchanged.

◆ swap_endian16()

uint16_t swap_endian16 ( uint16_t x)
extern

Reverses the byte order of a 16-bit unsigned integer.

Swaps the high and low bytes. Uses __builtin_bswap16 when available for single-instruction code generation.

Parameters
x16-bit value to byte-swap.
Returns
The byte-swapped 16-bit value.

Algorithm:

  1. If __builtin_bswap16 is available, use it directly.
  2. Otherwise shift the low byte up 8 bits and OR with the high byte shifted down.
* @param x  16-bit value to byte-swap.
* 
Returns
The byte-swapped 16-bit value.

◆ swap_endian32()

uint32_t swap_endian32 ( uint32_t x)
extern

Reverses the byte order of a 32-bit unsigned integer.

Swaps all four bytes end-for-end. Uses __builtin_bswap32 when available for single-instruction code generation.

Parameters
x32-bit value to byte-swap.
Returns
The byte-swapped 32-bit value.

Algorithm:

  1. If __builtin_bswap32 is available, use it directly.
  2. Otherwise mask and shift each of the four bytes into its mirror position.
* @param x  32-bit value to byte-swap.
* 
Returns
The byte-swapped 32-bit value.

◆ swap_endian64()

uint64_t swap_endian64 ( uint64_t x)
extern

Reverses the byte order of a 64-bit unsigned integer.

Swaps all eight bytes end-for-end. Uses __builtin_bswap64 when available for single-instruction code generation.

Parameters
x64-bit value to byte-swap.
Returns
The byte-swapped 64-bit value.

Algorithm:

  1. If __builtin_bswap64 is available, use it directly.
  2. Otherwise mask and shift each of the eight bytes into its mirror position.
* @param x  64-bit value to byte-swap.
* 
Returns
The byte-swapped 64-bit value.

Copyright (c) 2026 Jim Kueneman all rights reserved. See the License