Path: blob/main_old/src/common/Float16ToFloat32.py
1693 views
# Copyright 2012 The ANGLE Project Authors. All rights reserved.1# Use of this source code is governed by a BSD-style license that can be2# found in the LICENSE file.3#45# This script generates a function that converts 16-bit precision floating6# point numbers to 32-bit.7# It is based on ftp://ftp.fox-toolkit.org/pub/fasthalffloatconversion.pdf.89#include "common/mathutil.h"101112def convertMantissa(i):13if i == 0:14return 015elif i < 1024:16m = i << 1317e = 018while not (m & 0x00800000):19e -= 0x0080000020m = m << 121m &= ~0x0080000022e += 0x3880000023return m | e24else:25return 0x38000000 + ((i - 1024) << 13)262728def convertExponent(i):29if i == 0:30return 031elif i in range(1, 31):32return i << 2333elif i == 31:34return 0x4780000035elif i == 32:36return 0x8000000037elif i in range(33, 63):38return 0x80000000 + ((i - 32) << 23)39else:40return 0xC7800000414243def convertOffset(i):44if i == 0 or i == 32:45return 046else:47return 1024484950print """//51// Copyright 2012 The ANGLE Project Authors. All rights reserved.52// Use of this source code is governed by a BSD-style license that can be53// found in the LICENSE file.54//5556// This file is automatically generated.5758namespace gl59{60"""6162print "const static unsigned g_mantissa[2048] = {"63for i in range(0, 2048):64print " %#010x," % convertMantissa(i)65print "};\n"6667print "const static unsigned g_exponent[64] = {"68for i in range(0, 64):69print " %#010x," % convertExponent(i)70print "};\n"7172print "const static unsigned g_offset[64] = {"73for i in range(0, 64):74print " %#010x," % convertOffset(i)75print "};\n"7677print """float float16ToFloat32(unsigned short h)78{79unsigned i32 = g_mantissa[g_offset[h >> 10] + (h & 0x3ff)] + g_exponent[h >> 10];80return bitCast<float>(i32);81}82}83"""848586