| OLD | NEW |
| (Empty) |
| 1 /* Test file for l2b constants. | |
| 2 | |
| 3 Copyright 2007, 2008, 2009 Free Software Foundation, Inc. | |
| 4 Contributed by the Arenaire and Cacao projects, INRIA. | |
| 5 | |
| 6 This file is part of the GNU MPFR Library. | |
| 7 | |
| 8 The GNU MPFR Library is free software; you can redistribute it and/or modify | |
| 9 it under the terms of the GNU Lesser General Public License as published by | |
| 10 the Free Software Foundation; either version 2.1 of the License, or (at your | |
| 11 option) any later version. | |
| 12 | |
| 13 The GNU MPFR Library is distributed in the hope that it will be useful, but | |
| 14 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | |
| 15 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public | |
| 16 License for more details. | |
| 17 | |
| 18 You should have received a copy of the GNU Lesser General Public License | |
| 19 along with the GNU MPFR Library; see the file COPYING.LIB. If not, write to | |
| 20 the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, | |
| 21 MA 02110-1301, USA. */ | |
| 22 | |
| 23 /* Execute this program with an argument to generate code that initializes | |
| 24 the l2b constants. */ | |
| 25 | |
| 26 #include <stdio.h> | |
| 27 #include <stdlib.h> | |
| 28 #include "mpfr-test.h" | |
| 29 | |
| 30 /* Must be a multiple of 4 */ | |
| 31 static const int bits2use[] = {16, 32, 64, 96, 128, 256}; | |
| 32 #define size_of_bits2use ((sizeof bits2use) / sizeof bits2use[0]) | |
| 33 | |
| 34 static __mpfr_struct l2b[BASE_MAX-1][2]; | |
| 35 | |
| 36 static void | |
| 37 print_mpfr (mpfr_srcptr x, const char *name) | |
| 38 { | |
| 39 unsigned char temp[16]; /* buffer for the base-256 string */ | |
| 40 unsigned char *ptr; /* pointer to its first non-zero byte */ | |
| 41 int size; /* size of the string */ | |
| 42 int i; /* bits2use index */ | |
| 43 int j; /* output limb index */ | |
| 44 int k; /* byte index (in output limb) */ | |
| 45 int r; /* digit index, relative to ptr */ | |
| 46 | |
| 47 if (printf ("#if 0\n") < 0) | |
| 48 { fprintf (stderr, "Error in printf\n"); exit (1); } | |
| 49 for (i = 0; i < size_of_bits2use; i++) | |
| 50 { | |
| 51 if (printf ("#elif BITS_PER_MP_LIMB == %d\n" | |
| 52 "const mp_limb_t %s__tab[] = { 0x", bits2use[i], name) < 0) | |
| 53 { fprintf (stderr, "Error in printf\n"); exit (1); } | |
| 54 size = mpn_get_str (temp, 256, MPFR_MANT (x), MPFR_LIMB_SIZE (x)); | |
| 55 MPFR_ASSERTN (size <= 16); | |
| 56 ptr = temp; | |
| 57 /* Skip leading zeros. */ | |
| 58 while (*ptr == 0) | |
| 59 { | |
| 60 ptr++; | |
| 61 size--; | |
| 62 MPFR_ASSERTN (size > 0); | |
| 63 } | |
| 64 MPFR_ASSERTN (*ptr >= 128); | |
| 65 for (j = (MPFR_PREC (x) - 1) / bits2use[i]; j >= 0; j--) | |
| 66 { | |
| 67 r = j * (bits2use[i] / 8); | |
| 68 for (k = 0; k < bits2use[i] / 8; k++) | |
| 69 if (printf ("%02x", r < size ? ptr[r++] : 0) < 0) | |
| 70 { fprintf (stderr, "Error in printf\n"); exit (1); } | |
| 71 if (printf (j == 0 ? " };\n" : ", 0x") < 0) | |
| 72 { fprintf (stderr, "Error in printf\n"); exit (1); } | |
| 73 } | |
| 74 } | |
| 75 if (printf ("#endif\n\n") < 0) | |
| 76 { fprintf (stderr, "Error in printf\n"); exit (1); } | |
| 77 } | |
| 78 | |
| 79 static void | |
| 80 compute_l2b (int output) | |
| 81 { | |
| 82 mpfr_ptr p; | |
| 83 mpfr_srcptr t; | |
| 84 int beta, i; | |
| 85 int error = 0; | |
| 86 char buffer[30]; | |
| 87 | |
| 88 for (beta = 2; beta <= BASE_MAX; beta++) | |
| 89 { | |
| 90 for (i = 0; i < 2; i++) | |
| 91 { | |
| 92 p = &l2b[beta-2][i]; | |
| 93 | |
| 94 /* Compute the value */ | |
| 95 if (i == 0) | |
| 96 { | |
| 97 /* 23-bit upper approximation to log(b)/log(2) */ | |
| 98 mpfr_init2 (p, 23); | |
| 99 mpfr_set_ui (p, beta, GMP_RNDU); | |
| 100 mpfr_log2 (p, p, GMP_RNDU); | |
| 101 } | |
| 102 else | |
| 103 { | |
| 104 /* 76-bit upper approximation to log(2)/log(b) */ | |
| 105 mpfr_init2 (p, 77); | |
| 106 mpfr_set_ui (p, beta, GMP_RNDD); | |
| 107 mpfr_log2 (p, p, GMP_RNDD); | |
| 108 mpfr_ui_div (p, 1, p, GMP_RNDU); | |
| 109 } | |
| 110 | |
| 111 sprintf (buffer, "mpfr_l2b_%d_%d", beta, i); | |
| 112 if (output) | |
| 113 print_mpfr (p, buffer); | |
| 114 | |
| 115 /* Check the value */ | |
| 116 t = &__gmpfr_l2b[beta-2][i]; | |
| 117 if (t == NULL || MPFR_PREC (t) == 0 || !mpfr_equal_p (p, t)) | |
| 118 { | |
| 119 if (!output) | |
| 120 { | |
| 121 error = 1; | |
| 122 printf ("Error for constant %s\n", buffer); | |
| 123 } | |
| 124 } | |
| 125 | |
| 126 if (!output) | |
| 127 mpfr_clear (p); | |
| 128 } | |
| 129 } | |
| 130 | |
| 131 if (output) | |
| 132 { | |
| 133 if (printf ("const __mpfr_struct __gmpfr_l2b[BASE_MAX-1][2] = {\n") | |
| 134 < 0) | |
| 135 { fprintf (stderr, "Error in printf\n"); exit (1); } | |
| 136 for (beta = 2; beta <= BASE_MAX; beta++) | |
| 137 { | |
| 138 for (i = 0; i < 2; i++) | |
| 139 { | |
| 140 p = &l2b[beta-2][i]; | |
| 141 if (printf (" %c {%3d,%2d,%3ld, (mp_limb_t *) " | |
| 142 "mpfr_l2b_%d_%d__tab }%s\n", i == 0 ? '{' : ' ', | |
| 143 (int) MPFR_PREC (p), MPFR_SIGN (p), | |
| 144 (long) MPFR_GET_EXP (p), beta, i, | |
| 145 i == 0 ? "," : beta < BASE_MAX ? " }," : " } };") | |
| 146 < 0) | |
| 147 { fprintf (stderr, "Error in printf\n"); exit (1); } | |
| 148 mpfr_clear (p); | |
| 149 } | |
| 150 } | |
| 151 } | |
| 152 | |
| 153 /* If there was an error, the test fails. */ | |
| 154 if (error) | |
| 155 exit (1); | |
| 156 } | |
| 157 | |
| 158 int | |
| 159 main (int argc, char *argv[]) | |
| 160 { | |
| 161 if (argc != 1) | |
| 162 { | |
| 163 /* Generate code that initializes the l2b constants. */ | |
| 164 compute_l2b (1); | |
| 165 } | |
| 166 else | |
| 167 { | |
| 168 /* Check the l2b constants. */ | |
| 169 tests_start_mpfr (); | |
| 170 compute_l2b (0); | |
| 171 tests_end_mpfr (); | |
| 172 } | |
| 173 return 0; | |
| 174 } | |
| OLD | NEW |