1# Copyright 2012 The ANGLE Project Authors. All rights reserved. 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4# 5 6# This script generates a function that converts 16-bit precision floating 7# point numbers to 32-bit. 8# It is based on ftp://ftp.fox-toolkit.org/pub/fasthalffloatconversion.pdf. 9 10#include "common/mathutil.h" 11 12 13def convertMantissa(i): 14 if i == 0: 15 return 0 16 elif i < 1024: 17 m = i << 13 18 e = 0 19 while not (m & 0x00800000): 20 e -= 0x00800000 21 m = m << 1 22 m &= ~0x00800000 23 e += 0x38800000 24 return m | e 25 else: 26 return 0x38000000 + ((i - 1024) << 13) 27 28 29def convertExponent(i): 30 if i == 0: 31 return 0 32 elif i in range(1, 31): 33 return i << 23 34 elif i == 31: 35 return 0x47800000 36 elif i == 32: 37 return 0x80000000 38 elif i in range(33, 63): 39 return 0x80000000 + ((i - 32) << 23) 40 else: 41 return 0xC7800000 42 43 44def convertOffset(i): 45 if i == 0 or i == 32: 46 return 0 47 else: 48 return 1024 49 50 51print """// 52// Copyright 2012 The ANGLE Project Authors. All rights reserved. 53// Use of this source code is governed by a BSD-style license that can be 54// found in the LICENSE file. 55// 56 57// This file is automatically generated. 58 59namespace gl 60{ 61""" 62 63print "const static unsigned g_mantissa[2048] = {" 64for i in range(0, 2048): 65 print " %#010x," % convertMantissa(i) 66print "};\n" 67 68print "const static unsigned g_exponent[64] = {" 69for i in range(0, 64): 70 print " %#010x," % convertExponent(i) 71print "};\n" 72 73print "const static unsigned g_offset[64] = {" 74for i in range(0, 64): 75 print " %#010x," % convertOffset(i) 76print "};\n" 77 78print """float float16ToFloat32(unsigned short h) 79{ 80 unsigned i32 = g_mantissa[g_offset[h >> 10] + (h & 0x3ff)] + g_exponent[h >> 10]; 81 return bitCast<float>(i32); 82} 83} 84""" 85