473,387 Members | 1,561 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

force jump table with switch statement (visual c)

Apologies if I'm sending this to the wrong group. If so I'd be grateful
if somebody could point me to the right one.
I'm using microsoft visual c++ 6.0 compiler. My code is C, I just use
the c++ compiler for easier positioning of declarations here and there.
I am putting together a small interpreter that will do a great deal of
iteration and I'd like to avoid using function pointers to my opcode
code because I don't need any machine-level stack pushing, popping,
calling and returning and these seem quite expensive compared to simple
goto's.
Can anybody advise what compile options I might use to force a jump
table when a switch statement is compiled? If I have to use a different
compiler, I will. Also, is there a disassembler or somesuch that will
let me confirm that a jump table was produced?
thanks,
pc
Dec 9 '05 #1
9 3847
paul c wrote:
Apologies if I'm sending this to the wrong group. If so I'd be grateful
if somebody could point me to the right one.

I'm using microsoft visual c++ 6.0 compiler. My code is C, I just use the
c++ compiler for easier positioning of declarations here and there.
Try microsoft.public.vc or one of its subgroups.
I am putting together a small interpreter that will do a great deal of
iteration and I'd like to avoid using function pointers to my opcode
code because I don't need any machine-level stack pushing, popping,
calling and returning and these seem quite expensive compared to simple
goto's.
Can anybody advise what compile options I might use to force a jump
table when a switch statement is compiled? If I have to use a different
compiler, I will. Also, is there a disassembler or somesuch that will
let me confirm that a jump table was produced?

You definitely want a compiler-specific ng. Standard C says nothing about this.

S.
Dec 9 '05 #2
paul c <to************@oohay.ac> writes:
[...]
I'm using microsoft visual c++ 6.0 compiler. My code is C, I just use
the c++ compiler for easier positioning of declarations here and there.
If you're mixing declarations and statements, your code isn't legal C
according to the C90 standard, but it probably is according to the C99
standard.

But note that compiling C code with a C++ compiler might cause some
problems. C isn't a strict subset of C++. The most obvious
difference is that C++ has some extra keywords; a variable called
"class" is perfectly legal in C, but not in C++. A more subtle
difference is the handling of void*. In C, a void* value can be
implicitly converted to any pointer-to-object type; in C++, it can't.
In particular, this:

some_type *ptr = malloc(sizeof *ptr);

is legal (and good style) in C, but a C++ compiler will complain about
a type mismatch.

If you don't have a C compiler that allows mixing declarations and
statements, you might consider just not mixing declarations and
statements. You can bring declarations closer to where they're used
with nested blocks.

[...]
Can anybody advise what compile options I might use to force a jump
table when a switch statement is compiled? If I have to use a
different compiler, I will. Also, is there a disassembler or somesuch
that will let me confirm that a jump table was produced?


I don't have an answer to that question (and if I did it would be
off-topic here). But it's probably not worth worrying about. If your
compiler is at all decent, it should do a reasonably good job of
generating the best possible code for a swtich statement. That might
not necessarily be a jump table. Use a command-line option to
increase the optimization level, and measure the actual speed of your
code; if it's fast enough, don't worry about it.

If you're sufficiently curious, most compilers have an option to
generate an assembly listing so you can examine the generated code.
Just remember that there's a good chance that the compiler knows more
than you do about the best code to generate.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 9 '05 #3
Thanks to Keith T and Skarmander for those answers. (Although I'm not
an Intel assembler expert, even vc's debug output sometimes produces a
jump table but not always - my target is a single process through which
hundreds of users will access memory one at a time but potentially
millions of memory accesses each time a user calls it - function
pointers are safer from a coding point of view but my measurements
suggest they are pretty expensive for my limited use of their capabilities.)
cheers,
pc

Dec 9 '05 #4

"Keith Thompson" <ks***@mib.org> wrote
If you're sufficiently curious, most compilers have an option to
generate an assembly listing so you can examine the generated code.
Just remember that there's a good chance that the compiler knows more
than you do about the best code to generate.

You can still normally beat a compiler with hand-optimised assembly. It's
quite a long time since I've had to do this, however.
Dec 10 '05 #5
Malcolm wrote:
"Keith Thompson" <ks***@mib.org> wrote
If you're sufficiently curious, most compilers have an option to
generate an assembly listing so you can examine the generated code.
Just remember that there's a good chance that the compiler knows more
than you do about the best code to generate.


You can still normally beat a compiler with hand-optimised assembly. It's
quite a long time since I've had to do this, however.

Could be an option, as I think there are only a couple of possible
processors for this program, basically generic Intel and Mac (which I
assume is still Motorola) and although I'd have to learn a bit of
assembler, for my purpose only a couple of dozen assembler lines are
needed provided they can be macro-ized.
Made up some more tests and the Visual C++ 6.0 jump table performance
didn't really outshine the function pointer alternative. Will have to
look more closely at the generated machine code.
thanks,
pc

Dec 11 '05 #6
>"Keith Thompson" <ks***@mib.org> wrote
If you're sufficiently curious, most compilers have an option to
generate an assembly listing so you can examine the generated code.
Just remember that there's a good chance that the compiler knows more
than you do about the best code to generate.

In article <dn**********@nwrdmz01.dmz.ncs.ea.ibs-infra.bt.com>
Malcolm <re*******@btinternet.com> wrote:You can still normally beat a compiler with hand-optimised assembly. It's
quite a long time since I've had to do this, however.


