Math.abs() with char type may return incorrect result after C2 superword optimization. It can be reproduced by below Java code and commands.
public class Bug {
private static int SIZE = 60000;
private static char[] a = new char[SIZE];
private static char[] b = new char[SIZE];
public static void main(String[] args) {
for (int i = 0; i < SIZE; i++) {
a[i] = b[i] = (char) i;
}
for (int i = 0; i < SIZE; i++) {
a[i] = (char) Math.abs(a[i]);
}
for (int i = 0; i < SIZE; i++) {
if (a[i] != b[i]) {
throw new RuntimeException("Broken!");
}
}
System.out.println("OK");
}
}
// $ java -Xint Bug
// OK
// $ java -Xcomp -XX:-TieredCompilation Bug
// Exception in thread "main" java.lang.RuntimeException: Broken!
// at Bug.main(Bug.java:15)
This may cause loss of data (although in few cases) so I would change the priority to P2.
Affect versions: 13, 14, 15, 16, 17 (both x86 and AArch64) and 11u (x86-only)