aboutsummaryrefslogtreecommitdiff
path: root/src/dtype.h
diff options
context:
space:
mode:
authorOnur Gündüz <ogunduz@gmail.com>2026-06-16 16:34:12 +0200
committerOnur Gündüz <ogunduz@gmail.com>2026-06-16 16:34:12 +0200
commit78f5588d761f0df66eb11c2b79bee7c22fb0d132 (patch)
tree302b5f567b649fc2e9d2ffad383130bc0a469f1f /src/dtype.h
parentd1ba1ebc06ef0d62ca932388fae83e6992594719 (diff)
downloadffwd-78f5588d761f0df66eb11c2b79bee7c22fb0d132.tar.gz
ffwd-78f5588d761f0df66eb11c2b79bee7c22fb0d132.zip
api: dedup scalar bf16-to-f32 into dtype.h
Diffstat (limited to 'src/dtype.h')
-rw-r--r--src/dtype.h19
1 files changed, 19 insertions, 0 deletions
diff --git a/src/dtype.h b/src/dtype.h
new file mode 100644
index 0000000..5c1ce8f
--- /dev/null
+++ b/src/dtype.h
@@ -0,0 +1,19 @@
+#ifndef EMBED_DTYPE_H
+#define EMBED_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
+ * embed_bf16_to_f32_buf for hot, contiguous conversions. */
+static inline float embed_bf16_to_f32(uint16_t bf16) {
+ uint32_t bits = (uint32_t)bf16 << 16;
+ float f;
+ memcpy(&f, &bits, sizeof(f));
+ return f;
+}
+
+#endif /* EMBED_DTYPE_H */