Skip to main content

competitive/num/mint/
mod.rs

1//! modint
2
3#[cfg(target_arch = "x86_64")]
4use crate::tools::{advise_huge_pages, avx512_enabled, avx512_supported};
5use crate::{
6    algebra::DotProduct,
7    num::{BarrettReduction, One, Zero},
8    tools::{FastOutput, FastPrint, RandomSpec, Scan, ScanSource, SerdeByteStr, Xorshift},
9};
10
11#[codesnip::entry("MIntBase", include("scanner", "zero_one"))]
12pub use mint_base::{MInt, MIntBase, MIntConvert};
13
14#[cfg_attr(nightly, codesnip::entry("MIntBase"))]
15mod mint_base;
16
17#[cfg_attr(
18    nightly,
19    codesnip::entry("MInt", include("MIntBase", "BarrettReduction"))
20)]
21pub mod mint_basic;
22
23#[cfg_attr(nightly, codesnip::entry("montgomery", include("MIntBase")))]
24pub mod montgomery;
25
26#[codesnip::entry("MIntDotProduct")]
27pub use mint_dot_product::MIntDotProduct;
28#[cfg_attr(
29    nightly,
30    codesnip::entry(when("MInt", "MIntDotProduct"), include("simd_matrix", "avx_helper"))
31)]
32mod mint_basic_dot_product;
33#[cfg_attr(
34    nightly,
35    codesnip::entry("MIntDotProduct", include("MIntBase", "ring"))
36)]
37mod mint_dot_product;
38#[cfg_attr(
39    nightly,
40    codesnip::entry(
41        when("montgomery", "MIntDotProduct"),
42        include("simd_matrix", "montgomery_simd", "avx_helper")
43    )
44)]
45mod montgomery_dot_product;
46#[cfg(target_arch = "x86_64")]
47#[cfg_attr(nightly, codesnip::entry("montgomery_simd"))]
48pub mod montgomery_simd;
49
50#[cfg(target_arch = "x86_64")]
51#[cfg_attr(
52    nightly,
53    codesnip::entry("simd_matrix", include("MIntBase", "avx_helper", "_huge_pages"))
54)]
55mod simd_matrix;
56
57#[codesnip::entry(when("MIntBase", "fastio"))]
58impl<M> FastPrint for MInt<M>
59where
60    M: MIntBase<Inner: FastPrint>,
61{
62    #[inline]
63    fn fast_print<W: std::io::Write>(&self, writer: &mut FastOutput<W>) {
64        self.inner().fast_print(writer);
65    }
66}
67
68#[codesnip::entry(when("MIntBase", "coding"))]
69impl<M> SerdeByteStr for MInt<M>
70where
71    M: MIntBase<Inner: SerdeByteStr>,
72{
73    fn serialize(&self, buf: &mut Vec<u8>) {
74        self.inner().serialize(buf)
75    }
76
77    fn deserialize<I>(iter: &mut I) -> Self
78    where
79        I: Iterator<Item = u8>,
80    {
81        Self::new_unchecked(M::Inner::deserialize(iter))
82    }
83}
84
85#[cfg_attr(nightly, codesnip::entry(when("MIntBase", "random_generator")))]
86mod random_spec {
87    use super::*;
88    use std::ops::{RangeFull, RangeTo};
89
90    impl<M> RandomSpec<MInt<M>> for RangeFull
91    where
92        M: MIntBase,
93        RangeTo<M::Inner>: RandomSpec<M::Inner>,
94    {
95        fn rand(&self, rng: &mut Xorshift) -> MInt<M> {
96            MInt::<M>::new_unchecked(rng.random(..M::get_mod()))
97        }
98    }
99}