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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
use crate::ffi::c_char;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum BLASLayout {
    #[default]
    Undefined = -1,
    RowMajor = 101,
    ColMajor = 102,
    // extension of current crate
    Sequential = 103,
    NonContiguous = 104,
}

pub type BALSOrder = BLASLayout;
pub use BLASLayout::{ColMajor as BLASColMajor, RowMajor as BLASRowMajor};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum BLASTranspose {
    #[default]
    Undefined = -1,
    NoTrans = 111,
    Trans = 112,
    ConjTrans = 113,
    ConjNoTrans = 114,
}

pub use BLASTranspose::{ConjTrans as BLASConjTrans, NoTrans as BLASNoTrans, Trans as BLASTrans};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum BLASUpLo {
    #[default]
    Undefined = -1,
    Upper = 121,
    Lower = 122,
}

pub use BLASUpLo::{Lower as BLASLower, Upper as BLASUpper};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum BLASDiag {
    #[default]
    Undefined = -1,
    NonUnit = 131,
    Unit = 132,
}

pub use BLASDiag::{NonUnit as BLASNonUnit, Unit as BLASUnit};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum BLASSide {
    #[default]
    Undefined = -1,
    Left = 141,
    Right = 142,
}

pub use BLASSide::{Left as BLASLeft, Right as BLASRight};

impl From<char> for BLASLayout {
    #[inline]
    fn from(c: char) -> Self {
        match c.to_ascii_uppercase() {
            'R' => BLASRowMajor,
            'C' => BLASColMajor,
            _ => panic!("Invalid character for BLASOrder: {}", c),
        }
    }
}

impl From<BLASLayout> for char {
    #[inline]
    fn from(layout: BLASLayout) -> Self {
        match layout {
            BLASRowMajor => 'R',
            BLASColMajor => 'C',
            _ => panic!("Invalid BLASOrder: {:?}", layout),
        }
    }
}

impl From<BLASLayout> for c_char {
    #[inline]
    fn from(layout: BLASLayout) -> Self {
        match layout {
            BLASRowMajor => 'R' as c_char,
            BLASColMajor => 'C' as c_char,
            _ => panic!("Invalid BLASOrder: {:?}", layout),
        }
    }
}

impl From<char> for BLASTranspose {
    #[inline]
    fn from(c: char) -> Self {
        match c.to_ascii_uppercase() {
            'N' => BLASNoTrans,
            'T' => BLASTrans,
            'C' => BLASConjTrans,
            _ => panic!("Invalid character for BLASTrans: {}", c),
        }
    }
}

impl From<BLASTranspose> for char {
    #[inline]
    fn from(trans: BLASTranspose) -> Self {
        match trans {
            BLASNoTrans => 'N',
            BLASTrans => 'T',
            BLASConjTrans => 'C',
            _ => panic!("Invalid BLASTrans: {:?}", trans),
        }
    }
}

impl From<BLASTranspose> for c_char {
    #[inline]
    fn from(trans: BLASTranspose) -> Self {
        match trans {
            BLASNoTrans => 'N' as c_char,
            BLASTrans => 'T' as c_char,
            BLASConjTrans => 'C' as c_char,
            _ => panic!("Invalid BLASTrans: {:?}", trans),
        }
    }
}

impl From<char> for BLASUpLo {
    #[inline]
    fn from(c: char) -> Self {
        match c.to_ascii_uppercase() {
            'U' => BLASUpper,
            'L' => BLASLower,
            _ => panic!("Invalid character for BLASUpLo: {}", c),
        }
    }
}

impl From<BLASUpLo> for char {
    #[inline]
    fn from(uplo: BLASUpLo) -> Self {
        match uplo {
            BLASUpper => 'U',
            BLASLower => 'L',
            _ => panic!("Invalid BLASUpLo: {:?}", uplo),
        }
    }
}

impl From<BLASUpLo> for c_char {
    #[inline]
    fn from(uplo: BLASUpLo) -> Self {
        match uplo {
            BLASUpper => 'U' as c_char,
            BLASLower => 'L' as c_char,
            _ => panic!("Invalid BLASUpLo: {:?}", uplo),
        }
    }
}

