[fpc-devel] New subscriber with some suggestions

J. Gareth Moreton gareth at moreton-family.com
Thu Feb 26 20:43:29 CET 2015


 Hello Free Pascal

 I've been programming in Object Pascal for over 15 years, and over the
past few years have started picking up Intel x86 assembly language.  I am
very interested in learning more about the development of Free Pascal and
possibly contributing some optimisations or additional features to the
units.

 I'd like to contribute possible improvements and optimisations to the Math
unit in particular.  For example, an assembler implementation of DivMod
(using the standard register parameter convention and {$ASMMODE Intel}) -
apologies if I got the code slightly wrong... I had to port it from another
function where the arrangement of the parameters were different in that the
Result parameter was actually a function result (and hence could be written
to EAX), while Remainder was referenced in ECX rather than on the stack:

procedure DivMod(Dividend: DWord; Divisor: DWord; var Result, Remainder: DWord); assembler;
asm
{$IFDEF CPU64}
  MOV R10, RDX
  MOV RAX, RCX
  XOR RDX, RDX   { Zero RDX because the DIV operation uses RDX:RAX as the numerator }
  DIV R10        { After this call, quotient is in RAX and remainder is in RDX }
  MOV [R8], RAX
  MOV [R9], RDX
{$ENDIF}
{$IFDEF CPU32}
  PUSH EBX
  MOV EBX, EDX
  XOR EDX, EDX   { Zero EDX because the DIV operation uses EDX:EAX as the numerator }
  DIV EBX        { After this call, quotient is in EAX and remainder is in EDX }
  MOV EBX, [ESP+4] { Since we pushed EBX onto the stack, the address to "Remainder" is one step back }
  MOV [ECX], EAX
  MOV [EBX], EDX
  POP EBX
{$ENDIF}
end;

 It was mentioned in the comments of DivMod that a compiler directive could
be used (e.g. FPC_MATH_HAS_CPUDIVMOD) to run assembler versions of these
functions, so the above would be a first preliminary suggestion (Intel
x86/x64 assember definitely has it, with DIV defined to return the quotient
in EAX and the remainder in EDX).

 I would also like to suggest including an IncMod function (which
effectively calculates "Value := (Value + 1) mod Divisor"), which has uses
in certain mathematical and cryptographic fields and could be written very
efficiently in assembly language (e.g. ADD EAX, 1; CMP EAX, EDX ... and
then setting EAX to zero if the zero flag is set).  In a similar vein, an
ISqrt function might have some uses as well (or just an overloaded version
of sqrt that takes an unsigned integer parameter and result), and there are
algorithms that can calculate the integer portion of the answer far, far
faster than a floating-point square root.  Would these be viable additions
and improvements?

 Apologies if this isn't the best place to be submitting suggestions - I'm
still learning the system.

 Yours faithfully,

 J. Gareth Moreton aka. Curious Kit
 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20150226/18027cc6/attachment.html>


More information about the fpc-devel mailing list