poulpy/poulpy-hal/src/reference/fft64/reim/mod.rs
poulpy-hal/src/reference/fft64/reim/mod.rsRaw
1// ----------------------------------------------------------------------
2// DISCLAIMER
3//
4// This module contains code that has been directly ported from the
5// spqlios-arithmetic library
6// (https://github.com/tfhe/spqlios-arithmetic), which is licensed
7// under the Apache License, Version 2.0.
8//
9// The porting process from C to Rust was done with minimal changes
10// in order to preserve the semantics and performance characteristics
11// of the original implementation.
12//
13// Both Poulpy and spqlios-arithmetic are distributed under the terms
14// of the Apache License, Version 2.0. See the LICENSE file for details.
15//
16// ----------------------------------------------------------------------
17
18#![allow(bad_asm_style)]
19
20mod conversion;
21mod fft_ref;
22mod fft_vec;
23mod ifft_ref;
24mod table_fft;
25mod table_ifft;
26mod zero;
27
28pub use conversion::*;
29pub use fft_ref::*;
30pub use fft_vec::*;
31pub use ifft_ref::*;
32pub use table_fft::*;
33pub use table_ifft::*;
34pub use zero::*;
35
36#[inline(always)]
37pub(crate) fn as_arr<const size: usize, R: Float + FloatConst>(x: &[R]) -> &[R; size] {
38 debug_assert!(x.len() >= size);
39 unsafe { &*(x.as_ptr() as *const [R; size]) }
40}
41
42#[inline(always)]
43pub(crate) fn as_arr_mut<const size: usize, R: Float + FloatConst>(x: &mut [R]) -> &mut [R; size] {
44 debug_assert!(x.len() >= size);
45 unsafe { &mut *(x.as_mut_ptr() as *mut [R; size]) }
46}
47
48use rand_distr::num_traits::{Float, FloatConst};
49#[inline(always)]
50pub(crate) fn frac_rev_bits<R: Float + FloatConst>(x: usize) -> R {
51 let half: R = R::from(0.5).unwrap();
52
53 match x {
54 0 => R::zero(),
55 1 => half,
56 _ => {
57 if x.is_multiple_of(2) {
58 frac_rev_bits::<R>(x >> 1) * half
59 } else {
60 frac_rev_bits::<R>(x >> 1) * half + half
61 }
62 }
63 }
64}
65
66pub trait ReimDFTExecute<D, T> {
67 fn reim_dft_execute(table: &D, data: &mut [T]);
68}
69
70pub trait ReimFromZnx {
71 fn reim_from_znx(res: &mut [f64], a: &[i64]);
72}
73
74pub trait ReimToZnx {
75 fn reim_to_znx(res: &mut [i64], divisor: f64, a: &[f64]);
76}
77
78pub trait ReimToZnxInplace {
79 fn reim_to_znx_inplace(res: &mut [f64], divisor: f64);
80}
81
82pub trait ReimAdd {
83 fn reim_add(res: &mut [f64], a: &[f64], b: &[f64]);
84}
85
86pub trait ReimAddInplace {
87 fn reim_add_inplace(res: &mut [f64], a: &[f64]);
88}
89
90pub trait ReimSub {
91 fn reim_sub(res: &mut [f64], a: &[f64], b: &[f64]);
92}
93
94pub trait ReimSubInplace {
95 fn reim_sub_inplace(res: &mut [f64], a: &[f64]);
96}
97
98pub trait ReimSubNegateInplace {
99 fn reim_sub_negate_inplace(res: &mut [f64], a: &[f64]);
100}
101
102pub trait ReimNegate {
103 fn reim_negate(res: &mut [f64], a: &[f64]);
104}
105
106pub trait ReimNegateInplace {
107 fn reim_negate_inplace(res: &mut [f64]);
108}
109
110pub trait ReimMul {
111 fn reim_mul(res: &mut [f64], a: &[f64], b: &[f64]);
112}
113
114pub trait ReimMulInplace {
115 fn reim_mul_inplace(res: &mut [f64], a: &[f64]);
116}
117
118pub trait ReimAddMul {
119 fn reim_addmul(res: &mut [f64], a: &[f64], b: &[f64]);
120}
121
122pub trait ReimCopy {
123 fn reim_copy(res: &mut [f64], a: &[f64]);
124}
125
126pub trait ReimZero {
127 fn reim_zero(res: &mut [f64]);
128}