| 1 | /* |
| 2 | * Copyright (c) 2025. |
| 3 | * |
| 4 | * This software is free software; |
| 5 | * |
| 6 | * You can redistribute it or modify it under terms of the MIT, Apache License or Zlib license |
| 7 | */ |
| 8 | |
| 9 | #[cfg(target_arch = "aarch64")] |
| 10 | use core::arch::aarch64::*; |
| 11 | |
| 12 | #[cfg(target_arch = "aarch64")] |
| 13 | #[target_feature(enable = "neon")] |
| 14 | pub unsafe fn upsample_horizontal_neon( |
| 15 | input: &[i16], in_near: &[i16], in_far: &[i16], scratch: &mut [i16], output: &mut [i16] |
| 16 | ) { |
| 17 | assert_eq!(input.len() * 2, output.len()); |
| 18 | assert!(input.len() > 2); |
| 19 | |
| 20 | let len = input.len(); |
| 21 | |
| 22 | if len < 10 { |
| 23 | return super::scalar::upsample_horizontal(input, in_near, in_far, scratch, output); |
| 24 | } |
| 25 | |
| 26 | // First two pixels |
| 27 | output[0] = input[0]; |
| 28 | output[1] = (input[0] * 3 + input[1] + 2) >> 2; |
| 29 | |
| 30 | let v_three = vdupq_n_s16(3); |
| 31 | let v_two = vdupq_n_s16(2); |
| 32 | |
| 33 | let upsample8 = |input: &[i16; 10], output: &mut [i16; 16]| { |
| 34 | let in_ptr = input.as_ptr(); |
| 35 | let out_ptr = output.as_mut_ptr(); |
| 36 | |
| 37 | // SAFETY: The input is 10 * 16 bit long, so the loads are safe. |
| 38 | let (v_prev, v_curr, v_next) = unsafe { |
| 39 | ( |
| 40 | vld1q_s16(in_ptr), |
| 41 | vld1q_s16(in_ptr.add(1)), |
| 42 | vld1q_s16(in_ptr.add(2)) |
| 43 | ) |
| 44 | }; |
| 45 | |
| 46 | let v_common = vaddq_s16(vmulq_s16(v_curr, v_three), v_two); |
| 47 | let v_even = vshrq_n_s16::<2>(vaddq_s16(v_common, v_prev)); |
| 48 | let v_odd = vshrq_n_s16::<2>(vaddq_s16(v_common, v_next)); |
| 49 | |
| 50 | let v_res_1 = vzip1q_s16(v_even, v_odd); |
| 51 | let v_res_2 = vzip2q_s16(v_even, v_odd); |
| 52 | |
| 53 | // SAFETY: The output is 16 * 16 bit long, so the stores are safe. |
| 54 | unsafe { |
| 55 | vst1q_s16(out_ptr, v_res_1); |
| 56 | vst1q_s16(out_ptr.add(8), v_res_2); |
| 57 | } |
| 58 | }; |
| 59 | |
| 60 | for (input, output) in input |
| 61 | .windows(10) |
| 62 | .step_by(8) |
| 63 | .zip(output[2..].chunks_exact_mut(16)) |
| 64 | { |
| 65 | upsample8(input.try_into().unwrap(), output.try_into().unwrap()); |
| 66 | } |
| 67 | |
| 68 | // Upsample the remainder. This may have some overlap, but that's fine. |
| 69 | if let Some(rest_input) = input.last_chunk::<10>() { |
| 70 | let end = output.len() - 2; |
| 71 | if let Some(rest_output) = output[..end].last_chunk_mut::<16>() { |
| 72 | upsample8(rest_input, rest_output); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | // Last two pixels. |
| 77 | output[output.len() - 2] = (3 * input[len - 1] + input[len - 2] + 2) >> 2; |
| 78 | output[output.len() - 1] = input[len - 1]; |
| 79 | } |
| 80 | |
| 81 | #[cfg(target_arch = "aarch64")] |
| 82 | #[target_feature(enable = "neon")] |
| 83 | pub unsafe fn upsample_vertical_neon( |
| 84 | input: &[i16], in_near: &[i16], in_far: &[i16], scratch: &mut [i16], output: &mut [i16] |
| 85 | ) { |
| 86 | assert_eq!(input.len() * 2, output.len()); |
| 87 | assert_eq!(in_near.len(), input.len()); |
| 88 | assert_eq!(in_far.len(), input.len()); |
| 89 | |
| 90 | let len = input.len(); |
| 91 | |
| 92 | if len < 16 { |
| 93 | return super::scalar::upsample_vertical(input, in_near, in_far, scratch, output); |
| 94 | } |
| 95 | |
| 96 | let middle = output.len() / 2; |
| 97 | let (out_top, out_bottom) = output.split_at_mut(middle); |
| 98 | |
| 99 | let v_three = vdupq_n_s16(3); |
| 100 | let v_two = vdupq_n_s16(2); |
| 101 | |
| 102 | let upsample8 = |input: &[i16; 8], |
| 103 | in_near: &[i16; 8], |
| 104 | in_far: &[i16; 8], |
| 105 | out_top: &mut [i16; 8], |
| 106 | out_bottom: &mut [i16; 8]| { |
| 107 | // SAFETY: Inputs are all 8 * 16 bit long, so the loads are safe. |
| 108 | let (v_in, v_near, v_far) = unsafe { |
| 109 | ( |
| 110 | vld1q_s16(input.as_ptr()), |
| 111 | vld1q_s16(in_near.as_ptr()), |
| 112 | vld1q_s16(in_far.as_ptr()) |
| 113 | ) |
| 114 | }; |
| 115 | |
| 116 | let v_common = vaddq_s16(vmulq_s16(v_in, v_three), v_two); |
| 117 | let v_out_top = vshrq_n_s16::<2>(vaddq_s16(v_common, v_near)); |
| 118 | let v_out_bottom = vshrq_n_s16::<2>(vaddq_s16(v_common, v_far)); |
| 119 | |
| 120 | // SAFETY: Outputs are 8 * 16 bit long, so the stores are safe. |
| 121 | unsafe { |
| 122 | vst1q_s16(out_top.as_mut_ptr(), v_out_top); |
| 123 | vst1q_s16(out_bottom.as_mut_ptr(), v_out_bottom); |
| 124 | } |
| 125 | }; |
| 126 | |
| 127 | let chunks = input |
| 128 | .chunks_exact(8) |
| 129 | .zip(in_near.chunks_exact(8)) |
| 130 | .zip(in_far.chunks_exact(8)) |
| 131 | .zip(out_top.chunks_exact_mut(8)) |
| 132 | .zip(out_bottom.chunks_exact_mut(8)); |
| 133 | |
| 134 | for ((((input, in_near), in_far), out_top), out_bottom) in chunks { |
| 135 | upsample8( |
| 136 | input.try_into().unwrap(), |
| 137 | in_near.try_into().unwrap(), |
| 138 | in_far.try_into().unwrap(), |
| 139 | out_top.try_into().unwrap(), |
| 140 | out_bottom.try_into().unwrap() |
| 141 | ); |
| 142 | } |
| 143 | |
| 144 | // Upsample the remainder. |
| 145 | if let Some(rest) = input.last_chunk::<8>() { |
| 146 | if let Some(rest_near) = in_near.last_chunk::<8>() { |
| 147 | if let Some(rest_far) = in_far.last_chunk::<8>() { |
| 148 | if let Some(mut rest_top) = out_top.last_chunk_mut::<8>() { |
| 149 | if let Some(mut rest_bottom) = out_bottom.last_chunk_mut::<8>() { |
| 150 | upsample8(rest, rest_near, rest_far, &mut rest_top, &mut rest_bottom); |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | #[cfg(target_arch = "aarch64")] |
| 159 | #[target_feature(enable = "neon")] |
| 160 | pub unsafe fn upsample_hv_neon( |
| 161 | input: &[i16], in_near: &[i16], in_far: &[i16], scratch_space: &mut [i16], output: &mut [i16] |
| 162 | ) { |
| 163 | assert_eq!(input.len() * 4, output.len()); |
| 164 | |
| 165 | assert!(input.len() * 2 <= scratch_space.len()); |
| 166 | let scratch_space = &mut scratch_space[..input.len() * 2]; |
| 167 | |
| 168 | // SAFETY: caller guarantees NEON is available |
| 169 | unsafe { |
| 170 | upsample_vertical_neon(input, in_near, in_far, &mut [], scratch_space); |
| 171 | |
| 172 | let scratch_half = scratch_space.len() / 2; |
| 173 | let output_half = output.len() / 2; |
| 174 | |
| 175 | let (scratch_top, scratch_bottom) = scratch_space.split_at_mut(scratch_half); |
| 176 | let (out_top, out_bottom) = output.split_at_mut(output_half); |
| 177 | |
| 178 | let mut t = [0]; |
| 179 | upsample_horizontal_neon(scratch_top, &[], &[], &mut t, out_top); |
| 180 | upsample_horizontal_neon(scratch_bottom, &[], &[], &mut t, out_bottom); |
| 181 | } |
| 182 | } |