1 /* mpfr_dump, mpfr_fdump -- dump a float (for the tests and debugging purpose) 2 3 Copyright 1999-2023 Free Software Foundation, Inc. 4 Contributed by the AriC and Caramba 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 3 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.LESSER. If not, see 20 https://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc., 21 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */ 22 23 #include "mpfr-impl.h" 24 25 /* mpfr_dump is mainly for debugging purpose. It outputs a MPFR number 26 * in some unspecified format, then a newline character. This function 27 * is part of the API, but the output format may change without breaking 28 * the ABI. 29 * 30 * Since this is for debugging, it can detect invalid data (or UBF), and 31 * in such a case, it outputs information between exclamation marks: 32 * - 'N': the number is not normalized (MSB = 0); 33 * - 'T': there are non-zero trailing bits (output in square brackets); 34 * - 'U': this is an UBF number (internal use only); 35 * - '<': the exponent is < the current emin; 36 * - '>': the exponent is > the current emax. 37 * 38 * The format may depend on MPFR_* environment variables. For instance, 39 * in the future, some environment variables could specify prefix and 40 * suffix strings to colorize parts of the output that correspond to 41 * invalid data. 42 */ 43 44 void 45 mpfr_fdump (FILE *stream, mpfr_srcptr x) 46 { 47 if (MPFR_IS_NEG (x)) 48 fprintf (stream, "-"); 49 50 if (MPFR_IS_NAN (x)) 51 fprintf (stream, "@NaN@"); 52 else if (MPFR_IS_INF (x)) 53 fprintf (stream, "@Inf@"); 54 else if (MPFR_IS_ZERO (x)) 55 fprintf (stream, "0"); 56 else 57 { 58 mp_limb_t *mx; 59 mpfr_prec_t px; 60 mp_size_t n; 61 char invalid[4]; 62 int first = 1, i = 0; 63 64 mx = MPFR_MANT (x); 65 px = MPFR_PREC (x); 66 67 fprintf (stream, "0."); 68 for (n = (px - 1) / GMP_NUMB_BITS; n >= 0; n--) 69 { 70 mp_limb_t wd, t; 71 72 wd = mx[n]; 73 if (first) 74 { 75 if (! MPFR_LIMB_MSB (wd)) 76 invalid[i++] = 'N'; 77 first = 0; 78 } 79 for (t = MPFR_LIMB_HIGHBIT; t != 0; t >>= 1) 80 { 81 putc ((wd & t) == 0 ? '0' : '1', stream); 82 if (--px == 0) 83 { 84 MPFR_ASSERTD (n == 0); 85 if (t != 0 && (wd & (t - 1)) != 0) 86 { 87 putc ('[', stream); 88 invalid[i++] = 'T'; 89 } 90 else 91 break; /* the trailing bits (all 0's) are not output */ 92 } 93 } 94 } 95 if (px < 0) /* there are non-zero trailing bits */ 96 putc (']', stream); 97 98 if (MPFR_IS_UBF (x)) 99 { 100 #ifndef MPFR_USE_MINI_GMP 101 gmp_fprintf (stream, "E%Zd", MPFR_ZEXP (x)); 102 #else /* mini-gmp has no gmp_fprintf */ 103 fprintf (stream, "E"); 104 mpz_out_str (stream, 10, MPFR_ZEXP (x)); 105 #endif 106 invalid[i++] = 'U'; 107 } 108 else 109 { 110 mpfr_exp_t e = MPFR_EXP (x); 111 112 fprintf (stream, "E%" MPFR_EXP_FSPEC "d", (mpfr_eexp_t) e); 113 if (e < __gmpfr_emin) 114 invalid[i++] = '<'; 115 else if (e > __gmpfr_emax) 116 invalid[i++] = '>'; 117 } 118 119 if (i != 0) 120 { 121 invalid[i] = '\0'; 122 fprintf (stream, "!!!%s!!!", invalid); 123 } 124 } 125 putc ('\n', stream); 126 } 127 128 void 129 mpfr_dump (mpfr_srcptr x) 130 { 131 mpfr_fdump (stdout, x); 132 } 133