[BACK]Return to t-pow_ui.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib / gmp / mpz / tests

Annotation of OpenXM_contrib/gmp/mpz/tests/t-pow_ui.c, Revision 1.1.1.1

1.1       maekawa     1: /* Test mpz_pow_ui and mpz_ui_pow_ui.
                      2:
                      3: Copyright (C) 1997, 1999 Free Software Foundation, Inc.
                      4:
                      5: This file is part of the GNU MP Library.
                      6:
                      7: The GNU MP Library is free software; you can redistribute it and/or modify
                      8: it under the terms of the GNU Lesser General Public License as published by
                      9: the Free Software Foundation; either version 2.1 of the License, or (at your
                     10: option) any later version.
                     11:
                     12: The GNU MP Library is distributed in the hope that it will be useful, but
                     13: WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
                     14: or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
                     15: License for more details.
                     16:
                     17: You should have received a copy of the GNU Lesser General Public License
                     18: along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
                     19: the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
                     20: MA 02111-1307, USA. */
                     21:
                     22: #include <stdio.h>
                     23: #include "gmp.h"
                     24: #include "gmp-impl.h"
                     25: #include "urandom.h"
                     26:
                     27: void debug_mp ();
                     28: void ref_mpz_pow_ui ();
                     29:
                     30: main (argc, argv)
                     31:      int argc;
                     32:      char **argv;
                     33: {
                     34:   mpz_t base;
                     35:   mpz_t result, ref_result;
                     36:   mp_size_t base_size;
                     37:   unsigned long int exp;
                     38:   int i;
                     39:   int reps = 50000;
                     40:
                     41:   if (argc == 2)
                     42:      reps = atoi (argv[1]);
                     43:
                     44:   mpz_init (base);
                     45:   mpz_init (result);
                     46:   mpz_init (ref_result);
                     47:
                     48:   for (i = 0; i < reps; i++)
                     49:     {
                     50:       base_size = urandom () % 8 - 4;
                     51:       exp = urandom () % 16;
                     52:
                     53:       mpz_random2 (base, base_size);
                     54:
                     55:       ref_mpz_pow_ui (ref_result, base, exp);
                     56:
                     57:       mpz_pow_ui (result, base, exp);
                     58:       if (mpz_cmp (result, ref_result))
                     59:        {
                     60:          fprintf (stderr, "ERROR (mpz_pow_ui):\n");
                     61:          debug_mp ("      base = ", base, -16);
                     62:          fprintf (stderr, "       exp = %lu (0x%lX)\n", exp, exp);
                     63:          debug_mp ("    result = ", result, -16);
                     64:          debug_mp ("ref_result = ", ref_result, -16);
                     65:          abort ();
                     66:        }
                     67:
                     68:       if (mpz_cmp_ui (base, 0L) >= 0
                     69:          && mpz_cmp_ui (base, ~(unsigned long int) 0) <= 0)
                     70:        {
                     71:          mpz_ui_pow_ui (result, mpz_get_ui (base), exp);
                     72:          if (mpz_cmp (result, ref_result))
                     73:            {
                     74:              fprintf (stderr, "ERROR (mpz_ui_pow_ui):\n");
                     75:              debug_mp ("      base = ", base, -16);
                     76:              fprintf (stderr, "       exp = %lu (0x%lX)\n", exp, exp);
                     77:              debug_mp ("    result = ", result, -16);
                     78:              debug_mp ("ref_result = ", ref_result, -16);
                     79:              abort ();
                     80:            }
                     81:        }
                     82:     }
                     83:
                     84:   exit (0);
                     85: }
                     86:
                     87: void
                     88: ref_mpz_pow_ui (w, u, e)
                     89:      mpz_t w;
                     90:      const mpz_t u;
                     91:      unsigned long int e;
                     92: {
                     93:   mpz_t s, t;
                     94:   unsigned long int i;
                     95:
                     96:   mpz_init_set_ui (t, 1);
                     97:   mpz_init_set (s, u);
                     98:
                     99:   if ((e & 1) != 0)
                    100:     mpz_mul (t, t, s);
                    101:
                    102:   for (i = 2; i <= e; i <<= 1)
                    103:     {
                    104:       mpz_mul (s, s, s);
                    105:       if ((i & e) != 0)
                    106:        mpz_mul (t, t, s);
                    107:     }
                    108:
                    109:   mpz_set (w, t);
                    110:   mpz_clear (s);
                    111:   mpz_clear (t);
                    112: }
                    113:
                    114: void
                    115: debug_mp (str, x, base)
                    116:      char *str;
                    117:      mpz_t x;
                    118:      int base;
                    119: {
                    120:   fprintf (stderr, "%s", str);
                    121:   mpz_out_str (stderr, base, x); fputc ('\n', stderr);
                    122: }

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