|
NEP 4.3.0
NetCDF Expansion Pack
|
Internal numeric conversion and quantization for nxCDF. More...
#include "nxcdf/nxcdf.h"#include "config.h"#include <float.h>#include <limits.h>#include <math.h>#include <stdint.h>#include <stdlib.h>#include <string.h>#include "nxcdf_internal.h"Macros | |
| #define | CONVERT_SOURCE(T, REAL) |
| Macro: expand a loop converting one source C type to every standard destination type. | |
| #define | DEST_INT(ID, D, LOW, HIGH, FILL) |
| Macro: one case of CONVERT_SOURCE writing an integer destination with guarded conversion. | |
Functions | |
| static int | numeric (nc_type t) |
| Test whether a type is a standard numeric atomic type. | |
| static uint16_t | encode_bfloat (float f, int *range) |
| Encode a float as bfloat16 with round-to-nearest-even. | |
| static uint16_t | encode_bfloat_double (double d, int *range) |
| Encode a double as bfloat16 with a single final rounding. | |
| static float | decode_half (uint16_t h) |
| Decode an IEEE 754 binary16 bit pattern as float. | |
| static uint16_t | encode_half (float f, int *range) |
| Encode a float as IEEE 754 binary16 with round-to-nearest-even. | |
| static uint16_t | encode_half_double (double d, int *range) |
| Encode a double as IEEE 754 binary16 with a single final rounding. | |
| static void | quantize (void *data, nc_type t, size_t n, const void *fill, int mode, int nsd) |
| Apply netCDF quantization in place after conversion. | |
| static int | integer_range (nc_type source, nc_type target, unsigned long long u, long long s) |
| Check integer range without passing 64-bit boundaries through floating point. | |
| int | nxcdf_convert_type (const void *src, void *dest, nc_type src_type, nc_type dest_type, size_t len, int *range_error, const void *fill_value, int strict_nc3, int quantize_mode, int nsd, int fill_on_overflow) |
| NEP's internal numeric conversion engine for nxCDF. | |
Internal numeric conversion and quantization for nxCDF.
| #define CONVERT_SOURCE | ( | T, | |
| REAL ) |
Macro: expand a loop converting one source C type to every standard destination type.
Used inside nxcdf_convert_type()'s switch (src_type); generates the per-destination loops, including DEST_INT cases with range checking and NC_FLOAT/NC_DOUBLE cases with overflow detection.
| T | Source C type. |
| REAL | Whether the source is floating point. |
| #define DEST_INT | ( | ID, | |
| D, | |||
| LOW, | |||
| HIGH, | |||
| FILL ) |
Macro: one case of CONVERT_SOURCE writing an integer destination with guarded conversion.
Integer sources are checked by integer_range(). Real sources get one of two checks, keyed on fill_on_overflow: the netcdf-c attribute semantics check the raw input before truncation (a float like -0.5 still overflows an unsigned destination), while the NEP variable semantics check the truncated value, so fractions near a bound simply truncate. Out-of-range values set *range_error; the store is the fill value when fill_on_overflow is set (NEP write semantics) or the raw cast otherwise (netcdf-c attribute-read semantics). The classic NC_UBYTE→NC_BYTE overflow is tolerated under _NCProperties strict-nc3 semantics.
| ID | NetCDF destination type. |
| D | Destination C type. |
| LOW | Inclusive lower bound. |
| HIGH | Exclusive upper bound. |
| FILL | Default fill value. |
|
static |
Decode an IEEE 754 binary16 bit pattern as float.
The conversion is exact: subnormals are normalized, infinities and NaN payloads are preserved into the float NaN.
| h | Encoded bits. |
|
static |
Encode a float as bfloat16 with round-to-nearest-even.
bfloat16 is the top 16 bits of an IEEE-754 float, so encoding is a right shift — but the dropped bits must round correctly. Adding 0x7fff + lsb before shifting implements ties-to-even. NaN inputs are preserved as NaN; finite values that overflow to the bfloat16 infinity exponent raise range.
| f | Source value. |
| range | Receives a range flag on finite overflow. |
|
static |
Encode a double as bfloat16 with a single final rounding.
A naive double→float→bfloat16 sequence rounds twice and can land on the wrong representable value (double rounding). To avoid it, when the float result is inexact the neighboring float on the correct side of d is chosen before the final bfloat16 rounding — so only one rounding decides the outcome. Values beyond FLT_MAX become bfloat16 infinities with range set.
| d | Source value. |
| range | Receives a range flag on finite overflow. |
|
static |
Encode a float as IEEE 754 binary16 with round-to-nearest-even.
binary16 keeps the sign, rebases the exponent by -112 and rounds the significand to 10 bits. Finite magnitudes of 65520 or more overflow to infinity with range set (the tie at 65520 rounds to even, which is infinity); magnitudes in (65504, 65520) round to the largest finite 0x7bff. Results below 2^-14 become subnormals or signed zero; that underflow is not a range error. NaN stays a quiet NaN. Mirrors f32_to_f16() in src/nep_float16.c, which is the reference implementation.
| f | Source value. |
| range | Receives a range flag on finite overflow. |
|
static |
Encode a double as IEEE 754 binary16 with a single final rounding.
Same round-to-odd technique as encode_bfloat_double(): when the float result is inexact the neighboring float on the correct side of d is chosen before the final binary16 rounding. float's 24-bit significand has 13 spare bits over binary16's 11, so this equals rounding the double directly. Values beyond FLT_MAX become binary16 infinities with range set.
| d | Source value. |
| range | Receives a range flag on finite overflow. |
|
static |
Check integer range without passing 64-bit boundaries through floating point.
Comparing a long long to 2^63 via floating point is imprecise near the boundary (exactly the values that matter for NC_UINT64 and NC_INT64), so integer-to-integer conversions are range-checked entirely in integer arithmetic: unsigned sources go through u, signed through s.
| source | Source type. |
| target | Integer destination type. |
| u | Source converted to unsigned long long, used for unsigned sources. |
| s | Source converted to long long, used for signed sources. |
|
static |
Test whether a type is a standard numeric atomic type.
"Standard numeric" means the classic netCDF numeric types (NC_BYTE through NC_DOUBLE, including the unsigned set) — the ones nxcdf_convert_type() handles itself. NC_CHAR, NC_STRING, small floats, bitfields, references and user-defined types are excluded.
| t | NetCDF type. |
| int nxcdf_convert_type | ( | const void * | src, |
| void * | dest, | ||
| nc_type | src_type, | ||
| nc_type | dest_type, | ||
| size_t | len, | ||
| int * | range_error, | ||
| const void * | fill_value, | ||
| int | strict_nc3, | ||
| int | quantize_mode, | ||
| int | nsd, | ||
| int | fill_on_overflow ) |
NEP's internal numeric conversion engine for nxCDF.
Converts between standard numeric atomic types, and between NC_BFLOAT16/NC_FLOAT16 storage and NC_FLOAT/NC_DOUBLE buffers. Modeled on netcdf-c's libsrc4/nc4var.c conversion but owned by NEP so behavior (in particular NC_ERANGE reporting, issue #381) is consistent across all I/O paths: variable I/O, attributes and quantized writes.
Three strategies: standard numerics convert elementwise in C with exact range checks; NC_FLOAT16 pairs use decode_half() and encode_half()/encode_half_double(); NC_BFLOAT16 pairs use encode_bfloat() with round-to-nearest-even. Both 16-bit types decode exactly and narrow with round-to-nearest-even, single rounding from double — HDF5 performs no application numeric conversion. Quantization is applied to the converted output last. Callers may probe with NULL buffers to validate a conversion before allocating.
| src | Source buffer (may be NULL for a validation-only probe). |
| dest | Destination buffer (may be NULL for a probe). |
| src_type | Resolved source type. |
| dest_type | Resolved destination type. |
| len | Element count. |
| range_error | Required output range flag. |
| fill_value | Optional destination fill. |
| strict_nc3 | Classic byte-range convention. |
| quantize_mode | Quantization mode. |
| nsd | Quantization precision. |
| fill_on_overflow | Nonzero to store the fill value on real-to-integer overflow (NEP variable semantics); zero stores the raw truncated cast like netcdf-c (attribute read semantics). |
range_error, not the return value.
|
static |
Apply netCDF quantization in place after conversion.
Quantization throws away low-order mantissa bits of float/double data to make compression more effective. The three modes: NC_QUANTIZE_BITGROOM alternately sets/zeros the tail bits, NC_QUANTIZE_GRANULARBR keeps a per-value decimal precision, and NC_QUANTIZE_BITROUND keeps a fixed bit count and rounds. Fill values, zeros and non-finite values are left untouched.
All bit manipulation goes through memcpy to stay legal under strict aliasing — the buffer was written as a real float/double, and reading it as an integer through a cast would be undefined.
| data | Numeric destination buffer. |
| t | Destination type (NC_FLOAT or NC_DOUBLE). |
| n | Element count. |
| fill | Optional fill value. |
| mode | Validated quantization mode. |
| nsd | Validated precision. |