14a238c70SJohn Marino /* mpfr_print_binary -- print the internal binary representation of a
24a238c70SJohn Marino floating-point number
34a238c70SJohn Marino
4*ab6d115fSJohn Marino Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
5*ab6d115fSJohn Marino Contributed by the AriC and Caramel projects, INRIA.
64a238c70SJohn Marino
74a238c70SJohn Marino This file is part of the GNU MPFR Library.
84a238c70SJohn Marino
94a238c70SJohn Marino The GNU MPFR Library is free software; you can redistribute it and/or modify
104a238c70SJohn Marino it under the terms of the GNU Lesser General Public License as published by
114a238c70SJohn Marino the Free Software Foundation; either version 3 of the License, or (at your
124a238c70SJohn Marino option) any later version.
134a238c70SJohn Marino
144a238c70SJohn Marino The GNU MPFR Library is distributed in the hope that it will be useful, but
154a238c70SJohn Marino WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
164a238c70SJohn Marino or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
174a238c70SJohn Marino License for more details.
184a238c70SJohn Marino
194a238c70SJohn Marino You should have received a copy of the GNU Lesser General Public License
204a238c70SJohn Marino along with the GNU MPFR Library; see the file COPYING.LESSER. If not, see
214a238c70SJohn Marino http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
224a238c70SJohn Marino 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
234a238c70SJohn Marino
244a238c70SJohn Marino #include "mpfr-impl.h"
254a238c70SJohn Marino
264a238c70SJohn Marino void
mpfr_fprint_binary(FILE * stream,mpfr_srcptr x)274a238c70SJohn Marino mpfr_fprint_binary (FILE *stream, mpfr_srcptr x)
284a238c70SJohn Marino {
294a238c70SJohn Marino if (MPFR_IS_NAN (x))
304a238c70SJohn Marino {
314a238c70SJohn Marino fprintf (stream, "@NaN@");
324a238c70SJohn Marino return;
334a238c70SJohn Marino }
344a238c70SJohn Marino
354a238c70SJohn Marino if (MPFR_SIGN (x) < 0)
364a238c70SJohn Marino fprintf (stream, "-");
374a238c70SJohn Marino
384a238c70SJohn Marino if (MPFR_IS_INF (x))
394a238c70SJohn Marino fprintf (stream, "@Inf@");
404a238c70SJohn Marino else if (MPFR_IS_ZERO (x))
414a238c70SJohn Marino fprintf (stream, "0");
424a238c70SJohn Marino else
434a238c70SJohn Marino {
444a238c70SJohn Marino mp_limb_t *mx;
454a238c70SJohn Marino mpfr_prec_t px;
464a238c70SJohn Marino mp_size_t n;
474a238c70SJohn Marino
484a238c70SJohn Marino mx = MPFR_MANT (x);
494a238c70SJohn Marino px = MPFR_PREC (x);
504a238c70SJohn Marino
514a238c70SJohn Marino fprintf (stream, "0.");
524a238c70SJohn Marino for (n = (px - 1) / GMP_NUMB_BITS; ; n--)
534a238c70SJohn Marino {
544a238c70SJohn Marino mp_limb_t wd, t;
554a238c70SJohn Marino
564a238c70SJohn Marino MPFR_ASSERTN (n >= 0);
574a238c70SJohn Marino wd = mx[n];
584a238c70SJohn Marino for (t = MPFR_LIMB_HIGHBIT; t != 0; t >>= 1)
594a238c70SJohn Marino {
604a238c70SJohn Marino putc ((wd & t) == 0 ? '0' : '1', stream);
614a238c70SJohn Marino if (--px == 0)
624a238c70SJohn Marino {
634a238c70SJohn Marino mpfr_exp_t ex;
644a238c70SJohn Marino
654a238c70SJohn Marino ex = MPFR_GET_EXP (x);
664a238c70SJohn Marino MPFR_ASSERTN (ex >= LONG_MIN && ex <= LONG_MAX);
674a238c70SJohn Marino fprintf (stream, "E%ld", (long) ex);
684a238c70SJohn Marino return;
694a238c70SJohn Marino }
704a238c70SJohn Marino }
714a238c70SJohn Marino }
724a238c70SJohn Marino }
734a238c70SJohn Marino }
744a238c70SJohn Marino
754a238c70SJohn Marino void
mpfr_print_binary(mpfr_srcptr x)764a238c70SJohn Marino mpfr_print_binary (mpfr_srcptr x)
774a238c70SJohn Marino {
784a238c70SJohn Marino mpfr_fprint_binary (stdout, x);
794a238c70SJohn Marino }
804a238c70SJohn Marino
814a238c70SJohn Marino void
mpfr_print_mant_binary(const char * str,const mp_limb_t * p,mpfr_prec_t r)824a238c70SJohn Marino mpfr_print_mant_binary(const char *str, const mp_limb_t *p, mpfr_prec_t r)
834a238c70SJohn Marino {
844a238c70SJohn Marino int i;
854a238c70SJohn Marino mpfr_prec_t count = 0;
864a238c70SJohn Marino char c;
87*ab6d115fSJohn Marino mp_size_t n = MPFR_PREC2LIMBS (r);
884a238c70SJohn Marino
894a238c70SJohn Marino printf("%s ", str);
904a238c70SJohn Marino for(n-- ; n>=0 ; n--)
914a238c70SJohn Marino {
924a238c70SJohn Marino for(i = GMP_NUMB_BITS-1 ; i >=0 ; i--)
934a238c70SJohn Marino {
944a238c70SJohn Marino c = (p[n] & (((mp_limb_t)1L)<<i)) ? '1' : '0';
954a238c70SJohn Marino putchar(c);
964a238c70SJohn Marino count++;
974a238c70SJohn Marino if (count == r)
984a238c70SJohn Marino putchar('[');
994a238c70SJohn Marino }
1004a238c70SJohn Marino putchar('.');
1014a238c70SJohn Marino }
1024a238c70SJohn Marino putchar('\n');
1034a238c70SJohn Marino }
1044a238c70SJohn Marino
1054a238c70SJohn Marino void
mpfr_dump_mant(const mp_limb_t * p,mpfr_prec_t r,mpfr_prec_t precx,mpfr_prec_t error)1064a238c70SJohn Marino mpfr_dump_mant (const mp_limb_t *p, mpfr_prec_t r, mpfr_prec_t precx,
1074a238c70SJohn Marino mpfr_prec_t error)
1084a238c70SJohn Marino {
1094a238c70SJohn Marino int i;
1104a238c70SJohn Marino mpfr_prec_t count = 0;
1114a238c70SJohn Marino char c;
112*ab6d115fSJohn Marino mp_size_t n = MPFR_PREC2LIMBS (r);
1134a238c70SJohn Marino
1144a238c70SJohn Marino for(n-- ; n>=0 ; n--)
1154a238c70SJohn Marino {
1164a238c70SJohn Marino for(i = GMP_NUMB_BITS-1 ; i >=0 ; i--)
1174a238c70SJohn Marino {
1184a238c70SJohn Marino c = (p[n] & (((mp_limb_t)1L)<<i)) ? '1' : '0';
1194a238c70SJohn Marino putchar(c);
1204a238c70SJohn Marino count++;
1214a238c70SJohn Marino if (count == precx)
1224a238c70SJohn Marino putchar (',');
1234a238c70SJohn Marino if (count == error)
1244a238c70SJohn Marino putchar('[');
1254a238c70SJohn Marino }
1264a238c70SJohn Marino putchar('.');
1274a238c70SJohn Marino }
1284a238c70SJohn Marino putchar('\n');
1294a238c70SJohn Marino }
130