Re: Help needed: Code generation for CASE/SWITCH statements

"William A. Barath" <wi534@victoria.tc.ca>
7 Dec 1997 22:01:55 -0500

          From comp.compilers

Related articles
[3 earlier articles]
Re: Help needed: Code generation for CASE/SWITCH statements preston@cs.rice.edu (1997-12-05)
Re: Help needed: Code generation for CASE/SWITCH statements dlmoore@ix.netcom.com (David L Moore) (1997-12-05)
Re: Help needed: Code generation for CASE/SWITCH statements drh@microsoft.com (Dave Hanson) (1997-12-05)
Re: Help needed: Code generation for CASE/SWITCH statements wilson@marker.cs.utah.edu (Wilson C Hsieh) (1997-12-05)
Re: Help needed: Code generation for CASE/SWITCH statements conway@mundook.cs.mu.OZ.AU (1997-12-05)
Re: Help needed: Code generation for CASE/SWITCH statements gclind01@spd.louisville.edu (1997-12-07)
Re: Help needed: Code generation for CASE/SWITCH statements wi534@victoria.tc.ca (William A. Barath) (1997-12-07)
Re: Help needed: Code generation for CASE/SWITCH statements sethml@ugcs.caltech.edu (1997-12-07)
Re: Help needed: Code generation for CASE/SWITCH statements henry@zoo.toronto.edu (Henry Spencer) (1997-12-10)
Re: Help needed: Code generation for CASE/SWITCH statements a_s_t_o_r@guardian.no (Alexander Kjeldaas) (1997-12-10)
Re: Help needed: Code generation for CASE/SWITCH statements vonbrand@inf.utfsm.cl (Horst von Brand) (1997-12-12)
Re: Help needed: Code generation for CASE/SWITCH statements wi534@victoria.tc.canada (William A. Barath) (1997-12-12)
Re: Help needed: Code generation for CASE/SWITCH statements henry@zoo.toronto.edu (Henry Spencer) (1997-12-15)
[4 later articles]
| List of all articles for this month |

From: "William A. Barath" <wi534@victoria.tc.ca>
Newsgroups: comp.compilers,comp.lang.c.moderated
Date: 7 Dec 1997 22:01:55 -0500
Organization: Victoria Freenet Association
References: <clcm-19971204-0012@plethora.net>
Keywords: optimize, code

On 4 Dec 1997, Jakob wrote:


|I have a number of different tactics for implementing a switch (among
|them a simple jump table and a series of conditional branches), and
|would like the compiler to make a semi-intelligent decision as to
|which to use for a particular switch.


Before you look at general literature, make sure you know you
machine's organization. If you are compiling byte code or some such
then this won't apply, but a machine which has CPU instruction caching
and speculative execution will end up giving you very mixed results
depending on your strategy. For example some CPUs actually 'remember'
which branch path they took on their last path through a code block
and will start executing code along that path before evaluation of the
branch operand has completed. This will create no overhead because
the code (and likely referenced data) will be cached. A jump table on
the other hand eliminates this little peek into the future, and often
incurrs cache load performance hits. On a 3rd generation or less
advanced CPU, or when compiling byte code, the only reason not to use
them is bloat.


Something else to consider: in Pascal, one can apply a range to a case
statement, so jump tables are often very inefficient. A good tradeoff
would be to build a switch block with conditional branches and jump
tables covering limited ranges for sequential (or nearly sequential)
case expressions. Efficiently arranged conditional branches can handle
many case statements with few tests. Ie. within 5 (executed) tests
the code can select from between up to 15 jump points. However,
anything more than that is guaranteed to be faster with a jump table.


Wil Barath, aka WseM : "I feel as though I see my pen to write"
Author of VPM, EDITPLN, and other VGA Planets support programs
Visit my homepage! -------------> http://victoria.tc.ca/~wi534
--


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.