[BACK]Return to trace.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib / gmp / mpn / tests

Annotation of OpenXM_contrib/gmp/mpn/tests/trace.c, Revision 1.1

1.1     ! maekawa     1: /* trace.c -- Support for diagnostic traces. */
        !             2:
        !             3: /*
        !             4: Copyright (C) 1999, 2000 Free Software Foundation, Inc.
        !             5:
        !             6: This file is part of the GNU MP Library.
        !             7:
        !             8: The GNU MP 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 MP 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 MP Library; see the file COPYING.LIB.  If not, write to
        !            20: the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
        !            21: MA 02111-1307, USA.
        !            22: */
        !            23:
        !            24: #include <stdio.h>
        !            25: #include "gmp.h"
        !            26: #include "gmp-impl.h"
        !            27: #include "try.h"
        !            28:
        !            29:
        !            30: /* Number base for the various trace printing routines.
        !            31:    Set this in main() or with the debugger.
        !            32:    If hexadecimal is going to be fed into GNU bc, remember to use -16
        !            33:    because bc requires upper case.  */
        !            34:
        !            35: int  mp_trace_base = 10;
        !            36:
        !            37:
        !            38: /* Print "name=value\n" to stdout for an mpq_t value.  */
        !            39: void
        !            40: mpq_trace (const char *name, mpq_srcptr q)
        !            41: {
        !            42:   if (name != NULL && name[0] != '\0')
        !            43:     printf ("%s=", name);
        !            44:
        !            45:   switch (ABS (mp_trace_base)) {
        !            46:   case  8: printf ("oct");                          break;
        !            47:   case 10:                                          break;
        !            48:   case 16: printf ("0x");                           break;
        !            49:   default: printf ("base%d:", ABS (mp_trace_base)); break;
        !            50:   }
        !            51:
        !            52:   mpq_out_str (stdout, mp_trace_base, q);
        !            53:
        !            54:   /* It's not very interesting to know when numbers are unnormalized.
        !            55:      mpz's should always be and ought to be checked with ASSERT() if in doubt.
        !            56:      mpn's can often be unnormalized without affecting anything.  */
        !            57:   /* if (!mpq_normalized_p (z))  printf (" (unnorm)"); */
        !            58:
        !            59:   printf ("\n");
        !            60: }
        !            61:
        !            62:
        !            63: /* Print "name=value\n" to stdout for an mpz_t value.  */
        !            64: void
        !            65: mpz_trace (const char *name, mpz_srcptr z)
        !            66: {
        !            67:   mpq_t      q;
        !            68:   mpz_t      one;
        !            69:   mp_limb_t  one_limb;
        !            70:
        !            71:   PTR(one) = &one_limb;
        !            72:   SIZ(one) = 1;
        !            73:   one_limb = 1;
        !            74:
        !            75:   q->_mp_num = *z;
        !            76:   q->_mp_den = *one;
        !            77:
        !            78:   mpq_trace(name, q);
        !            79: }
        !            80:
        !            81:
        !            82: /* Print "namenum=value\n" to stdout for an mpz_t value.
        !            83:    "name" should have a "%d" to get the number. */
        !            84: void
        !            85: mpz_tracen (const char *name, int num, mpz_srcptr z)
        !            86: {
        !            87:   if (name != NULL && name[0] != '\0')
        !            88:     {
        !            89:       printf (name, num);
        !            90:       putchar ('=');
        !            91:     }
        !            92:   mpz_trace (NULL, z);
        !            93: }
        !            94:
        !            95:
        !            96: /* Print "name=value\n" to stdout for an mpn style ptr,size. */
        !            97: void
        !            98: mpn_trace (const char *name, mp_srcptr ptr, mp_size_t size)
        !            99: {
        !           100:   mpz_t  z;
        !           101:   MPN_NORMALIZE (ptr, size);
        !           102:   PTR(z) = (mp_ptr) ptr;
        !           103:   SIZ(z) = (int) size;
        !           104:   mpz_trace (name, z);
        !           105: }
        !           106:
        !           107:
        !           108: /* Print "namenum=value\n" to stdout for an mpn style ptr,size.
        !           109:    "name" should have a "%d" to get the number.  */
        !           110: void
        !           111: mpn_tracen (const char *name, int num, mp_srcptr ptr, mp_size_t size)
        !           112: {
        !           113:   if (name != NULL && name[0] != '\0')
        !           114:     {
        !           115:       printf (name, num);
        !           116:       putchar ('=');
        !           117:     }
        !           118:   mpn_trace (NULL, ptr, size);
        !           119: }
        !           120:
        !           121:
        !           122: /* Print "namenum=value\n" to stdout for an array of mpn style ptr,size.
        !           123:
        !           124:    "a" is an array of pointers, each a[i] is a pointer to "size" many limbs.
        !           125:    The formal parameter isn't mp_srcptr because that causes compiler
        !           126:    warnings, but the values aren't modified.
        !           127:
        !           128:    "name" should have a printf style "%d" to get the array index.  */
        !           129:
        !           130: void
        !           131: mpn_tracea (const char *name, const mp_ptr *a, int count, mp_size_t size)
        !           132: {
        !           133:   int i;
        !           134:   for (i = 0; i < count; i++)
        !           135:     mpn_tracen (name, i, a[i], size);
        !           136: }
        !           137:
        !           138:
        !           139: /* Print "value\n" to a file for an mpz_t value.  Any previous contents of
        !           140:    the file are overwritten, so you need different file names each time this
        !           141:    is called.
        !           142:
        !           143:    Overwriting the file is a feature, it means you get old data replaced
        !           144:    when you run a test program repeatedly.  */
        !           145:
        !           146: void
        !           147: mpn_trace_file (const char *filename, mp_srcptr ptr, mp_size_t size)
        !           148: {
        !           149:   FILE   *fp;
        !           150:   mpz_t  z;
        !           151:
        !           152:   fp = fopen (filename, "w");
        !           153:   if (fp == NULL)
        !           154:     {
        !           155:       perror ("fopen");
        !           156:       abort();
        !           157:     }
        !           158:
        !           159:   MPN_NORMALIZE (ptr, size);
        !           160:   PTR(z) = (mp_ptr) ptr;
        !           161:   SIZ(z) = (int) size;
        !           162:
        !           163:   mpz_out_str (fp, mp_trace_base, z);
        !           164:   fprintf (fp, "\n");
        !           165:
        !           166:   if (ferror (fp) || fclose (fp) != 0)
        !           167:     {
        !           168:       printf ("error writing %s\n", filename);
        !           169:       abort();
        !           170:     }
        !           171: }
        !           172:
        !           173:
        !           174: /* Print "value\n" to a set of files, one file for each element of the given
        !           175:    array of mpn style ptr,size.  Any previous contents of the files are
        !           176:    overwritten, so you need different file names each time this is called.
        !           177:    Each file is "filenameN" where N is 0 to count-1.
        !           178:
        !           179:    "a" is an array of pointers, each a[i] is a pointer to "size" many limbs.
        !           180:    The formal parameter isn't mp_srcptr because that causes compiler
        !           181:    warnings, but the values aren't modified.
        !           182:
        !           183:    Overwriting the files is a feature, it means you get old data replaced
        !           184:    when you run a test program repeatedly.  The output style isn't
        !           185:    particularly pretty, but at least it gets something out, and you can cat
        !           186:    the files into bc, or whatever. */
        !           187:
        !           188: void
        !           189: mpn_tracea_file (const char *filename,
        !           190:   const mp_ptr *a, int count, mp_size_t size)
        !           191: {
        !           192:   char  *s;
        !           193:   int   i;
        !           194:   TMP_DECL (marker);
        !           195:
        !           196:   TMP_MARK (marker);
        !           197:   s = (char *) TMP_ALLOC (strlen (filename) + 50);
        !           198:
        !           199:   for (i = 0; i < count; i++)
        !           200:     {
        !           201:       sprintf (s, "%s%d", filename, i);
        !           202:       mpn_trace_file (s, a[i], size);
        !           203:     }
        !           204:
        !           205:   TMP_FREE (marker);
        !           206: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>