From 4068d6baff78b203477abbd3c3453a0058ecee56 Mon Sep 17 00:00:00 2001 From: "Kristian H. Kristensen" Date: Thu, 16 Jan 2020 14:56:53 -0800 Subject: [PATCH] glsl: Add ir_constant constructor for fp16 Reviewed-by: Ian Romanick Part-of: --- src/compiler/glsl/ir.cpp | 13 +++++++++++++ src/compiler/glsl/ir.h | 2 ++ src/util/half_float.h | 20 ++++++++++++++++++-- 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/compiler/glsl/ir.cpp b/src/compiler/glsl/ir.cpp index f1e8a80db46..57551850757 100644 --- a/src/compiler/glsl/ir.cpp +++ b/src/compiler/glsl/ir.cpp @@ -682,6 +682,19 @@ ir_constant::ir_constant(const struct glsl_type *type, memcpy(& this->value, data, sizeof(this->value)); } +ir_constant::ir_constant(float16_t f16, unsigned vector_elements) + : ir_rvalue(ir_type_constant) +{ + assert(vector_elements <= 4); + this->type = glsl_type::get_instance(GLSL_TYPE_FLOAT16, vector_elements, 1); + for (unsigned i = 0; i < vector_elements; i++) { + this->value.f16[i] = f16.bits; + } + for (unsigned i = vector_elements; i < 16; i++) { + this->value.f[i] = 0; + } +} + ir_constant::ir_constant(float f, unsigned vector_elements) : ir_rvalue(ir_type_constant) { diff --git a/src/compiler/glsl/ir.h b/src/compiler/glsl/ir.h index cd1c53c29e7..c43c03c89a7 100644 --- a/src/compiler/glsl/ir.h +++ b/src/compiler/glsl/ir.h @@ -30,6 +30,7 @@ #include "util/ralloc.h" #include "util/format/u_format.h" +#include "util/half_float.h" #include "compiler/glsl_types.h" #include "list.h" #include "ir_visitor.h" @@ -2190,6 +2191,7 @@ public: ir_constant(bool b, unsigned vector_elements=1); ir_constant(unsigned int u, unsigned vector_elements=1); ir_constant(int i, unsigned vector_elements=1); + ir_constant(float16_t f16, unsigned vector_elements=1); ir_constant(float f, unsigned vector_elements=1); ir_constant(double d, unsigned vector_elements=1); ir_constant(uint64_t u64, unsigned vector_elements=1); diff --git a/src/util/half_float.h b/src/util/half_float.h index 84112e57bd4..c9fad9a9400 100644 --- a/src/util/half_float.h +++ b/src/util/half_float.h @@ -33,8 +33,8 @@ extern "C" { #endif -#define FP16_ONE 0x3C00 -#define FP16_ZERO 0 +#define FP16_ONE ((uint16_t) 0x3c00) +#define FP16_ZERO ((uint16_t) 0) uint16_t _mesa_float_to_half(float val); float _mesa_half_to_float(uint16_t val); @@ -62,6 +62,22 @@ _mesa_half_is_negative(uint16_t h) } +#ifdef __cplusplus + +/* Helper class for disambiguating fp16 from uint16_t in C++ overloads */ + +struct float16_t { + uint16_t bits; + float16_t(float f) : bits(_mesa_float_to_half(f)) {} + float16_t(double d) : bits(_mesa_float_to_half(d)) {} + float16_t(uint16_t bits) : bits(bits) {} + static float16_t one() { return float16_t(FP16_ONE); } + static float16_t zero() { return float16_t(FP16_ZERO); } +}; + +#endif + + #ifdef __cplusplus } /* extern C */ #endif -- 2.30.2