Optimized CRC Computer

| Home | Libraries | People | FAQ | More

## Optimized CRC Computer

    #include <boost/cstdint.hpp>  // for boost::uintmax_t
    #include <cstddef>            // for std::size_t

    namespace boost
    {
        template < std::size_t Bits, uintmax_t TruncPoly, uintmax_t InitRem,
         uintmax_t FinalXor, bool ReflectIn, bool ReflectRem >
        class crc_optimal;
    }

The `boost::crc_optimal` class template acts as an unaugmented-CRC processor that can accept input at the byte-level. It takes all the Rocksoft™ Model CRC Algorithm parameters as template parameters. Like in `crc_basic`, the WIDTH stays as the first parameter and determines the built-in unsigned integer type used for division registers. The other Rocksoft™ Model CRC Algorithm parameters move up to the template parameter field in the same relative order they were in the `crc_basic` constructor. (Some parameters have defaults.) Objects based from `crc_optimal` can either be default-constructed, giving it the same behavior as a `crc_basic` object with the equivalent settings, or use one parameter, which overrides the initial remainder value[2] without permanently affecting the initial-remainder attribute.

Besides template parameters and construction, `crc_optimal` differs from `crc_basic` interface-wise by:

- Adding five class-static immutable data members corresponding to the new template parameters.

  Table 9.3. Additional RMCA Expressions in boost::crc_optimal

  New Member | Equivalent
  --- | ---
  `truncated_polynominal` | `get_truncated_polynominal`
  `initial_remainder` | `get_initial_remainder`
  `reflect_input` | `get_reflect_input`
  `reflect_remainder` | `get_reflect_remainder`
  `final_xor_value` | `get_final_xor_value`

- Removing the `process_bit` and `process_bits` member functions.
- Adding two versions of the `operator ()` member function. The single-argument version forwards to `process_byte`, making it suitable to STL algorithms that take (and possibly return) function objects[3]. The argument-less version forwards to `checksum`, making it suitable for STL algorithms that expect generator objects[4].
- Merging the two `reset` member functions into one. (It uses a single parameter that can have a default argument).

The major difference between `crc_basic` and `crc_optimal` is the internals. Objects from `crc_basic` run their CRC algorithm one bit at a time, no matter which unit is used as input. Objects from `crc_optimal`, when WIDTH is at least `CHAR_BIT`[5], use a byte-indexed table-driven CRC algorithm which is a lot faster than processing individual bits.

Since all of the parameters are specified at compile-time, you cannot reuse a single computer object for separate runs with differing parameters. The type uses the automatically-defined copy/move/assign and destruction routines.

[2] i.e. The interim-remainder before any input is read.

[3] Like `std::for_each`.

[4] Albeit this object won't change its return value within code that only uses it as a generator.

[5] i.e. The optimizations are suspended if the WIDTH only justifies using part of a single byte.