use std::ops::{Add, Div, Mul, Neg, Sub};
use crate::dtype::Element;
pub trait ResizeAcc:
Element
+ Add<Output = Self>
+ Sub<Output = Self>
+ Mul<Output = Self>
+ Div<Output = Self>
+ Neg<Output = Self>
{
const AA_BLOCK: usize;
fn constant(v: f64) -> Self;
fn from_i64(v: i64) -> Self;
fn fma(a: Self, b: Self, c: Self) -> Self;
fn abs(self) -> Self;
fn floor_index(self) -> i64;
fn ceil_count(self) -> usize;
fn aa_center(scale: Self, i: usize) -> Self;
fn aa_bound(x: Self) -> i64;
fn aa_arg(d: Self, invscale: Self) -> Self;
fn load<E: Element>(e: E) -> Self;
fn store<E: Element>(self) -> E;
}
impl ResizeAcc for f32 {
const AA_BLOCK: usize = 4;
#[inline(always)]
fn constant(v: f64) -> Self {
v as f32
}
#[inline(always)]
fn from_i64(v: i64) -> Self {
v as f32
}
#[inline(always)]
fn fma(a: Self, b: Self, c: Self) -> Self {
a.mul_add(b, c)
}
#[inline(always)]
fn abs(self) -> Self {
f32::abs(self)
}
#[inline(always)]
fn floor_index(self) -> i64 {
self.floor() as i64
}
#[inline(always)]
fn ceil_count(self) -> usize {
self.ceil() as usize
}
#[inline(always)]
fn aa_center(scale: Self, i: usize) -> Self {
(f64::from(scale) * (i as f64 + 0.5)) as f32
}
#[inline(always)]
fn aa_bound(x: Self) -> i64 {
(f64::from(x) + 0.5) as i64
}
#[inline(always)]
fn aa_arg(d: Self, invscale: Self) -> Self {
((f64::from(d) + 0.5) * f64::from(invscale)) as f32
}
#[inline(always)]
fn load<E: Element>(e: E) -> Self {
e.to_f32()
}
#[inline(always)]
fn store<E: Element>(self) -> E {
E::from_f32(self)
}
}
impl ResizeAcc for f64 {
const AA_BLOCK: usize = 2;
#[inline(always)]
fn constant(v: f64) -> Self {
v
}
#[inline(always)]
fn from_i64(v: i64) -> Self {
v as f64
}
#[inline(always)]
fn fma(a: Self, b: Self, c: Self) -> Self {
a.mul_add(b, c)
}
#[inline(always)]
fn abs(self) -> Self {
f64::abs(self)
}
#[inline(always)]
fn floor_index(self) -> i64 {
(self as f32).floor() as i64
}
#[inline(always)]
fn ceil_count(self) -> usize {
self.ceil() as usize
}
#[inline(always)]
fn aa_center(scale: Self, i: usize) -> Self {
scale * (i as f64 + 0.5)
}
#[inline(always)]
fn aa_bound(x: Self) -> i64 {
(x + 0.5) as i64
}
#[inline(always)]
fn aa_arg(d: Self, invscale: Self) -> Self {
(d + 0.5) * invscale
}
#[inline(always)]
fn load<E: Element>(e: E) -> Self {
e.to_f64()
}
#[inline(always)]
fn store<E: Element>(self) -> E {
E::from_f64(self)
}
}