summaryrefslogtreecommitdiff
path: root/tests/mpn/t-matrix22.c
blob: 44832e4e7d47bb39dd05c11936064790be2bb187 (plain)
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
/* Tests matrix22_mul.

Copyright 2008 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 http://www.gnu.org/licenses/.  */

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

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

struct matrix {
  mp_size_t alloc;
  mp_size_t n;
  mp_ptr e00, e01, e10, e11;
};

static void
matrix_init (struct matrix *M, mp_size_t n)
{
  mp_ptr p = refmpn_malloc_limbs (4*(n+1));
  M->e00 = p; p += n+1;
  M->e01 = p; p += n+1;
  M->e10 = p; p += n+1;
  M->e11 = p;
  M->alloc = n + 1;
  M->n = 0;
}

static void
matrix_clear (struct matrix *M)
{
  refmpn_free_limbs (M->e00);
}

static void
matrix_copy (struct matrix *R, const struct matrix *M)
{
  R->n = M->n;
  MPN_COPY (R->e00, M->e00, M->n);
  MPN_COPY (R->e01, M->e01, M->n);
  MPN_COPY (R->e10, M->e10, M->n);
  MPN_COPY (R->e11, M->e11, M->n);
}

/* Used with same size, so no need for normalization. */
static int
matrix_equal_p (const struct matrix *A, const struct matrix *B)
{
  return (A->n == B->n
	  && mpn_cmp (A->e00, B->e00, A->n) == 0
	  && mpn_cmp (A->e01, B->e01, A->n) == 0
	  && mpn_cmp (A->e10, B->e10, A->n) == 0
	  && mpn_cmp (A->e11, B->e11, A->n) == 0);
}

static void
matrix_random(struct matrix *M, mp_size_t n, gmp_randstate_ptr rands)
{
  M->n = n;
  mpn_random (M->e00, n);
  mpn_random (M->e01, n);
  mpn_random (M->e10, n);
  mpn_random (M->e11, n);
}

#define MUL(rp, ap, an, bp, bn) do { \
    if (an > bn)		     \
      mpn_mul (rp, ap, an, bp, bn);  \
    else			     \
      mpn_mul (rp, bp, bn, ap, an);  \
  } while(0)

static void
ref_matrix22_mul (struct matrix *R,
		  const struct matrix *A,
		  const struct matrix *B, mp_ptr tp)
{
  mp_size_t an, bn, n;
  mp_ptr r00, r01, r10, r11, a00, a01, a10, a11, b00, b01, b10, b11;

  if (A->n >= B->n)
    {
      r00 = R->e00; a00 = A->e00; b00 = B->e00;
      r01 = R->e01; a01 = A->e01; b01 = B->e01;
      r10 = R->e10; a10 = A->e10; b10 = B->e10;
      r11 = R->e11; a11 = A->e11; b11 = B->e11;
      an = A->n, bn = B->n;
    }
  else
    {
      /* Transpose */
      r00 = R->e00; a00 = B->e00; b00 = A->e00;
      r01 = R->e10; a01 = B->e10; b01 = A->e10;
      r10 = R->e01; a10 = B->e01; b10 = A->e01;
      r11 = R->e11; a11 = B->e11; b11 = A->e11;
      an = B->n, bn = A->n;
    }
  n = an + bn;
  R->n = n + 1;

  mpn_mul (r00, a00, an, b00, bn);
  mpn_mul (tp, a01, an, b10, bn);
  r00[n] = mpn_add_n (r00, r00, tp, n);

  mpn_mul (r01, a00, an, b01, bn);
  mpn_mul (tp, a01, an, b11, bn);
  r01[n] = mpn_add_n (r01, r01, tp, n);

  mpn_mul (r10, a10, an, b00, bn);
  mpn_mul (tp, a11, an, b10, bn);
  r10[n] = mpn_add_n (r10, r10, tp, n);

  mpn_mul (r11, a10, an, b01, bn);
  mpn_mul (tp, a11, an, b11, bn);
  r11[n] = mpn_add_n (r11, r11, tp, n);
}

static void
one_test (const struct matrix *A, const struct matrix *B, int i)
{
  struct matrix R;
  struct matrix P;
  mp_ptr tp;

  matrix_init (&R, A->n + B->n + 1);
  matrix_init (&P, A->n + B->n + 1);

  tp = refmpn_malloc_limbs (mpn_matrix22_mul_itch (A->n, B->n));

  ref_matrix22_mul (&R, A, B, tp);
  matrix_copy (&P, A);
  mpn_matrix22_mul (P.e00, P.e01, P.e10, P.e11, A->n,
		    B->e00, B->e01, B->e10, B->e11, B->n, tp);
  P.n = A->n + B->n + 1;
  if (!matrix_equal_p (&R, &P))
    {
      fprintf (stderr, "ERROR in test %d\n", i);
      gmp_fprintf (stderr, "A = (%Nx, %Nx\n      %Nx, %Nx)\n"
		   "B = (%Nx, %Nx\n      %Nx, %Nx)\n"
		   "R = (%Nx, %Nx (expected)\n      %Nx, %Nx)\n"
		   "P = (%Nx, %Nx (incorrect)\n      %Nx, %Nx)\n",
		   A->e00, A->n, A->e01, A->n, A->e10, A->n, A->e11, A->n,
		   B->e00, B->n, B->e01, B->n, B->e10, B->n, B->e11, B->n,
		   R.e00, R.n, R.e01, R.n, R.e10, R.n, R.e11, R.n,
		   P.e00, P.n, P.e01, P.n, P.e10, P.n, P.e11, P.n);
      abort();
    }
  refmpn_free_limbs (tp);
  matrix_clear (&R);
  matrix_clear (&P);
}

#define MAX_SIZE (2+2*MATRIX22_STRASSEN_THRESHOLD)

int
main (int argc, char **argv)
{
  struct matrix A;
  struct matrix B;

  gmp_randstate_ptr rands;
  mpz_t bs;
  int i;

  tests_start ();
  rands = RANDS;

  matrix_init (&A, MAX_SIZE);
  matrix_init (&B, MAX_SIZE);
  mpz_init (bs);

  for (i = 0; i < 1000; i++)
    {
      mp_size_t an, bn;
      mpz_urandomb (bs, rands, 32);
      an = 1 + mpz_get_ui (bs) % MAX_SIZE;
      mpz_urandomb (bs, rands, 32);
      bn = 1 + mpz_get_ui (bs) % MAX_SIZE;

      matrix_random (&A, an, rands);
      matrix_random (&B, bn, rands);

      one_test (&A, &B, i);
    }
  mpz_clear (bs);
  matrix_clear (&A);
  matrix_clear (&B);

  tests_end ();
  return 0;
}