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

Optimization in Integer to string conversion

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Fixed
    • Icon: P4 P4
    • 9
    • 8
    • core-libs
    • b01
    • windows_7

      A DESCRIPTION OF THE REQUEST :
      In class Integer, method:

         static void getChars(int i, int index, char[] buf);

      we have an integer multiplication of a variable by the constant 52429. In the same file there a "FIX ME" comment stating that the multiplication should be replaced by the equivalent shift-add instructions.

      I am contributing the code to do that. Turns out the bit pattern of the constant is very regular and so the multiplication can be done very efficiently with shift and add.

      Here is the function to do that. You should of course inline the code in place.

      static int multBy52429(int y) {
          int z1 = (y << 3) + (y << 2);
          z1 = (z1 << 4) + z1;
          z1 = (z1 << 8) + z1;
          
          return z1 + y;
        }


      JUSTIFICATION :
      Performance reasons.


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      I tested the above using the following so correctness is guaranteed (it produces the right answer for ALL inputs).

        @Test
        public void testMultBy52429() {
          final int y = 52429;
          assertTrue(IntStream.rangeClosed(Integer.MIN_VALUE, Integer.MAX_VALUE).allMatch(i -> y * i == multBy52429(i)));
        }


      ---------- BEGIN SOURCE ----------
      See above for source code for the feature and the test.

      ---------- END SOURCE ----------

            bpb Brian Burkhalter
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: