com.google.common.primitives.UnsignedLongs Java Examples

The following examples show how to use com.google.common.primitives.UnsignedLongs. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TextFormatGetterTest.java    From heroic with Apache License 2.0 6 votes vote down vote up
@Test
public void testSpan () throws Exception {

  Random random = new Random(1234);
  SpanId generateSpanId = SpanId.generateRandomId(random);
  String spanId = UnsignedLongs.toString(spanIdToLong(generateSpanId));
  String traceId = TraceId.generateRandomId(random).toLowerBase16();

  final List<String> headers = new ArrayList<>();
  headers.add(traceId + "/" + spanId + ";o=1");
  doReturn(headers).when(request).getRequestHeader("X-Cloud-Trace-Context");

  final SpanContext spanContext = textFormat.extract(request, textFormatGetter);

  assertEquals(generateSpanId, spanContext.getSpanId());
}
 
Example #2
Source File: LongMath.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
long squareMod(long a, long m) {
  long aHi = a >>> 32; // < 2^31
  long aLo = a & 0xFFFFFFFFL; // < 2^32

  /*
   * a^2 == aHi^2 * 2^64 + aHi * aLo * 2^33 + aLo^2
   *     == (aHi^2 * 2^32 + aHi * aLo * 2) * 2^32 + aLo^2
   * We carry out this computation in modular arithmetic.  Since times2ToThe32Mod accepts any
   * unsigned long, we don't have to do a mod on every operation, only when intermediate
   * results can exceed 2^63.
   */
  long result = times2ToThe32Mod(aHi * aHi /* < 2^62 */, m); // < m < 2^63
  long hiLo = aHi * aLo * 2;
  if (hiLo < 0) {
    hiLo = UnsignedLongs.remainder(hiLo, m);
  }
  // hiLo < 2^63
  result += hiLo; // result < 2^64
  result = times2ToThe32Mod(result, m); // result < m < 2^63
  return plusMod(result, UnsignedLongs.remainder(aLo * aLo /* < 2^64 */, m), m);
}
 
Example #3
Source File: LongMath.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
long mulMod(long a, long b, long m) {
  long aHi = a >>> 32; // < 2^31
  long bHi = b >>> 32; // < 2^31
  long aLo = a & 0xFFFFFFFFL; // < 2^32
  long bLo = b & 0xFFFFFFFFL; // < 2^32

  /*
   * a * b == aHi * bHi * 2^64 + (aHi * bLo + aLo * bHi) * 2^32 + aLo * bLo.
   *       == (aHi * bHi * 2^32 + aHi * bLo + aLo * bHi) * 2^32 + aLo * bLo
   *
   * We carry out this computation in modular arithmetic. Since times2ToThe32Mod accepts any
   * unsigned long, we don't have to do a mod on every operation, only when intermediate
   * results can exceed 2^63.
   */
  long result = times2ToThe32Mod(aHi * bHi /* < 2^62 */, m); // < m < 2^63
  result += aHi * bLo; // aHi * bLo < 2^63, result < 2^64
  if (result < 0) {
    result = UnsignedLongs.remainder(result, m);
  }
  // result < 2^63 again
  result += aLo * bHi; // aLo * bHi < 2^63, result < 2^64
  result = times2ToThe32Mod(result, m); // result < m < 2^63
  return plusMod(result, UnsignedLongs.remainder(aLo * bLo /* < 2^64 */, m), m);
}
 
