-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.rs
283 lines (234 loc) · 7.52 KB
/
lib.rs
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
use std::ops;
/// Pass by val, return by ref: 4 * f32, addition.
pub fn case_4_f32_add(a: [f32; 4], b: [f32; 4]) -> [f32; 4] {
[a[0] + b[0], a[1] + b[1], a[2] + b[2], a[3] + b[3]]
}
/// Pass by val, return by ref: 4 * f32, subtraction.
pub fn case_4_f32_sub(a: [f32; 4], b: [f32; 4]) -> [f32; 4] {
[a[0] - b[0], a[1] - b[1], a[2] - b[2], a[3] - b[3]]
}
/// Pass by val, return by ref: 4 * f32, multiplication.
pub fn case_4_f32_mul(a: [f32; 4], b: [f32; 4]) -> [f32; 4] {
[a[0] * b[0], a[1] * b[1], a[2] * b[2], a[3] * b[3]]
}
/// Pass by val, return by ref: 4 * f32, division.
pub fn case_4_f32_div(a: [f32; 4], b: [f32; 4]) -> [f32; 4] {
[a[0] / b[0], a[1] / b[1], a[2] / b[2], a[3] / b[3]]
}
/// Pass by val, return by ref: 4 * i32, addition.
pub fn case_4_i32_add(a: [i32; 4], b: [i32; 4]) -> [i32; 4] {
[a[0] + b[0], a[1] + b[1], a[2] + b[2], a[3] + b[3]]
}
/// Pass by val, return by ref: 4 * i32, subtraction.
pub fn case_4_i32_sub(a: [i32; 4], b: [i32; 4]) -> [i32; 4] {
[a[0] - b[0], a[1] - b[1], a[2] - b[2], a[3] - b[3]]
}
/// Pass by val, return by ref: 4 * i32, multiplication.
pub fn case_4_i32_mul(a: [i32; 4], b: [i32; 4]) -> [i32; 4] {
[a[0] * b[0], a[1] * b[1], a[2] * b[2], a[3] * b[3]]
}
// Ignored, the zero division checks overwhelm output.
// /// Pass by val, return by ref: 4 * i32, division.
// pub fn case_4_i32_div(a: [i32; 4], b: [i32; 4]) -> [i32; 4] {
// [a[0] / b[0], a[1] / b[1], a[2] / b[2], a[3] / b[3]]
// }
/// Pass by val, return by ref: 4 * i32, bitwise and.
pub fn case_4_i32_and(a: [i32; 4], b: [i32; 4]) -> [i32; 4] {
[a[0] & b[0], a[1] & b[1], a[2] & b[2], a[3] & b[3]]
}
/// Pass by val, return by ref: 4 * i32, bitwise or.
pub fn case_4_i32_xor(a: [i32; 4], b: [i32; 4]) -> [i32; 4] {
[a[0] | b[0], a[1] | b[1], a[2] | b[2], a[3] | b[3]]
}
/// Pass by val, return by ref: 4 * i32, bitwise xor.
pub fn case_4_i32_or(a: [i32; 4], b: [i32; 4]) -> [i32; 4] {
[a[0] ^ b[0], a[1] ^ b[1], a[2] ^ b[2], a[3] ^ b[3]]
}
/// Pass by val, return by ref: 4 * i32, bitwise shift left.
pub fn case_4_i32_shl(a: [i32; 4], b: [i32; 4]) -> [i32; 4] {
[a[0] << b[0], a[1] << b[1], a[2] << b[2], a[3] << b[3]]
}
/// Pass by val, return by ref: 4 * i32, bitwise shift right.
pub fn case_4_i32_shr(a: [i32; 4], b: [i32; 4]) -> [i32; 4] {
[a[0] >> b[0], a[1] >> b[1], a[2] >> b[2], a[3] >> b[3]]
}
/// Pass by ref, return by val: 4 * f32, addition.
pub fn case_4_f32_add_pass_ref(a: &[f32; 4], b: &[f32; 4]) -> [f32; 4] {
let mut c = [0.0; 4];
for i in 0..4 {
c[i] = a[i] + b[i];
}
c
}
/// Pass by val, return by ref: 4 * f32, addition.
pub fn case_4_f32_add_ret_ref(a: &[f32; 4], b: &[f32; 4], c: &mut [f32; 4]) {
for i in 0..4 {
c[i] = a[i] + b[i];
}
}
/// Pass by val, return by val: 4 * i32, addition and subtraction.
/// Not essential, we can work around this.
pub fn case_4_i32_add_sub(a: [i32; 4], b: [i32; 4], c: [i32; 4]) -> [i32; 4] {
[a[0] + b[0] - c[0], a[1] + b[1] - c[1], b[2] + a[2] - c[2], a[3] + b[3] - c[3]]
}
/// Pass by val, return by val: 4 * i32, addition and subtraction, commutative.
/// Not essential, we can work around this.
pub fn case_4_i32_add_sub_com(a: [i32; 4], b: [i32; 4], c: [i32; 4]) -> [i32; 4] {
[a[0] + b[0] - c[0], -c[1] + a[1] + b[1], b[2] - c[2] + a[2], a[3] - c[3] + b[3]]
}
/// Pass by ref, return by val: 4 * f32, addition loop.
pub fn case_4_f32_add_loop(a: [f32; 4], b: [f32; 4]) -> [f32; 4] {
let mut c = [0.0; 4];
for i in 0..4 {
c[i] = a[i] + b[i];
}
c
}
/// Pass by ref, return by ref: 4 * f32, addition loop.
pub fn case_4_f32_add_ret_ref_loop(a: [f32; 4], b: [f32; 4], c: &mut [f32; 4]) {
for i in 0..4 {
c[i] = a[i] + b[i];
}
}
/// Pass by val, return by val: Vec4, addition and subtraction.
pub fn case_vec4_add_sub(a: Vec4, b: Vec4, c: Vec4) -> Vec4 {
a + b - c
}
/// Pass by val, return by val: Vec4, addition cancelling c.
/// Not essential, we can attempt to work around this.
pub fn case_vec4_add_sub_to_cancel_c(a: Vec4, b: Vec4, c: Vec4) -> Vec4 {
a + b - c + c
}
/// Pass by val, return by val: Vec4, addition simplify with multiply 16.
/// Not essential, we can attempt to work around this.
pub fn case_vec4_add_to_mul_16(a: Vec4, b: Vec4) -> Vec4 {
a + b + b + b + b + b + b + b + b + b + b + b + b + b + b + b + b
}
/// Pass by val, return by val: Vec4, addition simplify with multiply 15.
/// Not essential, we can attempt to work around this.
pub fn case_vec4_add_to_mul_15(a: Vec4, b: Vec4) -> Vec4 {
a + b + b + b + b + b + b + b + b + b + b + b + b + b + b + b
}
/// Vec 4 * f32
#[derive(Copy, Clone)]
pub struct Vec4 {
x: f32,
y: f32,
z: f32,
w: f32,
}
impl ops::AddAssign for Vec4 {
fn add_assign(&mut self, rhs: Self) {
self.x += rhs.x;
self.y += rhs.y;
self.z += rhs.z;
self.w += rhs.w;
}
}
impl ops::Add for Vec4 {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self { x: self.x + rhs.x, y: self.y + rhs.y, z: self.z + rhs.z, w: self.w + rhs.w }
}
}
impl ops::SubAssign for Vec4 {
fn sub_assign(&mut self, rhs: Self) {
self.x -= rhs.x;
self.y -= rhs.y;
self.z -= rhs.z;
self.w -= rhs.w;
}
}
impl ops::Sub for Vec4 {
type Output = Self;
fn sub(self, rhs: Self) -> Self::Output {
Self { x: self.x - rhs.x, y: self.y - rhs.y, z: self.z - rhs.z, w: self.w - rhs.w }
}
}
impl ops::DivAssign for Vec4 {
fn div_assign(&mut self, rhs: Self) {
self.x /= rhs.x;
self.y /= rhs.y;
self.z /= rhs.z;
self.w /= rhs.w;
}
}
impl ops::Div for Vec4 {
type Output = Self;
fn div(self, rhs: Self) -> Self::Output {
Self { x: self.x / rhs.x, y: self.y / rhs.y, z: self.z / rhs.z, w: self.w / rhs.w }
}
}
impl ops::MulAssign for Vec4 {
fn mul_assign(&mut self, rhs: Self) {
self.x *= rhs.x;
self.y *= rhs.y;
self.z *= rhs.z;
self.w *= rhs.w;
}
}
impl ops::Mul for Vec4 {
type Output = Self;
fn mul(self, rhs: Self) -> Self::Output {
Self { x: self.x * rhs.x, y: self.y * rhs.y, z: self.z * rhs.z, w: self.w * rhs.w }
}
}
/// Vec 3 * f32
#[derive(Copy, Clone)]
pub struct Vec3 {
x: f32,
y: f32,
z: f32,
}
impl ops::AddAssign for Vec3 {
fn add_assign(&mut self, rhs: Self) {
self.x += rhs.x;
self.y += rhs.y;
self.z += rhs.z;
}
}
impl ops::Add for Vec3 {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self { x: self.x + rhs.x, y: self.y + rhs.y, z: self.z + rhs.z }
}
}
impl ops::SubAssign for Vec3 {
fn sub_assign(&mut self, rhs: Self) {
self.x -= rhs.x;
self.y -= rhs.y;
self.z -= rhs.z;
}
}
impl ops::Sub for Vec3 {
type Output = Self;
fn sub(self, rhs: Self) -> Self::Output {
Self { x: self.x - rhs.x, y: self.y - rhs.y, z: self.z - rhs.z }
}
}
impl ops::DivAssign for Vec3 {
fn div_assign(&mut self, rhs: Self) {
self.x /= rhs.x;
self.y /= rhs.y;
self.z /= rhs.z;
}
}
impl ops::Div for Vec3 {
type Output = Self;
fn div(self, rhs: Self) -> Self::Output {
Self { x: self.x / rhs.x, y: self.y / rhs.y, z: self.z / rhs.z }
}
}
impl ops::MulAssign for Vec3 {
fn mul_assign(&mut self, rhs: Self) {
self.x *= rhs.x;
self.y *= rhs.y;
self.z *= rhs.z;
}
}
impl ops::Mul for Vec3 {
type Output = Self;
fn mul(self, rhs: Self) -> Self::Output {
Self { x: self.x * rhs.x, y: self.y * rhs.y, z: self.z * rhs.z }
}
}