Skip to content

Commit 043648d

Browse files
mmereckiigcbot
authored andcommitted
Fix FP8 conversion in IGCConstantFolder
Fix `inf` and `nan` handling in FP8 conversion in `IGCConstantFolder`
1 parent 9f40a1e commit 043648d

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

IGC/common/IGCConstantFolder.cpp

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -193,40 +193,40 @@ static constexpr FloatSemantics semHF8 = { 8, -6, 4, 8, true };
193193

194194
inline uint32_t Round(
195195
uint32_t man,
196-
uint32_t manShift,
196+
uint32_t numLostBits,
197197
bool isNegative,
198198
uint32_t roundingMode)
199199
{
200-
uint32_t lostBitsMask = BITMASK(manShift);
200+
uint32_t lostBitsMask = BITMASK(numLostBits);
201201
uint32_t lostBits = man & lostBitsMask;
202-
uint32_t lostBitsHalfMinusOne = BITMASK(manShift - 1);
203-
uint32_t tieToEvenBias = (man & BIT(manShift)) >> manShift;
202+
uint32_t lostBitsHalfMinusOne = BITMASK(numLostBits - 1);
203+
uint32_t tieToEvenBias = (man & BIT(numLostBits)) >> numLostBits;
204204

205205
switch (roundingMode)
206206
{
207207
case ROUND_TO_NEAREST_EVEN:
208208
man += lostBitsHalfMinusOne + tieToEvenBias;
209-
man >>= manShift;
209+
man >>= numLostBits;
210210
break;
211211
case ROUND_TO_NEGATIVE:
212-
man >>= manShift;
212+
man >>= numLostBits;
213213
if (lostBits != 0 && isNegative)
214214
{
215215
man += 1;
216216
}
217217
break;
218218
case ROUND_TO_POSITIVE:
219-
man >>= manShift;
219+
man >>= numLostBits;
220220
if (lostBits != 0 && !isNegative)
221221
{
222222
man += 1;
223223
}
224224
break;
225225
case ROUND_TO_ZERO:
226-
man >>= manShift;
226+
man >>= numLostBits;
227227
break;
228228
default:
229-
man >>= manShift;
229+
man >>= numLostBits;
230230
IGC_ASSERT_MESSAGE(0, "Unsupported rounding mode");
231231
break;
232232
}
@@ -251,6 +251,10 @@ inline uint32_t ConvertFloat(
251251
bool isPositive = !isNegative;
252252
bool isDenorm = expBits == 0 && manBits > 0;
253253
bool isZero = expBits == 0 && manBits == 0;
254+
bool isInf = srcSem.hasNoInf ? false : (expBits == BITMASK(srcNumExpBits) && manBits == 0);
255+
bool isNan = srcSem.hasNoInf ?
256+
((BITMASK(srcSem.sizeInBits - 1) & intVal) == BITMASK(srcSem.sizeInBits - 1)) :
257+
(expBits == BITMASK(srcNumExpBits) && manBits != 0);
254258

255259
int32_t srcExpBias = 1 - srcSem.minExponent;
256260
int32_t dstExpBias = 1 - dstSem.minExponent;
@@ -271,6 +275,20 @@ inline uint32_t ConvertFloat(
271275
maxVal = signVal | (BITMASK(dstSem.sizeInBits - 1) & ~1);
272276
}
273277

278+
// Handle special cases
279+
if (isZero)
280+
{
281+
return signVal;
282+
}
283+
if (isInf)
284+
{
285+
return infVal;
286+
}
287+
if (isNan)
288+
{
289+
return nanVal;
290+
}
291+
274292
// Normalize the mantissa
275293
while ((man & BIT(srcNumManBits)) == 0)
276294
{

0 commit comments

Comments
 (0)