Jump to content
  • Advertisement
Sign in to follow this  
JoshM

No Branching Zone

This topic is 4845 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've really been wondering lately - imagine you are writing a game with C++ and you have no "if" or "?:" - or any other method of branching. What would be impossible, if anything? And what would be slower? I suspect some things will become faster and easier to understand... EDIT: Loops are allowed as long as you don't cheat and use it as an if, like: for( ; testCondition ; ) { if-branch } for( ; !testCondition ; ) {else-branch) EDIT part 2: Ok, let me simplify this - what if you can't use "if", "?:", "case", or any other kind of hackery that simulates those operations? EDIT part 3: My terminology was wrong - I mean "CONDITIONAL branch." (thanks doynax) From wikipedia: "A conditional branch is a basic logical structure. It resembles a fork in the road; there are at least two paths that may be taken and one must be chosen." A function call is not what I'm talking about because there is no logic to evalute in order to determine which function to call - even a virtual function is (I believe) just a lookup into the vtable, right? [Edited by - JoshM on August 11, 2005 7:59:05 PM]

Share this post


Link to post
Share on other sites
Advertisement
A game entirely without loops and (non-inline?) function calls would be kind of limited.
Basic boolean/arithmetic operators can often be used to synthesize conditional expressions though.

I suppose that it'd be theoretically possible develop anything like this for some kind of infinity machine.

Share this post


Link to post
Share on other sites
It would be impossible to do anything that requires a program to act on the outcome of a previous operation. It would be possible to implement certain things with number transformations rather than explicit branch instructions, but then you'd just be implementing branching on a more abstract level.

Quote:
EDIT: Loops are allowed as long as you don't cheat and use it as an if, like:

for( ; testCondition ; ) { if-branch }
for( ; !testCondition ; ) {else-branch)

Loops with conditions are the equivalent of unconditional loops with an if(condition): break

Share this post


Link to post
Share on other sites
Quote:
Original post by igni ferroque
It would be impossible to do anything that requires a program to act on the outcome of a previous operation. It would be possible to implement certain things with number transformations rather than explicit branch instructions, but then you'd just be implementing branching on a more abstract level.


That's not exactly true. For example, here's a branch:

enum State{...} state;
switch( state )
{
case STATE_A: ProcessStateA(); break;
case STATE_B: ProcessStateB(); break;
etc...


And here's a non-branch:

State* state;
...
state->Process();

I wouldn't say that the second example is "a more abstract branch." It's a different paradigm. And if Process is a virtual function, then what happens when you call state->Process() depends entirely on the concrete class that state points to and thus depends on the result of a previous operation (ostensibly, assignment).

Share this post


Link to post
Share on other sites
Quote:
Original post by JoshM
That's not exactly true. For example, here's a branch:

enum State{...} state;
switch( state )
{
case STATE_A: ProcessStateA(); break;
case STATE_B: ProcessStateB(); break;
etc...


And here's a non-branch:

State* state;
...
state->Process();
I'd call them both a branches (including any kind of jump). It's just that the first is traditionally called a conditional branch.

Share this post


Link to post
Share on other sites
Quote:
Original post by JoshM
Updated original question to reflect clarifications based on feedback so far :)
Hm.. I'm not so sure that you should listen to my definition of branches in this case.
Logically a straight jump isn't really a branch (as in a fork in the program flow), it's just that most CPU architectures tends to refer to both as branches anyway.

Quote:
Original post by JoshM
A function call is not what I'm talking about because there is no logic to evalute in order to determine which function to call - even a virtual function is (I believe) just a lookup into the vtable, right?
Yeah, I believe that you're technically correct ('the best kind of correct').
The true branch (if any) in this case isn't the invocation itself but rather the assignment of the function pointer.

Share this post


Link to post
Share on other sites
You could use arrays of function pointers:

typedef int (*func)(int);

func[] fac_a = {fac, fac_0};

int fac(int n) {
return fac_a[n==1](n-1) * n;
}

int fac_0(int n) {
return 1;
}

Share this post


Link to post
Share on other sites
Quote:
imagine you are writing a game with C++ and you have no "if" or "?:" - or any other method of branching.

Of course I missed the most obvious answer. The word "game" generally implies success and failure conditions.

Of course, this game (hehe) is pointless without a more formal definition of what counts as a branch. Pretty much any computation boils down to conditional branches on some level.

Share this post


Link to post
Share on other sites
Quote:
Of course I missed the most obvious answer. The word "game" generally implies success and failure conditions.


Hmmm, I don't think about it that way - to me game implies only "conditions". Not necessarily only success and failure. There may be degrees of success and failure. Either way, let's say I have a guessing game where you have to guess my number. I could do it like this:


const char* responseCorrect = "Correct!";
const char* responseWrong = "Wrong!";
int i;
const char* responseTable[10];
for( i = 0; i < 10; ++i ) { responseTable = responseWrong; }
responseTable[ randomNumberFrom0To9 ] = responseCorrect;
int guessedNumber = getGuessedNumber();
cout << responseTable[ guessedNumber ];


Admittedly it's a lame and contrived example, but it's possible at least :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!