Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2012-03

AGG missing 3 asm conversions to work on 64-bit CPU's

10 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Graeme Geldenhuys

Hi,

Can anybody with Assembly knowledge help convert 3 methods in AggPas (<lcl>/components/aggpas/src) so that it could work under x86_64 systems?

The three methods are shr_int6(), shr_int16() and shr_int32() defined inside the agg_basics.pas unit.

For example, here is a one method that is often called, but because there is no 64-bit x86_64 implementation, the agg output is not correct (compared to the 32-bit version of my program).

function shr_int32;
begin
{$IFDEF AGG_CPU_386 }
 asm
  mov eax ,dword ptr [i ]
  mov ecx ,dword ptr [shift ]
  sar eax ,cl
  mov dword ptr [result ] ,eax
 end;
{$ENDIF }

{$IFDEF AGG_CPU_PPC }
asm
lwz r3,i
lwz r2,shift
sraw r3,r3,r2
stw r3,result
end;
{$ENDIF }

end;

I know FPC has built-in 'shr' functionality, but it seems Agg needs a specific implementation to be compatible with the C compilers. As noted by the code comment in the agg_basic.pas unit.

// SHR for signed integers is differently implemented in pascal compilers
// than in c++ compilers. On the assembler level, c++ is using the SAR and
// pascal is using SHR. That gives completely different result, when the
// number is negative. We have to be compatible with c++ implementation,
// thus instead of directly using SHR we emulate c++ solution.

Any help would be very much appreciated, thanks.

Regards, ? - Graeme - _______________________________________________ fpGUI - a cross-platform Free Pascal GUI toolkit http://fpgui.sourceforge.net

Reply by email

Graeme Geldenhuys
Florian Klaempfl wrote
FPC has also arithmetic right shifting intrinsics for a couple of versions: Sar* (*=ShortInt,SmallInt,Longint,Int64), use those.

Awesome, now it works! Thanks Florian.

Regards, ? - Graeme - _______________________________________________ fpGUI - a cross-platform Free Pascal GUI toolkit http://fpgui.sourceforge.net

Reply by email

Hans-Peter Diettrich
to Graeme Geldenhuys: Can anybody with Assembly knowledge help convert 3 methods in AggPas…
For example, here is a one method that is often called, but because there is no 64-bit x86_64 implementation, the agg output is not correct (compared to the 32-bit version of my program).

IMO the Delphi implementation is already broken, because it doesn't respect the signedness of the argument, so that the description "SHR n is equivalent to DIV 2^n"
doesn't hold.

Show quoted text 5 lines
// SHR for signed integers is differently implemented in pascal compilers // than in c++ compilers. On the assembler level, c++ is using the SAR and // pascal is using SHR. That gives completely different result, when the // number is negative. We have to be compatible with c++ implementation, // thus instead of directly using SHR we emulate c++ solution.

IMO this should read
"SHR is broken for signed integers in *Delphi*, so we need a correct additional implementation..."

DoDi

Reply by email

Graeme Geldenhuys
to Hans-Peter Diettrich
Hans-Peter Diettrich wrote
IMO this should read "SHR is broken for signed integers in *Delphi*, so we need a correct additional implementation..."

In my copy of AggPas, I completely removed that comment and replaced all three shr_xxx implementations with FPC's Sar*() calls like Florian suggested. 90% (guesstimate) of 64-bit AggPas seems to be working identical now to the 32-bit version. There are still some minor color shifting that occurs on certain graphics functions, and font support needs a bit of work. Other than that, AggPas works very nicely now.

Regards, ? - Graeme - _______________________________________________ fpGUI - a cross-platform Free Pascal GUI toolkit http://fpgui.sourceforge.net

Reply by email

Adrian Veith
to Graeme Geldenhuys: Can anybody with Assembly knowledge help convert 3 methods in AggPas…

I use this for cross plattform:

