private static boolean doAddSameScale()

in kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/common/FastHiveDecimalImpl.java [6671:6772]


  private static boolean doAddSameScale(
      int leftSignum,
      long leftFast0,
      long leftFast1,
      long leftFast2,
      int rightSignum,
      long rightFast0,
      long rightFast1,
      long rightFast2,
      int scale,
      FastHiveDecimal fastResult) {

    if (leftSignum == rightSignum) {
      if (!doAddSameScaleSameSign(
          /* resultSignum */ leftSignum,
          leftFast0,
          leftFast1,
          leftFast2,
          rightFast0,
          rightFast1,
          rightFast2,
          fastResult)) {
        // Handle overflow precision issue.
        if (scale > 0) {
          if (!fastRoundFractionalHalfUp(
              fastResult.fastSignum,
              fastResult.fast0,
              fastResult.fast1,
              fastResult.fast2,
              1,
              fastResult)) {
            return false;
          }
          scale--;
        } else {
          // Overflow.
          return false;
        }
      }
      fastResult.fastScale = scale;
    } else {
      // Just compare the magnitudes (i.e. signums set to 1).
      int compareTo =
          fastCompareTo(
              1, leftFast0, leftFast1, leftFast2, 0, 1, rightFast0, rightFast1, rightFast2, 0);
      if (compareTo == 0) {
        // They cancel each other.
        fastResult.fastReset();
        return true;
      }

      if (compareTo == 1) {
        if (!doSubtractSameScaleNoUnderflow(
            /* resultSignum */ leftSignum,
            leftFast0,
            leftFast1,
            leftFast2,
            rightFast0,
            rightFast1,
            rightFast2,
            fastResult)) {
          throw new RuntimeException("Unexpected underflow");
        }
      } else {
        if (!doSubtractSameScaleNoUnderflow(
            /* resultSignum */ rightSignum,
            rightFast0,
            rightFast1,
            rightFast2,
            leftFast0,
            leftFast1,
            leftFast2,
            fastResult)) {
          throw new RuntimeException("Unexpected underflow");
        }
      }
      fastResult.fastScale = scale;
    }

    if (fastResult.fastSignum != 0) {
      final int precision = fastRawPrecision(fastResult);
      fastResult.fastIntegerDigitCount = Math.max(0, precision - fastResult.fastScale);
    }

    final int resultTrailingZeroCount =
        fastTrailingDecimalZeroCount(
            fastResult.fast0,
            fastResult.fast1,
            fastResult.fast2,
            fastResult.fastIntegerDigitCount,
            fastResult.fastScale);
    if (resultTrailingZeroCount > 0) {
      doFastScaleDown(fastResult, resultTrailingZeroCount, fastResult);
      if (fastResult.fastSignum == 0) {
        fastResult.fastScale = 0;
      } else {
        fastResult.fastScale -= resultTrailingZeroCount;
      }
    }

    return true;
  }