main.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #include <util/stream/output.h>
  2. #define POLY_16 0x1021
  3. #define POLY_32 0xEDB88320UL
  4. #define POLY_64 ULL(0xE543279765927881)
  5. static void crc16init() {
  6. ui32 CRCTAB16[256];
  7. ui32 crc;
  8. int i, j;
  9. for (i = 0; i < 256; CRCTAB16[i++] = 0xFFFF & ((crc << 8) ^ (crc >> 8)))
  10. for (crc = i, j = 8; j > 0; j--)
  11. if (crc & 1)
  12. crc = (crc >> 1) ^ POLY_16;
  13. else
  14. crc >>= 1;
  15. for (size_t k = 0; k < 256; ++k) {
  16. Cout << " ULL(" << CRCTAB16[k] << ")";
  17. if (k != 255) {
  18. Cout << ",\n";
  19. }
  20. }
  21. }
  22. static void crc32init() {
  23. ui32 CRCTAB32[256];
  24. ui32 crc;
  25. int i, j;
  26. for (i = 0; i < 256; CRCTAB32[i++] = crc)
  27. for (crc = i, j = 8; j > 0; j--)
  28. if (crc & 1)
  29. crc = (crc >> 1) ^ POLY_32;
  30. else
  31. crc >>= 1;
  32. for (size_t k = 0; k < 256; ++k) {
  33. Cout << " ULL(" << CRCTAB32[k] << ")";
  34. if (k != 255) {
  35. Cout << ",\n";
  36. }
  37. }
  38. }
  39. int main() {
  40. Cout << "static const ui32 CRCTAB16[] = {\n\n";
  41. crc16init();
  42. Cout << "\n};\n\n";
  43. Cout << "static const ui32 CRCTAB32[] = {\n\n";
  44. crc32init();
  45. Cout << "\n};\n\n";
  46. }