Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > sci.electronics.design > #480558
| From | David Brown <david.brown@hesbynett.no> |
|---|---|
| Newsgroups | sci.electronics.design |
| Subject | Re: Ping Tom Gardner et. al. |
| Date | 2017-10-06 11:18 +0200 |
| Organization | A noiseless patient Spider |
| Message-ID | <or7hpn$mdb$1@dont-email.me> (permalink) |
| References | <kqABB.142601$po2.80988@fx36.iad> <or7bpu$efd$1@dont-email.me> |
On 06/10/17 09:36, David Brown wrote:
> On 06/10/17 02:49, bitrex wrote:
>> In case you aren't checking the other big thread I started, one more fun
>> use of modern C++ on embedded processors I thought might be of interest
>> is doing lookup table generation at compile time. Here's some code that
>> generates a table for some weird function:
>>
>> <https://pastebin.com/5jB4RRz4>
>>
>> (source:
>> <http://cplusadd.blogspot.com/2013/02/c11-compile-time-lookup-tablearray-with.html>)
>>
>
> C++14 allows a lot more freedom in constexpr functions. In particular,
> it lets you write loops. That means you can do the same sort of thing
> with much simpler and clearer code, and with much greater compile-time
> efficiency. These recursive template solutions avoid run-time costs,
> but scale very badly when you want larger tables. You then find
> yourself using binary recursion instead of linear recursion, and while
> everything works correctly (it's easy to check), your code review folks
> are sticking hot pokers in their eyes to avoid looking at it.
>
> So C++11 allows some fancy tricks like, but they should not be used
> until C++14 and generalised constexpr functions. (I can't remember
> off-hand if gcc 5.3 supports that - I /think/ it does.)
>
> For lower than C++14, write your table generators in Python - simple,
> clear, and you can output a table in C or C++ formats.
>
>>
>> Here's the asm output with gcc 5.3.0 with flags -std=c++11 -Os for the
>> MSP430:
>>
>> <https://pastebin.com/NYkjV9Ra>
>>
>> Look ma, no code! The compiler just puts a lookup table in the data
>> section.
>>
>> The output for x86 is even more compact using 64 bit magic numbers and
>> offsets. No math.h functions called anywhere:
>>
>> <https://pastebin.com/4vg0Y49S>
>
When you are trying to look at the code generated for things like this,
avoid std::cout like the plague - it clutters everything up.
Try this code:
#include <cmath>
template<unsigned N>
struct Table {
double values[N];
static constexpr unsigned length = N;
constexpr double& operator[](size_t i) {
return values[i];
}
constexpr const double& operator[](size_t i) const {
return values[i];
}
};
template <unsigned N, typename LambdaType>
constexpr Table<N> TableGenerator(LambdaType evalFunc) {
Table<N> table{};
for (unsigned i = 0; i < table.length; i++) {
table[i] = evalFunc(i);
}
return table;
}
/** Function that computes a value for each index **/
constexpr double myFunc(unsigned idx) {
return sin(0.2 * idx) + cos(0.5 * idx);
}
constexpr unsigned length = 100;
// create compile-time table
extern constexpr Table<length> table = TableGenerator<length>(myFunc);
Back to sci.electronics.design | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Ping Tom Gardner et. al. bitrex <bitrex@de.lete.earthlink.net> - 2017-10-05 20:49 -0400
Re: Ping Tom Gardner et. al. David Brown <david.brown@hesbynett.no> - 2017-10-06 09:36 +0200
Re: Ping Tom Gardner et. al. David Brown <david.brown@hesbynett.no> - 2017-10-06 11:18 +0200
Re: Ping Tom Gardner et. al. bitrex <bitrex@de.lete.earthlink.net> - 2017-10-06 05:59 -0400
Re: Ping Tom Gardner et. al. bitrex <bitrex@de.lete.earthlink.net> - 2017-10-06 06:05 -0400
Re: Ping Tom Gardner et. al. David Brown <david.brown@hesbynett.no> - 2017-10-06 12:20 +0200
Re: Ping Tom Gardner et. al. Tom Gardner <spamjunk@blueyonder.co.uk> - 2017-10-06 11:47 +0100
Re: Ping Tom Gardner et. al. David Brown <david.brown@hesbynett.no> - 2017-10-06 14:24 +0200
Re: Ping Tom Gardner et. al. Tom Gardner <spamjunk@blueyonder.co.uk> - 2017-10-06 13:55 +0100
Re: Ping Tom Gardner et. al. bitrex <bitrex@de.lete.earthlink.net> - 2017-10-06 09:12 -0400
Re: Ping Tom Gardner et. al. David Brown <david.brown@hesbynett.no> - 2017-10-06 16:37 +0200
Re: Ping Tom Gardner et. al. Tom Gardner <spamjunk@blueyonder.co.uk> - 2017-10-06 15:50 +0100
Re: Ping Tom Gardner et. al. bitrex <bitrex@de.lete.earthlink.net> - 2017-10-06 14:02 -0400
Re: Ping Tom Gardner et. al. David Brown <david.brown@hesbynett.no> - 2017-10-06 16:32 +0200
Re: Ping Tom Gardner et. al. Tom Gardner <spamjunk@blueyonder.co.uk> - 2017-10-06 17:44 +0100
Re: Ping Tom Gardner et. al. David Brown <david.brown@hesbynett.no> - 2017-10-16 11:18 +0200
Re: Ping Tom Gardner et. al. Jasen Betts <jasen@xnet.co.nz> - 2017-10-09 07:58 +0000
Re: Ping Tom Gardner et. al. David Brown <david.brown@hesbynett.no> - 2017-10-16 11:23 +0200
csiph-web