#include "progress.hpp" #include namespace progress { namespace { std::uint32_t population(std::uint32_t word) { std::uint32_t count = 0; for (unsigned bit = 0; bit != 16; ++bit) { if (word & (1U << bit)) { ++count; } } return count; } std::uint32_t runs(std::uint32_t word) { std::uint32_t count = 0; bool inside = false; for (unsigned bit = 0; bit != 16; ++bit) { const bool set = (word & (1U << bit)) != 0; if (set && !inside) { ++count; } inside = set; } return count; } std::uint32_t leading(std::uint32_t word) { std::uint32_t count = 0; for (unsigned bit = 16; bit != 0; --bit) { if (word & (1U << (bit - 1))) { break; } ++count; } return count; } std::uint32_t trailing(std::uint32_t word) { std::uint32_t count = 0; for (unsigned bit = 0; bit != 16; ++bit) { if (word & (1U << bit)) { break; } ++count; } return count; } std::uint32_t crc_byte(std::uint32_t crc, std::uint32_t byte) { crc ^= byte << 8; for (unsigned bit = 0; bit != 8; ++bit) { const bool carry = (crc & 32768) != 0; crc = (crc << 1) & 65535; if (carry) { crc ^= 4129; } } return crc; } void observe_order(Statistics& target, std::uint32_t word) { if (target.units == 0) { target.first = word; target.minimum = word; target.maximum = word; } else { target.changes += population(target.last ^ word); if (word > target.last) { ++target.rises; } else if (word < target.last) { ++target.falls; } else { ++target.repeats; } target.minimum = std::min(target.minimum, word); target.maximum = std::max(target.maximum, word); } target.last = word; } void observe_distribution(Statistics& target, std::uint32_t word) { const WordMetrics properties = measure_word(word); target.population += properties.population; target.runs += properties.runs; target.leading += properties.leading; target.trailing += properties.trailing; target.byte_sum += properties.byte_sum; if (word % 2 == 0) { ++target.even; } else { ++target.odd; } const std::size_t bucket = word / 16384; ++target.quartiles[bucket]; } } WordMetrics measure_word(std::uint32_t word) { WordMetrics result; result.population = population(word); result.runs = runs(word); result.leading = leading(word); result.trailing = trailing(word); result.byte_sum = (word & 255) + (word >> 8); return result; } std::uint32_t crc_word(std::uint32_t crc, std::uint32_t word) { crc = crc_byte(crc, word & 255); return crc_byte(crc, word >> 8); } void accumulate(Statistics& target, std::uint32_t word) { observe_order(target, word); observe_distribution(target, word); ++target.units; target.parity ^= word; target.crc = crc_word(target.crc, word); const std::uint64_t mixed = std::uint64_t(target.digest) * 131 + word; target.digest = mixed % digest_mod; const std::uint64_t weighted = std::uint64_t(target.units) * word + target.weighted; target.weighted = weighted % digest_mod; } std::uint32_t span(const Statistics& value) { if (value.units == 0) { return 0; } return value.maximum - value.minimum; } std::uint32_t dominant_quartile(const Statistics& value) { std::uint32_t selected = 0; std::uint32_t count = value.quartiles[0]; for (std::uint32_t bucket = 1; bucket < 4; ++bucket) { if (value.quartiles[bucket] > count) { count = value.quartiles[bucket]; selected = bucket; } } return selected; } }