aboutsummaryrefslogtreecommitdiff
path: root/vendor/gmp-6.3.0/tests/mpz/t-limbs.c
blob: 6526e929f9132166599aa69b0ffdd9b351f64b87 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/* Test mpz_limbs_* functions

Copyright 2013 Free Software Foundation, Inc.

This file is part of the GNU MP Library test suite.

The GNU MP Library test suite is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 3 of the License,
or (at your option) any later version.

The GNU MP Library test suite is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
Public License for more details.

You should have received a copy of the GNU General Public License along with
the GNU MP Library test suite.  If not, see https://www.gnu.org/licenses/.  */

#include <stdlib.h>
#include <stdio.h>

#include "gmp-impl.h"
#include "tests.h"

#define COUNT 100
#define BITSIZE 500

/* Like mpz_add. For simplicity, support positive inputs only. */
static void
alt_add (mpz_ptr r, mpz_srcptr a, mpz_srcptr b)
{
  mp_size_t an = mpz_size (a);
  mp_size_t bn = mpz_size (b);
  mp_ptr rp;

  ASSERT (an > 0);
  ASSERT (bn > 0);
  if (an < bn)
    {
      MP_SIZE_T_SWAP (an, bn);
      MPZ_SRCPTR_SWAP (a, b);
    }
  rp = mpz_limbs_modify (r, an + 1);
  rp[an] = mpn_add (rp, mpz_limbs_read (a), an, mpz_limbs_read (b), bn);
  mpz_limbs_finish (r, an + 1);
}

static void
check_funcs (const char *name,
	     void (*f)(mpz_ptr, mpz_srcptr, mpz_srcptr),
	     void (*ref_f)(mpz_ptr, mpz_srcptr, mpz_srcptr),
	     mpz_srcptr a, mpz_srcptr b)
{
  mpz_t r, ref;
  mpz_inits (r, ref, NULL);

  ref_f (ref, a, b);
  MPZ_CHECK_FORMAT (ref);
  f (r, a, b);
  MPZ_CHECK_FORMAT (r);

  if (mpz_cmp (r, ref) != 0)
    {
      printf ("%s failed, abits %u, bbits %u\n",
	      name,
	      (unsigned) mpz_sizeinbase (a, 2),
	      (unsigned) mpz_sizeinbase (b, 2));
      gmp_printf ("a = %Zx\n", a);
      gmp_printf ("b = %Zx\n", b);
      gmp_printf ("r = %Zx (bad)\n", r);
      gmp_printf ("ref = %Zx\n", ref);
      abort ();
    }
  mpz_clears (r, ref, NULL);
}

static void
check_add (void)
{
  gmp_randstate_ptr rands = RANDS;
  mpz_t bs, a, b;
  unsigned i;
  mpz_inits (bs, a, b, NULL);
  for (i = 0; i < COUNT; i++)
    {
      mpz_urandomb (bs, rands, 32);
      mpz_rrandomb (a, rands, 1 + mpz_get_ui (bs) % BITSIZE);
      mpz_urandomb (bs, rands, 32);
      mpz_rrandomb (b, rands, 1 + mpz_get_ui (bs) % BITSIZE);

      check_funcs ("add", alt_add, mpz_add, a, b);
    }
  mpz_clears (bs, a, b, NULL);
}

