NEP 4.3.0
NetCDF Expansion Pack
Loading...
Searching...
No Matches
nep_bfloat16.c File Reference
#include "config.h"
#include "nep.h"
#include "nxcdf/nxcdf.h"
#include <stdlib.h>
#include <string.h>

Functions

static uint16_t f32_to_bf16 (float f, int *range)
static uint16_t f64_to_bf16 (double d, int *range)
static float bf16_to_f32 (uint16_t b)
static int bf16_io (int ncid, int varid, const size_t *startp, const size_t *countp, const ptrdiff_t *stridep, void *buf, int put)
int nc_get_vars_bfloat16 (int ncid, int varid, const size_t *startp, const size_t *countp, const ptrdiff_t *stridep, nc_bfloat16_t *ip)
 Read a strided hyperslab of a variable as bfloat16.
int nc_put_vars_bfloat16 (int ncid, int varid, const size_t *startp, const size_t *countp, const ptrdiff_t *stridep, const nc_bfloat16_t *op)
 Write a strided hyperslab of a variable from bfloat16 values.
int nc_get_vara_bfloat16 (int ncid, int varid, const size_t *startp, const size_t *countp, nc_bfloat16_t *ip)
 Read a hyperslab of a variable as bfloat16.
int nc_put_vara_bfloat16 (int ncid, int varid, const size_t *startp, const size_t *countp, const nc_bfloat16_t *op)
 Write a hyperslab of a variable from bfloat16 values.
int nc_get_var1_bfloat16 (int ncid, int varid, const size_t *indexp, nc_bfloat16_t *ip)
 Read one value from a variable as bfloat16.
int nc_put_var1_bfloat16 (int ncid, int varid, const size_t *indexp, const nc_bfloat16_t *op)
 Write one value to a variable as bfloat16.
int nc_get_var_bfloat16 (int ncid, int varid, nc_bfloat16_t *ip)
 Read a whole variable as bfloat16.
int nc_put_var_bfloat16 (int ncid, int varid, const nc_bfloat16_t *op)
 Write a whole variable from bfloat16 values.

Detailed Description

This is the code file for the NEP bfloat16 I/O functions.

The nc_*_bfloat16 functions exchange bfloat16 values in memory with NC_BFLOAT16, NC_FLOAT and NC_DOUBLE variables. They are written on top of the public netCDF API, so they work with any file netCDF can open. Buffers are always accessed as bytes through memcpy, never as nc_bfloat16_t values, so the code is the same whether nc_bfloat16_t is the compiler's __bf16 or uint16_t.

Author
Edward Hartnett
Date
2026-11-08

Function Documentation

◆ bf16_io()

int bf16_io ( int ncid,
int varid,
const size_t * startp,
const size_t * countp,
const ptrdiff_t * stridep,
void * buf,
int put )
static

Common worker for the eight nc_*_bfloat16 functions. NC_BFLOAT16 variables are read or written as raw bits with nc_get_vars() or nc_put_vars(). NC_FLOAT and NC_DOUBLE variables go through a temporary float or double buffer and the typed nc_get_vars_* or nc_put_vars_* call, with the conversion done here.

Parameters
ncidNetCDF file ID.
varidVariable ID.
startpStart index in each dimension.
countpEdge count in each dimension.
stridepStride in each dimension, or NULL for a stride of 1.
bufThe bfloat16 buffer (read into for a get, read from for a put).
putNon-zero to write, zero to read.
Returns
NC_NOERR No error.
NC_ERANGE A finite value overflowed to +-inf on a get; all values are still converted.
NC_ECHAR The variable is NC_CHAR.
NC_EBADTYPE The variable is not NC_BFLOAT16, NC_FLOAT or NC_DOUBLE.
NC_EINVAL NULL start, count or buffer where one is needed.
NC_ENOMEM Out of memory.
Other errors from the underlying netCDF calls.
Author
Edward Hartnett, Intelligent Data Design, Inc.

◆ bf16_to_f32()

float bf16_to_f32 ( uint16_t b)
static

Convert a bfloat16 bit pattern to float. The conversion is exact.

Parameters
bThe bfloat16 bit pattern.
Returns
The float value.
Author
Edward Hartnett, Intelligent Data Design, Inc.

◆ f32_to_bf16()

