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

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

1.1       maekawa     1: /* Test mpz_powm_ui, mpz_mul. mpz_mod, mpz_mod_ui, mpz_div_ui.
                      2:
                      3: Copyright (C) 1991, 1993, 1994, 1996 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 Library General Public License as published by
                      9: the Free Software Foundation; either version 2 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 Library General Public
                     15: License for more details.
                     16:
                     17: You should have received a copy of the GNU Library 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:
                     29: #ifndef SIZE
                     30: #define SIZE 8
                     31: #endif
                     32:
                     33: main (argc, argv)
                     34:      int argc;
                     35:      char **argv;
                     36: {
                     37:   mpz_t base, mod;
                     38:   mpz_t r1, r2, base2;
                     39:   mp_size_t base_size, mod_size;
                     40:   mp_limb_t exp, exp2;
                     41:   int i;
                     42:   int reps = 10000;
                     43:
                     44:   if (argc == 2)
                     45:      reps = atoi (argv[1]);
                     46:
                     47:   mpz_init (base);
                     48:   mpz_init (mod);
                     49:   mpz_init (r1);
                     50:   mpz_init (r2);
                     51:   mpz_init (base2);
                     52:
                     53:   for (i = 0; i < reps; i++)
                     54:     {
                     55:       base_size = urandom () % SIZE /* - SIZE/2 */;
                     56:       mpz_random2 (base, base_size);
                     57:
                     58:       mpn_random2 (&exp, 1);
                     59:
                     60:       mod_size = urandom () % SIZE /* - SIZE/2 */;
                     61:       mpz_random2 (mod, mod_size);
                     62:       if (mpz_cmp_ui (mod, 0) == 0)
                     63:        continue;
                     64:
                     65:       /* This is mathematically undefined.  */
                     66:       if (mpz_cmp_ui (base, 0) == 0 && exp == 0)
                     67:        continue;
                     68:
                     69: #if 0
                     70:       putc ('\n', stderr);
                     71:       debug_mp (base, -16);
                     72:       debug_mp (mod, -16);
                     73: #endif
                     74:
                     75:       mpz_powm_ui (r1, base, (unsigned long int) exp, mod);
                     76:
                     77:       mpz_set_ui (r2, 1);
                     78:       mpz_set (base2, base);
                     79:       exp2 = exp;
                     80:
                     81:       mpz_mod (r2, r2, mod);   /* needed when exp==0 and mod==1 */
                     82:       while (exp2 != 0)
                     83:        {
                     84:          if (exp2 % 2 != 0)
                     85:            {
                     86:              mpz_mul (r2, r2, base2);
                     87:              mpz_mod (r2, r2, mod);
                     88:            }
                     89:          mpz_mul (base2, base2, base2);
                     90:          mpz_mod (base2, base2, mod);
                     91:          exp2 = exp2 / 2;
                     92:        }
                     93:
                     94: #if 0
                     95:       debug_mp (r1, -16);
                     96:       debug_mp (r2, -16);
                     97: #endif
                     98:
                     99:       if (mpz_cmp (r1, r2) != 0)
                    100:        abort ();
                    101:     }
                    102:
                    103:   exit (0);
                    104: }
                    105:
                    106: dump_abort (dividend, divisor)
                    107:      MP_INT *dividend, *divisor;
                    108: {
                    109:   fprintf (stderr, "ERROR\n");
                    110:   fprintf (stderr, "dividend = "); debug_mp (dividend, -16);
                    111:   fprintf (stderr, "divisor  = "); debug_mp (divisor, -16);
                    112:   abort();
                    113: }
                    114:
                    115: void
                    116: debug_mp (x, base)
                    117:      MP_INT *x;
                    118: {
                    119:   mpz_out_str (stderr, base, x); fputc ('\n', stderr);
                    120: }

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