impl From<char> for BLASDiag {
    #[inline]
    fn from(c: char) -> Self {
        match c.to_ascii_uppercase() {
            'N' => BLASNonUnit,
            'U' => BLASUnit,
            _ => panic!("Invalid character for BLASDiag: {}", c),
        }
    }
}

impl From<BLASDiag> for char {
    #[inline]
    fn from(diag: BLASDiag) -> Self {
        match diag {
            BLASNonUnit => 'N',
            BLASUnit => 'U',
            _ => panic!("Invalid BLASDiag: {:?}", diag),
        }
    }
}

impl From<BLASDiag> for c_char {
    #[inline]
    fn from(diag: BLASDiag) -> Self {
        match diag {
            BLASNonUnit => 'N' as c_char,
            BLASUnit => 'U' as c_char,
            _ => panic!("Invalid BLASDiag: {:?}", diag),
        }
    }
}

impl From<char> for BLASSide {
    #[inline]
    fn from(c: char) -> Self {
        match c.to_ascii_uppercase() {
            'L' => BLASLeft,
            'R' => BLASRight,
            _ => panic!("Invalid character for BLASSide: {}", c),
        }
    }
}

impl From<BLASSide> for char {
    #[inline]
    fn from(side: BLASSide) -> Self {
        match side {
            BLASLeft => 'L',
            BLASRight => 'R',
            _ => panic!("Invalid BLASSide: {:?}", side),
        }
    }
}

impl From<BLASSide> for c_char {
    #[inline]
    fn from(side: BLASSide) -> Self {
        match side {
            BLASLeft => 'L' as c_char,
            BLASRight => 'R' as c_char,
            _ => panic!("Invalid BLASSide: {:?}", side),
        }
    }
}

impl BLASLayout {
    #[inline]
    pub fn flip(&self) -> Self {
        match self {
            BLASRowMajor => BLASColMajor,
            BLASColMajor => BLASRowMajor,
            _ => panic!("Invalid BLASOrder: {:?}", self),
        }
    }
}

impl BLASUpLo {
    #[inline]
    pub fn flip(&self) -> Self {
        match self {
            BLASUpper => BLASLower,
            BLASLower => BLASUpper,
            _ => panic!("Invalid BLASUpLo: {:?}", self),
        }
    }
}

impl BLASSide {
    #[inline]
    pub fn flip(&self) -> Self {
        match self {
            BLASLeft => BLASRight,
            BLASRight => BLASLeft,
            _ => panic!("Invalid BLASSide: {:?}", self),
        }
    }
}

impl BLASTranspose {
    #[inline]
    pub fn flip(&self, hermi: bool) -> Self {
        match self {
            BLASNoTrans => match hermi {
                false => BLASTrans,
                true => BLASConjTrans,
            },
            BLASTrans => BLASNoTrans,
            BLASConjTrans => BLASNoTrans,
            _ => panic!("Invalid BLASTranspose: {:?}", self),
        }
    }
}

unsafe impl Send for BLASLayout {}
unsafe impl Send for BLASTranspose {}
unsafe impl Send for BLASUpLo {}
unsafe impl Send for BLASDiag {}
unsafe impl Send for BLASSide {}

unsafe impl Sync for BLASLayout {}
unsafe impl Sync for BLASTranspose {}
unsafe impl Sync for BLASUpLo {}
unsafe impl Sync for BLASDiag {}
unsafe impl Sync for BLASSide {}

impl BLASLayout {
    #[inline]
    pub fn is_cpref(&self) -> bool {
        match self {
            BLASRowMajor => true,
            BLASLayout::Sequential => true,
            _ => false,
        }
    }

    #[inline]
    pub fn is_fpref(&self) -> bool {
        match self {
            BLASColMajor => true,
            BLASLayout::Sequential => true,
            _ => false,
        }
    }
}

pub(crate) fn get_layout_row_preferred(by_first: &[Option<BLASLayout>], by_all: &[BLASLayout]) -> BLASLayout {
    for x in by_first {
        if let Some(x) = x {
            if x.is_cpref() {
                return BLASRowMajor;
            } else if x.is_fpref() {
                return BLASColMajor;
            }
        }
    }

    if by_all.iter().all(|f| f.is_cpref()) {
        return BLASRowMajor;
    } else if by_all.iter().all(|f| f.is_fpref()) {
        return BLASColMajor;
    } else {
        return BLASRowMajor;
    }
}