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

IEEE 754 half-precision (float16) conversion utilities. More...

Functions

uint16_t OpenLcbFloat16_from_float (float value)
 Converts a 32-bit float to a float16 bit pattern.
 
float OpenLcbFloat16_to_float (uint16_t half)
 Converts a float16 bit pattern to a 32-bit float.
 
uint16_t OpenLcbFloat16_negate (uint16_t half)
 Flips the sign/direction bit of a float16 value.
 
bool OpenLcbFloat16_is_nan (uint16_t half)
 Returns true if the float16 bit pattern represents NaN.
 
bool OpenLcbFloat16_is_zero (uint16_t half)
 Returns true if the float16 bit pattern represents positive or negative zero.
 
uint16_t OpenLcbFloat16_speed_with_direction (float speed, bool reverse)
 Encodes a speed magnitude and direction into a float16 bit pattern.
 
float OpenLcbFloat16_get_speed (uint16_t half)
 Returns the speed magnitude from a float16 bit pattern (ignores direction).
 
bool OpenLcbFloat16_get_direction (uint16_t half)
 Returns true if the direction bit is set (reverse).
 

Detailed Description

IEEE 754 half-precision (float16) conversion utilities.

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.

Converts between 32-bit IEEE 754 single-precision floats and 16-bit half-precision representations. Handles subnormals, overflow to infinity, and rounding via bit manipulation of the raw float pattern.

Author
Jim Kueneman
Date
4 Mar 2026

Function Documentation

◆ OpenLcbFloat16_from_float()

uint16_t OpenLcbFloat16_from_float ( float value)

Converts a 32-bit float to a float16 bit pattern.

Algorithm:

  1. Extract sign, exponent, mantissa from IEEE 754 single
  2. Handle special cases: zero, NaN, infinity
  3. Clamp overflow to max finite half value
  4. Normal range: rebias exponent, truncate mantissa
  5. Subnormal: shift mantissa into denormalized form
  6. Underflow: flush to signed zero
* @param value 32-bit float to convert
* 
Returns
16-bit float16 bit pattern

◆ OpenLcbFloat16_to_float()

float OpenLcbFloat16_to_float ( uint16_t half)

Converts a float16 bit pattern to a 32-bit float.

Algorithm:

  1. Extract sign, exponent, mantissa from half-precision fields
  2. Handle zero, subnormal (normalize), infinity/NaN, and normal cases
  3. Rebias exponent and expand mantissa to single-precision format
* @param half 16-bit float16 bit pattern
* 
Returns
32-bit float

◆ OpenLcbFloat16_negate()

uint16_t OpenLcbFloat16_negate ( uint16_t half)

Flips the sign/direction bit of a float16 value.

Flips the sign/direction bit of a float16 value and returns the result.

◆ OpenLcbFloat16_is_nan()

bool OpenLcbFloat16_is_nan ( uint16_t half)

Returns true if the float16 bit pattern represents NaN.

Returns true if the float16 bit pattern represents NaN (speed not available).

◆ OpenLcbFloat16_is_zero()

bool OpenLcbFloat16_is_zero ( uint16_t half)

Returns true if the float16 bit pattern represents positive or negative zero.

Parameters
half16-bit float16 bit pattern to test.
Returns
true if the value is zero, false otherwise.

◆ OpenLcbFloat16_speed_with_direction()

uint16_t OpenLcbFloat16_speed_with_direction ( float speed,
bool reverse )

Encodes a speed magnitude and direction into a float16 bit pattern.

Parameters
speedSpeed magnitude as a 32-bit float.
reversetrue for reverse direction, false for forward.
Returns
float16 bit pattern encoding the speed and direction.

◆ OpenLcbFloat16_get_speed()

float OpenLcbFloat16_get_speed ( uint16_t half)

Returns the speed magnitude from a float16 bit pattern (ignores direction).

Parameters
half16-bit float16 bit pattern to decode.
Returns
Speed magnitude as a 32-bit float.

◆ OpenLcbFloat16_get_direction()

bool OpenLcbFloat16_get_direction ( uint16_t half)

Returns true if the direction bit is set (reverse).

Parameters
half16-bit float16 bit pattern to test.
Returns
true if the direction bit is set (reverse), false otherwise.

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