[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.2

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

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