11#define SWAPINT(a,b) do{ int t=a; a=b; b=t; } while(0)
13#define ERROR(fmt,...) \
15 fprintf (stderr, "%s:%d: *** ERROR in function '%s()' ***\n", __FILE__, __LINE__, __FUNCTION__); \
16 fprintf(stderr, " ␛[31;1m " fmt " ␛[0m\n",##__VA_ARGS__); \
22static inline word
const * mzd_row_cons(
const mzd_t * mat,
const int row){
24#pragma GCC diagnostic push
25#pragma GCC diagnostic ignored "-Wdiscarded-qualifiers"
26 return mzd_row(mat,row);
27#pragma GCC diagnostic pop
36#define SETWD(pos) ((pos)>>6)
37#define SETBT(pos) ((pos)&0x3F)
38#define TIMESWORDSIZE(w) ((w)<<6)
40#define FIRSTBIT(x) __builtin_ctzll(x)
71static inline int m4ri_bitcount(word w){
72 return __builtin_popcountll(w);
77#define MASK(c) (((uint64_t)(-1)) / (__M4RI_TWOPOW(__M4RI_TWOPOW(c)) + 1))
78#define COUNT(x,c) ((x) & MASK(c)) + (((x) >> (__M4RI_TWOPOW(c))) & MASK(c))
80static inline int m4ri_bitcount(word w) {
81 uint64_t n = __M4RI_CONVERT_TO_UINT64_T(w);
91static inline int std_bitcount ( uint64_t x) {
92 uint64_t c1 = UINT64_C (0x5555555555555555 );
93 uint64_t c2 = UINT64_C (0x3333333333333333 );
94 uint64_t c4 = UINT64_C (0x0F0F0F0F0F0F0F0F );
96 x = (( x >> 2) & c2) + (x & c2);
97 x = ( x + (x >> 4) ) & c4;
98 x *= UINT64_C (0x0101010101010101 );
99 return (
int) (x >> 56);
123#if defined(__cplusplus) && !defined (_MSC_VER)
159 static inline int nextelement(
const word *
const set1,
const int m,
const int pos){
168 if (w >= m)
return -1;
169 setwd = set1[w] & (m4ri_ffff<<
SETBT(pos));
299 static inline mzp_t * mzp_rand(mzp_t *q){
301 printf(
"mzp_rand: permutation must be initialized!");
321 mzp_t *
perm_p(mzp_t *q,
const mzp_t *
p,rci_t start);
331 mzp_t *
perm_p_trans(mzp_t *q,
const mzp_t *
p,
const rci_t start);
417static inline void mzd_flip_bit(mzd_t *
const M, rci_t
const row, rci_t
const col ) {
418 word *
const rawrow = mzd_row(M,row);
419 __M4RI_FLIP_BIT(rawrow[col/m4ri_radix], col%m4ri_radix);
429static inline int gauss_one(mzd_t *M,
const int idx,
const int begrow){
431 rci_t startrow = begrow;
435 for(rci_t j = startrow ; j < M->nrows; ++j) {
436 if (mzd_read_bit(M, j, i)) {
437 mzd_row_swap(M, startrow, j);
439 for(rci_t ii = 0 ; ii < M->nrows; ++ii) {
440 if (ii != startrow) {
441 if (mzd_read_bit(M, ii, i)) {
442 mzd_row_add_offset(M, ii, startrow,0);
446 startrow = startrow + 1;
466static inline int sparse_syndrome_non_zero(
const csr_t *
const H,
const int cnt,
const int ee[]){
467 for(
int ir=0; ir < H->
rows; ir++){
469 for(
int iL = H->
p[ir], iE = 0; iL < H->
p[ir+1]; iL++){
471 while((iE < cnt) && (ee[iE] < ic))
512 int do_reduce(mzd_t *row,
const mzd_t *matP0,
const rci_t rankP0);
524 int do_dist_clus(
const csr_t *
const P,
const mzd_t *
const G,
int debug,
int wmax,
int start,
const int rankG);
534#if defined(__cplusplus) && !defined (_MSC_VER)
int syndrome_bit_count(const mzd_t *const row, const csr_t *const spaQ)
Compute the Hamming weight of the syndrome of a dense row vector against spaQ.
mzp_t * mzp_rand_len(mzp_t *q, rci_t length)
Generate a random permutation of a given length in-place.
int do_reduce(mzd_t *row, const mzd_t *matP0, const rci_t rankP0)
Reduce a row vector against a matrix in standard form.
int csr_csr_mul_non_zero(const csr_t *const A, const csr_t *const B)
Check if the product of two sparse matrices A * B^T is non-zero.
void make_err(mzd_t *row, double p)
Generate a random binary error vector with error probability p.
int do_dist_clus(const csr_t *const P, const mzd_t *const G, int debug, int wmax, int start, const int rankG)
(Internal) Run cluster distance algorithm on matrices.
mzd_t * syndrome_vector(mzd_t *syndrome, mzd_t *row, csr_t *spaQ, int clear)
Calculate syndrome vector change.
size_t mzd_weight_row(const mzd_t *A, rci_t i)
Compute the Hamming weight of a specific row in a dense matrix.
csr_t * csr_from_pairs(csr_t *mat, const int nz, int_pair *const prs, const int nrows, const int ncols)
Construct a CSR sparse matrix from an array of coordinate pairs.
mzd_t * mzd_generator_from_csr(mzd_t *G, const csr_t *const H)
Construct the generator matrix from a parity check matrix in CSR form.
csr_t * csr_transpose(csr_t *dst, const csr_t *const p)
Transpose a compressed CSR sparse matrix.
csr_t * csr_mm_read(char *fin, csr_t *mat, int transpose)
Read a sparse matrix from a Matrix Market (.mtx) file into CSR format.
csr_t * Lx_for_CSS_code(const csr_t *const Hx, const csr_t *const Hz)
Construct the logical matrix Lx for a quantum CSS code given Hx and Hz.
csr_t * csr_init(csr_t *mat, int rows, int cols, int nzmax)
Initialize or reallocate a CSR sparse matrix to target size.
mzd_t * mzd_from_csr(mzd_t *dst, const csr_t *p)
Convert a CSR sparse matrix to an MZD dense matrix.
mzp_t * perm_p_trans(mzp_t *q, const mzp_t *p, const rci_t start)
Apply transposed pivot permutation p to permutation q in-place.
void csr_out(const csr_t *mat)
Print raw contents of a CSR matrix to stdout.
void csr_print(const csr_t *const smat, const char str[])
Print a CSR matrix with a label string (formatted output).
csr_t * csr_free(csr_t *p)
Free memory allocated for a CSR sparse matrix.
void csr_compress(csr_t *mat)
Compress CSR matrix (sort indices and remove duplicates/zeros).
int csr_max_row_wght(const csr_t *const p)
Get the maximum row weight of a CSR sparse matrix.
rci_t mzd_gauss_naive(mzd_t *M, mzp_t *q, int full)
Perform Gaussian elimination (naive) and return pivot column list.
size_t mzd_weight(const mzd_t *A)
Compute the number of set bits (Hamming weight) in a dense matrix A.
mzp_t * perm_p(mzp_t *q, const mzp_t *p, rci_t start)
Apply pivot permutation p to permutation q in-place starting from index.
void mzp_out(mzp_t const *p)
Print the permutation to stdout.
size_t mzd_weight_naive(const mzd_t *A)
Naive implementation to compute the Hamming weight of a dense matrix A.
mzd_t * csr_mzd_mul(mzd_t *C, const csr_t *S, const mzd_t *B, int clear)
Multiply a sparse matrix by a dense matrix.
size_t product_weight_csr_mzd(const csr_t *A, const mzd_t *B, int transpose)
Compute the weight of the product of a sparse matrix and a dense matrix.
csr_t * csr_apply_perm(csr_t *dst, const csr_t *const src, const mzp_t *const perm)
Permute columns of a CSR sparse matrix.
int rand_uniform(const int max)
Return a uniformly distributed random integer in the range [0, max-1].