1 /* mpfr_print_binary -- print the internal binary representation of a 2 floating-point number 3 4 Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc. 5 Contributed by the Arenaire and Caramel projects, INRIA. 6 7 This file is part of the GNU MPFR Library. 8 9 The GNU MPFR Library is free software; you can redistribute it and/or modify 10 it under the terms of the GNU Lesser General Public License as published by 11 the Free Software Foundation; either version 3 of the License, or (at your 12 option) any later version. 13 14 The GNU MPFR Library is distributed in the hope that it will be useful, but 15 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 16 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 17 License for more details. 18 19 You should have received a copy of the GNU Lesser General Public License 20 along with the GNU MPFR Library; see the file COPYING.LESSER. If not, see 21 http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc., 22 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */ 23 24 #include "mpfr-impl.h" 25 26 void 27 mpfr_fprint_binary (FILE *stream, mpfr_srcptr x) 28 { 29 if (MPFR_IS_NAN (x)) 30 { 31 fprintf (stream, "@NaN@"); 32 return; 33 } 34 35 if (MPFR_SIGN (x) < 0) 36 fprintf (stream, "-"); 37 38 if (MPFR_IS_INF (x)) 39 fprintf (stream, "@Inf@"); 40 else if (MPFR_IS_ZERO (x)) 41 fprintf (stream, "0"); 42 else 43 { 44 mp_limb_t *mx; 45 mpfr_prec_t px; 46 mp_size_t n; 47 48 mx = MPFR_MANT (x); 49 px = MPFR_PREC (x); 50 51 fprintf (stream, "0."); 52 for (n = (px - 1) / GMP_NUMB_BITS; ; n--) 53 { 54 mp_limb_t wd, t; 55 56 MPFR_ASSERTN (n >= 0); 57 wd = mx[n]; 58 for (t = MPFR_LIMB_HIGHBIT; t != 0; t >>= 1) 59 { 60 putc ((wd & t) == 0 ? '0' : '1', stream); 61 if (--px == 0) 62 { 63 mpfr_exp_t ex; 64 65 ex = MPFR_GET_EXP (x); 66 MPFR_ASSERTN (ex >= LONG_MIN && ex <= LONG_MAX); 67 fprintf (stream, "E%ld", (long) ex); 68 return; 69 } 70 } 71 } 72 } 73 } 74 75 void 76 mpfr_print_binary (mpfr_srcptr x) 77 { 78 mpfr_fprint_binary (stdout, x); 79 } 80 81 void 82 mpfr_print_mant_binary(const char *str, const mp_limb_t *p, mpfr_prec_t r) 83 { 84 int i; 85 mpfr_prec_t count = 0; 86 char c; 87 mp_size_t n = (r - 1) / GMP_NUMB_BITS + 1; 88 89 printf("%s ", str); 90 for(n-- ; n>=0 ; n--) 91 { 92 for(i = GMP_NUMB_BITS-1 ; i >=0 ; i--) 93 { 94 c = (p[n] & (((mp_limb_t)1L)<<i)) ? '1' : '0'; 95 putchar(c); 96 count++; 97 if (count == r) 98 putchar('['); 99 } 100 putchar('.'); 101 } 102 putchar('\n'); 103 } 104 105 void 106 mpfr_dump_mant (const mp_limb_t *p, mpfr_prec_t r, mpfr_prec_t precx, 107 mpfr_prec_t error) 108 { 109 int i; 110 mpfr_prec_t count = 0; 111 char c; 112 mp_size_t n = (r - 1) / GMP_NUMB_BITS + 1; 113 114 for(n-- ; n>=0 ; n--) 115 { 116 for(i = GMP_NUMB_BITS-1 ; i >=0 ; i--) 117 { 118 c = (p[n] & (((mp_limb_t)1L)<<i)) ? '1' : '0'; 119 putchar(c); 120 count++; 121 if (count == precx) 122 putchar (','); 123 if (count == error) 124 putchar('['); 125 } 126 putchar('.'); 127 } 128 putchar('\n'); 129 } 130