BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Pascal to BASM

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Language BASM
View previous topic :: View next topic  
Author Message
Alexandru Preda
Guest





PostPosted: Sun Feb 20, 2005 5:35 am    Post subject: Pascal to BASM Reply with quote



Hello,

I was wondering if anyone can help me converting the following function:

function LinearInterpolator(PWX, PWY: byte; C11, C21: PByte): byte;

function CombineReg(X, Y, W: byte): byte;
begin
if W = 0 then
Result := Y
else if W = 255 then
Result := X
else Result := round(X * W/255 + (1 - W/255) * Y);
end;

var C1, C3: byte;
begin
if PWX > $FF then
PWX := $FF;
if PWY > $FF then
PWY := $FF;

C1 := C11^;
Inc(C11);
C3 := C21^;
Inc(C21);

Result := CombineReg(CombineReg(C1, C11^, PWX), CombineReg(C3, C21^,
PWX), PWY);
end;



Thank you in advance.
Alex


Back to top
Pierre le Riche
Guest





PostPosted: Sun Feb 20, 2005 6:19 am    Post subject: Re: Pascal to BASM Reply with quote



Hi Alexandru,

Quote:
if PWX > $FF then
PWX := $FF;
if PWY > $FF then
PWY := $FF;

The above code does nothing - it always evaluates to false.

You can improve the subroutine as follows:

function CombineReg(X, Y, W: Cardinal): byte;
asm
{On entry:
eax = X,
edx = Y,
ecx = W}
{X = X * W}
imul eax, ecx
{W = 255 - W}
not cl
{Y = Y * (255 - W)}
imul edx, ecx
{Get the sum of the terms, and add a rounding factor (to round to
nearest whole number)}
lea eax, [eax + edx + 127]
{Multiply with 2^32/255}
mov edx, 16843009
mul edx
{Get the result in eax}
mov eax, edx
end;

Regards,
Pierre



Back to top
Alexandru Preda
Guest





PostPosted: Sun Feb 20, 2005 6:45 am    Post subject: Re: Pascal to BASM Reply with quote



Thank you very much, Pierre, it's just perfect!

Alex.


"Pierre le Riche" <pleriche (AT) hotmail (DOT) com> wrote

Quote:
Hi Alexandru,

if PWX > $FF then
PWX := $FF;
if PWY > $FF then
PWY := $FF;

The above code does nothing - it always evaluates to false.

You can improve the subroutine as follows:

function CombineReg(X, Y, W: Cardinal): byte;
asm
{On entry:
eax = X,
edx = Y,
ecx = W}
{X = X * W}
imul eax, ecx
{W = 255 - W}
not cl
{Y = Y * (255 - W)}
imul edx, ecx
{Get the sum of the terms, and add a rounding factor (to round to
nearest whole number)}
lea eax, [eax + edx + 127]
{Multiply with 2^32/255}
mov edx, 16843009
mul edx
{Get the result in eax}
mov eax, edx
end;

Regards,
Pierre





Back to top
Lars
Guest





PostPosted: Sun Feb 20, 2005 7:00 am    Post subject: Re: Pascal to BASM Reply with quote

Alexandru Preda wrote:
Quote:
Hello,

I was wondering if anyone can help me converting the following function:

First why not speed up the Pascal ?

Note not tested !

Quote:
function LinearInterpolator(PWX, PWY: byte; C11, C21: PByte): byte;

function CombineReg(X, Y, W: byte): byte;
begin
if W = 0 then
Result := Y
else if W = 255 then
Result := X


Quote:
else Result := round(X * W/255 + (1 - W/255) * Y);

else Result := ( (X * W) div 255 + ((1 - W) div 255) * Y);


Quote:
end;

var C1, C3: byte;
begin

Why test for if a byte > 255 ?

Quote:
if PWX > $FF then
PWX := $FF;
if PWY > $FF then
PWY := $FF;

// if PWX > $FF then
// PWX := $FF;
// if PWY > $FF then
// PWY := $FF;


Quote:

C1 := C11^;
Inc(C11);
C3 := C21^;
Inc(C21);

Result := CombineReg(CombineReg(C1, C11^, PWX), CombineReg(C3, C21^,
PWX), PWY);
end;



Lars G

Back to top
Alexandru Preda
Guest





PostPosted: Sun Feb 20, 2005 12:55 pm    Post subject: Re: Pascal to BASM Reply with quote

Hello,

That group of lines was left there by mistake, it isn't there anymore,
however, with no performance changes (I would believe the Delphi optimizer
removed it anyway).

The idea is that the code was originally developed for 32-bit graphics.
There is a MMX version as well, and I thought converting it to a "byte"
version (I only need 256 grayscale values) would speed the things a little.

Alex



"Lars" <lbg_ (AT) _adslhome (DOT) dk> wrote

Quote:
Alexandru Preda wrote:
Hello,

I was wondering if anyone can help me converting the following function:

First why not speed up the Pascal ?

Note not tested !

function LinearInterpolator(PWX, PWY: byte; C11, C21: PByte): byte;

function CombineReg(X, Y, W: byte): byte;
begin
if W = 0 then
Result := Y
else if W = 255 then
Result := X


else Result := round(X * W/255 + (1 - W/255) * Y);

else Result := ( (X * W) div 255 + ((1 - W) div 255) * Y);


end;

var C1, C3: byte;
begin

Why test for if a byte > 255 ?

if PWX > $FF then
PWX := $FF;
if PWY > $FF then
PWY := $FF;

// if PWX > $FF then
// PWX := $FF;
// if PWY > $FF then
// PWY := $FF;



C1 := C11^;
Inc(C11);
C3 := C21^;
Inc(C21);

Result := CombineReg(CombineReg(C1, C11^, PWX), CombineReg(C3, C21^,
PWX), PWY);
end;



Lars G



Back to top
Lord Crc
Guest





PostPosted: Sun Feb 20, 2005 2:35 pm    Post subject: Re: Pascal to BASM Reply with quote

On Sun, 20 Feb 2005 07:35:37 +0200, "Alexandru Preda"
<alexandru.preda (AT) adpharma (DOT) ro> wrote:

Quote:
else Result := round(X * W/255 + (1 - W/255) * Y);

I find this version not only faster but more intuative to understand:

result:= round( y * + (x - y) * (w / 255) );

or rewritten using integers

result:= (y shl 8 + (x - y) * w) shr 8;

- Asbjørn

Back to top
Alexandru Preda
Guest





PostPosted: Sun Feb 20, 2005 5:49 pm    Post subject: Re: Pascal to BASM Reply with quote

Good point.

Thank you

"Lord Crc" <lordcrc (AT) hotmail (DOT) com> wrote

Quote:
On Sun, 20 Feb 2005 07:35:37 +0200, "Alexandru Preda"
[email]alexandru.preda (AT) adpharma (DOT) ro[/email]> wrote:

else Result := round(X * W/255 + (1 - W/255) * Y);

I find this version not only faster but more intuative to understand:

result:= round( y * + (x - y) * (w / 255) );

or rewritten using integers

result:= (y shl 8 + (x - y) * w) shr 8;

- Asbjørn



Back to top
Pierre le Riche
Guest





PostPosted: Sun Feb 20, 2005 8:25 pm    Post subject: Re: Pascal to BASM Reply with quote

Quote:
result:= round( y * + (x - y) * (w / 255) );

or rewritten using integers

result:= (y shl 8 + (x - y) * w) shr 8;

Those two aren't equivalent: shr 8 divides by 256 not 255.

Pierre



Back to top
Pierre le Riche
Guest





PostPosted: Sun Feb 20, 2005 8:26 pm    Post subject: Re: Pascal to BASM Reply with quote

Quote:
Thank you very much, Pierre, it's just perfect!

You're welcome Smile.



Back to top
Lord Crc
Guest





PostPosted: Mon Feb 21, 2005 2:11 am    Post subject: Re: Pascal to BASM Reply with quote

On Sun, 20 Feb 2005 22:25:57 +0200, "Pierre le Riche"
<pleriche (AT) hotmail (DOT) com> wrote:

Quote:
Those two aren't equivalent: shr 8 divides by 256 not 255.

You're correct of course, i wrote the post just before running to the
bus :)

In many graphics applications this inaccuracy is acceptable. If it's
not, i'd explore look up tables or enabling the input to use the full
range 0-256.

- Asbjørn

Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Language BASM All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.