ape_table_trig_macros/
lib.rs

1use std::f32::consts::PI as PI_F32;
2use std::f64::consts::PI as PI_F64;
3
4const QUART_CIRC_F32: f32 = 0.5 * PI_F32;
5const QUART_CIRC_F64: f64 = 0.5 * PI_F64;
6
7const GEN_LIMIT_F32: f32 = QUART_CIRC_F32;
8const GEN_LIMIT_F64: f64 = QUART_CIRC_F64;
9
10use proc_macro::*;
11
12#[proc_macro]
13/// Generate a trig table of F32s. Provide the size of the table in usize.
14pub fn trig_table_gen_f32(tokens: TokenStream) -> TokenStream {
15    let size_str = format!("{}", tokens);
16
17    let size: usize = size_str.parse().expect("Please provide the size of the table as a usize!");
18
19    let mut generated_code: String = "[".to_string();
20
21    for i in 0..size {
22        // We only need to calculate the sin up to 0.5π
23        let radians = (i as f32)/(size as f32) * GEN_LIMIT_F32;
24
25        generated_code.push_str(&format!("{:e},", radians.sin()));
26    }
27
28    generated_code.push(']');
29    
30    generated_code.parse().expect("Could not parse for some reason!")
31}
32
33#[proc_macro]
34/// Generate a trig table of F64s. Provide the size of the table in usize.
35pub fn trig_table_gen_f64(tokens: TokenStream) -> TokenStream {
36    let size_str = format!("{}", tokens);
37
38    let size: usize = size_str.parse().expect("Please provide the size of the table as a usize!");
39
40    let mut generated_code: String = "[".to_string();
41
42    for i in 0..size {
43        // We only need to calculate the sin up to 0.5π
44        let radians = (i as f64)/(size as f64) * GEN_LIMIT_F64;
45
46        generated_code.push_str(&format!("{:e},", radians.sin()));
47    }
48
49    generated_code.push(']');
50    
51    generated_code.parse().expect("Could not parse for some reason!")
52}