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.

55 questions
253
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
  • 189,354
  • 112
  • 476
  • 821
22
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
  • 466,055
  • 116
  • 851
  • 1,175
9
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
  • 430
  • 3
  • 13
7
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,511
  • 10
  • 26
  • 32
6
votes
1 answer

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
  • 14,925
  • 29
  • 80
  • 100
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
  • 356
  • 1
  • 3
  • 13
6
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,559
  • 1
  • 11
  • 26
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
  • 405
  • 1
  • 4
  • 14
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
  • 21,495
  • 19
  • 103
  • 208
4
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,329
  • 14
  • 26
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
3
votes
2 answers

creating constant jump table; xcode; clang; asm

I have quite strange issue when try to create the jump table in my asm program for iphone (arm64): .globl my_func my_func: ... //jump (switch) table .L.f_switch: .short .L.case0 - .L.f_switch .short .L.case1 - .L.f_switch …
user3124812
  • 1,279
  • 1
  • 12
  • 27
3
votes
4 answers

Function array in Java?

Maybe I think to much in C, but I don't see a solution how to solve this properly in java. I get a response from my server which sends a string like this: command params The client receives that string and extracts the command. Now I would…
Devolus
  • 20,356
  • 11
  • 56
  • 104
1
2 3 4