11extern crate proc_macro;
22
33use proc_macro:: TokenStream ;
4- use quote:: { quote, ToTokens } ;
4+ use quote:: quote;
55use syn:: {
66 Ident , LitFloat , LitInt , LitStr , Result , Token , parse:: { Parse , ParseStream } , parse_macro_input
77} ;
@@ -55,6 +55,28 @@ impl Parse for ExpLutMacroInput {
5555}
5656
5757#[ proc_macro]
58+ /// Generate an exponential lookup table and related constants.
59+ ///
60+ /// Inputs are literals:
61+ /// - `start: f32` — lower bound (inclusive)
62+ /// - `end: f32` — upper bound (inclusive, requires `end > start`)
63+ /// - `steps: usize` — number of samples (must be >= 2)
64+ ///
65+ /// Expansion defines the following `const`s in the invocation scope:
66+ /// - `EXP_LOOKUP_START: f32`
67+ /// - `EXP_LOOKUP_END: f32`
68+ /// - `EXP_LOOKUP_STEPS: usize`
69+ /// - `EXP_LOOKUP_STEP_SIZE: f32`
70+ /// - `EXP_LOOKUP_LUT: [f32; EXP_LOOKUP_STEPS]` (values of `e^x` over `[start, end]`)
71+ ///
72+ /// Example:
73+ /// ```ignore
74+ /// exp_lut_macro::exp_lut_macro!(
75+ /// start: -20.0,
76+ /// end: 20.0,
77+ /// steps: 1000
78+ /// );
79+ /// ```
5880pub fn exp_lut_macro ( input : TokenStream ) -> TokenStream {
5981 let input = parse_macro_input ! ( input as ExpLutMacroInput ) ;
6082
@@ -71,12 +93,16 @@ pub fn exp_lut_macro(input: TokenStream) -> TokenStream {
7193 let end: f32 = input. end ;
7294 let step_size: f32 = ( end - start) / steps as f32 ;
7395
74- let expanded = quote ! {
75- const LUT : [ f32 ; #steps] = core:: array:: from_fn( |i| {
76- let x = #start + ( i as f32 * #step_size) ;
77- x. exp( )
78- } ) ;
79- } ;
96+ let lut: Vec < f32 > = ( 0 ..steps) . map ( |i| {
97+ let x = start + ( i as f32 * step_size) ;
98+ x. exp ( )
99+ } ) . collect ( ) ;
80100
81- TokenStream :: from ( expanded)
101+ TokenStream :: from ( quote ! {
102+ const EXP_LOOKUP_START : f32 = #start;
103+ const EXP_LOOKUP_END : f32 = #end;
104+ const EXP_LOOKUP_STEPS : usize = #steps;
105+ const EXP_LOOKUP_STEP_SIZE : f32 = #step_size;
106+ const EXP_LOOKUP_LUT : [ f32 ; #steps] = [ #( #lut) , * ] ;
107+ } )
82108}
0 commit comments