fn blockspan_ceilpos(value: f64) -> f64 { let rest = value % 1.0; if rest > 0.0 { return value - rest + 1.0; } value } fn blockspan_finite(values: vector) -> bool { let mut i = 0; while i < len(values) { let value = at(values, i); if !(value - value == 0.0) { return false; } i += 1; } true } fn blockspan_compute(offset: f64, length: f64, block: f64) -> vector { if length == 0.0 { return [offset, 0, 0]; } let stop = offset + length; if stop > 9007199254740991.0 { return []; } let first = offset - offset % block; let tail = blockspan_ceilpos(stop / block) * block; if tail > 9007199254740991.0 { return []; } [first, tail - first, offset - first] } // Expand a byte request to whole storage blocks. // Returns [aligned start, bytes to read, requested start within read]. A zero-length request incurs zero reads. Counts must remain exactly representable. pub fn blockspan_align(offset: f64, length: f64, block: f64) -> vector { if !(offset >= 0.0 && offset <= 9007199254740991.0 && offset % 1.0 == 0.0) { return []; } if !(length >= 0.0 && length <= 9007199254740991.0 && length % 1.0 == 0.0) { return []; } if !(block >= 1.0 && block <= 9007199254740991.0 && block % 1.0 == 0.0) { return []; } let result = blockspan_compute(offset, length, block); if !blockspan_finite(result) { return []; } result }