Example #4
Source File: LongMath.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
long mulMod(long a, long b, long m) {
  long aHi = a >>> 32; // < 2^31
  long bHi = b >>> 32; // < 2^31
  long aLo = a & 0xFFFFFFFFL; // < 2^32
  long bLo = b & 0xFFFFFFFFL; // < 2^32

  /*
   * a * b == aHi * bHi * 2^64 + (aHi * bLo + aLo * bHi) * 2^32 + aLo * bLo.
   *       == (aHi * bHi * 2^32 + aHi * bLo + aLo * bHi) * 2^32 + aLo * bLo
   *
   * We carry out this computation in modular arithmetic. Since times2ToThe32Mod accepts any
   * unsigned long, we don't have to do a mod on every operation, only when intermediate
   * results can exceed 2^63.
   */
  long result = times2ToThe32Mod(aHi * bHi /* < 2^62 */, m); // < m < 2^63
  result += aHi * bLo; // aHi * bLo < 2^63, result < 2^64
  if (result < 0) {
    result = UnsignedLongs.remainder(result, m);
  }
  // result < 2^63 again
  result += aLo * bHi; // aLo * bHi < 2^63, result < 2^64
  result = times2ToThe32Mod(result, m); // result < m < 2^63
  return plusMod(result, UnsignedLongs.remainder(aLo * bLo /* < 2^64 */, m), m);
}
 
Example #5
Source File: LongMath.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
long squareMod(long a, long m) {
  long aHi = a >>> 32; // < 2^31
  long aLo = a & 0xFFFFFFFFL; // < 2^32

  /*
   * a^2 == aHi^2 * 2^64 + aHi * aLo * 2^33 + aLo^2
   *     == (aHi^2 * 2^32 + aHi * aLo * 2) * 2^32 + aLo^2
   * We carry out this computation in modular arithmetic.  Since times2ToThe32Mod accepts any
   * unsigned long, we don't have to do a mod on every operation, only when intermediate
   * results can exceed 2^63.
   */
  long result = times2ToThe32Mod(aHi * aHi /* < 2^62 */, m); // < m < 2^63
  long hiLo = aHi * aLo * 2;
  if (hiLo < 0) {
    hiLo = UnsignedLongs.remainder(hiLo, m);
  }
  // hiLo < 2^63
  result += hiLo; // result < 2^64
  result = times2ToThe32Mod(result, m); // result < m < 2^63
  return plusMod(result, UnsignedLongs.remainder(aLo * aLo /* < 2^64 */, m), m);
}
 
Example #6
Source File: LongMath.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
long mulMod(long a, long b, long m) {
  long aHi = a >>> 32; // < 2^31
  long bHi = b >>> 32; // < 2^31
  long aLo = a & 0xFFFFFFFFL; // < 2^32
  long bLo = b & 0xFFFFFFFFL; // < 2^32

  /*
   * a * b == aHi * bHi * 2^64 + (aHi * bLo + aLo * bHi) * 2^32 + aLo * bLo.
   *       == (aHi * bHi * 2^32 + aHi * bLo + aLo * bHi) * 2^32 + aLo * bLo
   *
   * We carry out this computation in modular arithmetic. Since times2ToThe32Mod accepts any
   * unsigned long, we don't have to do a mod on every operation, only when intermediate
   * results can exceed 2^63.
   */
  long result = times2ToThe32Mod(aHi * bHi /* < 2^62 */, m); // < m < 2^63
  result += aHi * bLo; // aHi * bLo < 2^63, result < 2^64
  if (result < 0) {
    result = UnsignedLongs.remainder(result, m);
  }
  // result < 2^63 again
  result += aLo * bHi; // aLo * bHi < 2^63, result < 2^64
  result = times2ToThe32Mod(result, m); // result < m < 2^63
  return plusMod(result, UnsignedLongs.remainder(aLo * bLo /* < 2^64 */, m), m);
}
 
