Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > sci.electronics.design > #538809

Re: a couple of LC filter progs

From "Tim Williams" <tiwill@seventransistorlabs.com>
Newsgroups sci.electronics.design
Subject Re: a couple of LC filter progs
Date 2019-04-09 14:50 -0500
Organization A noiseless patient Spider
Message-ID <q8it1k$3qi$1@dont-email.me> (permalink)
References (8 earlier) <q8dijg01967@drn.newsguy.com> <qhmkae1mkr8pfknv186ganulorjdbtti12@4ax.com> <0001HW.225A9E5400411EAC70000E6E32EF@news.giganews.com> <3srkaelmkd8brc6i1jbdm3eio9dlcbget6@4ax.com> <0001HW.225C3CD800A246FA70000E6E32EF@news.giganews.com>

Show all headers | View raw


"Joseph Gwinn" <joegwinn@comcast.net> wrote in message 
news:0001HW.225C3CD800A246FA70000E6E32EF@news.giganews.com...
> They coded in plain C, inspected the generated assembly code, and
tweaked the C code until the assembly code was clean and fast. It turned out
that the resulting code was largely portable in that all C compilers
generated clean, fast code from the same tweaked C source code, after the
source code was tweaked to the first two compilers.
>

Expanding on this some more --

Just in my humble experience alone, your writing style can massively affect 
code generation.

The optimizer is terribly, terribly far from exhaustive.  (It /could/ be 
exhaustive -- but then users would complain of hours or years of compilation 
time for almost no benefit, and that's no good!)  If it doesn't figure out 
any simple tricks, it's just going to pick the best, mediocre solution and 
let that be that.

And mind this affects execution time about as much as it does code size. 
Often, compact code runs faster, especially on simpler embedded platforms. 
(Yeah, when pipelines and caches get involved, unrolled and inlined 
operations get more attractive, and the discrepancy between compact and fast 
code can grow.)

Things the optimizer is likely to check, can range from modestly unrolling 
or reordering loops, to factoring numerical expressions, to inlining 
functions and operating on the resulting mega-function, and more.  All of 
these grow quickly in complexity, however, and the pursuit can become 
self-defeating.

A recent example was a bit-packing function, on an 8-bit platform with 
hardware multiply.  I wrote this a few different ways.  By far, the worst 
was a mega-expression: between macros and carefully indented and inspected 
sub-expressions, the whole operation can be expressed entirely numerically. 
That's technically fine, but the compiler really throws up its proverbial 
hands and basically ends up writing out the expression long-form without any 
reuse of sub-expressions, or registers even(!).

The next best was using a bunch of variables (which are allocated on the 
stack normally, but these are optimized out quickly when there are enough 
free registers to put them there instead, which was the case here) to hold 
intermediate steps, and repeating common steps in a short loop.  But keep in 
mind that, if the variables are allocated in registers... you can't loop 
over them.  Doing it with a loop, forces it to allocate stack, get the 
pointer, and index the variables.  Plus the memory accesses themselves, 
which are slower.  It is not without overhead!  (This is a much better deal 
on, say, classic 16-bit machines (x86, 68k), and most everything since.)  It 
might even try it with the loop and array, then try it unrolled with 
registers, and keep the unrolled version because it's simply better!

I forget what I ended up with; I think I sliced the bit pattern differently, 
still using a loop but getting better reuse.  It's still ugly, like hundreds 
of bytes for something nearly trivial if the data were byte aligned.

All of this optimizing is subject to the constrains of the functions 
executed within each expression or statement.  C functions can do literally 
anything.  Side effects are the bane of optimization.  If the optimizer 
can't reason about being able to move a function up or down the expression 
tree, it's simply forced to treat that as a sequence point.  (Sure, it could 
reason about the function's contents as if inlining it, but that would be 
extremely costly.)

As far as I know, the optimizer is bad at guessing what functions do, in 
terms of side effects, so it can help greatly (and this is why they put the 
features in there) to add hints about the nature of the function (e.g., 
using const with parameters, writing pure functions when possible, etc.).

(FYI, most of my experience centers around GCC.  Most of this is motivated 
by my own observations, illuminated by some of the official documentation 
about the optimizing step.)

Tim

-- 
Seven Transistor Labs, LLC
Electrical Engineering Consultation and Design
Website: https://www.seventransistorlabs.com/ 