uint16_t f32_to_bf16 ( float f,
int * range )
static

Convert a float to a bfloat16 bit pattern, rounding to nearest with ties to even. NaN stays a quiet NaN, as in bfloat16_conv.h.

Parameters
fThe float to convert.
rangeSet to 1 when a finite input becomes +-inf; otherwise left unchanged.
Returns
The bfloat16 bit pattern.
Author
Edward Hartnett, Intelligent Data Design, Inc.

◆ f64_to_bf16()

uint16_t f64_to_bf16 ( double d,
int * range )
static

Convert a double to a bfloat16 bit pattern with a single rounding (to nearest, ties to even). The double is first rounded to float with round-to-odd (an inexact result gets an odd last bit), then converted with f32_to_bf16(). float has 16 more significand bits than bfloat16, so this equals rounding the double directly.

Parameters
dThe double to convert.
rangeSet to 1 when a finite input becomes +-inf; otherwise left unchanged.
Returns
The bfloat16 bit pattern.
Author
Edward Hartnett, Intelligent Data Design, Inc.

◆ nc_get_var1_bfloat16()

int nc_get_var1_bfloat16 ( int ncid,
int varid,
const size_t * indexp,
nc_bfloat16_t * ip )

Read one value from a variable as bfloat16.

Read one value from a variable as bfloat16. See nep.h for the variable types accepted and the conversion rules.

Author
Edward Hartnett, Intelligent Data Design, Inc.

◆ nc_get_var_bfloat16()

int nc_get_var_bfloat16 ( int ncid,
int varid,
nc_bfloat16_t * ip )

Read a whole variable as bfloat16.

Read a whole variable as bfloat16. See nep.h for the variable types accepted and the conversion rules.

Author
Edward Hartnett, Intelligent Data Design, Inc.

◆ nc_get_vara_bfloat16()

int nc_get_vara_bfloat16 ( int ncid,
int varid,
const size_t * startp,
const size_t * countp,
nc_bfloat16_t * ip )

Read a hyperslab of a variable as bfloat16.

Read a hyperslab of a variable as bfloat16. See nep.h for the variable types accepted and the conversion rules.

Author
Edward Hartnett, Intelligent Data Design, Inc.

◆ nc_get_vars_bfloat16()

int nc_get_vars_bfloat16 ( int ncid,
int varid,
const size_t * startp,
const size_t * countp,
const ptrdiff_t * stridep,
nc_bfloat16_t * ip )

Read a strided hyperslab of a variable as bfloat16.

Read a strided hyperslab of a variable as bfloat16. See nep.h for the variable types accepted and the conversion rules.

Author
Edward Hartnett, Intelligent Data Design, Inc.

◆ nc_put_var1_bfloat16()

int nc_put_var1_bfloat16 ( int ncid,
int varid,
const size_t * indexp,
const nc_bfloat16_t * op )

Write one value to a variable as bfloat16.

Write one value to a variable as bfloat16. See nep.h for the variable types accepted and the conversion rules.

Author
Edward Hartnett, Intelligent Data Design, Inc.

◆ nc_put_var_bfloat16()

int nc_put_var_bfloat16 ( int ncid,
int varid,
const nc_bfloat16_t * op )

Write a whole variable from bfloat16 values.

Write a whole variable from bfloat16 values. See nep.h for the variable types accepted and the conversion rules.

Author
Edward Hartnett, Intelligent Data Design, Inc.

◆ nc_put_vara_bfloat16()

int nc_put_vara_bfloat16 ( int ncid,
int varid,
const size_t * startp,
const size_t * countp,
const nc_bfloat16_t * op )

Write a hyperslab of a variable from bfloat16 values.

Write a hyperslab of a variable from bfloat16 values. See nep.h for the variable types accepted and the conversion rules.

Author
Edward Hartnett, Intelligent Data Design, Inc.

◆ nc_put_vars_bfloat16()

int nc_put_vars_bfloat16 ( int ncid,
int varid,
const size_t * startp,
const size_t * countp,
const ptrdiff_t * stridep,
const nc_bfloat16_t * op )

Write a strided hyperslab of a variable from bfloat16 values.

Write a strided hyperslab of a variable from bfloat16 values. See nep.h for the variable types accepted and the conversion rules.

Author
Edward Hartnett, Intelligent Data Design, Inc.