Skip to content

Commit 5db115c

Browse files
committed
Rename "simd" feature to not(platform_independent)
Also return fn pointers rather than using has_* functions.
1 parent 5a33d5d commit 5db115c

File tree

5 files changed

+32
-49
lines changed

5 files changed

+32
-49
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,5 @@ name = "large_image"
2727
harness = false
2828

2929
[features]
30-
default = ["rayon", "simd"]
31-
simd = []
30+
default = ["rayon"]
31+
platform_independent = []

src/arch/mod.rs

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,52 +7,27 @@ use std::is_x86_feature_detected;
77

88
/// Arch-specific implementation of YCbCr conversion. Returns the number of pixels that were
99
/// converted.
10-
pub fn color_convert_line_ycbcr(y: &[u8], cb: &[u8], cr: &[u8], output: &mut [u8]) -> usize {
10+
pub fn get_color_convert_line_ycbcr() -> Option<unsafe fn(&[u8], &[u8], &[u8], &mut [u8]) -> usize>
11+
{
1112
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
1213
#[allow(unsafe_code)]
1314
{
1415
if is_x86_feature_detected!("ssse3") {
15-
unsafe {
16-
return ssse3::color_convert_line_ycbcr(y, cb, cr, output);
17-
}
16+
return Some(ssse3::color_convert_line_ycbcr);
1817
}
1918
}
20-
return 0;
19+
None
2120
}
2221

2322
/// Arch-specific implementation of 8x8 IDCT.
24-
pub fn dequantize_and_idct_block_8x8(
25-
coefficients: &[i16],
26-
quantization_table: &[u16; 64],
27-
output_linestride: usize,
28-
output: &mut [u8],
29-
) {
23+
pub fn get_dequantize_and_idct_block_8x8() -> Option<unsafe fn(&[i16], &[u16; 64], usize, &mut [u8])>
24+
{
3025
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
3126
#[allow(unsafe_code)]
3227
{
3328
if is_x86_feature_detected!("ssse3") {
34-
unsafe {
35-
ssse3::dequantize_and_idct_block_8x8(
36-
coefficients,
37-
quantization_table,
38-
output_linestride,
39-
output,
40-
);
41-
return;
42-
}
29+
return Some(ssse3::dequantize_and_idct_block_8x8);
4330
}
4431
}
45-
unreachable!("No arch-specific IDCT available");
46-
}
47-
48-
/// Returns true if an arch-specific IDCT is avaliable, false otherwise.
49-
pub fn has_arch_specific_idct() -> bool {
50-
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
51-
#[allow(unsafe_code)]
52-
{
53-
if is_x86_feature_detected!("ssse3") {
54-
return true;
55-
}
56-
}
57-
return false;
32+
None
5833
}

src/decoder.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,10 +1257,19 @@ fn color_convert_line_ycbcr(data: &[Vec<u8>], output: &mut [u8]) {
12571257
assert!(data.len() == 3, "wrong number of components for ycbcr");
12581258
let [y, cb, cr]: &[_; 3] = data.try_into().unwrap();
12591259

1260-
#[cfg(feature = "simd")]
1261-
let arch_specific_pixels = crate::arch::color_convert_line_ycbcr(y, cb, cr, output);
1260+
#[cfg(not(feature = "platform_independent"))]
1261+
let arch_specific_pixels = {
1262+
if let Some(ycbcr) = crate::arch::get_color_convert_line_ycbcr() {
1263+
#[allow(unsafe_code)]
1264+
unsafe {
1265+
ycbcr(y, cb, cr, output)
1266+
}
1267+
} else {
1268+
0
1269+
}
1270+
};
12621271

1263-
#[cfg(not(feature = "simd"))]
1272+
#[cfg(feature = "platform_independent")]
12641273
let arch_specific_pixels = 0;
12651274

12661275
for (((chunk, y), cb), cr) in output

src/idct.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -244,14 +244,12 @@ pub fn dequantize_and_idct_block_8x8(
244244
output_linestride: usize,
245245
output: &mut [u8],
246246
) {
247-
#[cfg(feature = "simd")]
248-
if crate::arch::has_arch_specific_idct() {
249-
return crate::arch::dequantize_and_idct_block_8x8(
250-
coefficients,
251-
quantization_table,
252-
output_linestride,
253-
output,
254-
);
247+
#[cfg(not(feature = "platform_independent"))]
248+
if let Some(idct) = crate::arch::get_dequantize_and_idct_block_8x8() {
249+
#[allow(unsafe_code)]
250+
unsafe {
251+
return idct(coefficients, quantization_table, output_linestride, output);
252+
}
255253
}
256254

257255
let output = output.chunks_mut(output_linestride);
@@ -638,8 +636,8 @@ fn test_dequantize_and_idct_block_8x8_all_zero() {
638636
#[test]
639637
fn test_dequantize_and_idct_block_8x8_saturated() {
640638
// Arch-specific IDCT implementations need not handle i16::MAX values.
641-
#[cfg(feature = "simd")]
642-
if crate::arch::has_arch_specific_idct() {
639+
#[cfg(not(feature = "platform_independent"))]
640+
if crate::arch::get_dequantize_and_idct_block_8x8().is_some() {
643641
return;
644642
}
645643
let mut output = [0u8; 8 * 8];

src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
2929
#![deny(missing_docs)]
3030
#![deny(unsafe_code)]
31+
#![cfg_attr(feature = "platform_independent", forbid(unsafe_code))]
3132

3233
extern crate alloc;
3334
extern crate core;
@@ -41,7 +42,7 @@ pub use parser::CodingProcess;
4142

4243
use std::io;
4344

44-
#[cfg(feature = "simd")]
45+
#[cfg(not(feature = "platform_independent"))]
4546
mod arch;
4647
mod decoder;
4748
mod error;

0 commit comments

Comments
 (0)