Hacker News new | ask | show | jobs
by foldr 14 days ago
>do you see any reason that std::optional<uint8_t> max; couldn't just be a regular uint8_t?

It's so that

    if (max)
is truthy iff max has been assigned (including the case where it's been assigned zero).

I don't know if it's possible for max to be assigned zero in this specific context, or what the correct behavior is if max==0. However, the good thing about this code is that it clearly communicates that the intended check is 'has been assigned to' rather than 'is nonzero'.

The function, for reference:

    void WasmShuffleAnalyzer::TryReduceFromMSB(OpIndex input,
                                               const Simd128ShuffleOp& shuffle,
                                               uint8_t lower_limit,
                                               uint8_t upper_limit) {
      DemandedBytes demanded = GetDemandedBytes(&shuffle);
      std::optional<uint8_t> max = {};
      for (unsigned i = 0; i < demanded.bytes(); ++i) {
        uint8_t index = shuffle.shuffle[i];
        if (index >= lower_limit && index <= upper_limit) {
          max = std::max(static_cast<uint8_t>(index % kSimd128Size),
                         max.value_or(uint8_t{0}));
        }
      }
      if (max) {
        // input can be reduced.
        TRACE("Can reduce Op %d based upon max used index: %d\n", input.id(),
              max.value());
        demanded_byte_analysis_.Add(
            input, DemandedBytes::LowFromMaxShuffleIndex(max.value()));
      }
    }