Debian Codegraph @

debian/missing-sources/matrixmultiply/testdefs/testdefs.rs

raw
1
2 use matrixmultiply::{sgemm, dgemm};
3 #[cfg(feature="cgemm")]
4 use matrixmultiply::{cgemm, zgemm, CGemmOption};
5
6 // Common code for tests - generic treatment of f32, f64, c32, c64 and Gemm
7
8 pub trait Float : Copy + std::fmt::Debug + PartialEq {
9 fn zero() -> Self;
10 fn one() -> Self;
11 // construct as number x
12 fn from(x: i64) -> Self;
13 // construct as number x + yi, but ignore y if not complex
14 fn from2(x: i64, _y: i64) -> Self { Self::from(x) }
15 fn nan() -> Self;
16 fn real(self) -> Self { self }
17 fn imag(self) -> Self;
18 fn is_nan(self) -> bool;
19 fn is_complex() -> bool { false }
20 fn diff(self, rhs: Self) -> Self;
21 // absolute value as f64
22 fn abs_f64(self) -> f64;
23 fn relative_error_scale() -> f64;
24 fn mul_add_assign(&mut self, x: Self, y: Self);
25 }
26
27 impl Float for f32 {
28 fn zero() -> Self { 0. }
29 fn one() -> Self { 1. }
30 fn from(x: i64) -> Self { x as Self }
31 fn nan() -> Self { 0./0. }
32 fn imag(self) -> Self { 0. }
33 fn is_nan(self) -> bool { self.is_nan() }
34 fn diff(self, rhs: Self) -> Self { self - rhs }
35 fn abs_f64(self) -> f64 { self.abs() as f64 }
36 fn relative_error_scale() -> f64 { 1e-6 }
37 fn mul_add_assign(&mut self, x: Self, y: Self) {
38 *self += x * y;
39 }
40 }
41
42 impl Float for f64 {
43 fn zero() -> Self { 0. }
44 fn one() -> Self { 1. }
45 fn from(x: i64) -> Self { x as Self }
46 fn nan() -> Self { 0./0. }
47 fn imag(self) -> Self { 0. }
48 fn is_nan(self) -> bool { self.is_nan() }
49 fn diff(self, rhs: Self) -> Self { self - rhs }
50 fn abs_f64(self) -> f64 { self.abs() }
51 fn relative_error_scale() -> f64 { 1e-12 }
52 fn mul_add_assign(&mut self, x: Self, y: Self) {
53 *self += x * y;
54 }
55 }
56
57 #[allow(non_camel_case_types)]
58 #[cfg(feature="cgemm")]
59 pub type c32 = [f32; 2];
60 #[allow(non_camel_case_types)]
61 #[cfg(feature="cgemm")]
62 pub type c64 = [f64; 2];
63
64 #[cfg(feature="cgemm")]
65 impl Float for c32 {
66 fn zero() -> Self { [0., 0.] }
67 fn one() -> Self { [1., 0.] }
68 fn from(x: i64) -> Self { [x as _, 0.] }
69 fn from2(x: i64, y: i64) -> Self { [x as _, y as _] }
70 fn nan() -> Self { [0./0., 0./0.] }
71 fn real(self) -> Self { [self[0], 0.] }
72 fn imag(self) -> Self { [self[1], 0.] }
73 fn is_nan(self) -> bool { self[0].is_nan() || self[1].is_nan() }
74 fn is_complex() -> bool { true }
75 fn diff(self, rhs: Self) -> Self {
76 [self[0] - rhs[0], self[1] - rhs[1]]
77 }
78 fn abs_f64(self) -> f64 {
79 (self[0].powi(2) + self[1].powi(2)).sqrt() as f64
80 }
81 fn relative_error_scale() -> f64 { 1e-6 }
82 fn mul_add_assign(&mut self, x: Self, y: Self) {
83 let [re, im] = c32_mul(x, y);
84 self[0] += re;
85 self[1] += im;
86 }
87 }
88
89 #[cfg(feature="cgemm")]
90 impl Float for c64 {
91 fn zero() -> Self { [0., 0.] }
92 fn one() -> Self { [1., 0.] }
93 fn from(x: i64) -> Self { [x as _, 0.] }
94 fn from2(x: i64, y: i64) -> Self { [x as _, y as _] }
95 fn nan() -> Self { [0./0., 0./0.] }
96 fn real(self) -> Self { [self[0], 0.] }
97 fn imag(self) -> Self { [self[1], 0.] }
98 fn is_nan(self) -> bool { self[0].is_nan() || self[1].is_nan() }
99 fn is_complex() -> bool { true }
100 fn diff(self, rhs: Self) -> Self {
101 [self[0] - rhs[0], self[1] - rhs[1]]
102 }
103 fn abs_f64(self) -> f64 {
104 (self[0].powi(2) + self[1].powi(2)).sqrt()
105 }
106 fn relative_error_scale() -> f64 { 1e-12 }
107 fn mul_add_assign(&mut self, x: Self, y: Self) {
108 let [re, im] = c64_mul(x, y);
109 self[0] += re;
110 self[1] += im;
111 }
112 }
113
114
115 #[cfg(feature = "cgemm")]
116 #[inline(always)]
117 pub(crate) fn c32_mul(x: c32, y: c32) -> c32 {
118 let [a, b] = x;
119 let [c, d] = y;
120 [a * c - b * d, b * c + a * d]
121 }
122
123 #[cfg(feature = "cgemm")]
124 #[inline(always)]
125 pub(crate) fn c64_mul(x: c64, y: c64) -> c64 {
126 let [a, b] = x;
127 let [c, d] = y;
128 [a * c - b * d, b * c + a * d]
129 }
130
131
132 pub trait Gemm : Sized {
133 unsafe fn gemm(
134 m: usize, k: usize, n: usize,
135 alpha: Self,
136 a: *const Self, rsa: isize, csa: isize,
137 b: *const Self, rsb: isize, csb: isize,
138 beta: Self,
139 c: *mut Self, rsc: isize, csc: isize);
140 }
141
142 impl Gemm for f32 {
143 unsafe fn gemm(
144 m: usize, k: usize, n: usize,
145 alpha: Self,
146 a: *const Self, rsa: isize, csa: isize,
147 b: *const Self, rsb: isize, csb: isize,
148 beta: Self,
149 c: *mut Self, rsc: isize, csc: isize) {
150 sgemm(
151 m, k, n,
152 alpha,
153 a, rsa, csa,
154 b, rsb, csb,
155 beta,
156 c, rsc, csc)
157 }
158 }
159
160 impl Gemm for f64 {
161 unsafe fn gemm(
162 m: usize, k: usize, n: usize,
163 alpha: Self,
164 a: *const Self, rsa: isize, csa: isize,
165 b: *const Self, rsb: isize, csb: isize,
166 beta: Self,
167 c: *mut Self, rsc: isize, csc: isize) {
168 dgemm(
169 m, k, n,
170 alpha,
171 a, rsa, csa,
172 b, rsb, csb,
173 beta,
174 c, rsc, csc)
175 }
176 }
177
178 #[cfg(feature="cgemm")]
179 impl Gemm for c32 {
180 unsafe fn gemm(
181 m: usize, k: usize, n: usize,
182 alpha: Self,
183 a: *const Self, rsa: isize, csa: isize,
184 b: *const Self, rsb: isize, csb: isize,
185 beta: Self,
186 c: *mut Self, rsc: isize, csc: isize) {
187 cgemm(
188 CGemmOption::Standard,
189 CGemmOption::Standard,
190 m, k, n,
191 alpha,
192 a, rsa, csa,
193 b, rsb, csb,
194 beta,
195 c, rsc, csc)
196 }
197 }
198
199 #[cfg(feature="cgemm")]
200 impl Gemm for c64 {
201 unsafe fn gemm(
202 m: usize, k: usize, n: usize,
203 alpha: Self,
204 a: *const Self, rsa: isize, csa: isize,
205 b: *const Self, rsb: isize, csb: isize,
206 beta: Self,
207 c: *mut Self, rsc: isize, csc: isize) {
208 zgemm(
209 CGemmOption::Standard,
210 CGemmOption::Standard,
211 m, k, n,
212 alpha,
213 a, rsa, csa,
214 b, rsb, csb,
215 beta,
216 c, rsc, csc)
217 }
218 }
219