mirror of
https://github.com/GoogleChromeLabs/squoosh.git
synced 2025-11-18 19:49:04 +00:00
Make Rust rotate code smaller (#462)
* Make Rust rotate code smaller * Back on the rust happy path
This commit is contained in:
2
codecs/rotate/.gitignore
vendored
Normal file
2
codecs/rotate/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
target
|
||||
Cargo.lock
|
||||
14
codecs/rotate/Cargo.toml
Normal file
14
codecs/rotate/Cargo.toml
Normal file
@@ -0,0 +1,14 @@
|
||||
[package]
|
||||
name = "rotate"
|
||||
version = "0.1.0"
|
||||
authors = ["Surma <surma@google.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[lib]
|
||||
name = "rotate"
|
||||
path = "rotate.rs"
|
||||
crate-type = ["cdylib", "rlib"]
|
||||
|
||||
[profile.release]
|
||||
lto = true
|
||||
opt-level = "s"
|
||||
@@ -7,12 +7,11 @@ echo "Compiling wasm"
|
||||
echo "============================================="
|
||||
(
|
||||
rustup run nightly \
|
||||
rustc \
|
||||
--target=wasm32-unknown-unknown \
|
||||
-C opt-level=3 \
|
||||
-o rotate.wasm \
|
||||
rotate.rs
|
||||
wasm-strip rotate.wasm
|
||||
cargo build \
|
||||
--target wasm32-unknown-unknown \
|
||||
--release
|
||||
cp target/wasm32-unknown-unknown/release/rotate.wasm .
|
||||
wasm-strip rotate.wasm
|
||||
)
|
||||
echo "============================================="
|
||||
echo "Compiling wasm done"
|
||||
|
||||
@@ -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 {}
|
||||
}
|
||||
|
||||
Binary file not shown.
Reference in New Issue
Block a user