Skip to content

Commit 9e03f7f

Browse files
committed
Auto vectorise?
1 parent 2bdcaf1 commit 9e03f7f

File tree

1 file changed

+42
-16
lines changed

1 file changed

+42
-16
lines changed

src/day13.rs

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use aoc_runner_derive::aoc;
2+
use std::arch::x86_64::*;
23

34
#[aoc(day13, part1)]
45
pub fn part1(s: &str) -> u64 {
@@ -16,38 +17,63 @@ pub fn part2(s: &str) -> u64 {
1617
}
1718
}
1819

19-
// #[target_feature(enable = "avx2,bmi1,bmi2,cmpxchg16b,lzcnt,movbe,popcnt")]
20-
fn inner<const OFFSET: i64>(s: &str) -> u64 {
20+
#[target_feature(enable = "avx2,bmi1,bmi2,cmpxchg16b,lzcnt,movbe,popcnt")]
21+
unsafe fn inner<const OFFSET: i64>(s: &str) -> u64 {
2122
let s = s.as_bytes();
2223

2324
let mut sum = 0;
2425

26+
let mut axs = [0i64; 320];
27+
let mut ays = [0i64; 320];
28+
let mut bxs = [0i64; 320];
29+
let mut bys = [0i64; 320];
30+
let mut xs = [0i64; 320];
31+
let mut ys = [0i64; 320];
32+
33+
let mut p = 0;
2534
let mut i = 0;
26-
while i < s.len() {
27-
let ax = ((s[i + 12] - b'0') * 10 + s[i + 13] - b'0') as i64;
28-
let ay = ((s[i + 18] - b'0') * 10 + s[i + 19] - b'0') as i64;
35+
while p < s.len() {
36+
let ax = ((s[p + 12] - b'0') * 10 + s[p + 13] - b'0') as i64;
37+
let ay = ((s[p + 18] - b'0') * 10 + s[p + 19] - b'0') as i64;
2938

30-
let bx = ((s[i + 33] - b'0') * 10 + s[i + 34] - b'0') as i64;
31-
let by = ((s[i + 39] - b'0') * 10 + s[i + 40] - b'0') as i64;
32-
i += 51;
39+
let bx = ((s[p + 33] - b'0') * 10 + s[p + 34] - b'0') as i64;
40+
let by = ((s[p + 39] - b'0') * 10 + s[p + 40] - b'0') as i64;
41+
p += 51;
3342

3443
let mut x = 0;
35-
while s[i] != b',' {
44+
while s[p] != b',' {
3645
x *= 10;
37-
x += (s[i] - b'0') as i64;
38-
i += 1;
46+
x += (s[p] - b'0') as i64;
47+
p += 1;
3948
}
4049
x += OFFSET;
41-
i += 4;
50+
p += 4;
4251

4352
let mut y = 0;
44-
while i < s.len() && s[i] != b'\n' {
53+
while s[p] != b'\n' {
4554
y *= 10;
46-
y += (s[i] - b'0') as i64;
47-
i += 1;
55+
y += (s[p] - b'0') as i64;
56+
p += 1;
4857
}
4958
y += OFFSET;
50-
i += 2;
59+
p += 2;
60+
61+
*axs.get_unchecked_mut(i) = ax;
62+
*ays.get_unchecked_mut(i) = ay;
63+
*bxs.get_unchecked_mut(i) = bx;
64+
*bys.get_unchecked_mut(i) = by;
65+
*xs.get_unchecked_mut(i) = x;
66+
*ys.get_unchecked_mut(i) = y;
67+
i += 1;
68+
}
69+
70+
for i in 0..i {
71+
let ax = *axs.get_unchecked(i);
72+
let ay = *ays.get_unchecked(i);
73+
let bx = *bxs.get_unchecked(i);
74+
let by = *bys.get_unchecked(i);
75+
let x = *xs.get_unchecked(i);
76+
let y = *ys.get_unchecked(i);
5177

5278
let numerator = x * by - y * bx;
5379
let denominator = ax * by - ay * bx;

0 commit comments

Comments
 (0)