Make Rust rotate code smaller (#462)

* Make Rust rotate code smaller

* Back on the rust happy path
This commit is contained in:
Surma
2019-02-15 09:47:26 +00:00
committed by Jake Archibald
parent ca5162ed32
commit 91e7c9c5ad
6 changed files with 100 additions and 38 deletions

View File

@@ -1,8 +1,22 @@
#![no_std]
#![no_main]
use std::slice::from_raw_parts_mut;
use core::panic::PanicInfo;
use core::slice::from_raw_parts_mut;
// This function is taken from
// https://rustwasm.github.io/book/reference/code-size.html
#[cfg(not(debug_assertions))]
#[inline]
pub fn unwrap_abort<T>(o: Option<T>) -> T {
use std::process;
match o {
Some(t) => t,
None => process::abort(),
}
}
// Normal panic-y behavior for debug builds
#[cfg(debug_assertions)]
unsafe fn unchecked_unwrap<T>(o: Option<T>) -> T {
o.unwrap()
}
#[no_mangle]
fn rotate(input_width: isize, input_height: isize, rotate: isize) {
@@ -69,13 +83,8 @@ fn rotate(input_width: isize, input_height: isize, rotate: isize) {
for d2 in 0..d2_limit {
for d1 in 0..d1_limit {
let in_idx = (d1_start + d1 * d1_advance) * d1_multiplier + (d2_start + d2 * d2_advance) * d2_multiplier;
out_b[i as usize] = in_b[in_idx as usize];
*unwrap_abort(out_b.get_mut(i as usize)) = *unwrap_abort(in_b.get(in_idx as usize));
i += 1;
}
}
}
#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
loop {}
}