#![no_std]
#![allow(internal_features, incomplete_features)]
#![feature(
generic_const_exprs,
maybe_uninit_array_assume_init,
const_mut_refs,
const_refs_to_cell,
const_maybe_uninit_array_assume_init,
core_intrinsics,
const_maybe_uninit_write,
const_eval_select
)]
use core::intrinsics::const_eval_select;
use core::mem::MaybeUninit as MU;
const BLOCK_SIZE: usize = 16;
const NBR_SEGMENTS: usize = 4;
const RECURSIVE_LIMIT: usize = 128;
const SMALL_LEN: usize = 255;
const MEDIUM_LEN: usize = 1024 * 1024;
const unsafe fn transpose_small<T: Copy>(
input: &[T],
output: &mut [MU<T>],
width: usize,
height: usize,
) {
let mut x = 0;
while x != width {
let mut y = 0;
while y != height {
let input_index = x + y * width;
let output_index = y + x * height;
(*output.as_mut_ptr().add(output_index)).write(*input.as_ptr().add(input_index));
y += 1;
}
x += 1;
}
}
unsafe fn transpose_block<T: Copy>(
input: &[T],
output: &mut [MU<T>],
width: usize,
height: usize,
start_x: usize,
start_y: usize,
block_width: usize,
block_height: usize,
) {
for inner_x in 0..block_width {
for inner_y in 0..block_height {
let x = start_x + inner_x;
let y = start_y + inner_y;
let input_index = x + y * width;
let output_index = y + x * height;
(*output.as_mut_ptr().add(output_index)).write(*input.as_ptr().add(input_index));
}
}
}
unsafe fn transpose_block_segmented<T: Copy>(
input: &[T],
output: &mut [MU<T>],
width: usize,
height: usize,
start_x: usize,
start_y: usize,
block_width: usize,
block_height: usize,
) {
let height_per_div = block_height / NBR_SEGMENTS;
for subblock in 0..NBR_SEGMENTS {
for inner_x in 0..block_width {
for inner_y in 0..height_per_div {
let x = start_x + inner_x;
let y = start_y + inner_y + subblock * height_per_div;
let input_index = x + y * width;
let output_index = y + x * height;
(*output.as_mut_ptr().add(output_index)).write(*input.as_ptr().add(input_index));
}
}
}
}
fn transpose_tiled<T: Copy>(
input: &[T],
output: &mut [MU<T>],
input_width: usize,
input_height: usize,
) {
let x_block_count = input_width / BLOCK_SIZE;
let y_block_count = input_height / BLOCK_SIZE;
let remainder_x = input_width - x_block_count * BLOCK_SIZE;
let remainder_y = input_height - y_block_count * BLOCK_SIZE;
for y_block in 0..y_block_count {
for x_block in 0..x_block_count {
unsafe {
transpose_block(
input,
output,
input_width,
input_height,
x_block * BLOCK_SIZE,
y_block * BLOCK_SIZE,
BLOCK_SIZE,
BLOCK_SIZE,
);
}
}
if remainder_x > 0 {
unsafe {
transpose_block(
input,
output,
input_width,
input_height,
input_width - remainder_x,
y_block * BLOCK_SIZE,
remainder_x,
BLOCK_SIZE,
);
}
}
}
if remainder_y > 0 {
for x_block in 0..x_block_count {
unsafe {
transpose_block(
input,
output,
input_width,
input_height,
x_block * BLOCK_SIZE,
input_height - remainder_y,
BLOCK_SIZE,
remainder_y,
);
}
}
if remainder_x > 0 {
unsafe {
transpose_block(
input,
output,
input_width,
input_height,
input_width - remainder_x,
input_height - remainder_y,
remainder_x,
remainder_y,
);
}
}
}
}
fn transpose_recursive<T: Copy>(
input: &[T],
output: &mut [MU<T>],
row_start: usize,
row_end: usize,
col_start: usize,
col_end: usize,
total_columns: usize,
total_rows: usize,
) {
let nbr_rows = row_end - row_start;
let nbr_cols = col_end - col_start;
if (nbr_rows <= RECURSIVE_LIMIT && nbr_cols <= RECURSIVE_LIMIT)
|| nbr_rows <= 2
|| nbr_cols <= 2
{
let x_block_count = nbr_cols / BLOCK_SIZE;
let y_block_count = nbr_rows / BLOCK_SIZE;
let remainder_x = nbr_cols - x_block_count * BLOCK_SIZE;
let remainder_y = nbr_rows - y_block_count * BLOCK_SIZE;
for y_block in 0..y_block_count {
for x_block in 0..x_block_count {
unsafe {
transpose_block_segmented(
input,
output,
total_columns,
total_rows,
col_start + x_block * BLOCK_SIZE,
row_start + y_block * BLOCK_SIZE,
BLOCK_SIZE,
BLOCK_SIZE,
);
}
}
if remainder_x > 0 {
unsafe {
transpose_block(
input,
output,
total_columns,
total_rows,
col_start + x_block_count * BLOCK_SIZE,
row_start + y_block * BLOCK_SIZE,
remainder_x,
BLOCK_SIZE,
);
}
}
}
if remainder_y > 0 {
for x_block in 0..x_block_count {
unsafe {
transpose_block(
input,
output,
total_columns,
total_rows,
col_start + x_block * BLOCK_SIZE,
row_start + y_block_count * BLOCK_SIZE,
BLOCK_SIZE,
remainder_y,
);
}
}
if remainder_x > 0 {
unsafe {
transpose_block(
input,
output,
total_columns,
total_rows,
col_start + x_block_count * BLOCK_SIZE,
row_start + y_block_count * BLOCK_SIZE,
remainder_x,
remainder_y,
);
}
}
}
} else if nbr_rows >= nbr_cols {
transpose_recursive(
input,
output,
row_start,
row_start + (nbr_rows / 2),
col_start,
col_end,
total_columns,
total_rows,
);
transpose_recursive(
input,
output,
row_start + (nbr_rows / 2),
row_end,
col_start,
col_end,
total_columns,
total_rows,
);
} else {
transpose_recursive(
input,
output,
row_start,
row_end,
col_start,
col_start + (nbr_cols / 2),
total_columns,
total_rows,
);
transpose_recursive(
input,
output,
row_start,
row_end,
col_start + (nbr_cols / 2),
col_end,
total_columns,
total_rows,
);
}
}
pub const unsafe fn transpose<T: Copy>(
input: &[T],
output: &mut [MU<T>],
input_width: usize,
input_height: usize,
) {
const fn const_version<T: Copy>(a: &[T], b: &mut [MU<T>], c: usize, d: usize) {
unsafe { transpose_small(a, b, c, d) }
}
fn normal<T: Copy>(a: &[T], b: &mut [MU<T>], c: usize, d: usize) {
unsafe { transpose_(a, b, c, d) }
}
const_eval_select(
(input, output, input_width, input_height),
const_version,
normal,
)
}
pub const fn transposed<T: Copy, const W: usize, const H: usize>(input: [T; W * H]) -> [T; W * H] {
let mut output = [const { MU::uninit() }; W * H];
unsafe { transpose(&input, &mut output, W, H) };
unsafe { MU::array_assume_init(output) }
}
unsafe fn transpose_<T: Copy>(
input: &[T],
output: &mut [MU<T>],
input_width: usize,
input_height: usize,
) {
if input_width * input_height != output.len() || input_width * input_height != input.len() {
#[cfg(debug_assertions)]
panic!();
#[cfg(not(debug_assertions))]
unsafe {
core::hint::unreachable_unchecked()
};
}
if input.len() <= SMALL_LEN {
unsafe { transpose_small(input, output, input_width, input_height) };
} else if input.len() <= MEDIUM_LEN {
transpose_tiled(input, output, input_width, input_height);
} else {
transpose_recursive(
input,
output,
0,
input_height,
0,
input_width,
input_width,
input_height,
);
}
}