gfx/angle/src/libGLESv2/Float16ToFloat32.py

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/angle/src/libGLESv2/Float16ToFloat32.py	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,78 @@
     1.4 +# Copyright (c) 2012 The ANGLE Project Authors. All rights reserved.
     1.5 +# Use of this source code is governed by a BSD-style license that can be
     1.6 +# found in the LICENSE file.
     1.7 +#
     1.8 +
     1.9 +# This script generates a function that converts 16-bit precision floating
    1.10 +# point numbers to 32-bit.
    1.11 +# It is based on ftp://ftp.fox-toolkit.org/pub/fasthalffloatconversion.pdf.
    1.12 +
    1.13 +def convertMantissa(i):
    1.14 +    if i == 0:
    1.15 +        return 0
    1.16 +    elif i < 1024:
    1.17 +        m = i << 13
    1.18 +        e = 0
    1.19 +        while not (m & 0x00800000):
    1.20 +            e -= 0x00800000
    1.21 +            m = m << 1
    1.22 +        m &= ~0x00800000
    1.23 +        e += 0x38800000
    1.24 +        return m | e
    1.25 +    else:
    1.26 +        return 0x38000000 + ((i - 1024) << 13)
    1.27 +
    1.28 +def convertExponent(i):
    1.29 +    if i == 0:
    1.30 +        return 0
    1.31 +    elif i in range(1, 31):
    1.32 +        return i << 23
    1.33 +    elif i == 31:
    1.34 +        return 0x47800000
    1.35 +    elif i == 32:
    1.36 +        return 0x80000000
    1.37 +    elif i in range(33, 63):
    1.38 +        return 0x80000000 + ((i - 32) << 23)
    1.39 +    else:
    1.40 +        return 0xC7800000
    1.41 +
    1.42 +def convertOffset(i):
    1.43 +    if i == 0 or i == 32:
    1.44 +        return 0
    1.45 +    else:
    1.46 +        return 1024
    1.47 +
    1.48 +print """//
    1.49 +// Copyright (c) 2012 The ANGLE Project Authors. All rights reserved.
    1.50 +// Use of this source code is governed by a BSD-style license that can be
    1.51 +// found in the LICENSE file.
    1.52 +//
    1.53 +
    1.54 +// This file is automatically generated.
    1.55 +
    1.56 +namespace gl
    1.57 +{
    1.58 +"""
    1.59 +
    1.60 +print "const static unsigned g_mantissa[2048] = {"
    1.61 +for i in range(0, 2048):
    1.62 +    print "    %#010x," % convertMantissa(i)
    1.63 +print "};\n"
    1.64 +
    1.65 +print "const static unsigned g_exponent[64] = {"
    1.66 +for i in range(0, 64):
    1.67 +    print "    %#010x," % convertExponent(i)
    1.68 +print "};\n"
    1.69 +
    1.70 +print "const static unsigned g_offset[64] = {"
    1.71 +for i in range(0, 64):
    1.72 +    print "    %#010x," % convertOffset(i)
    1.73 +print "};\n"
    1.74 +
    1.75 +print """float float16ToFloat32(unsigned short h)
    1.76 +{
    1.77 +    unsigned i32 = g_mantissa[g_offset[h >> 10] + (h & 0x3ff)] + g_exponent[h >> 10];
    1.78 +    return *(float*) &i32;
    1.79 +}
    1.80 +}
    1.81 +"""

mercurial