Questions tagged [jump-table]

A jump table (also known as a branch table) is used to transfer program control (branching) to another part of the program by storing a table of branch instructions.

63 questions
282
votes
12 answers

Is 'switch' faster than 'if'?

Is a switch statement actually faster than an if statement? I ran the code below on Visual Studio 2010's x64 C++ compiler with the /Ox flag: #include #include #include #define MAX_COUNT (1 << 29) size_t counter =…
user541686
  • 205,094
  • 128
  • 528
  • 886
24
votes
11 answers

How to store goto labels in an array and then jump to them?

I want to declare an array of "jumplabels". Then I want to jump to a "jumplabel" in this array. But I have not any idea how to do this. It should look like the following code: function() { "gotolabel" s[3]; s[0] = s0; s[1] = s1; s[2]…
youllknow
11
votes
7 answers

Does "default" switch case disturb jump table optimization?

In my code I'm used to write fall-back default cases containing asserts like the following, to guard me against forgetting to update the switch in case semantics change switch(mode) { case ModeA: ... ; case ModeB: ... ; case .. /* many of them ...…
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
11
votes
2 answers

c switch and jump tables

It is my understanding that a switch statement in c/c++ will sometimes compile to a jump table. My question is, are there any thumb rules to assure that? In my case I'm doing something like this: enum myenum{ MY_CASE0= 0, MY_CASE0= 1,…
Daniel Miron
  • 460
  • 3
  • 14
8
votes
3 answers

jump table examples in C

Please give me some examples of jump table usage. I have seen this example on wikipedia: #include #include typedef void (*Handler)(void); /* A pointer to a handler function */ /* The functions */ void func3 (void) {…
user1128265
  • 2,891
  • 10
  • 29
  • 34
7
votes
1 answer

How can I dynamically hint a branch target to an x64 CPU?

I'd like to know how to write efficient jump tables for x64 processors, either in C, C++ or assembly. The input is known in advance, but impossible to predict algorithmically. Assuming I can look as far ahead as I want in the input stream, is…
Nathan Kurz
  • 1,649
  • 1
  • 14
  • 28
6
votes
2 answers

Get a label address out of the function scope in gcc/clang (C++)

I'm making some kind of interpreter and I'm computing a static const jump table thanks to local label addresses. You know the drill, static const int JUMP_TABLE[] = { &&case0 - &&case0, &&case1 - &&case0 and so on. For various reasons, mostly…
Tramboi
  • 151
  • 5
6
votes
9 answers

Is there anything like branch/jump table in Java?

Does Java have something similar to a branch or jump table? A branch or jump table table is, according to wikipedia, a term used to describe an efficient method of transferring program control (branching) to another part of a program (or a…
jbu
  • 15,831
  • 29
  • 82
  • 105
6
votes
2 answers

Switch-Case: declaration-with-initialization & declaration-and-then-assignment

In the switch-case statements declaration-with-initialization is invalid but declaration-and-then-assignment is allowed. As shown in the following code snippet. What is difference between these two type of initializations from the compiler side?…
Manjeet Dahiya
  • 485
  • 1
  • 4
  • 15
5
votes
1 answer

Convert x86 Assembly Jump Table to C

I have this x86 assembly code and I'm trying to convert it to C: .GLOBAL calculate calculate: pushl %ebp movl %esp,%ebp movl 12(%ebp),%eax movl 8(%ebp),%ecx cmpl $2,%ecx ja done jmp *operations(,%ecx,4) operation1: …
Benck
  • 515
  • 1
  • 5
  • 17
5
votes
1 answer

Switch statement without jump table

Is it possible to use a switch statement without a jump table? GCC creates stupid (and in my case unusable) jump tables which I want to avoid.
kaetzacoatl
  • 1,419
  • 1
  • 19
  • 27
4
votes
4 answers

"Local" labels in C and jump table implementation

I'm trying to make a macro-based jump table in C. Here's some example code: #include "stdio.h" #define GOTO(X) static void* caseArg[] = {&&I0, &&R0, &&S0, &&F0, &&G0, &&H0}; \ goto *caseArg[X]; #define FINISH() goto caseEnd; int main(int…
Dr.Kameleon
  • 22,532
  • 20
  • 115
  • 223
4
votes
0 answers

Why JavaScript doesn't implement jump table for its switch statement?

Hello I've read this in a book, The fact that the case expressions are evaluated at run-time makes the JavaScript switch statement much different from (and less efficient than) the switch statement of C, C++, and Java. In those languages, the …
4
votes
1 answer

Strategies to develop STM32 in application programming

I'm looking for ideas about how to develop a In Application Programming module. We've developed in "C" a STM32F2xx connection box for our android system. In essence this is only an multi-device to bluetooth information bridge. Now I'm thinking in…
rpr
  • 141
  • 2
  • 7
4
votes
4 answers

Implementing a Jump Table in Java

How do I make the switch/case statement in this simple Calculator program into a jump table. import java.lang.*; import java.util.*; public class Calculator { private int solution; private static int x, y, ops; private char operators; …
Richie Miz
  • 49
  • 7
1
2 3 4 5