ggml : static inlines + add public f16 <-> f32 conversions

pull/368/head
Georgi Gerganov 3 years ago
parent 44fc35e56a
commit daae73f559
No known key found for this signature in database
GPG Key ID: 449E073F9DC10735

@ -124,6 +124,9 @@ typedef double ggml_float;
// //
#include <arm_neon.h> #include <arm_neon.h>
#define GGML_COMPUTE_FP16_TO_FP32(x) (x)
#define GGML_COMPUTE_FP32_TO_FP16(x) (x)
#define GGML_FP16_TO_FP32(x) (x) #define GGML_FP16_TO_FP32(x) (x)
#define GGML_FP32_TO_FP16(x) (x) #define GGML_FP32_TO_FP16(x) (x)
@ -169,7 +172,7 @@ static inline uint32_t fp32_to_bits(float f) {
return fp32.as_bits; return fp32.as_bits;
} }
float ggml_compute_fp16_to_fp32(ggml_fp16_t h) { static inline float ggml_compute_fp16_to_fp32(ggml_fp16_t h) {
const uint32_t w = (uint32_t) h << 16; const uint32_t w = (uint32_t) h << 16;
const uint32_t sign = w & UINT32_C(0x80000000); const uint32_t sign = w & UINT32_C(0x80000000);
const uint32_t two_w = w + w; const uint32_t two_w = w + w;
@ -192,7 +195,7 @@ float ggml_compute_fp16_to_fp32(ggml_fp16_t h) {
return fp32_from_bits(result); return fp32_from_bits(result);
} }
ggml_fp16_t ggml_compute_fp32_to_fp16(float f) { static inline ggml_fp16_t ggml_compute_fp32_to_fp16(float f) {
#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) || defined(__GNUC__) && !defined(__STRICT_ANSI__) #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) || defined(__GNUC__) && !defined(__STRICT_ANSI__)
const float scale_to_inf = 0x1.0p+112f; const float scale_to_inf = 0x1.0p+112f;
const float scale_to_zero = 0x1.0p-110f; const float scale_to_zero = 0x1.0p-110f;
@ -253,6 +256,16 @@ inline static float ggml_lookup_fp16_to_fp32(ggml_fp16_t f) {
#endif #endif
// note: do not use these inside ggml.c
// these are meant to be used via the ggml.h API
float ggml_fp16_to_fp32(ggml_fp16_t x) {
return GGML_FP16_TO_FP32(x);
}
ggml_fp16_t ggml_fp32_to_fp16(float x) {
return GGML_FP32_TO_FP16(x);
}
// //
// timing // timing
// //

Loading…
Cancel
Save