NEP 4.3.0
NetCDF Expansion Pack
Loading...
Searching...
No Matches
float16_conv.h File Reference

Conversions between IEEE 754 float32 and binary16 bit patterns. More...

#include "nxcdf/nxcdf.h"
#include <stdint.h>
#include <string.h>

Go to the source code of this file.

Macros

#define F16_FILL_IN   9.99e+20f
#define F16_REL_ERR_MAX   0.00048828125
#define F16_ABS_ERR_MAX   2.9802322387695312e-08

Functions

static uint16_t float_to_float16 (float f)
 Convert a float32 to an IEEE 754 binary16 bit pattern, round to nearest, ties to even.
static float float16_to_float (uint16_t h)
 Convert an IEEE 754 binary16 bit pattern to float32 (exact).

Detailed Description

Conversions between IEEE 754 float32 and binary16 bit patterns.

nxCDF stores NC_FLOAT16 variables as raw 16-bit IEEE 754 binary16 bit patterns; conversion to and from float32 is done in user code. This header provides the two conversions used by the nxCDF float16 examples.

Author
Edward Hartnett, Intelligent Data Design, Inc.

Macro Definition Documentation

◆ F16_ABS_ERR_MAX

#define F16_ABS_ERR_MAX   2.9802322387695312e-08

Absolute-error floor: half the smallest binary16 subnormal (2^-25). A point also passes when its absolute error is under this floor, so subnormal underflow does not fail the gate.

◆ F16_FILL_IN

#define F16_FILL_IN   9.99e+20f

GDAS input fill value; points equal to it are left out of error metrics.

◆ F16_REL_ERR_MAX

#define F16_REL_ERR_MAX   0.00048828125

Maximum relative error for binary16 round-to-nearest (2^-11).

Function Documentation

◆ float16_to_float()

float float16_to_float ( uint16_t h)
inlinestatic

Convert an IEEE 754 binary16 bit pattern to float32 (exact).

Parameters
hThe binary16 bit pattern.
Returns
The exact float32 value.

◆ float_to_float16()

uint16_t float_to_float16 ( float f)
inlinestatic

Convert a float32 to an IEEE 754 binary16 bit pattern, round to nearest, ties to even.

NaN stays a quiet NaN. Finite magnitudes of 65520 or more overflow to infinity; magnitudes in (65504, 65520) round to the largest finite value 0x7bff. Results below 2^-14 become subnormals or signed zero.

Parameters
fThe float32 value to convert.
Returns
The binary16 bit pattern in the low 16 bits of a uint16_t.