flat assembler
Message board for the users of flat assembler.

Index > Compiler Internals > [requests] Branching Hint Prefix

Author
Thread Post new topic Reply to topic
chris



Joined: 05 Jan 2006
Posts: 62
Location: China->US->China->?
chris
Hi, Tomasz, from the intel's manual(Vol. 2A), I found that there are two so-called "branching hint prefix" that could be used with all conditional jumps, namely 2Eh and 3Eh(I didn't find the mnemonics for them), that can give CPU a hint of whether the branching is likely to take or not, hence an optimization of instruction prefetching. It seems that fasm doesn't support these prefixes, and I have to use macros to implement them. Would it be nicer if fasm support them natively? Razz

example usage of 2Eh and 3Eh:
Code:
macro brn jcc*,operand*
{
  db 3eh       ; jcc prefix - likely to branch
  jcc operand
}

macro nbr jcc*,operand*
{
  db 2eh        ; jcc prefix - not likely to branch
  jcc operand
}

; -------------------------------------------------------
...
cinvoke __getmainargs,argc,argv,envp,0,sinf
cmp dword[argc],1
nbr jz,print_usage ; <-- not likely to take this branch
...

;--------------------------------------------------------
...
cmp     [wmsg],WM_COMMAND
brn   je,wmcommand ; <-- likely to take this branch
cmp     [wmsg],WM_CREATE
je      wmcreate
cmp     [wmsg],WM_DESTROY
je      wmdestroy
...
    
Post 16 Apr 2006, 12:57
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
http://www.sandpile.org/ia32/opc_1.htm

CS:

Hint Not Taken
for Jcc (P4+)

DS:

Hint Taken
for Jcc (P4+)
Post 16 Apr 2006, 15:24
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 7752
Location: Kraków, Poland
Tomasz Grysztar
Post 18 Apr 2006, 18:50
View user's profile Send private message Visit poster's website Reply with quote
chris



Joined: 05 Jan 2006
Posts: 62
Location: China->US->China->?
chris
Thanks, locodelassembly and Tomasz. It seems using cs and ds is enought here. Razz
Code:
; ------------------------------------------------------- 
... 
cinvoke __getmainargs,argc,argv,envp,0,sinf 
cmp dword[argc],1 
cs
jz,print_usage ; <-- not likely to take this branch 
... 

;-------------------------------------------------------- 
... 
cmp     [wmsg],WM_COMMAND 
ds
je      wmcommand ; <-- likely to take this branch 
cmp     [wmsg],WM_CREATE 
je      wmcreate 
cmp     [wmsg],WM_DESTROY 
je      wmdestroy 
...
    
Post 02 May 2006, 07:09
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 7752
Location: Kraków, Poland
Tomasz Grysztar
I have just found out in the Agner Fog's Optimization Manual, that FS prefix also has an effect on branches:

Anger Fog wrote:
[...]I discovered an undocumented prefix, 64H, which does the trick on the
P4. This prefix doesn't change the static prediction, but it controls the state of the local
predictor after the first event so that it will toggle between state "weakly not taken" and
"weakly taken", regardless of whether the branch is taken or not taken the first time.This
trick can be summarized in the following rule:
A branch which is taken exactly every second time, and which doesn't correlate with any of
the preceding 16 branch events, can be predicted well on the P4 if it is preceded by a 64H
prefix. This prefix is coded in the following way:
Code:
DB 64H ; hint prefix for alternating branch
JNZ X1 ; branch instruction    
No prefix is needed if the branch can see a previous instance of itself in the 16-bit
prehistory.

Of course with fasm you can write it just as:
Code:
FS JNZ X1    
Post 08 Jun 2006, 09:32
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on YouTube, Twitter.

Website powered by rwasa.