|
1 # Copyright (c) 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 def convertMantissa(i): |
|
11 if i == 0: |
|
12 return 0 |
|
13 elif i < 1024: |
|
14 m = i << 13 |
|
15 e = 0 |
|
16 while not (m & 0x00800000): |
|
17 e -= 0x00800000 |
|
18 m = m << 1 |
|
19 m &= ~0x00800000 |
|
20 e += 0x38800000 |
|
21 return m | e |
|
22 else: |
|
23 return 0x38000000 + ((i - 1024) << 13) |
|
24 |
|
25 def convertExponent(i): |
|
26 if i == 0: |
|
27 return 0 |
|
28 elif i in range(1, 31): |
|
29 return i << 23 |
|
30 elif i == 31: |
|
31 return 0x47800000 |
|
32 elif i == 32: |
|
33 return 0x80000000 |
|
34 elif i in range(33, 63): |
|
35 return 0x80000000 + ((i - 32) << 23) |
|
36 else: |
|
37 return 0xC7800000 |
|
38 |
|
39 def convertOffset(i): |
|
40 if i == 0 or i == 32: |
|
41 return 0 |
|
42 else: |
|
43 return 1024 |
|
44 |
|
45 print """// |
|
46 // Copyright (c) 2012 The ANGLE Project Authors. All rights reserved. |
|
47 // Use of this source code is governed by a BSD-style license that can be |
|
48 // found in the LICENSE file. |
|
49 // |
|
50 |
|
51 // This file is automatically generated. |
|
52 |
|
53 namespace gl |
|
54 { |
|
55 """ |
|
56 |
|
57 print "const static unsigned g_mantissa[2048] = {" |
|
58 for i in range(0, 2048): |
|
59 print " %#010x," % convertMantissa(i) |
|
60 print "};\n" |
|
61 |
|
62 print "const static unsigned g_exponent[64] = {" |
|
63 for i in range(0, 64): |
|
64 print " %#010x," % convertExponent(i) |
|
65 print "};\n" |
|
66 |
|
67 print "const static unsigned g_offset[64] = {" |
|
68 for i in range(0, 64): |
|
69 print " %#010x," % convertOffset(i) |
|
70 print "};\n" |
|
71 |
|
72 print """float float16ToFloat32(unsigned short h) |
|
73 { |
|
74 unsigned i32 = g_mantissa[g_offset[h >> 10] + (h & 0x3ff)] + g_exponent[h >> 10]; |
|
75 return *(float*) &i32; |
|
76 } |
|
77 } |
|
78 """ |