gen-fpioconst.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* Generate data for fpioconst.c.
  2. Copyright (C) 2012-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <inttypes.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <gmp.h>
  20. #include <stdint.h>
  21. int
  22. main (void)
  23. {
  24. FILE *out32 = fopen ("fpioconst-32", "w");
  25. if (out32 == NULL)
  26. abort ();
  27. FILE *out64 = fopen ("fpioconst-64", "w");
  28. if (out64 == NULL)
  29. abort ();
  30. FILE *outtable = fopen ("fpioconst-table", "w");
  31. if (outtable == NULL)
  32. abort ();
  33. mpz_t p;
  34. mpz_init (p);
  35. for (int i = 0; i <= 14; i++)
  36. {
  37. int j = 1 << i;
  38. mpz_ui_pow_ui (p, 10, j - 1);
  39. int exp_m = mpz_sizeinbase (p, 2);
  40. mpz_ui_pow_ui (p, 10, j);
  41. int exp_p = mpz_sizeinbase (p, 2);
  42. int size32 = 2 + (exp_p + 31) / 32;
  43. int size64 = 1 + (exp_p + 63) / 64;
  44. uint32_t data32[size32];
  45. uint64_t data64[size64];
  46. memset (data32, 0, sizeof data32);
  47. memset (data64, 0, sizeof data64);
  48. mpz_export (data32 + 2, NULL, -1, 4, 0, 0, p);
  49. mpz_export (data64 + 1, NULL, -1, 8, 0, 0, p);
  50. if (i == 0)
  51. {
  52. fprintf (out32, "#define TENS_P%d_IDX\t0\n", i);
  53. fprintf (out64, "#define TENS_P%d_IDX\t0\n", i);
  54. }
  55. else
  56. {
  57. fprintf (out32, "#define TENS_P%d_IDX\t"
  58. "(TENS_P%d_IDX + TENS_P%d_SIZE)\n",
  59. i, i - 1, i - 1);
  60. fprintf (out64, "#define TENS_P%d_IDX\t"
  61. "(TENS_P%d_IDX + TENS_P%d_SIZE)\n",
  62. i, i - 1, i - 1);
  63. }
  64. fprintf (out32, "#define TENS_P%d_SIZE\t%d\n", i, size32);
  65. fprintf (out64, "#define TENS_P%d_SIZE\t%d\n", i, size64);
  66. for (int k = 0; k < size32; k++)
  67. {
  68. if (k == 0)
  69. fprintf (out32, " [TENS_P%d_IDX] = ", i);
  70. else if (k % 6 == 5)
  71. fprintf (out32, "\n ");
  72. else
  73. fprintf (out32, " ");
  74. fprintf (out32, "0x%08"PRIx32",", data32[k]);
  75. }
  76. for (int k = 0; k < size64; k++)
  77. {
  78. if (k == 0)
  79. fprintf (out64, " [TENS_P%d_IDX] = ", i);
  80. else if (k % 3 == 2)
  81. fprintf (out64, "\n ");
  82. else
  83. fprintf (out64, " ");
  84. fprintf (out64, "0x%016"PRIx64"ull,", data64[k]);
  85. }
  86. fprintf (out32, "\n\n");
  87. fprintf (out64, "\n\n");
  88. const char *t = (i >= 10 ? "\t" : "\t\t");
  89. if (i == 0)
  90. fprintf (outtable, " { TENS_P%d_IDX, TENS_P%d_SIZE,%s%d,\t },\n",
  91. i, i, t, exp_p);
  92. else
  93. fprintf (outtable, " { TENS_P%d_IDX, TENS_P%d_SIZE,%s%d,\t%5d },\n",
  94. i, i, t, exp_p, exp_m);
  95. }
  96. if (fclose (out32) != 0)
  97. abort ();
  98. if (fclose (out64) != 0)
  99. abort ();
  100. if (fclose (outtable) != 0)
  101. abort ();
  102. return 0;
  103. }