1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Mesa 3-D graphics library 3bf215546Sopenharmony_ci * 4bf215546Sopenharmony_ci * Copyright (C) 1999-2007 Brian Paul All Rights Reserved. 5bf215546Sopenharmony_ci * Copyright (C) 2018-2019 Intel Corporation 6bf215546Sopenharmony_ci * 7bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 8bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"), 9bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation 10bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense, 11bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the 12bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions: 13bf215546Sopenharmony_ci * 14bf215546Sopenharmony_ci * The above copyright notice and this permission notice shall be included 15bf215546Sopenharmony_ci * in all copies or substantial portions of the Software. 16bf215546Sopenharmony_ci * 17bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 18bf215546Sopenharmony_ci * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 20bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 21bf215546Sopenharmony_ci * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 22bf215546Sopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 23bf215546Sopenharmony_ci * OTHER DEALINGS IN THE SOFTWARE. 24bf215546Sopenharmony_ci */ 25bf215546Sopenharmony_ci 26bf215546Sopenharmony_ci#ifndef _HALF_FLOAT_H_ 27bf215546Sopenharmony_ci#define _HALF_FLOAT_H_ 28bf215546Sopenharmony_ci 29bf215546Sopenharmony_ci#include <stdbool.h> 30bf215546Sopenharmony_ci#include <stdint.h> 31bf215546Sopenharmony_ci#include <string.h> 32bf215546Sopenharmony_ci#include "util/u_cpu_detect.h" 33bf215546Sopenharmony_ci 34bf215546Sopenharmony_ci#if defined(USE_X86_64_ASM) 35bf215546Sopenharmony_ci#include <immintrin.h> 36bf215546Sopenharmony_ci#endif 37bf215546Sopenharmony_ci 38bf215546Sopenharmony_ci#ifdef __cplusplus 39bf215546Sopenharmony_ciextern "C" { 40bf215546Sopenharmony_ci#endif 41bf215546Sopenharmony_ci 42bf215546Sopenharmony_ci#define FP16_ONE ((uint16_t) 0x3c00) 43bf215546Sopenharmony_ci#define FP16_ZERO ((uint16_t) 0) 44bf215546Sopenharmony_ci 45bf215546Sopenharmony_ciuint16_t _mesa_float_to_half_slow(float val); 46bf215546Sopenharmony_cifloat _mesa_half_to_float_slow(uint16_t val); 47bf215546Sopenharmony_ciuint8_t _mesa_half_to_unorm8(uint16_t v); 48bf215546Sopenharmony_ciuint16_t _mesa_uint16_div_64k_to_half(uint16_t v); 49bf215546Sopenharmony_ci 50bf215546Sopenharmony_ci/* 51bf215546Sopenharmony_ci * _mesa_float_to_float16_rtz_slow is no more than a wrapper to the counterpart 52bf215546Sopenharmony_ci * softfloat.h call. Still, softfloat.h conversion API is meant to be kept 53bf215546Sopenharmony_ci * private. In other words, only use the API published here, instead of 54bf215546Sopenharmony_ci * calling directly the softfloat.h one. 55bf215546Sopenharmony_ci */ 56bf215546Sopenharmony_ciuint16_t _mesa_float_to_float16_rtz_slow(float val); 57bf215546Sopenharmony_ci 58bf215546Sopenharmony_cistatic inline uint16_t 59bf215546Sopenharmony_ci_mesa_float_to_half(float val) 60bf215546Sopenharmony_ci{ 61bf215546Sopenharmony_ci#if defined(USE_X86_64_ASM) 62bf215546Sopenharmony_ci if (util_get_cpu_caps()->has_f16c) { 63bf215546Sopenharmony_ci __m128 in = {val}; 64bf215546Sopenharmony_ci __m128i out; 65bf215546Sopenharmony_ci 66bf215546Sopenharmony_ci /* $0 = round to nearest */ 67bf215546Sopenharmony_ci __asm volatile("vcvtps2ph $0, %1, %0" : "=v"(out) : "v"(in)); 68bf215546Sopenharmony_ci return out[0]; 69bf215546Sopenharmony_ci } 70bf215546Sopenharmony_ci#endif 71bf215546Sopenharmony_ci return _mesa_float_to_half_slow(val); 72bf215546Sopenharmony_ci} 73bf215546Sopenharmony_ci 74bf215546Sopenharmony_cistatic inline float 75bf215546Sopenharmony_ci_mesa_half_to_float(uint16_t val) 76bf215546Sopenharmony_ci{ 77bf215546Sopenharmony_ci#if defined(USE_X86_64_ASM) 78bf215546Sopenharmony_ci if (util_get_cpu_caps()->has_f16c) { 79bf215546Sopenharmony_ci __m128i in = {val}; 80bf215546Sopenharmony_ci __m128 out; 81bf215546Sopenharmony_ci 82bf215546Sopenharmony_ci __asm volatile("vcvtph2ps %1, %0" : "=v"(out) : "v"(in)); 83bf215546Sopenharmony_ci return out[0]; 84bf215546Sopenharmony_ci } 85bf215546Sopenharmony_ci#endif 86bf215546Sopenharmony_ci return _mesa_half_to_float_slow(val); 87bf215546Sopenharmony_ci} 88bf215546Sopenharmony_ci 89bf215546Sopenharmony_cistatic inline uint16_t 90bf215546Sopenharmony_ci_mesa_float_to_float16_rtz(float val) 91bf215546Sopenharmony_ci{ 92bf215546Sopenharmony_ci#if defined(USE_X86_64_ASM) 93bf215546Sopenharmony_ci if (util_get_cpu_caps()->has_f16c) { 94bf215546Sopenharmony_ci __m128 in = {val}; 95bf215546Sopenharmony_ci __m128i out; 96bf215546Sopenharmony_ci 97bf215546Sopenharmony_ci /* $3 = round towards zero (truncate) */ 98bf215546Sopenharmony_ci __asm volatile("vcvtps2ph $3, %1, %0" : "=v"(out) : "v"(in)); 99bf215546Sopenharmony_ci return out[0]; 100bf215546Sopenharmony_ci } 101bf215546Sopenharmony_ci#endif 102bf215546Sopenharmony_ci return _mesa_float_to_float16_rtz_slow(val); 103bf215546Sopenharmony_ci} 104bf215546Sopenharmony_ci 105bf215546Sopenharmony_cistatic inline uint16_t 106bf215546Sopenharmony_ci_mesa_float_to_float16_rtne(float val) 107bf215546Sopenharmony_ci{ 108bf215546Sopenharmony_ci return _mesa_float_to_half(val); 109bf215546Sopenharmony_ci} 110bf215546Sopenharmony_ci 111bf215546Sopenharmony_cistatic inline bool 112bf215546Sopenharmony_ci_mesa_half_is_negative(uint16_t h) 113bf215546Sopenharmony_ci{ 114bf215546Sopenharmony_ci return !!(h & 0x8000); 115bf215546Sopenharmony_ci} 116bf215546Sopenharmony_ci 117bf215546Sopenharmony_ci 118bf215546Sopenharmony_ci#ifdef __cplusplus 119bf215546Sopenharmony_ci 120bf215546Sopenharmony_ci/* Helper class for disambiguating fp16 from uint16_t in C++ overloads */ 121bf215546Sopenharmony_ci 122bf215546Sopenharmony_cistruct float16_t { 123bf215546Sopenharmony_ci uint16_t bits; 124bf215546Sopenharmony_ci float16_t(float f) : bits(_mesa_float_to_half(f)) {} 125bf215546Sopenharmony_ci float16_t(double d) : bits(_mesa_float_to_half(d)) {} 126bf215546Sopenharmony_ci float16_t(uint16_t raw_bits) : bits(raw_bits) {} 127bf215546Sopenharmony_ci static float16_t one() { return float16_t(FP16_ONE); } 128bf215546Sopenharmony_ci static float16_t zero() { return float16_t(FP16_ZERO); } 129bf215546Sopenharmony_ci}; 130bf215546Sopenharmony_ci 131bf215546Sopenharmony_ci#endif 132bf215546Sopenharmony_ci 133bf215546Sopenharmony_ci 134bf215546Sopenharmony_ci#ifdef __cplusplus 135bf215546Sopenharmony_ci} /* extern C */ 136bf215546Sopenharmony_ci#endif 137bf215546Sopenharmony_ci 138bf215546Sopenharmony_ci#endif /* _HALF_FLOAT_H_ */ 139