/*1* Mesa 3-D graphics library2*3* Copyright (C) 1999-2007 Brian Paul All Rights Reserved.4* Copyright (C) 2018-2019 Intel Corporation5*6* Permission is hereby granted, free of charge, to any person obtaining a7* copy of this software and associated documentation files (the "Software"),8* to deal in the Software without restriction, including without limitation9* the rights to use, copy, modify, merge, publish, distribute, sublicense,10* and/or sell copies of the Software, and to permit persons to whom the11* Software is furnished to do so, subject to the following conditions:12*13* The above copyright notice and this permission notice shall be included14* in all copies or substantial portions of the Software.15*16* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS17* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,18* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL19* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR20* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,21* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR22* OTHER DEALINGS IN THE SOFTWARE.23*/2425#ifndef _HALF_FLOAT_H_26#define _HALF_FLOAT_H_2728#include <stdbool.h>29#include <stdint.h>30#include <string.h>31#include "util/u_cpu_detect.h"3233#if defined(USE_X86_64_ASM)34#include <immintrin.h>35#endif3637#ifdef __cplusplus38extern "C" {39#endif4041#define FP16_ONE ((uint16_t) 0x3c00)42#define FP16_ZERO ((uint16_t) 0)4344uint16_t _mesa_float_to_half_slow(float val);45float _mesa_half_to_float_slow(uint16_t val);46uint8_t _mesa_half_to_unorm8(uint16_t v);47uint16_t _mesa_uint16_div_64k_to_half(uint16_t v);4849/*50* _mesa_float_to_float16_rtz_slow is no more than a wrapper to the counterpart51* softfloat.h call. Still, softfloat.h conversion API is meant to be kept52* private. In other words, only use the API published here, instead of53* calling directly the softfloat.h one.54*/55uint16_t _mesa_float_to_float16_rtz_slow(float val);5657static inline uint16_t58_mesa_float_to_half(float val)59{60#if defined(USE_X86_64_ASM)61if (util_get_cpu_caps()->has_f16c) {62__m128 in = {val};63__m128i out;6465/* $0 = round to nearest */66__asm volatile("vcvtps2ph $0, %1, %0" : "=v"(out) : "v"(in));67return out[0];68}69#endif70return _mesa_float_to_half_slow(val);71}7273static inline float74_mesa_half_to_float(uint16_t val)75{76#if defined(USE_X86_64_ASM)77if (util_get_cpu_caps()->has_f16c) {78__m128i in = {val};79__m128 out;8081__asm volatile("vcvtph2ps %1, %0" : "=v"(out) : "v"(in));82return out[0];83}84#endif85return _mesa_half_to_float_slow(val);86}8788static inline uint16_t89_mesa_float_to_float16_rtz(float val)90{91#if defined(USE_X86_64_ASM)92if (util_get_cpu_caps()->has_f16c) {93__m128 in = {val};94__m128i out;9596/* $3 = round towards zero (truncate) */97__asm volatile("vcvtps2ph $3, %1, %0" : "=v"(out) : "v"(in));98return out[0];99}100#endif101return _mesa_float_to_float16_rtz_slow(val);102}103104static inline uint16_t105_mesa_float_to_float16_rtne(float val)106{107return _mesa_float_to_half(val);108}109110static inline bool111_mesa_half_is_negative(uint16_t h)112{113return !!(h & 0x8000);114}115116117#ifdef __cplusplus118119/* Helper class for disambiguating fp16 from uint16_t in C++ overloads */120121struct float16_t {122uint16_t bits;123float16_t(float f) : bits(_mesa_float_to_half(f)) {}124float16_t(double d) : bits(_mesa_float_to_half(d)) {}125float16_t(uint16_t raw_bits) : bits(raw_bits) {}126static float16_t one() { return float16_t(FP16_ONE); }127static float16_t zero() { return float16_t(FP16_ZERO); }128};129130#endif131132133#ifdef __cplusplus134} /* extern C */135#endif136137#endif /* _HALF_FLOAT_H_ */138139140