It has, however, become quite a bit more difficult lately (in my
experience at least). This is not so much because the compilers
are better -- although they are -- but rather because the machines
are much worse, in terms of predictability. An "obvious" optimization
that results in half the number of instructions in the innermost
loop sometimes results in twice the number of cycles in the innermost
loop.

Worse, sometimes the most significant optimization is to place the
branch that closes a loop at a particular position within the cache
line(s), which can require counting up the bytes of instructions
leading up to that point. "Counting" is something computers are,
in general, better at than people.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Dec 11 '05 #7
On 2005-12-11, Chris Torek <no****@torek.net> wrote:
Worse, sometimes the most significant optimization is to place the branch
that closes a loop at a particular position within the cache line(s), which
can require counting up the bytes of instructions leading up to that point.
"Counting" is something computers are, in general, better at than people.


Cue the story of Mel, who, I am lead to understand, was a *real* programmer.

--
Nathan Wagner
Dec 11 '05 #8
paul c wrote:
Malcolm wrote:
"Keith Thompson" <ks***@mib.org> wrote
If you're sufficiently curious, most compilers have an option to
generate an assembly listing so you can examine the generated code.
Just remember that there's a good chance that the compiler knows more
than you do about the best code to generate.
You can still normally beat a compiler with hand-optimised assembly.
It's quite a long time since I've had to do this, however.


Could be an option, as I think there are only a couple of possible
processors for this program, basically generic Intel and Mac (which I
assume is still Motorola) and although I'd have to learn a bit of
assembler, for my purpose only a couple of dozen assembler lines are
needed provided they can be macro-ized.


If the algorithm is optimised already, and the code is written sensibly
but still not fast enough, then this is probably the only reliable way
to get the required performance. After all, the next patch to the
compiler might change the way it handles the code you've written.
Made up some more tests and the Visual C++ 6.0 jump table performance
didn't really outshine the function pointer alternative. Will have to
look more closely at the generated machine code.


Which just goes to show why it is so often pointless trying to outsmart
the compiler with micro-optimisations.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Dec 11 '05 #9
Flash Gordon <sp**@flash-gordon.me.uk> writes:
paul c wrote:
Malcolm wrote:
"Keith Thompson" <ks***@mib.org> wrote

If you're sufficiently curious, most compilers have an option to
generate an assembly listing so you can examine the generated code.
Just remember that there's a good chance that the compiler knows more
than you do about the best code to generate.

You can still normally beat a compiler with hand-optimised
assembly. It's quite a long time since I've had to do this, however.

Could be an option, as I think there are only a couple of possible
processors for this program, basically generic Intel and Mac (which
I assume is still Motorola) and although I'd have to learn a bit of
assembler, for my purpose only a couple of dozen assembler lines are
needed provided they can be macro-ized.


If the algorithm is optimised already, and the code is written
sensibly but still not fast enough, then this is probably the only
reliable way to get the required performance. After all, the next
patch to the compiler might change the way it handles the code you've
written.


And the next version of the CPU might change the performance
tradeoffs. The patch to the compiler might be designed to generate
better code for the newer CPU; if you use assembly language, you can't
take advantage of the better generated code.

Use assembly if you must, but be prepared to throw it away later on.
If your assembly code is actually faster than the code generated by
the compiler, consider letting the compiler authors know about the
optimization they're missing (if it's likely to be generally useful).
Made up some more tests and the Visual C++ 6.0 jump table
performance didn't really outshine the function pointer alternative.
Will have to look more closely at the generated machine code.


Which just goes to show why it is so often pointless trying to
outsmart the compiler with micro-optimisations.


Indeed.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 11 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Charles Soto | last post by:
I've got a main loop script that calls two other scripts that do no user interaction. All they do is send a couple of mysql update statements. Then they use header() to call the main loop again. ...
2
by: kochs | last post by:
Hi, I have a problem here. Referring to the following programming structure, is there a way to cause the computer to overflow or stop by inputting wrong values of intTime and/or intDay? i.e. I...
9
by: cousaert | last post by:
Newsgroups: alt.comp.lang.learn.c-c++ Date: Fri, 27 Aug 2004 12:36:11 +0200 Lines: 36 User-Agent: KNode/0.7.6 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii...
4
by: Steve | last post by:
C# I am iterating through a collection of rows thus: foreach(DataRow myRow in dsTable.Table.Rows) { ........... } in this loop I have some conditions and when one is met I dont want it to
6
by: Todd A. Anderson | last post by:
I have a function foo of which I need to get the address. The problem is that when you say "&foo" (or just foo for that matter), you get the address of this function's entry in a jump table and...
12
by: | last post by:
Is it fine to call another method from Switch? Eg. Switch (stringVar) { case ("a"): somVar = "whatever"; Another_Method(); //call another method return;
43
by: dev_cool | last post by:
Hello friends, I'm a beginner in C programming. One of my friends asked me to write a program in C.The purpose of the program is print 1 to n without any conditional statement, loop or jump. ...
9
by: sam_cit | last post by:
Hi Everyone, I wanted to know as to how a switch case like the following one is converted into a jump table by the compiler, assuming that it does, switch(i) { case 4 : { ... printf("4");
1
by: =?Utf-8?B?bGlhbnF0bGl0?= | last post by:
Is using a jump statement more faster than using if statement with a jump statement example for(int i=0; i < 10; i++) { if(a == null) {
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.