Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > sci.electronics.design > #480563
| From | David Brown <david.brown@hesbynett.no> |
|---|---|
| Newsgroups | sci.electronics.design |
| Subject | Re: Ping Tom Gardner et. al. |
| Date | 2017-10-06 12:20 +0200 |
| Organization | A noiseless patient Spider |
| Message-ID | <or7leb$gol$1@dont-email.me> (permalink) |
| References | <kqABB.142601$po2.80988@fx36.iad> <or7bpu$efd$1@dont-email.me> <or7hpn$mdb$1@dont-email.me> <duIBB.11$m83.3@fx03.iad> <FzIBB.724$8%2.58@fx08.iad> |
On 06/10/17 12:05, bitrex wrote: > On 10/06/2017 05:59 AM, bitrex wrote: >> On 10/06/2017 05:18 AM, David Brown wrote: >>> 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. >> >> The problem is at high optimization levels unless you actually _do_ >> something with the table in "main" that has some kind of side-effect >> the compiler just expunges it as well. The most efficient code is >> nothing! >> >> IDK if there's some way to modify the sources you or I pasted using >> say "volatile" to avoid that and still have it apply heavy optimization > > The code you pasted does seem to generate the table by its lonesome for > the MSP430 using gcc 5.3. I guess it's architecture and table-size > dependent; x86-64 doesn't because for a table of that size it's using > magic constants immediate-loaded into registers and not even using the > .data section at all. The compiler will generate the same table here - whether you target the msp430 or the x86, compiler version, and optimisation levels (you may have to change "size_t" to "unsigned" in some cases, however). The trick is to use the "-c" flag to tell gcc to compile, but not link the code. If you put in a "main" function and add linking, the compiler can try to see what is /actually/ used and then omit things as needed. But when you compile a single translation unit, and include something that is "exported" - has external linkage - the compiler will generate appropriate code or data. So in my example here I have "extern constexpr Table<length> table = ..." . This tells the compiler that other modules may access "table" - it has to generate it. And the table will be put in .rodata, not .data - on the msp430, that will be linked into flash, not ram.
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