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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
use libc;
use std::mem::size_of;
use ffi::misc::BIT;
use ffi::misc::Rci;
use ffi::misc::Wi;
use ffi::misc::Word;
use ffi::misc::m4ri_one;
use ffi::misc::m4ri_radix;
#[repr(C)]
struct MzdBlock {
private: [u8; 0],
}
#[repr(C)]
pub struct Mzd {
pub nrows: Rci,
pub ncols: Rci,
pub width: Wi,
rowstride: Wi,
offset_vector: Wi,
row_offset: Wi,
flags: u8,
blockrows_log: u8,
padding: [u8; 62 - 2 * size_of::<Rci>() - 4 * size_of::<Wi>() - size_of::<Word>()
- 2 * size_of::<*const libc::c_void>()],
high_bitmask: Word,
blocks: *const MzdBlock,
pub rows: *const *mut Word,
}
extern "C" {
pub fn mzd_init(rows: Rci, columns: Rci) -> *mut Mzd;
fn mzd_free(matrix: *mut Mzd);
pub fn mzd_init_window(
matrix: *mut Mzd,
lowr: Rci,
lowc: Rci,
highr: Rci,
highc: Rci,
) -> *mut Mzd;
pub fn mzd_init_window_const(
matrix: *const Mzd,
lowr: Rci,
lowc: Rci,
highr: Rci,
highc: Rci,
) -> *const Mzd;
pub fn mzd_free_window(matrix: *mut Mzd);
pub fn mzd_row_swap(matrix: *mut Mzd, rowa: Rci, rowb: Rci);
pub fn mzd_copy_row(b: *mut Mzd, a: *const Mzd, j: Rci);
pub fn mzd_col_swap(matrix: *mut Mzd, cola: Rci, colb: Rci);
pub fn mzd_transpose(dest: *mut Mzd, source: *const Mzd) -> *mut Mzd;
pub fn mzd_mul_naive(dest: *mut Mzd, a: *const Mzd, b: *const Mzd) -> *mut Mzd;
pub fn mzd_addmul_naive(c: *mut Mzd, a: *const Mzd, b: *const Mzd) -> *mut Mzd;
pub fn _mzd_mul_va(c: *mut Mzd, v: *const Mzd, a: *const Mzd, clear: libc::c_int) -> *mut Mzd;
pub fn mzd_randomize(m: *mut Mzd);
pub fn mzd_equal(a: *const Mzd, b: *const Mzd) -> libc::c_int;
pub fn mzd_copy(dest: *mut Mzd, src: *const Mzd) -> *mut Mzd;
pub fn mzd_concat(c: *mut Mzd, a: *const Mzd, b: *const Mzd) -> *mut Mzd;
pub fn mzd_set_ui(a: *mut Mzd, n: libc::c_uint);
pub fn mzd_stack(c: *mut Mzd, a: *mut Mzd, b: *const Mzd) -> *mut Mzd;
pub fn mzd_submatrix(
s: *mut Mzd,
m: *const Mzd,
lowr: Rci,
lowc: Rci,
highr: Rci,
highc: Rci,
) -> *mut Mzd;
pub fn mzd_invert_naive(inv: *mut Mzd, a: *const Mzd, identity: *const Mzd) -> *mut Mzd;
pub fn mzd_add(c: *mut Mzd, a: *const Mzd, b: *const Mzd);
pub fn mzd_sub(c: *mut Mzd, a: *const Mzd, b: *const Mzd);
pub fn mzd_is_zero(a: *const Mzd);
}
#[inline]
pub unsafe fn mzd_write_bit(matrix: *mut Mzd, row: Rci, col: Rci, value: BIT) {
let therow: *const *mut Word = (*matrix).rows.offset(row as isize);
let column: *mut Word = (*therow).offset((col / m4ri_radix) as isize);
let pos = col % m4ri_radix;
let column_bitmasked: Word = *column & !(m4ri_one << pos);
let column_newbit: Word = (value as Word & m4ri_one) << pos;
debug_assert_eq!(column_newbit.count_ones(), value as u32);
*column = column_bitmasked | column_newbit;
}
#[inline]
pub unsafe fn mzd_read_bit(matrix: *const Mzd, row: Rci, col: Rci) -> BIT {
let therow: *const *mut Word = (*matrix).rows.offset(row as isize);
let column: Word = *(*therow).offset((col / m4ri_radix) as isize);
let thebit = (column >> (col % m4ri_radix)) & m4ri_one;
thebit as BIT
}
impl Drop for Mzd {
fn drop(&mut self) {
unsafe {
mzd_free(self);
}
}
}
#[cfg(test)]
mod test {
use super::*;
use std::mem;
use std::ptr;
#[test]
fn init() {
let result: libc::c_int;
unsafe {
assert_eq!(mem::size_of::<Mzd>(), 64);
let matrix = mzd_init(10, 10);
assert!(!(*matrix).blocks.is_null());
assert!(!(*matrix).rows.is_null());
mzd_randomize(matrix);
result = mzd_equal(matrix, matrix);
assert_eq!(result, 1);
let m2 = mzd_copy(ptr::null_mut(), matrix);
mzd_randomize(m2);
assert_eq!(mzd_equal(m2, matrix), 0);
}
}
}