futoContextHash function

BigInt futoContextHash(
  1. String word
)

64-bit ContextLM hash of word's UTF-8 bytes.

Implementation

BigInt futoContextHash(String word) {
  final bytes = utf8.encode(word);
  final n = bytes.length;
  BigInt read(int start, int width) {
    var value = BigInt.zero;
    for (var i = 0; i < width; i++) {
      value |= BigInt.from(bytes[start + i]) << (8 * i);
    }
    return value;
  }

  var state = _mix(_constants[0], _constants[1]);
  var left = BigInt.zero, right = BigInt.zero;
  if (n <= 16) {
    if (n >= 4) {
      final step = (n ~/ 8) * 4;
      left = (read(0, 4) << 32) | read(step, 4);
      right = (read(n - 4, 4) << 32) | read(n - 4 - step, 4);
    } else if (n > 0) {
      left =
          (BigInt.from(bytes.first) << 16) |
          (BigInt.from(bytes[n ~/ 2]) << 8) |
          BigInt.from(bytes.last);
    }
  } else {
    final lanes = [state, state, state];
    var offset = 0;
    if (n > 48) {
      while (offset + 48 <= n) {
        for (var lane = 0; lane < 3; lane++) {
          final p = offset + lane * 16;
          lanes[lane] = _mix(
            read(p, 8) ^ _constants[lane + 1],
            read(p + 8, 8) ^ lanes[lane],
          );
        }
        offset += 48;
      }
    }
    while (offset + 16 <= n) {
      lanes[0] = _mix(
        read(offset, 8) ^ _constants[1],
        read(offset + 8, 8) ^ lanes[0],
      );
      offset += 16;
    }
    state = n > 48 ? lanes[0] ^ lanes[1] ^ lanes[2] : lanes[0];
    left = read(n - 16, 8);
    right = read(n - 8, 8);
  }
  return _mix(
    _constants[1] ^ BigInt.from(n),
    _mix(left ^ _constants[1], right ^ state),
  );
}