libraries/v8/runtime.cc
// Compare two Smis as if they were converted to strings and then
// compared lexicographically.
Compare two Smis as if they were converted to strings and then
compared lexicographically.
Compare two Smis as if they were converted to strings and then compared lexicographically .
RUNTIME_FUNCTION(MaybeObject*, Runtime_SmiLexicographicCompare) {
  SealHandleScope shs(isolate);
  ASSERT(args.length() == 2);
  CONVERT_SMI_ARG_CHECKED(x_value, 0);
  CONVERT_SMI_ARG_CHECKED(y_value, 1);
  if (x_value == y_value) return Smi::FromInt(EQUAL);
  if (x_value == 0 || y_value == 0)
    return Smi::FromInt(x_value < y_value ? LESS : GREATER);
  uint32_t x_scaled = x_value;
  uint32_t y_scaled = y_value;
  if (x_value < 0 || y_value < 0) {
    if (y_value >= 0) return Smi::FromInt(LESS);
    if (x_value >= 0) return Smi::FromInt(GREATER);
    x_scaled = -x_value;
    y_scaled = -y_value;
  }
  static const uint32_t kPowersOf10[] = {
    1, 10, 100, 1000, 10*1000, 100*1000,
    1000*1000, 10*1000*1000, 100*1000*1000,
    1000*1000*1000
  };
  int x_log2 = IntegerLog2(x_scaled);
  int x_log10 = ((x_log2 + 1) * 1233) >> 12;
  x_log10 -= x_scaled < kPowersOf10[x_log10];
  int y_log2 = IntegerLog2(y_scaled);
  int y_log10 = ((y_log2 + 1) * 1233) >> 12;
  y_log10 -= y_scaled < kPowersOf10[y_log10];
  int tie = EQUAL;
  if (x_log10 < y_log10) {
    x_scaled *= kPowersOf10[y_log10 - x_log10 - 1];
    y_scaled /= 10;
    tie = LESS;
  } else if (y_log10 < x_log10) {
    y_scaled *= kPowersOf10[x_log10 - y_log10 - 1];
    x_scaled /= 10;
    tie = GREATER;
  }
  if (x_scaled < y_scaled) return Smi::FromInt(LESS);
  if (x_scaled > y_scaled) return Smi::FromInt(GREATER);
  return Smi::FromInt(tie);
}
RUNTIME_FUNCTION ( MaybeObject * , Runtime_SmiLexicographicCompare ) { SealHandleScope shs ( isolate ) ; ASSERT ( args . length ( ) == 2 ) ; CONVERT_SMI_ARG_CHECKED ( x_value , 0 ) ; CONVERT_SMI_ARG_CHECKED ( y_value , 1 ) ; if ( x_value == y_value ) return Smi :: FromInt ( EQUAL ) ; if ( x_value == 0 || y_value == 0 ) return Smi :: FromInt ( x_value < y_value ? LESS : GREATER ) ; uint32_t x_scaled = x_value ; uint32_t y_scaled = y_value ; if ( x_value < 0 || y_value < 0 ) { if ( y_value >= 0 ) return Smi :: FromInt ( LESS ) ; if ( x_value >= 0 ) return Smi :: FromInt ( GREATER ) ; x_scaled = - x_value ; y_scaled = - y_value ; } static const uint32_t kPowersOf10 [ ] = { 1 , 10 , 100 , 1000 , 10 * 1000 , 100 * 1000 , 1000 * 1000 , 10 * 1000 * 1000 , 100 * 1000 * 1000 , 1000 * 1000 * 1000 } ; int x_log2 = IntegerLog2 ( x_scaled ) ; int x_log10 = ( ( x_log2 + 1 ) * 1233 ) >> 12 ; x_log10 -= x_scaled < kPowersOf10 [ x_log10 ] ; int y_log2 = IntegerLog2 ( y_scaled ) ; int y_log10 = ( ( y_log2 + 1 ) * 1233 ) >> 12 ; y_log10 -= y_scaled < kPowersOf10 [ y_log10 ] ; int tie = EQUAL ; if ( x_log10 < y_log10 ) { x_scaled *= kPowersOf10 [ y_log10 - x_log10 - 1 ] ; y_scaled /= 10 ; tie = LESS ; } else if ( y_log10 < x_log10 ) { y_scaled *= kPowersOf10 [ x_log10 - y_log10 - 1 ] ; x_scaled /= 10 ; tie = GREATER ; } if ( x_scaled < y_scaled ) return Smi :: FromInt ( LESS ) ; if ( x_scaled > y_scaled ) return Smi :: FromInt ( GREATER ) ; return Smi :: FromInt ( tie ) ; }
Compare two Smis as if they were converted to strings and then
compared lexicographically.
Compare two Smis as if they were converted to strings and then compared lexicographically .
// If the integers are equal so are the string representations. // If one of the integers is zero the normal integer order is the // same as the lexicographic order of the string representations. // If only one of the integers is negative the negative number is // smallest because the char code of '-' is less than the char code // of any digit.  Otherwise, we make both values positive. // Use unsigned values otherwise the logic is incorrect for -MIN_INT on // architectures using 32-bit Smis. // If the integers have the same number of decimal digits they can be // compared directly as the numeric order is the same as the // lexicographic order.  If one integer has fewer digits, it is scaled // by some power of 10 to have the same number of digits as the longer // integer.  If the scaled integers are equal it means the shorter // integer comes first in the lexicographic order. // From http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog10 // X has fewer digits.  We would like to simply scale up X but that // might overflow, e.g when comparing 9 with 1_000_000_000, 9 would // be scaled up to 9_000_000_000. So we scale up by the next // smallest power and scale down Y to drop one digit. It is OK to // drop one digit from the longer integer since the final digit is // past the length of the shorter integer.