aboutsummaryrefslogtreecommitdiff
path: root/libffwd/dtype.h
diff options
context:
space:
mode:
authorOnur Gündüz <ogunduz@gmail.com>2026-06-20 20:10:56 +0200
committerOnur Gündüz <ogunduz@gmail.com>2026-06-21 05:57:19 +0200
commit6b969d25ce136eaf695222bd1a10bff1ecac0220 (patch)
treec23afb50c28b9deaddae5c6526a74ee11cf2d865 /libffwd/dtype.h
parentd4bdb8dae753462f621507d5fc3582e0e53da35c (diff)
downloadffwd-6b969d25ce136eaf695222bd1a10bff1ecac0220.tar.gz
ffwd-6b969d25ce136eaf695222bd1a10bff1ecac0220.zip
all: split Makefile and reorg library/tools
Diffstat (limited to 'libffwd/dtype.h')
-rw-r--r--libffwd/dtype.h55
1 files changed, 55 insertions, 0 deletions
diff --git a/libffwd/dtype.h b/libffwd/dtype.h
new file mode 100644
index 0000000..7a7a2ad
--- /dev/null
+++ b/libffwd/dtype.h
@@ -0,0 +1,55 @@
+#ifndef FFWD_DTYPE_H
+#define FFWD_DTYPE_H
+
+#include <stdint.h>
+#include <string.h>
+
+/* BF16 is the upper 16 bits of an IEEE-754 binary32, so widening one value is a
+ * left shift reinterpreted as a float. Shared by every host-side caller (the
+ * CPU loader, the safetensors reader); the CUDA backend keeps its own
+ * __nv_bfloat16 device kernels, and kernels.c keeps the SIMD buffer variant
+ * bf16_to_f32_buf for hot, contiguous conversions. */
+static inline float ffwd_bf16_to_f32(uint16_t bf16) {
+ uint32_t bits = (uint32_t)bf16 << 16;
+ float f;
+ memcpy(&f, &bits, sizeof(f));
+ return f;
+}
+
+/* IEEE-754 binary16 -> binary32. Unlike BF16, F16 has a 5-bit exponent and a
+ * 10-bit mantissa, so widening is a field re-pack, not a shift: rebias the
+ * exponent (15 -> 127), shift the mantissa up by 13, and special-case zeros,
+ * subnormals, and inf/NaN. Used by the loaders to accept F16 safetensors
+ * snapshots (e.g. the public multilingual-e5 weights); there is no fused F16
+ * kernel, so weights are widened to F32 once at load. */
+static inline float ffwd_f16_to_f32(uint16_t f16) {
+ uint32_t sign = (uint32_t)(f16 & 0x8000u) << 16;
+ uint32_t exp = (f16 >> 10) & 0x1fu;
+ uint32_t mant = f16 & 0x3ffu;
+ uint32_t bits;
+ if (exp == 0) {
+ if (mant == 0) {
+ bits = sign; /* signed zero */
+ } else {
+ /* Subnormal half: renormalize into a normal float. Shift the
+ * mantissa left until its leading 1 reaches the hidden-bit
+ * position, decrementing the exponent for each shift. */
+ exp = 127 - 15 + 1;
+ while ((mant & 0x400u) == 0) {
+ mant <<= 1;
+ exp--;
+ }
+ mant &= 0x3ffu;
+ bits = sign | (exp << 23) | (mant << 13);
+ }
+ } else if (exp == 0x1fu) {
+ bits = sign | 0x7f800000u | (mant << 13); /* inf or NaN */
+ } else {
+ bits = sign | ((exp + (127 - 15)) << 23) | (mant << 13);
+ }
+ float f;
+ memcpy(&f, &bits, sizeof(f));
+ return f;
+}
+
+#endif /* FFWD_DTYPE_H */