Recent

Author Topic: Contributions to improved Math unit  (Read 1961 times)

CuriousKit

  • Jr. Member
  • **
  • Posts: 76
Contributions to improved Math unit
« on: February 26, 2015, 07:20:03 pm »
Hello Free Pascal

Though I've only recently registered, 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.  Long story short, 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:

Code: [Select]
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;

I would also like to suggest including an IncMod function (which effectively does "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).

What would be the best way to go about submitting these contributions, whether as is or for future improvement? (I'm still getting used to operand reordering and predictive branching, for example)
« Last Edit: February 26, 2015, 07:33:49 pm by CuriousKit »

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 7625
Re: Contributions to improved Math unit
« Reply #1 on: February 26, 2015, 07:40:11 pm »
Development is best discussed on the fpc-devel maillist, individual improvements with proper motivation/tests can be submitted via the bugtracker at bugs.freepascal.org

It is better to make multiple tickets with (universal diff) patches for individual routines, rather than  post a blob of modified source.

CuriousKit

  • Jr. Member
  • **
  • Posts: 76
Re: Contributions to improved Math unit
« Reply #2 on: February 26, 2015, 07:42:23 pm »
Okay, I'll start investigating.  Thanks for the prompt response.