The Highest-Level Feature of C

B

BartC

Scott Fluhrer said:
Minor nit: unless the compiler can prove that the lower 10 bits of x is
zero, it'd have to do something like:

if ( (x & 0x3ff) != 0) {

More of a major nit if it doesn't work! Then the compiler would have to do
more work to decide whether the optimisation is worth doing.
 
B

BartC

Richard Damon said:
Minor nit: unless the compiler can prove that the lower 10 bits of x is
zero, it'd have to do something like:

if ( (x & 0x3ff) != 0) {
/* Default case */
} else switch (x>>10) {
case 1:
case 2:
case 3:
}

The jump table method already requires the compiler to test the value to
be sure it is in range before using the jump table (to make sure it uses
an item IN the jump table), doing something like:

if((x & 0xF3FF) != 0) go to default
else go to table[x>>10] with entries in the table for 0, 1, 2, and 3

Wouldn't it still need a check for being in range after (or even before)
shifting?
would be nearly as efficient as the case of 1, 2, and 3, which would
need a comparison for x <= 3 (and > or >= 0).

(I've used a single unsigned compare here, when the jump-table includes an
entry for x=0.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,077
Messages
2,570,566
Members
47,202
Latest member
misc.

Latest Threads

Top