Skip to main content

fixed_dsp/basic/
shift.rs

1pub fn shift_i16(data: &mut [i16], shift: i8) {
2    if shift >= 0 {
3        let l = shift as u32;
4        for x in data.iter_mut() {
5            let shifted = if l >= 31 {
6                if *x >= 0 { i32::MAX } else { i32::MIN }
7            } else {
8                (*x as i32) << l
9            };
10            *x = shifted.clamp(i16::MIN as i32, i16::MAX as i32) as i16;
11        }
12    } else {
13        let r = (-shift) as u32;
14        for x in data.iter_mut() {
15            *x = if r >= 15 {
16                if *x < 0 { -1 } else { 0 }
17            } else {
18                ((*x as i32) >> r) as i16
19            };
20        }
21    }
22}
23
24pub fn shift_i32(data: &mut [i32], shift: i8) {
25    if shift >= 0 {
26        let l = shift as u32;
27        for x in data.iter_mut() {
28            let in_val = *x as i64;
29            let shifted = if l >= 63 {
30                if in_val >= 0 { i64::MAX } else { i64::MIN }
31            } else {
32                in_val << l
33            };
34            *x = shifted.clamp(i32::MIN as i64, i32::MAX as i64) as i32;
35        }
36    } else {
37        let r = (-shift) as u32;
38        for x in data.iter_mut() {
39            *x = if r >= 31 {
40                if *x < 0 { -1 } else { 0 }
41            } else {
42                *x >> r
43            };
44        }
45    }
46}