1 // Copyright 2016 The SwiftShader Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #ifndef sw_Half_hpp
16 #define sw_Half_hpp
17
18 #include "Math.hpp"
19
20 #include <algorithm>
21 #include <cmath>
22
23 namespace sw {
24
25 class half
26 {
27 public:
28 half() = default;
29 explicit half(float f);
30
31 operator float() const;
32
33 half &operator=(float f);
34
35 private:
36 unsigned short fp16i;
37 };
38
shortAsHalf(short s)39 inline half shortAsHalf(short s)
40 {
41 union
42 {
43 half h;
44 short s;
45 } hs;
46
47 hs.s = s;
48
49 return hs.h;
50 }
51
52 class RGB9E5
53 {
54 unsigned int R : 9;
55 unsigned int G : 9;
56 unsigned int B : 9;
57 unsigned int E : 5;
58
59 public:
RGB9E5(const float rgb[3])60 RGB9E5(const float rgb[3])
61 : RGB9E5(rgb[0], rgb[1], rgb[2])
62 {
63 }
64
RGB9E5(float r,float g,float b)65 RGB9E5(float r, float g, float b)
66 {
67 // Vulkan 1.1.117 section 15.2.1 RGB to Shared Exponent Conversion
68
69 // B is the exponent bias (15)
70 constexpr int g_sharedexp_bias = 15;
71
72 // N is the number of mantissa bits per component (9)
73 constexpr int g_sharedexp_mantissabits = 9;
74
75 // Emax is the maximum allowed biased exponent value (31)
76 constexpr int g_sharedexp_maxexponent = 31;
77
78 constexpr float g_sharedexp_max =
79 ((static_cast<float>(1 << g_sharedexp_mantissabits) - 1) /
80 static_cast<float>(1 << g_sharedexp_mantissabits)) *
81 static_cast<float>(1 << (g_sharedexp_maxexponent - g_sharedexp_bias));
82
83 // Clamp components to valid range. NaN becomes 0.
84 const float red_c = std::min(!(r > 0) ? 0 : r, g_sharedexp_max);
85 const float green_c = std::min(!(g > 0) ? 0 : g, g_sharedexp_max);
86 const float blue_c = std::min(!(b > 0) ? 0 : b, g_sharedexp_max);
87
88 // We're reducing the mantissa to 9 bits, so we must round up if the next
89 // bit is 1. In other words add 0.5 to the new mantissa's position and
90 // allow overflow into the exponent so we can scale correctly.
91 constexpr int half = 1 << (23 - g_sharedexp_mantissabits);
92 const float red_r = bit_cast<float>(bit_cast<int>(red_c) + half);
93 const float green_r = bit_cast<float>(bit_cast<int>(green_c) + half);
94 const float blue_r = bit_cast<float>(bit_cast<int>(blue_c) + half);
95
96 // The largest component determines the shared exponent. It can't be lower
97 // than 0 (after bias subtraction) so also limit to the mimimum representable.
98 constexpr float min_s = 0.5f / (1 << g_sharedexp_bias);
99 float max_s = std::max(std::max(red_r, green_r), std::max(blue_r, min_s));
100
101 // Obtain the reciprocal of the shared exponent by inverting the bits,
102 // and scale by the new mantissa's size. Note that the IEEE-754 single-precision
103 // format has an implicit leading 1, but this shared component format does not.
104 float scale = bit_cast<float>((bit_cast<int>(max_s) & 0x7F800000) ^ 0x7F800000) * (1 << (g_sharedexp_mantissabits - 2));
105
106 R = static_cast<unsigned int>(round(red_c * scale));
107 G = static_cast<unsigned int>(round(green_c * scale));
108 B = static_cast<unsigned int>(round(blue_c * scale));
109 E = (bit_cast<unsigned int>(max_s) >> 23) - 127 + 15 + 1;
110 }
111
operator unsigned int() const112 operator unsigned int() const
113 {
114 return *reinterpret_cast<const unsigned int *>(this);
115 }
116
toRGB16F(half rgb[3]) const117 void toRGB16F(half rgb[3]) const
118 {
119 constexpr int offset = 24; // Exponent bias (15) + number of mantissa bits per component (9) = 24
120
121 const float factor = (1u << E) * (1.0f / (1 << offset));
122 rgb[0] = half(R * factor);
123 rgb[1] = half(G * factor);
124 rgb[2] = half(B * factor);
125 }
126 };
127
128 class R11G11B10F
129 {
130 public:
R11G11B10F(const float rgb[3])131 R11G11B10F(const float rgb[3])
132 {
133 R = float32ToFloat11(rgb[0]);
134 G = float32ToFloat11(rgb[1]);
135 B = float32ToFloat10(rgb[2]);
136 }
137
operator unsigned int() const138 operator unsigned int() const
139 {
140 return *reinterpret_cast<const unsigned int *>(this);
141 }
142
toRGB16F(half rgb[3]) const143 void toRGB16F(half rgb[3]) const
144 {
145 rgb[0] = float11ToFloat16(R);
146 rgb[1] = float11ToFloat16(G);
147 rgb[2] = float10ToFloat16(B);
148 }
149
float11ToFloat16(unsigned short fp11)150 static inline half float11ToFloat16(unsigned short fp11)
151 {
152 return shortAsHalf(fp11 << 4); // Sign bit 0
153 }
154
float10ToFloat16(unsigned short fp10)155 static inline half float10ToFloat16(unsigned short fp10)
156 {
157 return shortAsHalf(fp10 << 5); // Sign bit 0
158 }
159
float32ToFloat11(float fp32)160 static inline unsigned short float32ToFloat11(float fp32)
161 {
162 const unsigned int float32MantissaMask = 0x7FFFFF;
163 const unsigned int float32ExponentMask = 0x7F800000;
164 const unsigned int float32SignMask = 0x80000000;
165 const unsigned int float32ValueMask = ~float32SignMask;
166 const unsigned int float32ExponentFirstBit = 23;
167 const unsigned int float32ExponentBias = 127;
168
169 const unsigned short float11Max = 0x7BF;
170 const unsigned short float11MantissaMask = 0x3F;
171 const unsigned short float11ExponentMask = 0x7C0;
172 const unsigned short float11BitMask = 0x7FF;
173 const unsigned int float11ExponentBias = 14;
174
175 const unsigned int float32Maxfloat11 = 0x477E0000;
176 const unsigned int float32MinNormfloat11 = 0x38800000;
177 const unsigned int float32MinDenormfloat11 = 0x35000080;
178
179 const unsigned int float32Bits = *reinterpret_cast<unsigned int *>(&fp32);
180 const bool float32Sign = (float32Bits & float32SignMask) == float32SignMask;
181
182 unsigned int float32Val = float32Bits & float32ValueMask;
183
184 if((float32Val & float32ExponentMask) == float32ExponentMask)
185 {
186 // INF or NAN
187 if((float32Val & float32MantissaMask) != 0)
188 {
189 return float11ExponentMask |
190 (((float32Val >> 17) | (float32Val >> 11) | (float32Val >> 6) | (float32Val)) &
191 float11MantissaMask);
192 }
193 else if(float32Sign)
194 {
195 // -INF is clamped to 0 since float11 is positive only
196 return 0;
197 }
198 else
199 {
200 return float11ExponentMask;
201 }
202 }
203 else if(float32Sign)
204 {
205 // float11 is positive only, so clamp to zero
206 return 0;
207 }
208 else if(float32Val > float32Maxfloat11)
209 {
210 // The number is too large to be represented as a float11, set to max
211 return float11Max;
212 }
213 else if(float32Val < float32MinDenormfloat11)
214 {
215 // The number is too small to be represented as a denormalized float11, set to 0
216 return 0;
217 }
218 else
219 {
220 if(float32Val < float32MinNormfloat11)
221 {
222 // The number is too small to be represented as a normalized float11
223 // Convert it to a denormalized value.
224 const unsigned int shift = (float32ExponentBias - float11ExponentBias) -
225 (float32Val >> float32ExponentFirstBit);
226 float32Val =
227 ((1 << float32ExponentFirstBit) | (float32Val & float32MantissaMask)) >> shift;
228 }
229 else
230 {
231 // Rebias the exponent to represent the value as a normalized float11
232 float32Val += 0xC8000000;
233 }
234
235 return ((float32Val + 0xFFFF + ((float32Val >> 17) & 1)) >> 17) & float11BitMask;
236 }
237 }
238
float32ToFloat10(float fp32)239 static inline unsigned short float32ToFloat10(float fp32)
240 {
241 const unsigned int float32MantissaMask = 0x7FFFFF;
242 const unsigned int float32ExponentMask = 0x7F800000;
243 const unsigned int float32SignMask = 0x80000000;
244 const unsigned int float32ValueMask = ~float32SignMask;
245 const unsigned int float32ExponentFirstBit = 23;
246 const unsigned int float32ExponentBias = 127;
247
248 const unsigned short float10Max = 0x3DF;
249 const unsigned short float10MantissaMask = 0x1F;
250 const unsigned short float10ExponentMask = 0x3E0;
251 const unsigned short float10BitMask = 0x3FF;
252 const unsigned int float10ExponentBias = 14;
253
254 const unsigned int float32Maxfloat10 = 0x477C0000;
255 const unsigned int float32MinNormfloat10 = 0x38800000;
256 const unsigned int float32MinDenormfloat10 = 0x35800040;
257
258 const unsigned int float32Bits = *reinterpret_cast<unsigned int *>(&fp32);
259 const bool float32Sign = (float32Bits & float32SignMask) == float32SignMask;
260
261 unsigned int float32Val = float32Bits & float32ValueMask;
262
263 if((float32Val & float32ExponentMask) == float32ExponentMask)
264 {
265 // INF or NAN
266 if((float32Val & float32MantissaMask) != 0)
267 {
268 return float10ExponentMask |
269 (((float32Val >> 18) | (float32Val >> 13) | (float32Val >> 3) | (float32Val)) &
270 float10MantissaMask);
271 }
272 else if(float32Sign)
273 {
274 // -INF is clamped to 0 since float10 is positive only
275 return 0;
276 }
277 else
278 {
279 return float10ExponentMask;
280 }
281 }
282 else if(float32Sign)
283 {
284 // float10 is positive only, so clamp to zero
285 return 0;
286 }
287 else if(float32Val > float32Maxfloat10)
288 {
289 // The number is too large to be represented as a float10, set to max
290 return float10Max;
291 }
292 else if(float32Val < float32MinDenormfloat10)
293 {
294 // The number is too small to be represented as a denormalized float10, set to 0
295 return 0;
296 }
297 else
298 {
299 if(float32Val < float32MinNormfloat10)
300 {
301 // The number is too small to be represented as a normalized float10
302 // Convert it to a denormalized value.
303 const unsigned int shift = (float32ExponentBias - float10ExponentBias) -
304 (float32Val >> float32ExponentFirstBit);
305 float32Val =
306 ((1 << float32ExponentFirstBit) | (float32Val & float32MantissaMask)) >> shift;
307 }
308 else
309 {
310 // Rebias the exponent to represent the value as a normalized float10
311 float32Val += 0xC8000000;
312 }
313
314 return ((float32Val + 0x1FFFF + ((float32Val >> 18) & 1)) >> 18) & float10BitMask;
315 }
316 }
317
318 private:
319 unsigned int R : 11;
320 unsigned int G : 11;
321 unsigned int B : 10;
322 };
323
324 } // namespace sw
325
326 #endif // sw_Half_hpp
327