cfx_math/nth_root/root_degree/
mod.rs1mod exponential_table;
2mod logarithmic_table;
3
4use self::exponential_table::{make_table_u32, make_table_u64, search_table};
5pub use self::logarithmic_table::{
6 cbrt_lookup, rt4_lookup, rt5_lookup, sqrt_lookup,
7};
8use super::const_generic::SubU1;
9use typenum::{Unsigned, U10, U11, U12, U2, U3, U4, U5, U6, U7, U8, U9};
10
11pub trait RootDegree: Unsigned + SubU1 {
12 const LOOKUP_BITS: u32 = 0;
13
14 fn nth_root_lookup(_input: u64) -> u64 { unimplemented!() }
15}
16
17impl RootDegree for U2 {
18 const LOOKUP_BITS: u32 = 16;
19
20 #[inline]
21 fn nth_root_lookup(input: u64) -> u64 { sqrt_lookup(input) }
22}
23
24impl RootDegree for U3 {
25 const LOOKUP_BITS: u32 = 20;
26
27 #[inline]
28 fn nth_root_lookup(input: u64) -> u64 { cbrt_lookup(input) }
29}
30
31impl RootDegree for U4 {
32 const LOOKUP_BITS: u32 = 25;
33
34 #[inline]
35 fn nth_root_lookup(input: u64) -> u64 { rt4_lookup(input) }
36}
37
38impl RootDegree for U5 {
39 const LOOKUP_BITS: u32 = 28;
40
41 #[inline]
42 fn nth_root_lookup(input: u64) -> u64 { rt5_lookup(input) }
43}
44impl RootDegree for U6 {
45 const LOOKUP_BITS: u32 = 30;
46
47 #[inline]
48 fn nth_root_lookup(input: u64) -> u64 {
49 const TABLE: [u32; 32] = make_table_u32(6);
50 search_table::<_, 32>(input, &TABLE, 6)
51 }
52}
53impl RootDegree for U7 {
54 const LOOKUP_BITS: u32 = 28;
55
56 #[inline]
57 fn nth_root_lookup(input: u64) -> u64 {
58 const TABLE: [u32; 16] = make_table_u32(7);
59 search_table::<_, 16>(input, &TABLE, 7)
60 }
61}
62impl RootDegree for U8 {
63 const LOOKUP_BITS: u32 = 32;
64
65 #[inline]
66 fn nth_root_lookup(input: u64) -> u64 {
67 const TABLE: [u32; 16] = make_table_u32(8);
68 search_table::<_, 16>(input, &TABLE, 8)
69 }
70}
71impl RootDegree for U9 {
72 const LOOKUP_BITS: u32 = 36;
73
74 #[inline]
75 fn nth_root_lookup(input: u64) -> u64 {
76 const TABLE: [u64; 16] = make_table_u64(9);
77 search_table::<_, 16>(input, &TABLE, 9)
78 }
79}
80impl RootDegree for U10 {
81 const LOOKUP_BITS: u32 = 40;
82
83 #[inline]
84 fn nth_root_lookup(input: u64) -> u64 {
85 const TABLE: [u64; 16] = make_table_u64(10);
86 search_table::<_, 16>(input, &TABLE, 10)
87 }
88}
89impl RootDegree for U11 {
90 const LOOKUP_BITS: u32 = 44;
91
92 #[inline]
93 fn nth_root_lookup(input: u64) -> u64 {
94 const TABLE: [u64; 16] = make_table_u64(11);
95 search_table::<_, 16>(input, &TABLE, 11)
96 }
97}
98impl RootDegree for U12 {
99 const LOOKUP_BITS: u32 = 48;
100
101 #[inline]
102 fn nth_root_lookup(input: u64) -> u64 {
103 const TABLE: [u64; 16] = make_table_u64(12);
104 search_table::<_, 16>(input, &TABLE, 12)
105 }
106}