poulpy/poulpy-backend/src/cpu_spqlios/spqlios-arithmetic/spqlios/arithmetic/zn_arithmetic.h
poulpy-backend/src/cpu_spqlios/spqlios-arithmetic/spqlios/arithmetic/zn_arithmetic.hRaw
1#ifndef SPQLIOS_ZN_ARITHMETIC_H
2#define SPQLIOS_ZN_ARITHMETIC_H
3
4#include <stdint.h>
5
6#include "../commons.h"
7
8typedef enum z_module_type_t { DEFAULT } Z_MODULE_TYPE;
9
10/** @brief opaque structure that describes the module and the hardware */
11typedef struct z_module_info_t MOD_Z;
12
13/**
14 * @brief obtain a module info for ring dimension N
15 * the module-info knows about:
16 * - the dimension N (or the complex dimension m=N/2)
17 * - any moduleuted fft or ntt items
18 * - the hardware (avx, arm64, x86, ...)
19 */
20EXPORT MOD_Z* new_z_module_info(Z_MODULE_TYPE mode);
21EXPORT void delete_z_module_info(MOD_Z* module_info);
22
23typedef struct tndbl_approxdecomp_gadget_t TNDBL_APPROXDECOMP_GADGET;
24
25EXPORT TNDBL_APPROXDECOMP_GADGET* new_tndbl_approxdecomp_gadget(const MOD_Z* module, //
26 uint64_t k,
27 uint64_t ell); // base 2^k, and size
28
29EXPORT void delete_tndbl_approxdecomp_gadget(TNDBL_APPROXDECOMP_GADGET* ptr);
30
31/** @brief sets res = gadget_decompose(a) (int8_t* output) */
32EXPORT void i8_approxdecomp_from_tndbl(const MOD_Z* module, // N
33 const TNDBL_APPROXDECOMP_GADGET* gadget, // gadget
34 int8_t* res, uint64_t res_size, // res (in general, size ell.a_size)
35 const double* a, uint64_t a_size); // a
36
37/** @brief sets res = gadget_decompose(a) (int16_t* output) */
38EXPORT void i16_approxdecomp_from_tndbl(const MOD_Z* module, // N
39 const TNDBL_APPROXDECOMP_GADGET* gadget, // gadget
40 int16_t* res, uint64_t res_size, // res (in general, size ell.a_size)
41 const double* a, uint64_t a_size); // a
42/** @brief sets res = gadget_decompose(a) (int32_t* output) */
43EXPORT void i32_approxdecomp_from_tndbl(const MOD_Z* module, // N
44 const TNDBL_APPROXDECOMP_GADGET* gadget, // gadget
45 int32_t* res, uint64_t res_size, // res (in general, size ell.a_size)
46 const double* a, uint64_t a_size); // a
47
48/** @brief opaque type that represents a prepared matrix */
49typedef struct zn32_vmp_pmat_t ZN32_VMP_PMAT;
50
51/** @brief size in bytes of a prepared matrix (for custom allocation) */
52EXPORT uint64_t bytes_of_zn32_vmp_pmat(const MOD_Z* module, // N
53 uint64_t nrows, uint64_t ncols); // dimensions
54
55/** @brief allocates a prepared matrix (release with delete_zn32_vmp_pmat) */
56EXPORT ZN32_VMP_PMAT* new_zn32_vmp_pmat(const MOD_Z* module, // N
57 uint64_t nrows, uint64_t ncols); // dimensions
58
59/** @brief deletes a prepared matrix (release with free) */
60EXPORT void delete_zn32_vmp_pmat(ZN32_VMP_PMAT* ptr); // dimensions
61
62/** @brief prepares a vmp matrix (contiguous row-major version) */
63EXPORT void zn32_vmp_prepare_contiguous( //
64 const MOD_Z* module,
65 ZN32_VMP_PMAT* pmat, // output
66 const int32_t* mat, uint64_t nrows, uint64_t ncols); // a
67
68/** @brief prepares a vmp matrix (mat[row]+col*N points to the item) */
69EXPORT void zn32_vmp_prepare_dblptr( //
70 const MOD_Z* module,
71 ZN32_VMP_PMAT* pmat, // output
72 const int32_t** mat, uint64_t nrows, uint64_t ncols); // a
73
74/** @brief prepares a vmp matrix (mat[row]+col*N points to the item) */
75EXPORT void zn32_vmp_prepare_row( //
76 const MOD_Z* module,
77 ZN32_VMP_PMAT* pmat, // output
78 const int32_t* row, uint64_t row_i, uint64_t nrows, uint64_t ncols); // a
79
80/** @brief applies a vmp product (int32_t* input) */
81EXPORT void zn32_vmp_apply_i32( //
82 const MOD_Z* module, //
83 int32_t* res, uint64_t res_size, // res
84 const int32_t* a, uint64_t a_size, // a
85 const ZN32_VMP_PMAT* pmat, uint64_t nrows, uint64_t ncols); // prep matrix
86
87/** @brief applies a vmp product (int16_t* input) */
88EXPORT void zn32_vmp_apply_i16( //
89 const MOD_Z* module, //
90 int32_t* res, uint64_t res_size, // res
91 const int16_t* a, uint64_t a_size, // a
92 const ZN32_VMP_PMAT* pmat, uint64_t nrows, uint64_t ncols); // prep matrix
93
94/** @brief applies a vmp product (int8_t* input) */
95EXPORT void zn32_vmp_apply_i8( //
96 const MOD_Z* module, //
97 int32_t* res, uint64_t res_size, // res
98 const int8_t* a, uint64_t a_size, // a
99 const ZN32_VMP_PMAT* pmat, uint64_t nrows, uint64_t ncols); // prep matrix
100
101// explicit conversions
102
103/** reduction mod 1, output in torus32 space */
104EXPORT void dbl_to_tn32(const MOD_Z* module, //
105 int32_t* res, uint64_t res_size, // res
106 const double* a, uint64_t a_size // a
107);
108
109/** real centerlift mod 1, output in double space */
110EXPORT void tn32_to_dbl(const MOD_Z* module, //
111 double* res, uint64_t res_size, // res
112 const int32_t* a, uint64_t a_size // a
113);
114
115/** round to the nearest int, output in i32 space.
116 * WARNING: ||a||_inf must be <= 2^18 in this function
117 */
118EXPORT void dbl_round_to_i32(const MOD_Z* module, //
119 int32_t* res, uint64_t res_size, // res
120 const double* a, uint64_t a_size // a
121);
122
123/** small int (int32 space) to double
124 * WARNING: ||a||_inf must be <= 2^18 in this function
125 */
126EXPORT void i32_to_dbl(const MOD_Z* module, //
127 double* res, uint64_t res_size, // res
128 const int32_t* a, uint64_t a_size // a
129);
130
131/** round to the nearest int, output in int64 space
132 * WARNING: ||a||_inf must be <= 2^50 in this function
133 */
134EXPORT void dbl_round_to_i64(const MOD_Z* module, //
135 int64_t* res, uint64_t res_size, // res
136 const double* a, uint64_t a_size // a
137);
138
139/** small int (int64 space, <= 2^50) to double
140 * WARNING: ||a||_inf must be <= 2^50 in this function
141 */
142EXPORT void i64_to_dbl(const MOD_Z* module, //
143 double* res, uint64_t res_size, // res
144 const int64_t* a, uint64_t a_size // a
145);
146
147#endif // SPQLIOS_ZN_ARITHMETIC_H