33
44#include "structures.h"
55
6- void build_sparse_matrix (dyn_array_classic * A , dyn_array relations , dyn_array_classic primes , unsigned long * nonzero , double * density , unsigned long * nb_lines , dyn_array_classic * rel_weight )
6+ void build_sparse_matrix (dyn_array_classic * matrix , dyn_array relations , dyn_array_classic factor_base , unsigned long * nonzero_count , double * density , unsigned long * row_count , dyn_array_classic * rel_weight )
77{
8- for (unsigned long i = 0 ; i < relations .len ; i ++ ) append_classic (rel_weight ,0 );
9- * nonzero = 0 ;
10- * nb_lines = 0 ;
11- mpz_t tmp2 ;
12- mpz_init (tmp2 );
13- for (unsigned long i = 0 ; i < relations .len ; i ++ )
8+ mpz_t current_prime , tmp_mpz ;
9+ mpz_inits (current_prime , tmp_mpz , NULL );
10+
11+ * nonzero_count = 0 ;
12+ * row_count = 0 ;
13+
14+ for (size_t i = 0 ; i < relations .len ; i ++ ) append_classic (rel_weight , 0 );
15+
16+ for (size_t i = 0 ; i < relations .len ; i ++ )
1417 {
15- if (mpz_cmp_ui (* (relations .start + i ),0 ) < 0 )
18+ if (mpz_cmp_ui (* (relations .start + i ), 0 ) < 0 )
1619 {
17- append_classic (A , i );
18- * nonzero += 1 ;
19- * ( rel_weight -> start + i ) += 1 ;
20+ append_classic (matrix , i );
21+ ( * nonzero_count ) ++ ;
22+ rel_weight -> start [ i ] ++ ;
2023 }
2124 }
22- append_classic (A ,relations .len );
23- * nb_lines += 1 ;
24- for (unsigned long i = 0 ; i < primes .len ; i ++ )
25+
26+ append_classic (matrix , relations .len );
27+ (* row_count )++ ;
28+
29+ bool parity_bit ;
30+ unsigned int bit_count ;
31+
32+ for (size_t i = 0 ; i < factor_base .len ; i ++ )
2533 {
26- for (unsigned long j = 0 ; j < relations .len ; j ++ )
34+ mpz_set_ui (current_prime , * (factor_base .start + i ));
35+
36+ for (size_t j = 0 ; j < relations .len ; j ++ )
2737 {
28- unsigned long tmp = 0 ;
29- mpz_set_ui (tmp2 ,* (primes .start + i ));
30- while (mpz_divisible_p (* (relations .start + j ),tmp2 ))
31- {
32- tmp ^= 1 ;
33- mpz_mul_ui (tmp2 ,tmp2 ,* (primes .start + i ));
34- }
35- if (tmp )
38+ bit_count = mpz_remove (tmp_mpz , * (relations .start + j ), current_prime );
39+ parity_bit = bit_count & 1 ;
40+
41+ if (parity_bit )
3642 {
37- append_classic (A , j );
38- * nonzero += 1 ;
39- * ( rel_weight -> start + j ) += 1 ;
43+ append_classic (matrix , j );
44+ ( * nonzero_count ) ++ ;
45+ rel_weight -> start [ j ] ++ ;
4046 }
4147 }
42- if (* (A -> start + A -> len - 1 ) != relations .len )
48+ if (* (matrix -> start + matrix -> len - 1 ) != relations .len )
4349 {
44- append_classic (A , relations .len );
45- * nb_lines += 1 ;
50+ append_classic (matrix , relations .len );
51+ ( * row_count ) ++ ;
4652 }
4753 }
48- * density = (double ) (* nonzero )/(* nb_lines );
49- mpz_clear ( tmp2 );
54+ * density = (double ) (* nonzero_count )/(* row_count );
55+ mpz_clears ( current_prime , tmp_mpz , NULL );
5056}
5157
52- void build_dense_matrix (dyn_array relations , dyn_array_classic primes , unsigned long relations_len , unsigned long base_size , mpz_t * dense_matrix )
58+ void build_dense_matrix (dyn_array relations , dyn_array_classic factor_base , unsigned long relations_len , unsigned long base_size , mpz_t * dense_matrix )
5359{
5460
55- mpz_t tmp , tmp2 ;
56- mpz_inits (tmp , tmp2 , NULL );
61+ mpz_t accumulator , current_prime , tmp_mpz ;
62+ mpz_inits (accumulator , current_prime , tmp_mpz , NULL );
5763
5864 mpz_t * restrict DM = dense_matrix ;
5965 mpz_t * restrict RELS = relations .start ;
60- unsigned long * restrict PRIMES = primes .start ;
66+ unsigned long * restrict PRIMES = factor_base .start ;
6167
62- unsigned long tmp_char ;
68+ bool parity_bit ;
69+ unsigned int bit_count ;
6370
64- for (unsigned long i = 0 ; i < relations_len ; i ++ )
71+ for (size_t i = 0 ; i < relations_len ; i ++ )
6572 {
66- if (mpz_cmp_ui (* ( RELS + i ) , 0 ) < 0 ) mpz_set_ui (tmp , 1 );
67- else mpz_set_ui (tmp , 0 );
73+ if (mpz_cmp_ui (RELS [ i ] , 0 ) < 0 ) mpz_set_ui (accumulator , 1 );
74+ else mpz_set_ui (accumulator , 0 );
6875
69- for (unsigned long j = 0 ; j < primes .len ; j ++ )
76+ for (size_t j = 0 ; j < factor_base .len ; j ++ )
7077 {
71- mpz_mul_2exp (tmp , tmp , 1 );
78+ mpz_set_ui (current_prime , PRIMES [j ]);
79+ mpz_mul_2exp (accumulator , accumulator , 1 );
7280
73- tmp_char = 0 ;
74- mpz_set_ui (tmp2 , * (PRIMES + j ));
75-
76- while (mpz_divisible_p (* (RELS + i ), tmp2 ))
77- {
78- tmp_char ^= 1 ;
79- mpz_mul_ui (tmp2 , tmp2 , * (PRIMES + j ));
80- }
81+ bit_count = mpz_remove (tmp_mpz , RELS [i ], current_prime );
82+ parity_bit = bit_count & 1 ;
8183
82- mpz_add_ui (tmp , tmp , tmp_char );
84+ mpz_add_ui (accumulator , accumulator , parity_bit );
8385 }
8486
85- mpz_set (DM [i ], tmp );
87+ mpz_set (DM [i ], accumulator );
8688
8789 }
88- mpz_clears (tmp , tmp2 , NULL );
90+ mpz_clears (accumulator , current_prime , tmp_mpz , NULL );
8991}
0 commit comments