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

Simplify Java implementation of Integer/Long.numberOfTrailingZeros()

    Details

    • Type: Enhancement
    • Status: In Progress
    • Priority: P5
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: core-libs
    • Labels:
    • Subcomponent:
    • Understanding:
      Fix Understood

      Description

      Currently, the Java implementation of numberOfTrailingZeros() is derived from HD:

          public static int numberOfTrailingZeros(long i) {
              // HD, Figure 5-14
              int x, y;
              if (i == 0) return 64;
              int n = 63;
              y = (int)i; if (y != 0) { n = n -32; x = y; } else x = (int)(i>>>32);
              y = x <<16; if (y != 0) { n = n -16; x = y; }
              y = x << 8; if (y != 0) { n = n - 8; x = y; }
              y = x << 4; if (y != 0) { n = n - 4; x = y; }
              y = x << 2; if (y != 0) { n = n - 2; x = y; }
              return n - ((x << 1) >>> 31);
          }

      It can be simplified through re-use of numberOfLeadingZeros() as
          public static int numberOfTrailingZeros_02(int i) {
              if (i == 0) return 32;
              return 31 - numberOfLeadingZeros(i & -i);
          }

      This will reduce bytecode size, but also this shows better performance with both c1 and c2 compilers.

      Long.numberOfTrailingZeros() can also be simplified via delegating to Integer's version.
      This variant also shows better performance.

        Attachments

          Activity

            People

            • Assignee:
              igerasim Ivan Gerasimov
              Reporter:
              igerasim Ivan Gerasimov
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated: