Skip to content

Commit 9ea5948

Browse files
KanclerzPiotrigcbot
authored andcommitted
Prevent overflows with abs_diff
For signed types calculations were performed in the signed type and the overflow could occure, then result was casted to unsigned. Now one of the operands is casted to unsigned type forcing calculations to be performed on unsigned which prevents overflow.
1 parent 0487b74 commit 9ea5948

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

IGC/BiFModule/Implementation/Integer/abs_diff.cl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ uchar SPIRV_OVERLOADABLE SPIRV_OCL_BUILTIN(s_abs_diff, _i8_i8, )( char x,
1717
uchar result;
1818
if (x > y)
1919
{
20-
result = (uchar)(x - y);
20+
result = (uchar)x - y;
2121
}
2222
else
2323
{
24-
result = (uchar)(y - x);
24+
result = (uchar)y - x;
2525
}
2626
return result;
2727
}
@@ -195,11 +195,11 @@ ushort SPIRV_OVERLOADABLE SPIRV_OCL_BUILTIN(s_abs_diff, _i16_i16, )( short x,
195195
ushort result;
196196
if (x > y)
197197
{
198-
result = (ushort)(x - y);
198+
result = (ushort)x - y;
199199
}
200200
else
201201
{
202-
result = (ushort)(y - x);
202+
result = (ushort)y - x;
203203
}
204204
return result;
205205
}
@@ -373,11 +373,11 @@ uint SPIRV_OVERLOADABLE SPIRV_OCL_BUILTIN(s_abs_diff, _i32_i32, )( int x,
373373
uint result;
374374
if (x > y)
375375
{
376-
result = (uint)(x - y);
376+
result = (uint)x - y;
377377
}
378378
else
379379
{
380-
result = (uint)(y - x);
380+
result = (uint)y - x;
381381
}
382382
return result;
383383
}
@@ -551,11 +551,11 @@ ulong SPIRV_OVERLOADABLE SPIRV_OCL_BUILTIN(s_abs_diff, _i64_i64, )( long x,
551551
ulong result;
552552
if (x > y)
553553
{
554-
result = (ulong)(x - y);
554+
result = (ulong)x - y;
555555
}
556556
else
557557
{
558-
result = (ulong)(y - x);
558+
result = (ulong)y - x;
559559
}
560560
return result;
561561
}

0 commit comments

Comments
 (0)