File: F2matrix.cpp

package info (click to toggle)
flintqs 1%3A1.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, sid
  • size: 272 kB
  • sloc: cpp: 2,716; makefile: 16
file content (197 lines) | stat: -rw-r--r-- 5,083 bytes parent folder | download | duplicates (3)
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
/*============================================================================
    Copyright 2006 William Hart    

    This file is part of SIMPQS.

    SIMPQS 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 2 of the License, or
    (at your option) any later version.

    SIMPQS 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 SIMPQS; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA

============================================================================*/

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "F2matrix.h"

static u_int32_t bitPattern[]  =
{
  0x80000000, 0x40000000, 0x20000000, 0x10000000,
  0x08000000, 0x04000000, 0x02000000, 0x01000000,
  0x00800000, 0x00400000, 0x00200000, 0x00100000,
  0x00080000, 0x00040000, 0x00020000, 0x00010000,
  0x00008000, 0x00004000, 0x00002000, 0x00001000,
  0x00000800, 0x00000400, 0x00000200, 0x00000100,
  0x00000080, 0x00000040, 0x00000020, 0x00000010,
  0x00000008, 0x00000004, 0x00000002, 0x00000001
};

void insertEntry(matrix m, u_int32_t i, u_int32_t j)
{
     m[i][j / 32] |= bitPattern[j % 32];
     
     return;
}

void xorEntry(matrix m, u_int32_t i, u_int32_t j)
{
     m[i][j / 32] ^= bitPattern[j % 32];
     
     return;
}

u_int32_t getEntry(matrix m, u_int32_t i, u_int32_t j)
{
     return m[i][j / 32] & bitPattern[j % 32];
}

void swapRows(matrix m, u_int32_t x, u_int32_t y)
{
     row temp;
     
     temp = m[x];
     m[x] = m[y];
     m[y] = temp;
     
     return;
}


void clearRow(matrix m, u_int32_t numcols, u_int32_t row)
{
    int32_t dwords = numcols/32;
    
    if (numcols%32) dwords++;
    memset(m[row],0,dwords*4);
    
    return; 
}

void displayRow(matrix m, u_int32_t row, u_int32_t numPrimes)
{
     int32_t length = numPrimes/32;
     if (numPrimes%32) length++;
     length*=64;
     
     printf("[");
     for (int32_t j = 0; j < length/2; j++)
     {
        if (getEntry(m,row,j)) printf("1");
        else printf("0");
     }
     printf("  ");
     for (int32_t j = length/2; j < length; j++)
     {
        if (getEntry(m,row,j)) printf("1");
        else printf("0");
     }
     printf("]\n");
     return;
}

void xorRows(matrix m, u_int32_t source, u_int32_t dest, u_int32_t length)
{
  u_int32_t i, q, r;
  row x = m[dest];
  row y = m[source];
  
  r = length % 8; q = length - r;
  for (i=0; i < q; i += 8)
  {
    x[i] ^= y[i]; x[1+i] ^= y[1+i]; x[2+i] ^= y[2+i]; x[3+i] ^= y[3+i];
    x[4+i] ^= y[4+i]; x[5+i] ^= y[5+i]; x[6+i] ^= y[6+i]; x[7+i] ^= y[7+i];
  }
  switch (r)
  {
    case 7: x[i] ^= y[i]; i++;
    case 6: x[i] ^= y[i]; i++;
    case 5: x[i] ^= y[i]; i++;
    case 4: x[i] ^= y[i]; i++;
    case 3: x[i] ^= y[i]; i++;
    case 2: x[i] ^= y[i]; i++;
    case 1: x[i] ^= y[i]; i++;
  }
  
  return;
}

matrix constructMat(u_int32_t cols, u_int32_t rows)
{
     static matrix m;
     
     u_int32_t dwords = cols/32;
     if (cols%32) dwords++;
     m = (row *) calloc(sizeof(row),rows);
     if (m==NULL) 
     {
       printf("Unable to allocate memory for matrix!\n");
       exit(1);
     }

     for (u_int32_t i = 0; i < rows; i++)
     {
         m[i] = (row) calloc(2*dwords,sizeof(u_int32_t));  //two matrices, side by side
     }
     if (m[rows-1]==NULL) 
     {
        printf("Unable to allocate memory for matrix!\n");
        exit(1);
     }
     
     for (int32_t i = 0; i < rows; i++)  //make second matrix identity, i.e. 1's along diagonal
     {
         insertEntry(m,i,i+32*dwords);
     }
     
     return m;
}

//===========================================================================
// gaussReduce:
//
// Function: Apply Gaussian elimination to a matrix.
//
//===========================================================================
u_int32_t gaussReduce(matrix m, u_int32_t numPrimes, u_int32_t relSought,int32_t extras)
{
     static u_int32_t rowUpto = 0;
     static u_int32_t irow;
     static u_int32_t length = (numPrimes+extras)/32;
     
     if (numPrimes%32) length++;
     length*=2;
     
     for (int32_t icol = numPrimes-1; icol >= 0; icol--)
     {
         irow = rowUpto;
         while ((irow < relSought)&&(getEntry(m,irow,icol)==0UL)) irow++;
         if (irow < relSought) 
         {
             
             swapRows(m,rowUpto,irow);
             
             for (u_int32_t checkRow = rowUpto+1; checkRow<relSought; checkRow++)
             {
                 if (getEntry(m,checkRow,icol)!=0UL) 
                 {
                    xorRows(m,rowUpto,checkRow,length);
                 }
             }
             
             rowUpto++;
         }
     }
          
     return rowUpto;
}