use core::ffi::c_void;
use num_complex::{Complex32, Complex64};
use onemkl_sys::{MKL_Complex8, MKL_Complex16};
mod sealed {
pub trait Sealed {}
impl Sealed for f32 {}
impl Sealed for f64 {}
impl Sealed for num_complex::Complex32 {}
impl Sealed for num_complex::Complex64 {}
}
pub trait Scalar: sealed::Sealed + Copy + Send + Sync + 'static {
type Real: RealScalar;
fn zero() -> Self;
fn one() -> Self;
#[inline]
fn as_void_ptr(&self) -> *const c_void {
self as *const Self as *const c_void
}
#[inline]
fn as_mut_void_ptr(&mut self) -> *mut c_void {
self as *mut Self as *mut c_void
}
}
pub trait RealScalar: Scalar<Real = Self> + num_traits::Float {}
pub trait ComplexScalar: Scalar {
type MklType: Copy;
fn from_re_im(re: Self::Real, im: Self::Real) -> Self;
fn re(self) -> Self::Real;
fn im(self) -> Self::Real;
#[inline]
unsafe fn as_mkl_ptr(p: *const Self) -> *const Self::MklType {
p.cast::<Self::MklType>()
}
#[inline]
unsafe fn as_mkl_mut_ptr(p: *mut Self) -> *mut Self::MklType {
p.cast::<Self::MklType>()
}
}
impl Scalar for f32 {
type Real = f32;
#[inline]
fn zero() -> Self {
0.0
}
#[inline]
fn one() -> Self {
1.0
}
}
impl RealScalar for f32 {}
impl Scalar for f64 {
type Real = f64;
#[inline]
fn zero() -> Self {
0.0
}
#[inline]
fn one() -> Self {
1.0
}
}
impl RealScalar for f64 {}
impl Scalar for Complex32 {
type Real = f32;
#[inline]
fn zero() -> Self {
Complex32::new(0.0, 0.0)
}
#[inline]
fn one() -> Self {
Complex32::new(1.0, 0.0)
}
}
impl ComplexScalar for Complex32 {
type MklType = MKL_Complex8;
#[inline]
fn from_re_im(re: f32, im: f32) -> Self {
Complex32::new(re, im)
}
#[inline]
fn re(self) -> f32 {
self.re
}
#[inline]
fn im(self) -> f32 {
self.im
}
}
impl Scalar for Complex64 {
type Real = f64;
#[inline]
fn zero() -> Self {
Complex64::new(0.0, 0.0)
}
#[inline]
fn one() -> Self {
Complex64::new(1.0, 0.0)
}
}
impl ComplexScalar for Complex64 {
type MklType = MKL_Complex16;
#[inline]
fn from_re_im(re: f64, im: f64) -> Self {
Complex64::new(re, im)
}
#[inline]
fn re(self) -> f64 {
self.re
}
#[inline]
fn im(self) -> f64 {
self.im
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn complex_layouts_match_mkl() {
use core::mem::{align_of, size_of};
assert_eq!(size_of::<Complex32>(), size_of::<MKL_Complex8>());
assert_eq!(align_of::<Complex32>(), align_of::<MKL_Complex8>());
assert_eq!(size_of::<Complex64>(), size_of::<MKL_Complex16>());
assert_eq!(align_of::<Complex64>(), align_of::<MKL_Complex16>());
}
}