1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 by Southwest Research Institute (R)
4 ** Copyright (C) 2019 Intel Corporation.
5 ** Contact: http://www.qt-project.org/legal
6 **
7 ** This file is part of the QtCore module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** Commercial License Usage
11 ** Licensees holding valid commercial Qt licenses may use this file in
12 ** accordance with the commercial license agreement provided with the
13 ** Software or, alternatively, in accordance with the terms contained in
14 ** a written agreement between you and The Qt Company. For licensing terms
15 ** and conditions see https://www.qt.io/terms-conditions. For further
16 ** information use the contact form at https://www.qt.io/contact-us.
17 **
18 ** GNU Lesser General Public License Usage
19 ** Alternatively, this file may be used under the terms of the GNU Lesser
20 ** General Public License version 3 as published by the Free Software
21 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
22 ** packaging of this file. Please review the following information to
23 ** ensure the GNU Lesser General Public License version 3 requirements
24 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
25 **
26 ** GNU General Public License Usage
27 ** Alternatively, this file may be used under the terms of the GNU
28 ** General Public License version 2.0 or (at your option) the GNU General
29 ** Public license version 3 or any later version approved by the KDE Free
30 ** Qt Foundation. The licenses are as published by the Free Software
31 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
32 ** included in the packaging of this file. Please review the following
33 ** information to ensure the GNU General Public License requirements will
34 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
35 ** https://www.gnu.org/licenses/gpl-3.0.html.
36 **
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40
41 #include <stdint.h>
42 #include <stdio.h>
43
44 /*
45 * This tool generates the tables used by qfloat16 to implement a
46 * software-emulated version of IEEE 754 binary16. qfloat16 automatically uses
47 * CPU instructions to convert to and from float (IEEE 754 binary32), but if
48 * the CPU is not guaranteed to have those instructions available at compile
49 * time, then qfloat16 needs the tables to perform the conversion with
50 * reasonable performance.
51 *
52 * Because Qt requires float to be IEEE 754 binary32, these tables are
53 * platform-independent and will never change.
54 */
55
convertmantissa(int32_t i)56 uint32_t convertmantissa(int32_t i)
57 {
58 uint32_t m = i << 13; // Zero pad mantissa bits
59 uint32_t e = 0; // Zero exponent
60
61 while (!(m & 0x00800000)) { // While not normalized
62 e -= 0x00800000; // Decrement exponent (1<<23)
63 m <<= 1; // Shift mantissa
64 }
65 m &= ~0x00800000; // Clear leading 1 bit
66 e += 0x38800000; // Adjust bias ((127-14)<<23)
67 return m | e; // Return combined number
68 }
69
70 // we first build these tables up and then print them out as a separate step in order
71 // to more closely map the implementation given in the paper.
72 uint32_t basetable[512];
73 uint32_t shifttable[512];
74
main()75 int main()
76 {
77 uint32_t i;
78
79 printf("/* This file was generated by util/qfloat16-tables/gen_qfloat16_tables.cpp */\n\n");
80 printf("#include <QtCore/qfloat16.h>\n\n");
81
82 printf("QT_BEGIN_NAMESPACE\n\n");
83 printf("#if !defined(__ARM_FP16_FORMAT_IEEE)\n\n");
84
85 printf("const quint32 qfloat16::mantissatable[2048] = {\n");
86 printf("0,\n");
87 for (i = 1; i < 1024; i++)
88 printf("0x%XU,\n", convertmantissa(i));
89 for (i = 1024; i < 2048; i++)
90 printf("0x%XU,\n", 0x38000000U + ((i - 1024) << 13));
91 printf("};\n\n");
92
93 printf("const quint32 qfloat16::exponenttable[64] = {\n");
94 printf("0,\n");
95 for (i = 1; i < 31; i++)
96 printf("0x%XU,\n", i << 23);
97 printf("0x47800000U,\n"); // 31
98 printf("0x80000000U,\n"); // 32
99 for (i = 33; i < 63; i++)
100 printf("0x%XU,\n", 0x80000000U + ((i - 32) << 23));
101 printf("0xC7800000U,\n"); // 63
102 printf("};\n\n");
103
104 printf("const quint32 qfloat16::offsettable[64] = {\n");
105 printf("0,\n");
106 for (i = 1; i < 32; i++)
107 printf("1024U,\n");
108 printf("0,\n");
109 for (i = 33; i < 64; i++)
110 printf("1024U,\n");
111 printf("};\n\n");
112
113 int32_t e;
114 for (i = 0; i < 256; ++i) {
115 e = i - 127;
116 if (e < -24) { // Very small numbers map to zero
117 basetable[i | 0x000] = 0x0000;
118 basetable[i | 0x100] = 0x8000;
119 shifttable[i | 0x000] = 24;
120 shifttable[i | 0x100] = 24;
121
122 } else if (e < -14) { // Small numbers map to denorms
123 basetable[i | 0x000] = (0x0400 >> (-e - 14));
124 basetable[i | 0x100] = (0x0400 >> (-e - 14)) | 0x8000;
125 shifttable[i | 0x000] = -e - 1;
126 shifttable[i | 0x100] = -e - 1;
127
128 } else if (e <= 15) { // Normal numbers just lose precision
129 basetable[i | 0x000] = ((e + 15) << 10);
130 basetable[i | 0x100] = ((e + 15) << 10) | 0x8000;
131 shifttable[i | 0x000] = 13;
132 shifttable[i | 0x100] = 13;
133
134 } else if (e < 128) { // Large numbers map to Infinity
135 basetable[i | 0x000] = 0x7C00;
136 basetable[i | 0x100] = 0xFC00;
137 shifttable[i | 0x000] = 24;
138 shifttable[i | 0x100] = 24;
139
140 } else { // Infinity and NaN's stay Infinity and NaN's
141 basetable[i | 0x000] = 0x7C00;
142 basetable[i | 0x100] = 0xFC00;
143 shifttable[i | 0x000] = 13;
144 shifttable[i | 0x100] = 13;
145 }
146 }
147
148 printf("const quint32 qfloat16::basetable[512] = {\n");
149 for (i = 0; i < 512; i++)
150 printf("0x%XU,\n", basetable[i]);
151
152 printf("};\n\n");
153
154 printf("const quint32 qfloat16::shifttable[512] = {\n");
155 for (i = 0; i < 512; i++)
156 printf("0x%XU,\n", shifttable[i]);
157
158 printf("};\n\n");
159
160 printf("#endif // !__ARM_FP16_FORMAT_IEEE\n\n");
161 printf("QT_END_NAMESPACE\n");
162 return 0;
163 }
164