Back to sci.electronics.design | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

a couple of LC filter progs John Larkin <jjlarkin@highlandtechnology.com> - 2019-04-05 19:55 -0700
  Re: a couple of LC filter progs "John Miles, KE5FX" <jmiles@gmail.com> - 2019-04-06 20:07 -0700
    Re: a couple of LC filter progs John Larkin <jjlarkin@highlandtechnology.com> - 2019-04-07 07:47 -0700
      Re: a couple of LC filter progs Winfield Hill <hill@rowland.harvard.edu> - 2019-04-07 08:46 -0700
        Re: a couple of LC filter progs John Larkin <jjlarkin@highlandtechnology.com> - 2019-04-07 09:40 -0700
          Re: a couple of LC filter progs Winfield Hill <hill@rowland.harvard.edu> - 2019-04-07 09:49 -0700
            Re: a couple of LC filter progs John Larkin <jjlarkin@highlandtechnology.com> - 2019-04-07 10:17 -0700
              Re: a couple of LC filter progs Joseph Gwinn <joegwinn@comcast.net> - 2019-04-07 15:07 -0400
                Re: a couple of LC filter progs Winfield Hill <hill@rowland.harvard.edu> - 2019-04-07 12:21 -0700
                Re: a couple of LC filter progs John Larkin <jjlarkin@highlandtechnology.com> - 2019-04-07 13:24 -0700
                Re: a couple of LC filter progs Joseph Gwinn <joegwinn@comcast.net> - 2019-04-07 17:08 -0400
                Re: a couple of LC filter progs John Larkin <jjlarkin@highlandtechnology.com> - 2019-04-07 14:53 -0700
                Re: a couple of LC filter progs Joseph Gwinn <joegwinn@comcast.net> - 2019-04-08 22:37 -0400
                Re: a couple of LC filter progs "Tim Williams" <tiwill@seventransistorlabs.com> - 2019-04-09 00:57 -0500
                Re: a couple of LC filter progs Joseph Gwinn <joegwinn@comcast.net> - 2019-04-09 10:44 -0400
                Re: a couple of LC filter progs "Tim Williams" <tiwill@seventransistorlabs.com> - 2019-04-09 14:50 -0500
                Re: a couple of LC filter progs Gerhard Hoffmann <dk4xp@arcor.de> - 2019-04-09 23:02 +0200
                Re: a couple of LC filter progs "Tim Williams" <tiwill@seventransistorlabs.com> - 2019-04-09 19:30 -0500
                Re: a couple of LC filter progs Gerhard Hoffmann <dk4xp@arcor.de> - 2019-04-10 02:55 +0200
                Re: a couple of LC filter progs upsidedown@downunder.com - 2019-04-10 07:40 +0300
                Re: a couple of LC filter progs Gerhard Hoffmann <dk4xp@arcor.de> - 2019-04-10 11:26 +0200
                Re: a couple of LC filter progs upsidedown@downunder.com - 2019-04-10 21:20 +0300
                Re: a couple of LC filter progs Tauno Voipio <tauno.voipio@notused.fi.invalid> - 2019-04-10 11:50 +0300
                Re: a couple of LC filter progs Joseph Gwinn <joegwinn@comcast.net> - 2019-04-11 09:15 -0400
    Re: a couple of LC filter progs Simon S Aysdie <gwhite@ti.com> - 2019-04-07 10:22 -0700
  Re: a couple of LC filter progs "Tim Williams" <tiwill@seventransistorlabs.com> - 2019-04-07 00:41 -0500
    Re: a couple of LC filter progs John Larkin <jjlarkin@highlandtechnology.com> - 2019-04-07 07:54 -0700
  Re: a couple of LC filter progs Clifford Heath <no.spam@please.net> - 2019-04-07 17:56 +1000
    Re: a couple of LC filter progs John Larkin <jjlarkin@highlandtechnology.com> - 2019-04-07 07:55 -0700
      Re: a couple of LC filter progs Clifford Heath <no.spam@please.net> - 2019-04-08 07:59 +1000
        Re: a couple of LC filter progs John Larkin <jjlarkin@highland_snip_technology.com> - 2019-04-08 10:58 -0700
  Re: a couple of LC filter progs Simon S Aysdie <gwhite@ti.com> - 2019-04-07 10:03 -0700

csiph-web