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