~ubuntu-branches/ubuntu/quantal/gclcvs/quantal

« back to all changes in this revision

Viewing changes to gmp3/tests/mpz/t-fdiv_ui.c

  • Committer: Bazaar Package Importer
  • Author(s): Camm Maguire
  • Date: 2004-06-24 15:13:46 UTC
  • Revision ID: james.westby@ubuntu.com-20040624151346-xh0xaaktyyp7aorc
Tags: 2.7.0-26
C_GC_OFFSET is 2 on m68k-linux

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Test mpz_abs, mpz_add, mpz_cmp, mpz_cmp_ui, mpz_fdiv_qr_ui, mpz_fdiv_q_ui,
 
2
   mpz_fdiv_r_ui, mpz_mul, mpz_mul_ui.
 
3
 
 
4
Copyright 1993, 1994, 1996, 2000, 2001 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
#include <stdio.h>
 
24
#include <stdlib.h>
 
25
 
 
26
#include "gmp.h"
 
27
#include "gmp-impl.h"
 
28
#include "tests.h"
 
29
 
 
30
void dump_abort _PROTO ((mpz_t, unsigned long));
 
31
void debug_mp _PROTO ((mpz_t, int));
 
32
 
 
33
int
 
34
main (int argc, char **argv)
 
35
{
 
36
  mpz_t dividend;
 
37
  mpz_t quotient, remainder;
 
38
  mpz_t quotient2, remainder2;
 
39
  mpz_t temp;
 
40
  mp_size_t dividend_size;
 
41
  unsigned long divisor;
 
42
  int i;
 
43
  int reps = 10000;
 
44
  gmp_randstate_ptr rands;
 
45
  mpz_t bs;
 
46
  unsigned long bsi, size_range;
 
47
 
 
48
  tests_start ();
 
49
  rands = RANDS;
 
50
 
 
51
  mpz_init (bs);
 
52
 
 
53
  if (argc == 2)
 
54
     reps = atoi (argv[1]);
 
55
 
 
56
  mpz_init (dividend);
 
57
  mpz_init (quotient);
 
58
  mpz_init (remainder);
 
59
  mpz_init (quotient2);
 
60
  mpz_init (remainder2);
 
61
  mpz_init (temp);
 
62
 
 
63
  for (i = 0; i < reps; i++)
 
64
    {
 
65
      mpz_urandomb (bs, rands, 32);
 
66
      size_range = mpz_get_ui (bs) % 10 + 2; /* 0..2047 bit operands */
 
67
 
 
68
      do
 
69
        {
 
70
          mpz_rrandomb (bs, rands, 64);
 
71
          divisor = mpz_get_ui (bs);
 
72
        }
 
73
      while (divisor == 0);
 
74
 
 
75
      mpz_urandomb (bs, rands, size_range);
 
76
      dividend_size = mpz_get_ui (bs);
 
77
      mpz_rrandomb (dividend, rands, dividend_size);
 
78
 
 
79
      mpz_urandomb (bs, rands, 2);
 
80
      bsi = mpz_get_ui (bs);
 
81
      if ((bsi & 1) != 0)
 
82
        mpz_neg (dividend, dividend);
 
83
 
 
84
      /* printf ("%ld\n", SIZ (dividend)); */
 
85
 
 
86
      mpz_fdiv_qr_ui (quotient, remainder, dividend, divisor);
 
87
      mpz_fdiv_q_ui (quotient2, dividend, divisor);
 
88
      mpz_fdiv_r_ui (remainder2, dividend, divisor);
 
89
 
 
90
      /* First determine that the quotients and remainders computed
 
91
         with different functions are equal.  */
 
92
      if (mpz_cmp (quotient, quotient2) != 0)
 
93
        dump_abort (dividend, divisor);
 
94
      if (mpz_cmp (remainder, remainder2) != 0)
 
95
        dump_abort (dividend, divisor);
 
96
 
 
97
      /* Check if the sign of the quotient is correct.  */
 
98
      if (mpz_cmp_ui (quotient, 0) != 0)
 
99
        if ((mpz_cmp_ui (quotient, 0) < 0)
 
100
            != (mpz_cmp_ui (dividend, 0) < 0))
 
101
        dump_abort (dividend, divisor);
 
102
 
 
103
      /* Check if the remainder has the same sign as the divisor
 
104
         (quotient rounded towards minus infinity).  */
 
105
      if (mpz_cmp_ui (remainder, 0) != 0)
 
106
        if (mpz_cmp_ui (remainder, 0) < 0)
 
107
          dump_abort (dividend, divisor);
 
108
 
 
109
      mpz_mul_ui (temp, quotient, divisor);
 
110
      mpz_add (temp, temp, remainder);
 
111
      if (mpz_cmp (temp, dividend) != 0)
 
112
        dump_abort (dividend, divisor);
 
113
 
 
114
      mpz_abs (remainder, remainder);
 
115
      if (mpz_cmp_ui (remainder, divisor) >= 0)
 
116
        dump_abort (dividend, divisor);
 
117
    }
 
118
 
 
119
  mpz_clear (bs);
 
120
  mpz_clear (dividend);
 
121
  mpz_clear (quotient);
 
122
  mpz_clear (remainder);
 
123
  mpz_clear (quotient2);
 
124
  mpz_clear (remainder2);
 
125
  mpz_clear (temp);
 
126
 
 
127
  tests_end ();
 
128
  exit (0);
 
129
}
 
130
 
 
131
void
 
132
dump_abort (mpz_t dividend, unsigned long divisor)
 
133
{
 
134
  fprintf (stderr, "ERROR\n");
 
135
  fprintf (stderr, "dividend = "); debug_mp (dividend, -16);
 
136
  fprintf (stderr, "divisor  = %lX\n", divisor);
 
137
  abort();
 
138
}
 
139
 
 
140
void
 
141
debug_mp (mpz_t x, int base)
 
142
{
 
143
  mpz_out_str (stderr, base, x); fputc ('\n', stderr);
 
144
}