Example #7
Source File: LongMath.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
long squareMod(long a, long m) {
  long aHi = a >>> 32; // < 2^31
  long aLo = a & 0xFFFFFFFFL; // < 2^32

  /*
   * a^2 == aHi^2 * 2^64 + aHi * aLo * 2^33 + aLo^2
   *     == (aHi^2 * 2^32 + aHi * aLo * 2) * 2^32 + aLo^2
   * We carry out this computation in modular arithmetic.  Since times2ToThe32Mod accepts any
   * unsigned long, we don't have to do a mod on every operation, only when intermediate
   * results can exceed 2^63.
   */
  long result = times2ToThe32Mod(aHi * aHi /* < 2^62 */, m); // < m < 2^63
  long hiLo = aHi * aLo * 2;
  if (hiLo < 0) {
    hiLo = UnsignedLongs.remainder(hiLo, m);
  }
  // hiLo < 2^63
  result += hiLo; // result < 2^64
  result = times2ToThe32Mod(result, m); // result < m < 2^63
  return plusMod(result, UnsignedLongs.remainder(aLo * aLo /* < 2^64 */, m), m);
}
 
Example #8
Source File: LongMath.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
long squareMod(long a, long m) {
  long aHi = a >>> 32; // < 2^31
  long aLo = a & 0xFFFFFFFFL; // < 2^32

  /*
   * a^2 == aHi^2 * 2^64 + aHi * aLo * 2^33 + aLo^2
   *     == (aHi^2 * 2^32 + aHi * aLo * 2) * 2^32 + aLo^2
   * We carry out this computation in modular arithmetic.  Since times2ToThe32Mod accepts any
   * unsigned long, we don't have to do a mod on every operation, only when intermediate
   * results can exceed 2^63.
   */
  long result = times2ToThe32Mod(aHi * aHi /* < 2^62 */, m); // < m < 2^63
  long hiLo = aHi * aLo * 2;
  if (hiLo < 0) {
    hiLo = UnsignedLongs.remainder(hiLo, m);
  }
  // hiLo < 2^63
  result += hiLo; // result < 2^64
  result = times2ToThe32Mod(result, m); // result < m < 2^63
  return plusMod(result, UnsignedLongs.remainder(aLo * aLo /* < 2^64 */, m), m);
}
 
Example #9
Source File: LongMath.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
long mulMod(long a, long b, long m) {
  long aHi = a >>> 32; // < 2^31
  long bHi = b >>> 32; // < 2^31
  long aLo = a & 0xFFFFFFFFL; // < 2^32
  long bLo = b & 0xFFFFFFFFL; // < 2^32

  /*
   * a * b == aHi * bHi * 2^64 + (aHi * bLo + aLo * bHi) * 2^32 + aLo * bLo.
   *       == (aHi * bHi * 2^32 + aHi * bLo + aLo * bHi) * 2^32 + aLo * bLo
   *
   * We carry out this computation in modular arithmetic. Since times2ToThe32Mod accepts any
   * unsigned long, we don't have to do a mod on every operation, only when intermediate
   * results can exceed 2^63.
   */
  long result = times2ToThe32Mod(aHi * bHi /* < 2^62 */, m); // < m < 2^63
  result += aHi * bLo; // aHi * bLo < 2^63, result < 2^64
  if (result < 0) {
    result = UnsignedLongs.remainder(result, m);
  }
  // result < 2^63 again
  result += aLo * bHi; // aLo * bHi < 2^63, result < 2^64
  result = times2ToThe32Mod(result, m); // result < m < 2^63
  return plusMod(result, UnsignedLongs.remainder(aLo * bLo /* < 2^64 */, m), m);
}
 
