public static LongRangeSet modRange(long from, long to, long mod, long bits) {
    if (mod <= 0) throw new IllegalArgumentException();
    if (bits == 0) return empty();
    if (mod == 1 || mod > Long.SIZE) return range(from, to);
    int intMod = (int)mod;
    // Adjust from/to: they should point to minimal/maximal value which is actually allowed by mod bits
    long rotatedFrom = rotateRemainders(bits, intMod, remainder(from, intMod));
    from += Long.numberOfTrailingZeros(rotatedFrom);
    int toBit = (remainder(to, intMod) + 1) % intMod;
    long rotatedTo = rotateRemainders(bits, intMod, toBit);
    to -= intMod - (Long.SIZE - Long.numberOfLeadingZeros(rotatedTo));

    if (from > to) return empty();
    if (from == to) return new Point(from);
    // Try to reduce mod if the range is too small
    long length = to - from;
    if (length > 0 && length <= intMod / 2) {
      for (int newMod = (int)length; newMod <= intMod / 2; newMod++) {
        if (intMod % newMod == 0) {
          long newBits = 0;
          // `to` could be Long.MAX_VALUE; so `i >= from` condition is important to react on possible overflow
          for (long i = from; i >= from && i <= to; i++) {
            if (isSet(bits, remainder(i, intMod))) {
              newBits = setBit(newBits, remainder(i, newMod));
            }
          }
          intMod = newMod;
          bits = newBits;
          if (bits == 0) return empty();
          break;
        }
      }
    }
    // Try to reduce mod if bits are symmetrical
    if (intMod % 2 == 0) {
      int halfMod = intMod / 2;
      long rightHalf = extractBits(bits, 0, halfMod);
      long leftHalf = extractBits(bits, halfMod, halfMod);
      if (rightHalf == leftHalf) {
        return modRange(from, to, halfMod, leftHalf);
      }
    }
    if (Long.bitCount(bits) == intMod) return range(from, to);

    ModRange range = new ModRange(from, to, intMod, bits);
    LongRangeSet fullRange = range(from, to);
    return range.contains(fullRange) ? fullRange : range;
  }
public static LongRangeSet modRange(long from, long to, long mod, long bits)