Standard way to supply hints to branches

Richard (Rikki) Andrew Cattermole richard at cattermole.co.nz
Sat Aug 24 06:15:30 UTC 2024


On 24/08/2024 5:48 PM, Dukc wrote:
> Walter Bright kirjoitti 24.8.2024 klo 6.37:
>> It's not pedantically correct. More precisely, any code following the 
>> `if` is presumed to be the hot path. A branch instruction doesn't 
>> count, i.e. it's the branch-not-taken that is considered the hot path.
> Can you elaborate? I'm not sure what's the difference between `if` and a 
> branch instruction.

In pseudo assembly it would look something like this:

```
test condition;
jump-if-zero ColdPath;

HotPath:
	...
	goto End;

ColdPath:
	...

End:
	...
```


In pseudo code:

```
if (condition == 0)
	goto ColdPath;

HotPath:
{
}
goto End;

ColdPath:
{
}

End:
{
}
```

Converted to an if statement:

```d
if (condition) {
	// cold path
} else {
	// hot path
}

// hot path at end
``

In essence in the CPU, any instruction that jumps around conditionally 
can be assumed to not be taken. Note: this is very CPU dependent and in 
the last 15 years has gotten a LOT smarter.


More information about the Digitalmars-d mailing list