Example #10
Source File: PortNumber.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * Returns PortNumber instance from String representation.
 *
 * @param s String representation equivalent to {@link PortNumber#toString()}
 * @return {@link PortNumber} instance
 * @throws IllegalArgumentException if given String was malformed
 */
public static PortNumber fromString(String s) {
    checkNotNull(s);
    checkArgument(!s.isEmpty(), "cannot be empty");

    if (isAsciiDecimal(s.charAt(0))) {
        // unsigned decimal string
        return portNumber(s);
    } else if (s.startsWith("[")) {
        // named PortNumber
        Matcher matcher = NAMED.matcher(s);
        checkArgument(matcher.matches(), "Invalid named PortNumber %s", s);

        String name = matcher.group("name");
        String num = matcher.group("num");
        return portNumber(UnsignedLongs.parseUnsignedLong(num), name);
    }

    // Logical
    if (s.startsWith("UNKNOWN(") && s.endsWith(")")) {
        return portNumber(s.substring("UNKNOWN(".length(), s.length() - 1));
    } else {
        return Logical.valueOf(s).instance;
    }
}
 
Example #11
Source File: CloudTraceFormat.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
@Override
public <C /*>>> extends @NonNull Object*/> void inject(
    SpanContext spanContext, C carrier, Setter<C> setter) {
  checkNotNull(spanContext, "spanContext");
  checkNotNull(setter, "setter");
  checkNotNull(carrier, "carrier");
  StringBuilder builder =
      new StringBuilder()
          .append(spanContext.getTraceId().toLowerBase16())
          .append(SPAN_ID_DELIMITER)
          .append(UnsignedLongs.toString(spanIdToLong(spanContext.getSpanId())))
          .append(TRACE_OPTION_DELIMITER)
          .append(spanContext.getTraceOptions().isSampled() ? SAMPLED : NOT_SAMPLED);

  setter.put(carrier, HEADER_NAME, builder.toString());
}
 
Example #12
Source File: CircuitId.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * Deserialize circuit id from byte string.
 *
 * @param circuitId the circuit id byte string
 * @return a Circuit Id
 */
public static CircuitId deserialize(byte[] circuitId) {
    String cIdString = new String(circuitId, StandardCharsets.US_ASCII);
    List<String> splittedCircuitId = Lists.newArrayList(cIdString.split(SEPARATOR));
    checkArgument(splittedCircuitId.size() > 1, "Illegal circuit id.");
    // remove last element (vlan id)
    String vlanId = splittedCircuitId.remove(splittedCircuitId.size() - 1);

    // Reconstruct device Id
    String connectPoint = String.join(SEPARATOR, splittedCircuitId);

    String[] splittedConnectPoint = connectPoint.split(DEVICE_PORT_SEPARATOR);
    // Check connect point is valid or not
    checkArgument(splittedConnectPoint.length == 2,
                  "Connect point must be in \"deviceUri/portNumber\" format");

    // Check the port number is a number or not
    UnsignedLongs.decode(splittedConnectPoint[1]);

    return new CircuitId(connectPoint, VlanId.vlanId(vlanId));
}
 
Example #13
Source File: CastUtils.java    From sql-layer with GNU Affero General Public License v3.0 6 votes vote down vote up
public static long parseUnsignedLong(String st, TExecutionContext context)
{
    Object truncated = CastUtils.truncateNonDigits(st, context);

    if (truncated instanceof String)
        st = (String)truncated;
    else
        st = CastUtils.truncateNonDigitPlainString(((BigDecimal)truncated).toPlainString(),
                                                   context);

    long value;
    try 
    {
        value = UnsignedLongs.parseUnsignedLong(st);
    } catch (NumberFormatException e) { // overflow error
        context.reportOverflow(e.getMessage());

        // check wether the value is too big or too small
        if (st.charAt(0) == '-')
            value = 0;
        else
            value = UnsignedLongs.MAX_VALUE;
    }
    return value;
}
 
Example #14
Source File: LongMath.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
long squareMod(long a, long m) {
  long aHi = a >>> 32; // < 2^31
  long aLo = a & 0xFFFFFFFFL; // < 2^32

  /*
   * a^2 == aHi^2 * 2^64 + aHi * aLo * 2^33 + aLo^2
   *     == (aHi^2 * 2^32 + aHi * aLo * 2) * 2^32 + aLo^2
   * We carry out this computation in modular arithmetic.  Since times2ToThe32Mod accepts any
   * unsigned long, we don't have to do a mod on every operation, only when intermediate
   * results can exceed 2^63.
   */
  long result = times2ToThe32Mod(aHi * aHi /* < 2^62 */, m); // < m < 2^63
  long hiLo = aHi * aLo * 2;
  if (hiLo < 0) {
    hiLo = UnsignedLongs.remainder(hiLo, m);
  }
  // hiLo < 2^63
  result += hiLo; // result < 2^64
  result = times2ToThe32Mod(result, m); // result < m < 2^63
  return plusMod(
      result,
      UnsignedLongs.remainder(aLo * aLo /* < 2^64 */, m),
      m);
}
 
Example #15
Source File: NoteCoordinate.java    From org.openntf.domino with Apache License 2.0 5 votes vote down vote up
public static long[] getLongsFromUnid(final CharSequence unid) throws IllegalArgumentException {
	if (unid == null)
		throw new IllegalArgumentException("null is not a valid unid");
	if (DominoUtils.isUnid(unid)) {
		long[] result = new long[2];
		String first = "0x" + unid.subSequence(0, 16);
		result[0] = UnsignedLongs.decode(first);
		String last = "0x" + unid.subSequence(16, 32);
		result[1] = UnsignedLongs.decode(last);
		return result;
	} else {
		throw new IllegalArgumentException("Cannot convert a String of length " + unid.length() + ": " + unid);
	}
}
 
Example #16
Source File: LongMath.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Returns (a * 2^32) mod m. a may be any unsigned long.
 */

private long times2ToThe32Mod(long a, long m) {
  int remainingPowersOf2 = 32;
  do {
    int shift = Math.min(remainingPowersOf2, Long.numberOfLeadingZeros(a));
    // shift is either the number of powers of 2 left to multiply a by, or the biggest shift
    // possible while keeping a in an unsigned long.
    a = UnsignedLongs.remainder(a << shift, m);
    remainingPowersOf2 -= shift;
  } while (remainingPowersOf2 > 0);
  return a;
}
 
Example #17
Source File: PortNumber.java    From onos with Apache License 2.0 5 votes vote down vote up
private String decodeLogicalPort() {
    Logical logical = LOGICAL.get().get(number);
    if (logical != null) {
        // enum name
        return logical.toString();
    }
    return String.format("UNKNOWN(%s)", UnsignedLongs.toString(number));
}
 
Example #18
Source File: LongMath.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Returns (a * 2^32) mod m. a may be any unsigned long.
 */

private long times2ToThe32Mod(long a, long m) {
  int remainingPowersOf2 = 32;
  do {
    int shift = Math.min(remainingPowersOf2, Long.numberOfLeadingZeros(a));
    // shift is either the number of powers of 2 left to multiply a by, or the biggest shift
    // possible while keeping a in an unsigned long.
    a = UnsignedLongs.remainder(a << shift, m);
    remainingPowersOf2 -= shift;
  } while (remainingPowersOf2 > 0);
  return a;
}
 
Example #19
Source File: LongMath.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Returns (a * 2^32) mod m. a may be any unsigned long.
 */

private long times2ToThe32Mod(long a, long m) {
  int remainingPowersOf2 = 32;
  do {
    int shift = Math.min(remainingPowersOf2, Long.numberOfLeadingZeros(a));
    // shift is either the number of powers of 2 left to multiply a by, or the biggest shift
    // possible while keeping a in an unsigned long.
    a = UnsignedLongs.remainder(a << shift, m);
    remainingPowersOf2 -= shift;
  } while (remainingPowersOf2 > 0);
  return a;
}
 
Example #20
Source File: CloudTraceFormat.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Override
public <C /*>>> extends @NonNull Object*/> SpanContext extract(C carrier, Getter<C> getter)
    throws SpanContextParseException {
  checkNotNull(carrier, "carrier");
  checkNotNull(getter, "getter");
  try {
    String headerStr = getter.get(carrier, HEADER_NAME);
    if (headerStr == null || headerStr.length() < MIN_HEADER_SIZE) {
      throw new SpanContextParseException("Missing or too short header: " + HEADER_NAME);
    }
    checkArgument(headerStr.charAt(TRACE_ID_SIZE) == SPAN_ID_DELIMITER, "Invalid TRACE_ID size");

    TraceId traceId = TraceId.fromLowerBase16(headerStr.subSequence(0, TRACE_ID_SIZE));
    int traceOptionsPos = headerStr.indexOf(TRACE_OPTION_DELIMITER, TRACE_ID_SIZE);
    CharSequence spanIdStr =
        headerStr.subSequence(
            SPAN_ID_START_POS, traceOptionsPos < 0 ? headerStr.length() : traceOptionsPos);
    SpanId spanId = longToSpanId(UnsignedLongs.parseUnsignedLong(spanIdStr.toString(), 10));
    TraceOptions traceOptions = OPTIONS_NOT_SAMPLED;
    if (traceOptionsPos > 0) {
      String traceOptionsStr = headerStr.substring(traceOptionsPos + TRACE_OPTION_DELIMITER_SIZE);
      if ((UnsignedInts.parseUnsignedInt(traceOptionsStr, 10) & CLOUD_TRACE_IS_SAMPLED) != 0) {
        traceOptions = OPTIONS_SAMPLED;
      }
    }
    return SpanContext.create(traceId, spanId, traceOptions, TRACESTATE_DEFAULT);
  } catch (IllegalArgumentException e) {
    throw new SpanContextParseException("Invalid input", e);
  }
}
 
Example #21
Source File: FastByteOperations.java    From dble with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Lexicographically compare two arrays.
 *
 * @param buffer1       left operand: a byte[] or null
 * @param buffer2       right operand: a byte[] or null
 * @param memoryOffset1 Where to start comparing in the left buffer (pure memory address if buffer1 is null, or relative otherwise)
 * @param memoryOffset2 Where to start comparing in the right buffer (pure memory address if buffer1 is null, or relative otherwise)
 * @param length1       How much to compare from the left buffer
 * @param length2       How much to compare from the right buffer
 * @return 0 if equal, < 0 if left is less than right, etc.
 */

public static int compareTo(Object buffer1, long memoryOffset1, int length1,
                            Object buffer2, long memoryOffset2, int length2) {
    int minLength = Math.min(length1, length2);

    /*
     * Compare 8 bytes at a time. Benchmarking shows comparing 8 bytes at a
     * time is no slower than comparing 4 bytes at a time even on 32-bit.
     * On the other hand, it is substantially faster on 64-bit.
     */
    int wordComparisons = minLength & ~7;
    for (int i = 0; i < wordComparisons; i += Longs.BYTES) {
        long lw = THE_UNSAFE.getLong(buffer1, memoryOffset1 + (long) i);
        long rw = THE_UNSAFE.getLong(buffer2, memoryOffset2 + (long) i);

        if (lw != rw) {
            if (BIG_ENDIAN) {
                return UnsignedLongs.compare(lw, rw);
            }

            return UnsignedLongs.compare(Long.reverseBytes(lw), Long.reverseBytes(rw));
        }
    }

    for (int i = wordComparisons; i < minLength; i++) {
        int b1 = THE_UNSAFE.getByte(buffer1, memoryOffset1 + i) & 0xFF;
        int b2 = THE_UNSAFE.getByte(buffer2, memoryOffset2 + i) & 0xFF;
        if (b1 != b2) {
            return b1 - b2;
        }
    }

    return length1 - length2;
}
 
Example #22
Source File: FastByteOperations.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Lexicographically compare two arrays.
 *
 * @param buffer1 left operand: a byte[] or null
 * @param buffer2 right operand: a byte[] or null
 * @param memoryOffset1 Where to start comparing in the left buffer (pure memory address if buffer1 is null, or relative otherwise)
 * @param memoryOffset2 Where to start comparing in the right buffer (pure memory address if buffer1 is null, or relative otherwise)
 * @param length1 How much to compare from the left buffer
 * @param length2 How much to compare from the right buffer
 * @return 0 if equal, < 0 if left is less than right, etc.
 */

public static int compareTo(Object buffer1, long memoryOffset1, int length1,
                     Object buffer2, long memoryOffset2, int length2)
{
    int minLength = Math.min(length1, length2);

    /*
     * Compare 8 bytes at a time. Benchmarking shows comparing 8 bytes at a
     * time is no slower than comparing 4 bytes at a time even on 32-bit.
     * On the other hand, it is substantially faster on 64-bit.
     */
    int wordComparisons = minLength & ~7;
    for (int i = 0; i < wordComparisons ; i += Longs.BYTES)
    {
        long lw = theUnsafe.getLong(buffer1, memoryOffset1 + (long) i);
        long rw = theUnsafe.getLong(buffer2, memoryOffset2 + (long) i);

        if (lw != rw)
        {
            if (BIG_ENDIAN) {
                return UnsignedLongs.compare(lw, rw);
            }

            return UnsignedLongs.compare(Long.reverseBytes(lw), Long.reverseBytes(rw));
        }
    }

    for (int i = wordComparisons ; i < minLength ; i++)
    {
        int b1 = theUnsafe.getByte(buffer1, memoryOffset1 + i) & 0xFF;
        int b2 = theUnsafe.getByte(buffer2, memoryOffset2 + i) & 0xFF;
        if (b1 != b2) {
            return b1 - b2;
        }
    }

    return length1 - length2;
}
 
Example #23
Source File: NoteCoordinate.java    From org.openntf.domino with Apache License 2.0 5 votes vote down vote up
public static byte[] getBytesFromUnid(final CharSequence unid) {
	if (unid == null)
		return null;
	if (DominoUtils.isUnid(unid)) {
		String first = "0x" + unid.subSequence(0, 16);
		long flong = UnsignedLongs.decode(first);
		byte[] fbytes = Longs.toByteArray(flong);
		String last = "0x" + unid.subSequence(16, 32);
		long llong = UnsignedLongs.decode(last);
		byte[] lbytes = Longs.toByteArray(llong);
		return Bytes.concat(fbytes, lbytes);
	} else {
		throw new IllegalArgumentException("Cannot convert a String of length " + unid.length() + ": " + unid);
	}
}
 
Example #24
Source File: NoteCoordinate.java    From org.openntf.domino with Apache License 2.0 5 votes vote down vote up
public static long getLongFromReplid(final CharSequence replid) throws IllegalArgumentException {
	if (replid == null)
		throw new IllegalArgumentException("null is not a valid replica id");
	if (DominoUtils.isReplicaId(replid)) {
		String decode = "0x" + replid.toString();
		long result = UnsignedLongs.decode(decode);
		return result;
	} else {
		throw new IllegalArgumentException("Cannot convert a String of length " + replid.length() + ": " + replid);
	}
}
 
Example #25
Source File: LongMath.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Returns (a * 2^32) mod m. a may be any unsigned long.
 */

private long times2ToThe32Mod(long a, long m) {
  int remainingPowersOf2 = 32;
  do {
    int shift = Math.min(remainingPowersOf2, Long.numberOfLeadingZeros(a));
    // shift is either the number of powers of 2 left to multiply a by, or the biggest shift
    // possible while keeping a in an unsigned long.
    a = UnsignedLongs.remainder(a << shift, m);
    remainingPowersOf2 -= shift;
  } while (remainingPowersOf2 > 0);
  return a;
}
 
Example #26
Source File: SpanContextFactory.java    From cloud-trace-java with Apache License 2.0 5 votes vote down vote up
private SpanId parseSpanId(String input) {
  try {
    return new SpanId(UnsignedLongs.parseUnsignedLong(input));
  } catch (NumberFormatException ex) {
    return SpanId.invalid();
  }
}
 
Example #27
Source File: GaService.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
private List<JSONMap> unblindValues(final List<JSONMap> values, final boolean filterAsset,
                                    final boolean isUtxo) {
    if (!isElements())
        return values;

    final List<JSONMap> result = new ArrayList<>(values.size());
    final List<byte[]> unblinded = new ArrayList<>(3);
    for (final JSONMap v : values) {
        if ((isUtxo && v.get("value") == null) ||
            (!isUtxo && v.get("commitment") != null)) {
            // Blinded value: Unblind it
            final Long value;
            unblinded.clear();
            value = Wally.asset_unblind(v.getBytes("nonce_commitment"),
                                        getBlindingPrivKey(v),
                                        v.getBytes("range_proof"),
                                        v.getBytes("commitment"),
                                        null,
                                        v.getBytes("asset_tag"),
                                        unblinded);
            final byte[] assetId = unblinded.get(0);
            if (!Arrays.equals(assetId, mAssetId)) {
                if (filterAsset)
                    continue; // Ignore
                if (!isUtxo)
                    v.mData.put("is_relevant", false); // Mark irrelevant
            }
            v.mData.put("confidential", true);
            v.mData.put("value", UnsignedLongs.toString(value));
            if (isUtxo) {
                v.putBytes("assetId", assetId);
                v.putBytes("abf", unblinded.get(1));
                v.putBytes("vbf", unblinded.get(2));
            }
        }
        result.add(v);
    }
    return result;
}
 
Example #28
Source File: OpTime.java    From mongowp with Apache License 2.0 5 votes vote down vote up
@Override
public int compareTo(OpTime obj) {
  int diff = timestamp.compareTo(obj.getTimestamp());
  if (diff != 0) {
    return diff;
  }
  return UnsignedLongs.compare(term, obj.getTerm());
}
 
Example #29
Source File: LongMath.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Returns (a * 2^32) mod m. a may be any unsigned long.
 */
private long times2ToThe32Mod(long a, long m) {
  int remainingPowersOf2 = 32;
  do {
    int shift = Math.min(remainingPowersOf2, Long.numberOfLeadingZeros(a));
    // shift is either the number of powers of 2 left to multiply a by, or the biggest shift
    // possible while keeping a in an unsigned long.
    a = UnsignedLongs.remainder(a << shift, m);
    remainingPowersOf2 -= shift;
  } while (remainingPowersOf2 > 0);
  return a;
}
 
Example #30
Source File: LongMath.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
long mulMod(long a, long b, long m) {
  long aHi = a >>> 32; // < 2^31
  long bHi = b >>> 32; // < 2^31
  long aLo = a & 0xFFFFFFFFL; // < 2^32
  long bLo = b & 0xFFFFFFFFL; // < 2^32

  /*
   * a * b == aHi * bHi * 2^64 + (aHi * bLo + aLo * bHi) * 2^32 + aLo * bLo.
   *       == (aHi * bHi * 2^32 + aHi * bLo + aLo * bHi) * 2^32 + aLo * bLo
   *
   * We carry out this computation in modular arithmetic. Since times2ToThe32Mod accepts any
   * unsigned long, we don't have to do a mod on every operation, only when intermediate
   * results can exceed 2^63.
   */
  long result = times2ToThe32Mod(aHi * bHi /* < 2^62 */, m); // < m < 2^63
  result += aHi * bLo; // aHi * bLo < 2^63, result < 2^64
  if (result < 0) {
    result = UnsignedLongs.remainder(result, m);
  }
  // result < 2^63 again
  result += aLo * bHi; // aLo * bHi < 2^63, result < 2^64
  result = times2ToThe32Mod(result, m); // result < m < 2^63
  return plusMod(
      result,
      UnsignedLongs.remainder(aLo * bLo /* < 2^64 */, m),
      m);
}