1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Copyright © 2014 Intel Corporation 3bf215546Sopenharmony_ci * 4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"), 6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation 7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the 9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions: 10bf215546Sopenharmony_ci * 11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next 12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the 13bf215546Sopenharmony_ci * Software. 14bf215546Sopenharmony_ci * 15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21bf215546Sopenharmony_ci * IN THE SOFTWARE. 22bf215546Sopenharmony_ci */ 23bf215546Sopenharmony_ci#include "brw_reg.h" 24bf215546Sopenharmony_ci 25bf215546Sopenharmony_ciunion fu { 26bf215546Sopenharmony_ci float f; 27bf215546Sopenharmony_ci unsigned u; 28bf215546Sopenharmony_ci struct { 29bf215546Sopenharmony_ci unsigned mantissa:23; 30bf215546Sopenharmony_ci unsigned exponent:8; 31bf215546Sopenharmony_ci unsigned sign:1; 32bf215546Sopenharmony_ci } s; 33bf215546Sopenharmony_ci}; 34bf215546Sopenharmony_ci 35bf215546Sopenharmony_ciint 36bf215546Sopenharmony_cibrw_float_to_vf(float f) 37bf215546Sopenharmony_ci{ 38bf215546Sopenharmony_ci union fu fu = { .f = f }; 39bf215546Sopenharmony_ci 40bf215546Sopenharmony_ci /* ±0.0f is special cased. */ 41bf215546Sopenharmony_ci if (f == 0.0f) 42bf215546Sopenharmony_ci return fu.s.sign << 7; 43bf215546Sopenharmony_ci 44bf215546Sopenharmony_ci unsigned mantissa = fu.s.mantissa >> (23 - 4); 45bf215546Sopenharmony_ci unsigned exponent = fu.s.exponent - (127 - 3); 46bf215546Sopenharmony_ci unsigned vf = (fu.s.sign << 7) | (exponent << 4) | mantissa; 47bf215546Sopenharmony_ci 48bf215546Sopenharmony_ci /* 0.125 would have had the same representation as 0.0, so reject it. */ 49bf215546Sopenharmony_ci if ((vf & 0x7f) == 0) 50bf215546Sopenharmony_ci return -1; 51bf215546Sopenharmony_ci 52bf215546Sopenharmony_ci /* Make sure the mantissa fits in 4-bits and the exponent in 3-bits. */ 53bf215546Sopenharmony_ci if (fu.u & 0x7ffff || exponent > 7) 54bf215546Sopenharmony_ci return -1; 55bf215546Sopenharmony_ci 56bf215546Sopenharmony_ci return vf; 57bf215546Sopenharmony_ci} 58bf215546Sopenharmony_ci 59bf215546Sopenharmony_cifloat 60bf215546Sopenharmony_cibrw_vf_to_float(unsigned char vf) 61bf215546Sopenharmony_ci{ 62bf215546Sopenharmony_ci union fu fu; 63bf215546Sopenharmony_ci 64bf215546Sopenharmony_ci /* ±0.0f is special cased. */ 65bf215546Sopenharmony_ci if (vf == 0x00 || vf == 0x80) { 66bf215546Sopenharmony_ci fu.u = (unsigned)vf << 24; 67bf215546Sopenharmony_ci return fu.f; 68bf215546Sopenharmony_ci } 69bf215546Sopenharmony_ci 70bf215546Sopenharmony_ci fu.s.sign = vf >> 7; 71bf215546Sopenharmony_ci fu.s.exponent = ((vf & 0x70) >> 4) + (127 - 3); 72bf215546Sopenharmony_ci fu.s.mantissa = (vf & 0xf) << (23 - 4); 73bf215546Sopenharmony_ci 74bf215546Sopenharmony_ci return fu.f; 75bf215546Sopenharmony_ci} 76