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

Implementation of the byte-order swap helpers. More...

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

Implementation of the byte-order swap helpers.

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.

Each function reverses the byte order of its argument. On GCC/Clang the compiler built-in __builtin_bswapNN is used for optimal code generation; on other compilers a portable bit-shift fallback is provided.

Author
Jim Kueneman
Date
28 Feb 2026

Function Documentation

◆ swap_endian8()

uint8_t swap_endian8 ( uint8_t x)

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

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

◆ swap_endian16()

uint16_t swap_endian16 ( uint16_t x)

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

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)

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

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)

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

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