NEP 4.3.0
NetCDF Expansion Pack
Loading...
Searching...
No Matches
bfloat16_conv.h
Go to the documentation of this file.
1
12#ifndef BFLOAT16_CONV_H
13#define BFLOAT16_CONV_H
14
15#include "nxcdf/nxcdf.h"
16#include <stdint.h>
17#include <string.h>
18
20#define BF16_FILL_IN 9.99e+20f
21
23#define BF16_REL_ERR_MAX 0.00390625
24
35static inline uint16_t
37{
38 uint32_t u;
39
40 memcpy(&u, &f, sizeof(u));
41 if ((u & 0x7fffffffu) > 0x7f800000u)
42 return (uint16_t)((u >> 16) | 0x0040u);
43 return (uint16_t)((u + 0x7fffu + ((u >> 16) & 1u)) >> 16);
44}
45
52static inline float
54{
55 uint32_t u = (uint32_t)b << 16;
56 float f;
57
58 memcpy(&f, &u, sizeof(f));
59 return f;
60}
61
62#endif /* BFLOAT16_CONV_H */
static float bfloat16_to_float(uint16_t b)
Convert a bfloat16 bit pattern to float32 (exact).
Definition bfloat16_conv.h:53
static uint16_t float_to_bfloat16(float f)
Convert a float32 to a bfloat16 bit pattern, round to nearest, ties to even.
Definition bfloat16_conv.h:36
Public API for the nxCDF UDF backend.