> Message: 2 > Date: Wed, 4 Mar 2009 13:05:39 -0500 > From: David VanHorn <d...@mobilefusioninc.com> > Subject: [avr-gcc-list] Tables > To: AVR-GCC <avr-gcc-list@nongnu.org> > Message-ID: > <a8ed8b370903041005q5638beb8m66021895f9a11...@mail.gmail.com> > Content-Type: text/plain; charset="iso-8859-1" > > How would I construct a lookup table of logarithms in program memory? > I'll need the logarithm of N entries between 0 and 1, ex: If N = 10, then I > need log(0), log(0.1), log(0.2) etc. > I don't know what precision I'll need yet, but I'll need to be adjusting N, > so manually creating the table with a spreadsheet would be one way, though > icky. > I'm hoping there's a way to have the compiler generate it for me? > > The C preprocessor is too basic to do this sort of thing. Functions like logarithms come from the math library the the preprocessor can't make use of libraries at compile time.
This is a PERL script I use to generate sine tables to load onto a Mega48. It shouldn't be hard to adapt it to generate a log table too: #!/usr/bin/perl my $hz = 1000; my $rate = 288000; my $num = ($rate/$hz)>>0; print "signed short sine_table[$num] = {\n"; my $pi = atan2(1, 1)*4; for( my $i = 0; $i < $num; ++$i ) { my $pos = $i / ($rate/$hz); my $sine = sin(2 * $pi * $pos); printf("\t%d%s\n", ($sine*32767)>>0, $i < $num-1 ? "," : ""); } print "};\n"; Then I just do ./sine.pl > sine.h and #include sine.h Keep in mind there are algorithmic ways too do logs even without floating point support, they're going to be more compact than a table, but slower. If speed is your #1 concern then a table is best. Don't forget to use the appropriate function to read the data out of flash, you can't just index the array (unfortunately). Nicholas _______________________________________________ AVR-GCC-list mailing list AVR-GCC-list@nongnu.org http://lists.nongnu.org/mailman/listinfo/avr-gcc-list