Skip to content

Commit 4a2f703

Browse files
committed
Add changes to use union data type for better conversion to strong type - Based on 5f2e011e2eed2f685521c707b3e74280fcb81dd3 from llamafile
1 parent ef693e9 commit 4a2f703

File tree

3 files changed

+9
-7
lines changed

3 files changed

+9
-7
lines changed

ggml/include/ggml.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,6 @@ extern "C" {
346346

347347
// google brain half-precision bfloat16
348348
typedef struct { uint16_t bits; } ggml_bf16_t;
349-
GGML_API ggml_bf16_t ggml_make_bf16(uint16_t val);
350349
GGML_API ggml_bf16_t ggml_fp32_to_bf16(float);
351350
GGML_API float ggml_bf16_to_fp32(ggml_bf16_t); // consider just doing << 16
352351
GGML_API void ggml_bf16_to_fp32_row(const ggml_bf16_t *, float *, int64_t);

ggml/src/ggml-impl.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,15 @@ static inline ggml_bf16_t ggml_compute_fp32_to_bf16(float s) {
102102
return h;
103103
}
104104

105+
static inline ggml_bf16_t ggml_make_bf16(uint16_t h) {
106+
union {
107+
ggml_bf16_t f;
108+
uint16_t i;
109+
} u;
110+
u.i = h;
111+
return u.f;
112+
}
113+
105114
#define GGML_FP32_TO_BF16(x) ggml_compute_fp32_to_bf16(x)
106115
#define GGML_BF16_TO_FP32(x) ggml_compute_bf16_to_fp32(x)
107116

ggml/src/ggml.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -428,12 +428,6 @@ float ggml_bf16_to_fp32(ggml_bf16_t x) {
428428
return GGML_BF16_TO_FP32(x); // it just left shifts
429429
}
430430

431-
ggml_bf16_t ggml_make_bf16(uint16_t x) {
432-
ggml_bf16_t bf16_value;
433-
bf16_value.bits = x;
434-
return bf16_value;
435-
}
436-
437431
ggml_bf16_t ggml_fp32_to_bf16(float x) {
438432
#define ggml_fp32_to_bf16 do_not_use__ggml_fp32_to_bf16__in_ggml
439433
return GGML_FP32_TO_BF16(x);

0 commit comments

Comments
 (0)