Forum > General
Avoiding conditional jumps platform independent
lagprogramming:
Is "ord(comparison)" returning 0 for false and 1 for true, no matter the target of the build?
As example, function Eof, which can be found in rtl/inc/text.inc, contains:
--- Code: --- if TextRec(t).mode=fmOutput then
InOutRes:=104
else
InOutRes:=103;
--- End code ---
Can the above code be safely changed to "InOutRes:=103+ord(TextRec(t).mode=fmOutput);"?
I'm thinking that who knows, maybe there are target CPUs where "ord(boolean_evaluation)" pascal code might return a value for true different than 1. I'm pretty sure false would return 0. I mention that "boolean_evaluation" is indeed a comparison, not a boolean function result, variable or something like that.
marcov:
Probably yes, but "if TextRec(t).mode=fmOutput" as compare node might generate different code then ord(TextRec(t).mode=fmOutput) (e.g. using flags instead having values 0 and 1 in registers)
Probably the best way is to do this optimizer level to transform such things in a target dependent manner.
Laksen:
marcov is right. The compiler should know best :)
For example on ARM, this:
--- Code: ---function test1(a: longint): longint;
begin
if a = 123 then
test1:=103
else
test1:=104;
end;
function test2(a: longint): longint;
begin
test2:=a+ord(a=123);
end;
--- End code ---
Becomes:
--- Code: ---SYSTEM_$$_TEST1$LONGINT$$LONGINT:
cmp r0,#123
moveq r0,#103
movne r0,#104
bx r14
SYSTEM_$$_TEST2$LONGINT$$LONGINT:
cmp r0,#123
moveq r1,#1
movne r1,#0
add r0,r0,r1
bx r14
--- End code ---
lagprogramming:
@Laksen
What assembly code is produced for arm regarding the following two functions?
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function function1(var parameter:integer):boolean;beginif parameter<>0 then begin result:=true; parameter:=0; end else result:=false;end; function function2(var parameter:integer):boolean;beginresult:=parameter<>0;if result then parameter:=0;end;
Thank you
Thaddy:
simply compile with the -a option will give you the assembler output as <yourmodule>.s
for example: ppcrossarm -Tlinux -Mdelphi -O4 -CpARMV7A -s armasm.dpr (contains your functions) gives you:
--- Code: ---# Begin asmlist al_procedures
.section .text.n_p$armasm_$$_function1$longint$$boolean
.balign 4
.globl P$ARMASM_$$_FUNCTION1$LONGINT$$BOOLEAN
.type P$ARMASM_$$_FUNCTION1$LONGINT$$BOOLEAN,#function
P$ARMASM_$$_FUNCTION1$LONGINT$$BOOLEAN:
mov r1,r0
ldr r0,[r1]
cmp r0,#0
movne r0,#1
movne r2,#0
strne r2,[r1]
moveq r0,#0
bx r14
.Le0:
.size P$ARMASM_$$_FUNCTION1$LONGINT$$BOOLEAN, .Le0 - P$ARMASM_$$_FUNCTION1$LONGINT$$BOOLEAN
.section .text.n_p$armasm_$$_function2$longint$$boolean
.balign 4
.globl P$ARMASM_$$_FUNCTION2$LONGINT$$BOOLEAN
.type P$ARMASM_$$_FUNCTION2$LONGINT$$BOOLEAN,#function
P$ARMASM_$$_FUNCTION2$LONGINT$$BOOLEAN:
mov r1,r0
ldr r0,[r1]
cmp r0,#0
movne r0,#1
# Peephole CmpMovMov - Removed redundant moveq
cmp r0,#0
movne r2,#0
strne r2,[r1]
bx r14
.Le1:
.size P$ARMASM_$$_FUNCTION2$LONGINT$$BOOLEAN, .Le1 - P$ARMASM_$$_FUNCTION2$LONGINT$$BOOLEAN
--- End code ---
As you can see: no branching at all.
Navigation
[0] Message Index
[#] Next page