function  shr_int32(i ,shift : longint ) : longint; inline;
begin
   Result:= i div (1 shl shift);
end;
Graeme Geldenhuys wrote 53 lines
Hi, Can anybody with Assembly knowledge help convert 3 methods in AggPas (<lcl>/components/aggpas/src) so that it could work under x86_64 systems? The three methods are shr_int6(), shr_int16() and shr_int32() defined inside the agg_basics.pas unit. For example, here is a one method that is often called, but because there is no 64-bit x86_64 implementation, the agg output is not correct (compared to the 32-bit version of my program). function shr_int32; begin {$IFDEF AGG_CPU_386 } asm mov eax ,dword ptr [i ] mov ecx ,dword ptr [shift ] sar eax ,cl mov dword ptr [result ] ,eax end; {$ENDIF } {$IFDEF AGG_CPU_PPC } asm lwz r3,i lwz r2,shift sraw r3,r3,r2 stw r3,result end; {$ENDIF } end; I know FPC has built-in 'shr' functionality, but it seems Agg needs a specific implementation to be compatible with the C compilers. As noted by the code comment in the agg_basic.pas unit. // SHR for signed integers is differently implemented in pascal compilers // than in c++ compilers. On the assembler level, c++ is using the SAR and // pascal is using SHR. That gives completely different result, when the // number is negative. We have to be compatible with c++ implementation, // thus instead of directly using SHR we emulate c++ solution. Any help would be very much appreciated, thanks.

Reply by email

Mattias Gaertner
to Graeme Geldenhuys: In my copy of AggPas, I completely removed that comment and replaced all three shr_xxx…
Show quoted text 13 lines
On 9 March 2012 12:51, Hans-Peter Diettrich wrote: > > IMO this should read > "SHR is broken for signed integers in *Delphi*, so we need a correct > additional implementation..." In my copy of AggPas, I completely removed that comment and replaced all three shr_xxx implementations with FPC's Sar*() calls like Florian suggested. 90% (guesstimate) of 64-bit AggPas seems to be working identical now to the 32-bit version. There are still some minor color shifting that occurs on certain graphics functions, and font support needs a bit of work. Other than that, AggPas works very nicely now.

Should these changes be applied to the lazarus aggpas?

Mattias

Reply by email

Graeme Geldenhuys
to Mattias Gaertner
Mattias Gaertner wrote
Should these changes be applied to the lazarus aggpas?

Yes. Attached is the patch for this thread's mentioned problem - no x86_64 bit shifting implementation.

I'm working further on AggPas and fixed some other things too. I'll supply a few more patches as I go.

Regards, ? - Graeme - _______________________________________________ fpGUI - a cross-platform Free Pascal GUI toolkit http://fpgui.sourceforge.net -------------- next part -------------- A non-text attachment was scrubbed... Name: 0001-Removed-ASM-code-from-agg_basic.pas-and-replaced-it-.patch Type: text/x-patch Size: 2894 bytes Desc: not available URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20120309/3433e6f0/attachment.bin>

Reply by email

Mattias Gaertner
to Graeme Geldenhuys
Show quoted text 7 lines
On 9 March 2012 16:12, Mattias Gaertner wrote: > > Should these changes be applied to the lazarus aggpas? Yes. Attached is the patch for this thread's mentioned problem - no x86_64 bit shifting implementation.

Thanks.
Applied.

I'm working further on AggPas and fixed some other things too. I'll supply a few more patches as I go.

Mattias

Reply by email

Graeme Geldenhuys
to Florian Klaempfl
Florian Klaempfl wrote
Making the helpers inline functions is probably a good idea.

True, that could be an intermediate step, until all the shr_xxx() calls [there aren't that many] are replaced with SarXXX() calls, and the shr_xx() can be deleted completely.

Regards, ? - Graeme - _______________________________________________ fpGUI - a cross-platform Free Pascal GUI toolkit http://fpgui.sourceforge.net

Reply by email