35 #include "m4ri_config.h" 42 #include <emmintrin.h> 56 #define __M4RI_SSE2_CUTOFF 10 65 #define __M4RI_MAX_MZD_BLOCKSIZE (((size_t)1) << 27) 75 #define __M4RI_MUL_BLOCKSIZE MIN(((int)sqrt((double)(4 * __M4RI_CPU_L2_CACHE))) / 2, 2048) 162 #if 0 // Commented out in order to keep the size of mzd_t 64 bytes (one cache line). This could be added back if rows was ever removed. 204 static uint8_t
const mzd_flag_nonzero_offset = 0x1;
205 static uint8_t
const mzd_flag_nonzero_excess = 0x2;
206 static uint8_t
const mzd_flag_windowed_zerooffset = 0x4;
207 static uint8_t
const mzd_flag_windowed_zeroexcess = 0x8;
208 static uint8_t
const mzd_flag_windowed_ownsblocks = 0x10;
209 static uint8_t
const mzd_flag_multiple_blocks = 0x20;
219 return M->
flags & (mzd_flag_nonzero_offset | mzd_flag_windowed_zerooffset);
243 assert(M->
nrows == 0 || result == M->
rows[0]);
299 return n ? 0 : M->
nrows;
313 word* result = M->
blocks[0].begin + big_vector;
316 result = M->
blocks[n].begin + big_vector - n * (M->
blocks[0].size /
sizeof(
word));
318 assert(result == M->
rows[row]);
383 #define mzd_free_window mzd_free 395 if ((rowa == rowb) || (startblock >= M->
width))
404 word *a = M->
rows[rowa] + startblock;
405 word *b = M->
rows[rowb] + startblock;
410 for(
wi_t i = 0; i < width; ++i) {
416 tmp = (a[width] ^ b[width]) & mask_end;
420 __M4RI_DD_ROW(M, rowa);
421 __M4RI_DD_ROW(M, rowb);
442 word tmp = (a[0] ^ b[0]) & mask_begin;
447 for(
wi_t i = 1; i < width; ++i) {
452 tmp = (a[width] ^ b[width]) & mask_end;
462 __M4RI_DD_ROW(M, rowa);
463 __M4RI_DD_ROW(M, rowb);
514 int max_bit =
MAX(a_bit, b_bit);
515 int count_remaining = stop_row - start_row;
516 int min_bit = a_bit + b_bit - max_bit;
518 int offset = max_bit - min_bit;
525 if (a_word == b_word) {
527 count_remaining -= count;
529 int fast_count = count / 4;
530 int rest_count = count - 4 * fast_count;
533 while (fast_count--) {
535 xor_v[1] = ptr[rowstride];
536 xor_v[2] = ptr[2 * rowstride];
537 xor_v[3] = ptr[3 * rowstride];
538 xor_v[0] ^= xor_v[0] >> offset;
539 xor_v[1] ^= xor_v[1] >> offset;
540 xor_v[2] ^= xor_v[2] >> offset;
541 xor_v[3] ^= xor_v[3] >> offset;
546 xor_v[0] |= xor_v[0] << offset;
547 xor_v[1] |= xor_v[1] << offset;
548 xor_v[2] |= xor_v[2] << offset;
549 xor_v[3] |= xor_v[3] << offset;
551 ptr[rowstride] ^= xor_v[1];
552 ptr[2 * rowstride] ^= xor_v[2];
553 ptr[3 * rowstride] ^= xor_v[3];
554 ptr += 4 * rowstride;
556 while (rest_count--) {
558 xor_v ^= xor_v >> offset;
560 *ptr ^= xor_v | (xor_v << offset);
570 if (min_bit == a_bit) {
571 min_ptr = ptr + a_word;
572 max_offset = b_word - a_word;
574 min_ptr = ptr + b_word;
575 max_offset = a_word - b_word;
578 count_remaining -= count;
581 word xor_v = (min_ptr[0] ^ (min_ptr[max_offset] >> offset)) & mask;
583 min_ptr[max_offset] ^= xor_v << offset;
584 min_ptr += rowstride;
589 if (min_bit == a_bit)
590 min_ptr = ptr + a_word;
592 min_ptr = ptr + b_word;
644 M->
rows[x][block] ^= values << spot;
647 M->
rows[x][block + 1] ^= values >> space;
666 M->
rows[x][block] &= values << spot;
669 M->
rows[x][block + 1] &= values >> space;
686 M->
rows[x][block] &= ~(values << spot);
689 M->
rows[x][block + 1] &= ~(values >> space);
705 assert(dstrow < M->nrows && srcrow < M->nrows && coloffset < M->ncols);
709 word *src = M->
rows[srcrow] + startblock;
710 word *dst = M->
rows[dstrow] + startblock;
714 *dst++ ^= *src++ & mask_begin;
719 if (wide > not_aligned + 1)
726 __m128i* __src = (__m128i*)src;
727 __m128i* __dst = (__m128i*)dst;
728 __m128i*
const eof = (__m128i*)((
unsigned long)(src + wide) & ~0xFUL);
731 __m128i xmm1 = _mm_xor_si128(*__dst, *__src);
734 while(++__src < eof);
737 wide = ((
sizeof(
word)*wide)%16)/
sizeof(
word);
750 dst[i - 1] ^= src[i - 1] & ~mask_end;
752 __M4RI_DD_ROW(M, dstrow);
1044 #define mzd_sub mzd_add 1056 #define _mzd_sub _mzd_add 1073 word temp = (spill <= 0) ? M->
rows[x][block] << -spill : (M->
rows[x][block + 1] << (
m4ri_radix - spill)) | (M->
rows[x][block] >> spill);
1161 wi_t wide = A->
width - a_startblock - 1;
1163 word *a = A->
rows[a_row] + a_startblock;
1164 word *b = B->
rows[b_row] + b_startblock;
1166 #if __M4RI_HAVE_SSE2 1175 __m128i *a128 = (__m128i*)a;
1176 __m128i *b128 = (__m128i*)b;
1177 const __m128i *eof = (__m128i*)((
unsigned long)(a + wide) & ~0xFUL);
1180 *a128 = _mm_xor_si128(*a128, *b128);
1183 }
while(a128 < eof);
1187 wide = ((
sizeof(
word) * wide) % 16) /
sizeof(
word);
1190 #endif // __M4RI_HAVE_SSE2 1193 wi_t n = (wide + 7) / 8;
1195 case 0:
do { *(a++) ^= *(b++);
1196 case 7: *(a++) ^= *(b++);
1197 case 6: *(a++) ^= *(b++);
1198 case 5: *(a++) ^= *(b++);
1199 case 4: *(a++) ^= *(b++);
1200 case 3: *(a++) ^= *(b++);
1201 case 2: *(a++) ^= *(b++);
1202 case 1: *(a++) ^= *(b++);
1236 wi_t wide = A->
width - a_startblock - 1;
1237 word *a = A->
rows[a_row] + a_startblock;
1238 word *b = B->
rows[b_row] + b_startblock;
1239 word *c = C->
rows[c_row] + c_startblock;
1250 #if __M4RI_HAVE_SSE2 1259 __m128i *a128 = (__m128i*)a;
1260 __m128i *b128 = (__m128i*)b;
1261 __m128i *c128 = (__m128i*)c;
1262 const __m128i *eof = (__m128i*)((
unsigned long)(a + wide) & ~0xFUL);
1265 *c128 = _mm_xor_si128(*a128, *b128);
1269 }
while(a128 < eof);
1274 wide = ((
sizeof(
word) * wide) % 16) /
sizeof(
word);
1277 #endif // __M4RI_HAVE_SSE2 1280 wi_t n = (wide + 7) / 8;
1282 case 0:
do { *(c++) = *(a++) ^ *(b++);
1283 case 7: *(c++) = *(a++) ^ *(b++);
1284 case 6: *(c++) = *(a++) ^ *(b++);
1285 case 5: *(c++) = *(a++) ^ *(b++);
1286 case 4: *(c++) = *(a++) ^ *(b++);
1287 case 3: *(c++) = *(a++) ^ *(b++);
1288 case 2: *(c++) = *(a++) ^ *(b++);
1289 case 1: *(c++) = *(a++) ^ *(b++);
1398 unsigned long long hash = 0;
static void mzd_clear_bits(mzd_t const *M, rci_t const x, rci_t const y, int const n)
Clear n bits in M starting a position (x,y).
Definition: mzd.h:681
void mzd_copy_row(mzd_t *B, rci_t i, mzd_t const *A, rci_t j)
copy row j from A to row i from B.
Definition: mzd.c:2236
mzd_t * mzd_stack(mzd_t *C, mzd_t const *A, mzd_t const *B)
Stack A on top of B and write the result to C.
Definition: mzd.c:1846
#define __M4RI_WRITE_BIT(w, spot, value)
Write the value to the bit spot in the word w.
Definition: misc.h:236
static void mzd_combine_weird(mzd_t *C, rci_t const c_row, wi_t const c_startblock, mzd_t const *A, rci_t const a_row, wi_t const a_startblock, mzd_t const *B, rci_t const b_row, wi_t const b_startblock)
c_row[c_startblock:] = a_row[a_startblock:] + b_row[b_startblock:] for different offsets ...
Definition: mzd.h:1121
rci_t mzd_gauss_delayed(mzd_t *M, rci_t const startcol, int const full)
Gaussian elimination.
Definition: mzd.c:298
static word * mzd_first_row_next_block(mzd_t const *M, int n)
Get a pointer to the first word in block n.
Definition: mzd.h:257
mzd_t * mzd_invert_naive(mzd_t *INV, mzd_t const *A, mzd_t const *I)
Invert the matrix target using Gaussian elimination.
Definition: mzd.c:1880
static word mzd_read_bits(mzd_t const *M, rci_t const x, rci_t const y, int const n)
Definition: mzd.h:1069
word high_bitmask
Definition: mzd.h:175
struct mzd_t mzd_t
Dense matrices over GF(2).
mzd_t * mzd_addmul_naive(mzd_t *C, mzd_t const *A, mzd_t const *B)
Naive cubic matrix multiplication and addition.
Definition: mzd.c:1426
mzd_block_t * blocks
Definition: mzd.h:188
int BIT
Pretty for a boolean int.
Definition: misc.h:58
#define __M4RI_RIGHT_BITMASK(n)
create a bit mask to zero out all but the n rightmost bits.
Definition: misc.h:296
static int const m4ri_radix
The number of bits in a word.
Definition: misc.h:141
mzd_t * mzd_mul_naive(mzd_t *C, mzd_t const *A, mzd_t const *B)
Naive cubic matrix multiplication.
Definition: mzd.c:1408
uint8_t flags
Definition: mzd.h:153
static mzd_t const * mzd_init_window_const(mzd_t const *M, rci_t const lowr, rci_t const lowc, rci_t const highr, rci_t const highc)
Create a const window/view into a const matrix M.
Definition: mzd.h:372
Dense matrices over GF(2).
Definition: mzd.h:89
static void mzd_combine_even_in_place(mzd_t *A, rci_t const a_row, wi_t const a_startblock, mzd_t const *B, rci_t const b_row, wi_t const b_startblock)
a_row[a_startblock:] += b_row[b_startblock:] for offset 0
Definition: mzd.h:1158
int rci_t
Type of row and column indexes.
Definition: misc.h:66
#define MIN(x, y)
Return the minimal element of x and y.
Definition: misc.h:174
mzd_t * mzd_submatrix(mzd_t *S, mzd_t const *M, rci_t const lowr, rci_t const lowc, rci_t const highr, rci_t const highc)
Copy a submatrix.
Definition: mzd.c:2016
int mzd_equal(mzd_t const *A, mzd_t const *B)
Return TRUE if A == B.
Definition: mzd.c:1625
mzd_t * _mzd_mul_va(mzd_t *C, mzd_t const *v, mzd_t const *A, int const clear)
Matrix multiplication optimized for v*A where v is a vector.
Definition: mzd.c:1533
static void mzd_and_bits(mzd_t const *M, rci_t const x, rci_t const y, int const n, word values)
AND n bits from values to M starting a position (x,y).
Definition: mzd.h:660
mzd_t * mzd_concat(mzd_t *C, mzd_t const *A, mzd_t const *B)
Concatenate B to A and write the result to C.
Definition: mzd.c:1814
void mzd_free(mzd_t *A)
Free a matrix created with mzd_init.
Definition: mzd.c:281
static word const m4ri_ffff
A word with all bits set.
Definition: misc.h:153
mzd_t * mzd_transpose(mzd_t *DST, mzd_t const *A)
Transpose a matrix.
Definition: mzd.c:1377
static void mzd_write_bit(mzd_t *M, rci_t const row, rci_t const col, BIT const value)
Write the bit value to position M[row,col].
Definition: mzd.h:626
#define __M4RI_SSE2_CUTOFF
SSE2 cutoff in words.
Definition: mzd.h:56
mzd_t * mzd_add(mzd_t *C, mzd_t const *A, mzd_t const *B)
Set C = A+B.
Definition: mzd.c:1901
mzd_t * mzd_extract_l(mzd_t *L, mzd_t const *A)
Definition: mzd.c:2495
mzd_t * _mzd_mul_naive(mzd_t *C, mzd_t const *A, mzd_t const *B, int const clear)
Naive cubic matrix multiplication with the pre-transposed B.
Definition: mzd.c:1441
rci_t mzd_echelonize_naive(mzd_t *M, int const full)
Gaussian elimination.
Definition: mzd.c:327
rci_t nrows
Definition: mzd.h:94
static unsigned long long mzd_hash(mzd_t const *A)
Return hash value for matrix.
Definition: mzd.h:1397
static void mzd_col_swap_in_rows(mzd_t *M, rci_t const cola, rci_t const colb, rci_t const start_row, rci_t const stop_row)
Swap the two columns cola and colb but only between start_row and stop_row.
Definition: mzd.h:500
static void _mzd_row_swap(mzd_t *M, rci_t const rowa, rci_t const rowb, wi_t const startblock)
Swap the two rows rowa and rowb starting at startblock.
Definition: mzd.h:394
word ** rows
Definition: mzd.h:195
#define MAX(x, y)
Return the maximal element of x and y.
Definition: misc.h:163
mzd_t * mzd_init_window(mzd_t *M, rci_t const lowr, rci_t const lowc, rci_t const highr, rci_t const highc)
Create a window/view into the matrix M.
Definition: mzd.c:234
void mzd_randomize(mzd_t *M)
Fill matrix M with uniformly distributed bits.
Definition: mzd.c:1553
#define __M4RI_ALIGNMENT(addr, n)
Return alignment of addr w.r.t. n. For example the address 17 would be 1 aligned w.r.t. 16.
Definition: misc.h:421
mzd_t * mzd_copy(mzd_t *DST, mzd_t const *A)
Copy matrix A to DST.
Definition: mzd.c:1760
void mzd_row_add(mzd_t *M, rci_t const sourcerow, rci_t const destrow)
Add the rows sourcerow and destrow and stores the total in the row destrow.
Definition: mzd.c:294
static wi_t mzd_rows_in_block(mzd_t const *M, int n)
Total number of rows in this block.
Definition: mzd.h:288
static wi_t const mzd_paddingwidth
The minimum width where padding occurs.
Definition: mzd.h:202
wi_t offset_vector
Definition: mzd.h:126
#define RESTRICT
Definition: misc.h:703
mzd_t * _mzd_add(mzd_t *C, mzd_t const *A, mzd_t const *B)
Same as mzd_add but without any checks on the input.
Definition: mzd.c:1915
wi_t row_offset
Definition: mzd.h:132
static int mzd_row_to_block(mzd_t const *M, rci_t row)
Convert row to blocks index.
Definition: mzd.h:271
static int mzd_read_bits_int(mzd_t const *M, rci_t const x, rci_t const y, int const n)
Get n bits starting a position (x,y) from the matrix M.
Definition: mzd.h:1307
mzd_t * mzd_extract_u(mzd_t *U, mzd_t const *A)
Definition: mzd.c:2479
static void mzd_row_swap(mzd_t *M, rci_t const rowa, rci_t const rowb)
Swap the two rows rowa and rowb.
Definition: mzd.h:432
static int mzd_is_windowed(mzd_t const *M)
Test if a matrix is windowed.
Definition: mzd.h:218
static int mzd_owns_blocks(mzd_t const *M)
Test if this mzd_t should free blocks.
Definition: mzd.h:229
uint16_t offset
Definition: mzd.h:138
mzd_t * mzd_init(rci_t const r, rci_t const c)
Create a new matrix of dimension r x c.
Definition: mzd.c:146
word low_bitmask
Definition: mzd.h:181
#define __M4RI_LEFT_BITMASK(n)
create a bit mask to zero out all but the (n - 1) % m4ri_radix + 1 leftmost bits. ...
Definition: misc.h:271
static word * mzd_first_row(mzd_t const *M)
Get a pointer the first word.
Definition: mzd.h:241
double _mzd_density(mzd_t const *A, wi_t res, rci_t r, rci_t c)
Return the number of nonzero entries divided by nrows * ncols considering only the submatrix starting...
Definition: mzd.c:2417
uint8_t blockrows_log
Definition: mzd.h:160
static void mzd_combine_even(mzd_t *C, rci_t const c_row, wi_t const c_startblock, mzd_t const *A, rci_t const a_row, wi_t const a_startblock, mzd_t const *B, rci_t const b_row, wi_t const b_startblock)
c_row[c_startblock:] = a_row[a_startblock:] + b_row[b_startblock:] for offset 0
Definition: mzd.h:1232
static word const m4ri_one
The number one as a word.
Definition: misc.h:147
int mzd_is_zero(mzd_t const *A)
Zero test for matrix.
Definition: mzd.c:2185
double mzd_density(mzd_t const *A, wi_t res)
Return the number of nonzero entries divided by nrows * ncols.
Definition: mzd.c:2454
void mzd_col_swap(mzd_t *M, rci_t const cola, rci_t const colb)
Swap the two columns cola and colb.
Definition: mzd.c:2091
void mzd_set_ui(mzd_t *M, unsigned int const value)
Set the matrix M to the value equivalent to the integer value provided.
Definition: mzd.c:1593
wi_t rowstride
Definition: mzd.h:117
int mzd_find_pivot(mzd_t const *M, rci_t start_row, rci_t start_col, rci_t *r, rci_t *c)
Find the next nonzero entry in M starting at start_row and start_col.
Definition: mzd.c:2282
void mzd_combine(mzd_t *DST, rci_t const row3, wi_t const startblock3, mzd_t const *SC1, rci_t const row1, wi_t const startblock1, mzd_t const *SC2, rci_t const row2, wi_t const startblock2)
row3[col3:] = row1[col1:] + row2[col2:]
Definition: mzd.c:2068
static word * mzd_row(mzd_t const *M, rci_t row)
Get pointer to first word of row.
Definition: mzd.h:311
static void mzd_row_add_offset(mzd_t *M, rci_t dstrow, rci_t srcrow, rci_t coloffset)
Add the rows sourcerow and destrow and stores the total in the row destrow, but only begins at the co...
Definition: mzd.h:704
rci_t ncols
Definition: mzd.h:100
static BIT mzd_read_bit(mzd_t const *M, rci_t const row, rci_t const col)
Read the bit at position M[row,col].
Definition: mzd.h:610
int mzd_cmp(mzd_t const *A, mzd_t const *B)
Return -1,0,1 if if A < B, A == B or A > B respectively.
Definition: mzd.c:1712
#define __M4RI_UNLIKELY(cond)
Macro to help with branch prediction.
Definition: misc.h:449
wi_t width
Definition: mzd.h:108
uint64_t word
A word is the typical packed data structure to represent packed bits.
Definition: misc.h:85
int wi_t
Type of word indexes.
Definition: misc.h:74
#define __M4RI_GET_BIT(w, spot)
Get the bit spot (counting from the left) in the word w.
Definition: misc.h:226
void mzd_row_clear_offset(mzd_t *M, rci_t const row, rci_t const coloffset)
Clear the given row, but only begins at the column coloffset.
Definition: mzd.c:2261
static void mzd_xor_bits(mzd_t const *M, rci_t const x, rci_t const y, int const n, word values)
XOR n bits from values to M starting a position (x,y).
Definition: mzd.h:641
#define __M4RI_CONVERT_TO_INT(w)
Explicit conversion macro.
Definition: misc.h:97
rci_t mzd_first_zero_row(mzd_t const *A)
Return the first row with all zero entries.
Definition: mzd.c:2458