Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8297689

Fix incorrect result of Short.reverseBytes() call in loops

XMLWordPrintable

    • 19
    • b27
    • generic
    • generic

      Recently, we find calling `Short.reverseBytes()` in loops may generate
      incorrect result if the code is compiled by C2. Below is a simple case
      to reproduce.


      class Foo {
        static final int SIZE = 50;
        static int a[] = new int[SIZE];

        static void test() {
          for (int i = 0; i < SIZE; i++) {
            a[i] = Short.reverseBytes((short) a[i]);
          }
        }

        public static void main(String[] args) throws Exception {
          Class.forName("java.lang.Short");
          a[25] = 16;
          test();
          System.out.println(a[25]);
        }
      }

      // $ java -Xint Foo
      // 4096
      // $ java -Xcomp -XX:-TieredCompilation -XX:CompileOnly=Foo.test Foo
      // 268435456

            pli Pengfei Li
            pli Pengfei Li
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: