[futurebasic] Signed and unsigned comparison

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : May 2007 : Group Archive : Group : All Groups

From: Robert Purves <listrp@...>
Date: Sat, 12 May 2007 22:14:10 +1200
Comparisons (>, >=, <, <=) of pointer variables in FB are always  
signed. Needing an unsigned '>' comparison, I found it necessary to  
use the clumsy function UnsignedGreaterThan() below, with its  
unpleasant lapse into assembler.
Can someone supply a more elegant method?

Robert P.

'-------------------
register on

// unsigned comparison: _zTrue if p1 > p2
local fn UnsignedGreaterThan( p1 as pointer, p2 as pointer )
'~'1
dim as Boolean p1Greater

p1Greater = _false
` cmpl cr0,0,^p1,^p2 ; unsigned comparison
` bgt UnsignedGreaterThan ; branch if ( p1 > p2 )
exit fn
"UnsignedGreaterThan"
p1Greater = _zTrue
end fn = p1Greater

// signed comparison: _zTrue if p1 > p2
local fn FBGreaterThan( p1 as pointer, p2 as pointer )
'~'1
end fn = (p1 > p2)


local fn Test( p1 as pointer, p2 as pointer )
'~'1
print "0x" hex$( p1 ), "0x" hex$( p2 ),
print fn FBGreaterThan( p1, p2 ), fn UnsignedGreaterThan( p1, p2 )
end fn



print "p1", "p2", "FB p1>p2 ", "unsigned p1>p2"
// signed comparison is same as unsigned
fn Test( 1, 2 )
fn Test( 2, 1 )
fn Test( 0xFFFFFFFF, 0xFFFFFFFE )
fn Test( 0xFFFFFFFE, 0xFFFFFFFF )
print
// signed comparison is different from unsigned
fn Test( 1, 0xFFFFFFFF )
fn Test( 0xFFFFFFFF, 1 )
fn Test( 0x7FFFFFFF, 0x80000000 )
fn Test( 0x80000000, 0x7FFFFFFF )

do
HandleEvents
until 0
'----------------