Skip to content

Commit

Permalink
bf_crc: Add final XOR and refout to example impl
Browse files Browse the repository at this point in the history
This commit makes the following changes to the example implementation of
the CRC.
1. Fix the refin bug when `FEEDING` is wrongly set as `STANDARD_FEEDING`
when reflected input is activated under the feeding type of `AUTO`.
2. Add refout by adding the function `reflect_output` when reflected
output is activated.
3. Add final xor in the crc function before returning the crc.

Signed-off-by: Solomon Tan <solomonbstoner@yahoo.com.au>
  • Loading branch information
solomonbstoner committed Jun 12, 2021
1 parent d32abea commit bba13bc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
26 changes: 21 additions & 5 deletions bf_crc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,18 @@ uint64_t bf_crc::get_delta_time_in_ms(struct timeval const& start) {
return (end.tv_sec*1000 + end.tv_usec/1000.0) - (start.tv_sec*1000 + start.tv_usec/1000.0);
}

std::string bf_crc::show_reflect_output (void) {
return "uint32_t reflect_output(uint32_t x ) {\n"
" uint32_t reflection = 0;\n"
" uint32_t const one = 1;\n"
" uint32_t width_ = 16;\n"
" for( uint32_t i = 0 ; i < width_ ; ++i, x >>= 1 ) {\n"
" if ( x & one )\n"
" reflection |= ( one << (width_ - 1u - i) );\n"
" }\n"
" return reflection;\n"
"}\n";
}

void bf_crc::show_hit(crc_model_t model, std::vector<test_vector_t> test_vectors) {

Expand All @@ -167,8 +179,7 @@ void bf_crc::show_hit(crc_model_t model, std::vector<test_vector_t> test_vectors

// print an implementation

if(!model.reflected_output && !model.reflected_input &&
(model.feed_type == my_crc_basic::BYTEWISE_REVERSED || model.feed_type == my_crc_basic::AUTO) &&
if((model.feed_type == my_crc_basic::BYTEWISE_REVERSED || model.feed_type == my_crc_basic::AUTO) &&
(test_vectors.size() > 0) &&
(test_vectors[0].message.size() % 8 == 0)) {

Expand All @@ -179,9 +190,10 @@ void bf_crc::show_hit(crc_model_t model, std::vector<test_vector_t> test_vectors
<< std::endl
<< "#define STANDARD_FEEDING (7-i)" << std::endl
<< "#define BYTEWISE_REVERSE_FEEDING (i)" << std::endl
<< "#define FEEDING " << (model.feed_type == my_crc_basic::BYTEWISE_REVERSED ? "BYTEWISE_REVERSE_FEEDING" : "STANDARD_FEEDING") << std::endl
<< "#define FEEDING " << (model.reflected_input == true ? "BYTEWISE_REVERSE_FEEDING" : "STANDARD_FEEDING") << std::endl
<< std::endl
<< "uint16_t calc_crc(uint16_t poly, uint16_t initial, uint8_t * buf, unsigned int len) {" << std::endl
<< (model.reflected_output == true ? show_reflect_output() : std::string("")) << std::endl
<< "uint16_t calc_crc(uint16_t poly, uint16_t initial, uint16_t final_xor, uint8_t * buf, unsigned int len) {" << std::endl
<< " uint16_t crc = initial;" << std::endl
<< " unsigned int i, k;" << std::endl
<< "" << std::endl
Expand All @@ -193,6 +205,8 @@ void bf_crc::show_hit(crc_model_t model, std::vector<test_vector_t> test_vectors
<< " if(c15 ^ bit) crc ^= poly;" << std::endl
<< " }" << std::endl
<< " }" << std::endl
<< (model.reflected_output == true ? " crc = reflect_output(crc);" : "") << std::endl
<< " crc ^= final_xor;" << std::endl
<< " return crc;" << std::endl
<< "}" << std::endl
<< std::endl
Expand All @@ -201,7 +215,9 @@ void bf_crc::show_hit(crc_model_t model, std::vector<test_vector_t> test_vectors
<< " uint8_t buf[] = { " << bitset_to_byte_array(test_vectors[0].message) << "};" << std::endl
<< " uint16_t crc = " << (boost::format("0x%04x") % static_cast<int>(test_vectors[0].crc)) << ";" << std::endl
<< " " << std::endl
<< " if(calc_crc( "<< (boost::format("0x%04x") % static_cast<int>(model.polynomial)) << ", " << (boost::format("0x%04x") % static_cast<int>(model.initial)) << ", buf, sizeof(buf)) == crc) puts(\"CRC matches\");" << std::endl
<< " if(calc_crc( "<< (boost::format("0x%04x") % static_cast<int>(model.polynomial)) << ", "
<< (boost::format("0x%04x") % static_cast<int>(model.initial)) << ", "
<< (boost::format("0x%04x") % static_cast<int>(model.final_xor)) << ", buf, sizeof(buf)) == crc) puts(\"CRC matches\");" << std::endl
<< " else puts(\"CRC does not match\");" << std::endl
<< " " << std::endl
<< "}" << std::endl;
Expand Down
1 change: 1 addition & 0 deletions bf_crc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,7 @@ class bf_crc {

uint64_t get_delta_time_in_ms(struct timeval const& start);
void show_hit(crc_model_t model, std::vector<test_vector_t> test_vectors);
std::string show_reflect_output (void);
void print_stats(void);
std::string feed_type_to_str(my_crc_basic::FEED_TYPE feed_type);

Expand Down

0 comments on commit bba13bc

Please sign in to comment.