static void
alt_mul (mpz_ptr r, mpz_srcptr a, mpz_srcptr b)
{
  mp_size_t an = mpz_size (a);
  mp_size_t bn = mpz_size (b);
  mp_srcptr ap, bp;
  TMP_DECL;

  TMP_MARK;

  ASSERT (an > 0);
  ASSERT (bn > 0);
  if (an < bn)
    {
      MP_SIZE_T_SWAP (an, bn);
      MPZ_SRCPTR_SWAP (a, b);
    }
  /* NOTE: This copying seems unnecessary; better to allocate new
     result area, and free the old area when done. */
  if (r == a)
    {
      mp_ptr tp =  TMP_ALLOC_LIMBS (an);
      MPN_COPY (tp, mpz_limbs_read (a), an);
      ap = tp;
      bp = (a == b) ? ap : mpz_limbs_read (b);
    }
  else if (r == b)
    {
      mp_ptr tp = TMP_ALLOC_LIMBS (bn);
      MPN_COPY (tp, mpz_limbs_read (b), bn);
      bp = tp;
      ap = mpz_limbs_read (a);
    }
  else
    {
      ap = mpz_limbs_read (a);
      bp = mpz_limbs_read (b);
    }
  mpn_mul (mpz_limbs_write (r, an + bn),
	   ap, an, bp, bn);

  mpz_limbs_finish (r, an + bn);
}

void
check_mul (void)
{
  gmp_randstate_ptr rands = RANDS;
  mpz_t bs, a, b;
  unsigned i;
  mpz_inits (bs, a, b, NULL);
  for (i = 0; i < COUNT; i++)
    {
      mpz_urandomb (bs, rands, 32);
      mpz_rrandomb (a, rands, 1 + mpz_get_ui (bs) % BITSIZE);
      mpz_urandomb (bs, rands, 32);
      mpz_rrandomb (b, rands, 1 + mpz_get_ui (bs) % BITSIZE);

      check_funcs ("mul", alt_mul, mpz_mul, a, b);
    }
  mpz_clears (bs, a, b, NULL);
}

#define MAX_SIZE 100

static void
check_roinit (void)
{
  gmp_randstate_ptr rands = RANDS;
  mpz_t bs, a, b, r, ref;
  unsigned i;

  mpz_inits (bs, a, b, r, ref, NULL);

  for (i = 0; i < COUNT; i++)
    {
      mp_srcptr ap, bp;
      mp_size_t an, bn;
      mpz_urandomb (bs, rands, 32);
      mpz_rrandomb (a, rands, 1 + mpz_get_ui (bs) % BITSIZE);
      mpz_urandomb (bs, rands, 32);
      mpz_rrandomb (b, rands, 1 + mpz_get_ui (bs) % BITSIZE);

      an = mpz_size (a);
      ap = mpz_limbs_read (a);
      bn = mpz_size (b);
      bp = mpz_limbs_read (b);

      mpz_add (ref, a, b);
      {
	mpz_t a1, b1;
#if __STDC_VERSION__ >= 199901
	const mpz_t a2 = MPZ_ROINIT_N ( (mp_ptr) ap, an);
	const mpz_t b2 = MPZ_ROINIT_N ( (mp_ptr) bp, bn);

	mpz_set_ui (r, 0);
	mpz_add (r, a2, b2);
	if (mpz_cmp (r, ref) != 0)
	  {
	    printf ("MPZ_ROINIT_N failed\n");
	    gmp_printf ("a = %Zx\n", a);
	    gmp_printf ("b = %Zx\n", b);
	    gmp_printf ("r = %Zx (bad)\n", r);
	    gmp_printf ("ref = %Zx\n", ref);
	    abort ();
	  }
#endif
	mpz_set_ui (r, 0);
	mpz_add (r, mpz_roinit_n (a1, ap, an), mpz_roinit_n (b1, bp, bn));
	if (mpz_cmp (r, ref) != 0)
	  {
	    printf ("mpz_roinit_n failed\n");
	    gmp_printf ("a = %Zx\n", a);
	    gmp_printf ("b = %Zx\n", b);
	    gmp_printf ("r = %Zx (bad)\n", r);
	    gmp_printf ("ref = %Zx\n", ref);
	    abort ();
	  }
      }
    }
  mpz_clears (bs, a, b, r, ref, NULL);
}

int
main (int argc, char *argv[])
{
  tests_start ();
  tests_end ();

  check_add ();
  check_mul ();
  check_roinit ();

  return 0;

}