writeFp32Array method

void writeFp32Array(
  1. int id,
  2. List<double> values, [
  3. int? count
])

Writes an array of fp32 values (CORELIB_PLAN §4.8) — the first count of values, all of them when omitted: a single shared fixlen_word, then count × 4 little-endian bytes. The word is present even when empty so an empty fp32 array stays distinct from an empty fp64 array on the wire.

Implementation

void writeFp32Array(int id, List<double> values, [int? count]) {
  _writeHeader(id, WireType.arrayFixlen);
  final n = _countOf(count, values.length);
  _writeVarint(n);
  _writeVarint((4 << 3) | FixlenType.fp32);
  // Bit-exact fast path: a Float32List already holds the raw 32-bit elements,
  // so on a little-endian host its bytes are exactly the wire bytes — copy
  // them straight out with no widening to a double, so a signaling NaN
  // survives (§4.6). This is also faster than a per-element float write.
  if (values is Float32List && Endian.host == Endian.little) {
    _writeRaw(Uint8List.sublistView(values), 0, n * 4);
    return;
  }
  var p = _pos;
  if (p + n * 4 <= _buf.length) {
    final bd = _bufData;
    for (var k = 0; k < n; k++) {
      bd.setFloat32(p, values[k], Endian.little);
      p += 4;
    }
    _pos = p;
  } else {
    for (var k = 0; k < n; k++) {
      _putFloat32(values[k]);
    }
  }
}