3 Copyright 2008 Free Software Foundation, Inc.
5 This file is part of the GNU MP Library.
7 The GNU MP Library is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
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 Lesser General Public
15 License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
30 mp_ptr e00, e01, e10, e11;
34 matrix_init (struct matrix *M, mp_size_t n)
36 mp_ptr p = refmpn_malloc_limbs (4*(n+1));
46 matrix_clear (struct matrix *M)
48 refmpn_free_limbs (M->e00);
52 matrix_copy (struct matrix *R, const struct matrix *M)
55 MPN_COPY (R->e00, M->e00, M->n);
56 MPN_COPY (R->e01, M->e01, M->n);
57 MPN_COPY (R->e10, M->e10, M->n);
58 MPN_COPY (R->e11, M->e11, M->n);
61 /* Used with same size, so no need for normalization. */
63 matrix_equal_p (const struct matrix *A, const struct matrix *B)
66 && mpn_cmp (A->e00, B->e00, A->n) == 0
67 && mpn_cmp (A->e01, B->e01, A->n) == 0
68 && mpn_cmp (A->e10, B->e10, A->n) == 0
69 && mpn_cmp (A->e11, B->e11, A->n) == 0);
73 matrix_random(struct matrix *M, mp_size_t n, gmp_randstate_ptr rands)
76 mpn_random (M->e00, n);
77 mpn_random (M->e01, n);
78 mpn_random (M->e10, n);
79 mpn_random (M->e11, n);
82 #define MUL(rp, ap, an, bp, bn) do { \
84 mpn_mul (rp, ap, an, bp, bn); \
86 mpn_mul (rp, bp, bn, ap, an); \
90 ref_matrix22_mul (struct matrix *R,
91 const struct matrix *A,
92 const struct matrix *B, mp_ptr tp)
95 mp_ptr r00, r01, r10, r11, a00, a01, a10, a11, b00, b01, b10, b11;
99 r00 = R->e00; a00 = A->e00; b00 = B->e00;
100 r01 = R->e01; a01 = A->e01; b01 = B->e01;
101 r10 = R->e10; a10 = A->e10; b10 = B->e10;
102 r11 = R->e11; a11 = A->e11; b11 = B->e11;
103 an = A->n, bn = B->n;
108 r00 = R->e00; a00 = B->e00; b00 = A->e00;
109 r01 = R->e10; a01 = B->e10; b01 = A->e10;
110 r10 = R->e01; a10 = B->e01; b10 = A->e01;
111 r11 = R->e11; a11 = B->e11; b11 = A->e11;
112 an = B->n, bn = A->n;
117 mpn_mul (r00, a00, an, b00, bn);
118 mpn_mul (tp, a01, an, b10, bn);
119 r00[n] = mpn_add_n (r00, r00, tp, n);
121 mpn_mul (r01, a00, an, b01, bn);
122 mpn_mul (tp, a01, an, b11, bn);
123 r01[n] = mpn_add_n (r01, r01, tp, n);
125 mpn_mul (r10, a10, an, b00, bn);
126 mpn_mul (tp, a11, an, b10, bn);
127 r10[n] = mpn_add_n (r10, r10, tp, n);
129 mpn_mul (r11, a10, an, b01, bn);
130 mpn_mul (tp, a11, an, b11, bn);
131 r11[n] = mpn_add_n (r11, r11, tp, n);
135 one_test (const struct matrix *A, const struct matrix *B, int i)
141 matrix_init (&R, A->n + B->n + 1);
142 matrix_init (&P, A->n + B->n + 1);
144 tp = refmpn_malloc_limbs (mpn_matrix22_mul_itch (A->n, B->n));
146 ref_matrix22_mul (&R, A, B, tp);
148 mpn_matrix22_mul (P.e00, P.e01, P.e10, P.e11, A->n,
149 B->e00, B->e01, B->e10, B->e11, B->n, tp);
150 P.n = A->n + B->n + 1;
151 if (!matrix_equal_p (&R, &P))
153 fprintf (stderr, "ERROR in test %d\n", i);
154 gmp_fprintf (stderr, "A = (%Nx, %Nx\n %Nx, %Nx)\n"
155 "B = (%Nx, %Nx\n %Nx, %Nx)\n"
156 "R = (%Nx, %Nx (expected)\n %Nx, %Nx)\n"
157 "P = (%Nx, %Nx (incorrect)\n %Nx, %Nx)\n",
158 A->e00, A->n, A->e01, A->n, A->e10, A->n, A->e11, A->n,
159 B->e00, B->n, B->e01, B->n, B->e10, B->n, B->e11, B->n,
160 R.e00, R.n, R.e01, R.n, R.e10, R.n, R.e11, R.n,
161 P.e00, P.n, P.e01, P.n, P.e10, P.n, P.e11, P.n);
164 refmpn_free_limbs (tp);
169 #define MAX_SIZE (2+2*MATRIX22_STRASSEN_THRESHOLD)
172 main (int argc, char **argv)
177 gmp_randstate_ptr rands;
184 matrix_init (&A, MAX_SIZE);
185 matrix_init (&B, MAX_SIZE);
188 for (i = 0; i < 1000; i++)
191 mpz_urandomb (bs, rands, 32);
192 an = 1 + mpz_get_ui (bs) % MAX_SIZE;
193 mpz_urandomb (bs, rands, 32);
194 bn = 1 + mpz_get_ui (bs) % MAX_SIZE;
196 matrix_random (&A, an, rands);
197 matrix_random (&B, bn, rands);
199 one_test (&A, &B, i);