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

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

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

Go to the source code of this file.

Macros

#define BF16_FILL_IN   9.99e+20f
#define BF16_REL_ERR_MAX   0.00390625

Functions

static uint16_t float_to_bfloat16 (float f)
 Convert a float32 to a bfloat16 bit pattern, round to nearest, ties to even.
static float bfloat16_to_float (uint16_t b)
 Convert a bfloat16 bit pattern to float32 (exact).

Detailed Description

Conversions between IEEE 754 float32 and bfloat16 bit patterns.

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

Author
Edward Hartnett, Intelligent Data Design, Inc.

Macro Definition Documentation

◆ BF16_FILL_IN

#define BF16_FILL_IN   9.99e+20f

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

◆ BF16_REL_ERR_MAX

#define BF16_REL_ERR_MAX   0.00390625

Maximum relative error for bfloat16 round-to-nearest (2^-8).

Function Documentation

◆ bfloat16_to_float()

float bfloat16_to_float ( uint16_t b)
inlinestatic

Convert a bfloat16 bit pattern to float32 (exact).

Parameters
bThe bfloat16 bit pattern.
Returns
The exact float32 value.

◆ float_to_bfloat16()

uint16_t float_to_bfloat16 ( float f)
inlinestatic

Convert a float32 to a bfloat16 bit pattern, round to nearest, ties to even.

NaN stays a quiet NaN. Values that round past the bfloat16 maximum overflow to infinity, which is correct.

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