/*1* Mesa 3-D graphics library2*3* Copyright (C) 1999-2007 Brian Paul All Rights Reserved.4* Copyright 2015 Philip Taylor <[email protected]>5* Copyright 2018 Advanced Micro Devices, Inc.6* Copyright (C) 2018-2019 Intel Corporation7*8* Permission is hereby granted, free of charge, to any person obtaining a9* copy of this software and associated documentation files (the "Software"),10* to deal in the Software without restriction, including without limitation11* the rights to use, copy, modify, merge, publish, distribute, sublicense,12* and/or sell copies of the Software, and to permit persons to whom the13* Software is furnished to do so, subject to the following conditions:14*15* The above copyright notice and this permission notice shall be included16* in all copies or substantial portions of the Software.17*18* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS19* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,20* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL21* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR22* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,23* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR24* OTHER DEALINGS IN THE SOFTWARE.25*/2627#include <math.h>28#include <assert.h>29#include "half_float.h"30#include "rounding.h"31#include "softfloat.h"32#include "macros.h"33#include "u_math.h"3435typedef union { float f; int32_t i; uint32_t u; } fi_type;3637/**38* Convert a 4-byte float to a 2-byte half float.39*40* Not all float32 values can be represented exactly as a float16 value. We41* round such intermediate float32 values to the nearest float16. When the42* float32 lies exactly between to float16 values, we round to the one with43* an even mantissa.44*45* This rounding behavior has several benefits:46* - It has no sign bias.47*48* - It reproduces the behavior of real hardware: opcode F32TO16 in Intel's49* GPU ISA.50*51* - By reproducing the behavior of the GPU (at least on Intel hardware),52* compile-time evaluation of constant packHalf2x16 GLSL expressions will53* result in the same value as if the expression were executed on the GPU.54*/55uint16_t56_mesa_float_to_half_slow(float val)57{58const fi_type fi = {val};59const int flt_m = fi.i & 0x7fffff;60const int flt_e = (fi.i >> 23) & 0xff;61const int flt_s = (fi.i >> 31) & 0x1;62int s, e, m = 0;63uint16_t result;6465/* sign bit */66s = flt_s;6768/* handle special cases */69if ((flt_e == 0) && (flt_m == 0)) {70/* zero */71/* m = 0; - already set */72e = 0;73}74else if ((flt_e == 0) && (flt_m != 0)) {75/* denorm -- denorm float maps to 0 half */76/* m = 0; - already set */77e = 0;78}79else if ((flt_e == 0xff) && (flt_m == 0)) {80/* infinity */81/* m = 0; - already set */82e = 31;83}84else if ((flt_e == 0xff) && (flt_m != 0)) {85/* NaN */86m = 1;87e = 31;88}89else {90/* regular number */91const int new_exp = flt_e - 127;92if (new_exp < -14) {93/* The float32 lies in the range (0.0, min_normal16) and is rounded94* to a nearby float16 value. The result will be either zero, subnormal,95* or normal.96*/97e = 0;98m = _mesa_lroundevenf((1 << 24) * fabsf(fi.f));99}100else if (new_exp > 15) {101/* map this value to infinity */102/* m = 0; - already set */103e = 31;104}105else {106/* The float32 lies in the range107* [min_normal16, max_normal16 + max_step16)108* and is rounded to a nearby float16 value. The result will be109* either normal or infinite.110*/111e = new_exp + 15;112m = _mesa_lroundevenf(flt_m / (float) (1 << 13));113}114}115116assert(0 <= m && m <= 1024);117if (m == 1024) {118/* The float32 was rounded upwards into the range of the next exponent,119* so bump the exponent. This correctly handles the case where f32120* should be rounded up to float16 infinity.121*/122++e;123m = 0;124}125126result = (s << 15) | (e << 10) | m;127return result;128}129130uint16_t131_mesa_float_to_float16_rtz_slow(float val)132{133return _mesa_float_to_half_rtz_slow(val);134}135136/**137* Convert a 2-byte half float to a 4-byte float.138* Based on code from:139* http://www.opengl.org/discussion_boards/ubb/Forum3/HTML/008786.html140*/141float142_mesa_half_to_float_slow(uint16_t val)143{144union fi infnan;145union fi magic;146union fi f32;147148infnan.ui = 0x8f << 23;149infnan.f = 65536.0f;150magic.ui = 0xef << 23;151152/* Exponent / Mantissa */153f32.ui = (val & 0x7fff) << 13;154155/* Adjust */156f32.f *= magic.f;157/* XXX: The magic mul relies on denorms being available */158159/* Inf / NaN */160if (f32.f >= infnan.f)161f32.ui |= 0xff << 23;162163/* Sign */164f32.ui |= (uint32_t)(val & 0x8000) << 16;165166return f32.f;167}168169/**170* Convert 0.0 to 0x00, 1.0 to 0xff.171* Values outside the range [0.0, 1.0] will give undefined results.172*/173uint8_t _mesa_half_to_unorm8(uint16_t val)174{175const int m = val & 0x3ff;176const int e = (val >> 10) & 0x1f;177ASSERTED const int s = (val >> 15) & 0x1;178179/* v = round_to_nearest(1.mmmmmmmmmm * 2^(e-15) * 255)180* = round_to_nearest((1.mmmmmmmmmm * 255) * 2^(e-15))181* = round_to_nearest((1mmmmmmmmmm * 255) * 2^(e-25))182* = round_to_zero((1mmmmmmmmmm * 255) * 2^(e-25) + 0.5)183* = round_to_zero(((1mmmmmmmmmm * 255) * 2^(e-24) + 1) / 2)184*185* This happens to give the correct answer for zero/subnormals too186*/187assert(s == 0 && val <= FP16_ONE); /* check 0 <= this <= 1 */188/* (implies e <= 15, which means the bit-shifts below are safe) */189190uint32_t v = ((1 << 10) | m) * 255;191v = ((v >> (24 - e)) + 1) >> 1;192return v;193}194195/**196* Takes a uint16_t, divides by 65536, converts the infinite-precision197* result to fp16 with round-to-zero. Used by the ASTC decoder.198*/199uint16_t _mesa_uint16_div_64k_to_half(uint16_t v)200{201/* Zero or subnormal. Set the mantissa to (v << 8) and return. */202if (v < 4)203return v << 8;204205/* Count the leading 0s in the uint16_t */206#ifdef HAVE___BUILTIN_CLZ207int n = __builtin_clz(v) - 16;208#else209int n = 16;210for (int i = 15; i >= 0; i--) {211if (v & (1 << i)) {212n = 15 - i;213break;214}215}216#endif217218/* Shift the mantissa up so bit 16 is the hidden 1 bit,219* mask it off, then shift back down to 10 bits220*/221int m = ( ((uint32_t)v << (n + 1)) & 0xffff ) >> 6;222223/* (0{n} 1 X{15-n}) * 2^-16224* = 1.X * 2^(15-n-16)225* = 1.X * 2^(14-n - 15)226* which is the FP16 form with e = 14 - n227*/228int e = 14 - n;229230assert(e >= 1 && e <= 30);231assert(m >= 0 && m < 0x400);232233return (e << 10) | m;234}235236237