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

Optimization in Integer to string conversion

    XMLWordPrintable

    Details

    • Type: Enhancement
    • Status: Resolved
    • Priority: P4
    • Resolution: Fixed
    • Affects Version/s: 8
    • Fix Version/s: 9
    • Component/s: core-libs
    • Labels:
    • Subcomponent:
    • Resolved In Build:
      b01
    • OS:
      windows_7

      Description

      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 ----------

        Attachments

          Activity

            People

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

              Dates

              Created:
              Updated:
              Resolved: