printf_fp.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #include <inttypes.h>
  2. #include <math.h>
  3. #include <stdint.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #define ARRAY_SIZE(A) (sizeof(A) / sizeof(*(A)))
  7. #define FIELD(B, code) (((code) / BASE_##B) % VARS_##B)
  8. #define BASE_0 1
  9. #define VARS_0 2
  10. #define BASE_1 (BASE_0 * VARS_0)
  11. #define VARS_1 2
  12. #define BASE_2 (BASE_1 * VARS_1)
  13. #define VARS_2 2
  14. #define BASE_3 (BASE_2 * VARS_2)
  15. #define VARS_3 2
  16. #define BASE_4 (BASE_3 * VARS_3)
  17. #define VARS_4 2
  18. #define BASE_5 (BASE_4 * VARS_4)
  19. #define VARS_5 3
  20. #define BASE_6 (BASE_5 * VARS_5)
  21. #define VARS_6 5
  22. #define N_CODES (BASE_6 * VARS_6)
  23. /*
  24. * 0 1 2 3 4 5 6 7
  25. * %[#][0][-][ ][+][width][.precision]conversion
  26. *
  27. * 0, 1, 2, 3, 4: bit flags
  28. * 5: absent/narrow/wide
  29. * 6: absent/'.' is present, precision is absent/zero/small/big
  30. */
  31. static void make_format(char *fmt, int code, char conv)
  32. {
  33. *fmt++ = '%';
  34. if (FIELD(0, code))
  35. *fmt++ = '#';
  36. if (FIELD(1, code))
  37. *fmt++ = '0';
  38. if (FIELD(2, code))
  39. *fmt++ = '-';
  40. if (FIELD(3, code))
  41. *fmt++ = ' ';
  42. if (FIELD(4, code))
  43. *fmt++ = '+';
  44. switch (FIELD(5, code)) {
  45. case 0:
  46. break;
  47. case 1:
  48. *fmt++ = '1';
  49. break;
  50. case 2:
  51. *fmt++ = '4';
  52. *fmt++ = '0';
  53. break;
  54. }
  55. if (FIELD(6, code))
  56. *fmt++ = '.';
  57. switch (FIELD(6, code)) {
  58. case 0:
  59. case 1:
  60. break;
  61. case 2:
  62. *fmt++ = '0';
  63. break;
  64. case 3:
  65. *fmt++ = '1';
  66. break;
  67. case 4:
  68. *fmt++ = '2';
  69. *fmt++ = '0';
  70. break;
  71. }
  72. *fmt++ = conv;
  73. *fmt++ = 0;
  74. }
  75. #define CRC32_POLY 0xedb88320
  76. static inline uint32_t crc32(uint32_t crc, const void *p, size_t sz)
  77. {
  78. const uint8_t *data = p;
  79. while (sz --> 0) {
  80. crc ^= *data++;
  81. for (int i = 0; i < 8; ++i) {
  82. if (crc & 1)
  83. crc = (crc >> 1) ^ CRC32_POLY;
  84. else
  85. crc >>= 1;
  86. }
  87. }
  88. return crc;
  89. }
  90. static void test_float(const char *conv, const double v[], int n)
  91. {
  92. uint32_t crc = 0xffffffff;
  93. for (int i = 0; conv[i]; ++i) {
  94. for (int code = 0; code < N_CODES; ++code) {
  95. char fmt[20];
  96. char out[256];
  97. make_format(fmt, code, conv[i]);
  98. crc = crc32(crc, fmt, strlen(fmt));
  99. for (int j = 0; j < n; ++j) {
  100. snprintf(out, sizeof(out), fmt, v[j]);
  101. crc = crc32(crc, out, strlen(out));
  102. #ifdef DEBUG
  103. printf("%-20s -> %s\n", fmt, out);
  104. #endif
  105. }
  106. }
  107. }
  108. printf("CRC = %#010" PRIx32 "\n", crc);
  109. }