Page 3 of 3 FirstFirst 123
Results 21 to 25 of 25

Thread: C to Pascal conversion hq2x

  1. #21

    C to Pascal conversion hq2x

    Using SHR directly in Delphi and FPC does not gives the appropriate results while converting from C to Pascal.



    The >> in C while using integers is interptreted as sar in dissassemply!
    The >> in C while using longwords is interptreted as shl in dissassemply!


    Try implement the followng functions:


    Code:
    // Left shift between integers
    function _SHL(const x: integer; const bits: integer): integer; assembler;
    asm
    // mov eax, x
    // mov ecx, bits
    mov ecx, edx
    sal eax, cl
    end;
    
    // Left shift between longwords
    function _SHLW(const x: LongWord; const bits: LongWord): LongWord;
    begin
    result := x shl bits;
    end;
    
    // Right shift between integers
    function _SHR(const x: integer; const bits: integer): integer; assembler;
    asm
    mov ecx, edx
    sar eax, cl
    end;
    {begin
    result := x div (1 shl bits);
    end;}
    
    // Right shift between longwords
    function _SHRW(const x: LongWord; const bits: LongWord): LongWord;
    begin
    result := x shr bits;
    end;
    Last edited by Jimmy Valavanis; 30-03-2012 at 04:10 PM.

  2. #22

    C to Pascal conversion hq2x

    Jim, how well is this going to compile on different platforms though? The idea of keeping this in FPC is that you can build it for Mac, Linux, Windows, or any other platform that FPC supports. I know that staying at the top of the assembly tree (SHL, SHR, MOV, CPY, etc...) is fairly safe between Windows and Linux but haven't tested the theory on Mac .

  3. #23

    C to Pascal conversion hq2x

    the code posted will compile only on x86 or x86-64 cpu architectures...

    basicly the problem is that shr operator with signed arguments generates an unsigned shift which does not preserve the sign of the number....

  4. #24

    C to Pascal conversion hq2x

    the code posted will compile only on x86 or x86-64 cpu architectures...

    The following code will compile also on non x86 machines:

    Code:
    // Left shift between integers
    function _SHL(const x: integer; const bits: integer): integer;
    begin
    result := x * (1 shl bits);
    end;
    
    // Left shift between longwords
    function _SHLW(const x: LongWord; const bits: LongWord): LongWord;
    begin
    result := x shl bits;
    end;
    
    // Right shift between integers
    function _SHR(const x: integer; const bits: integer): integer;
    begin
    result := x div (1 shl bits);
    end;
    
    // Right shift between longwords
    function _SHRW(const x: LongWord; const bits: LongWord): LongWord;
    begin
    result := x shr bits;
    end;
    Last edited by Jimmy Valavanis; 30-03-2012 at 04:10 PM.

  5. #25

    C to Pascal conversion hq2x

    first - there is no such thing as a signed shift left so the function _SHL is completely redundant..._SHLU and _SHRU are just wrappers around the normal operators...._SHR does arithmetic shift by division and that is awfully slow, eliminating any advantage of performing a shift...you can as well as use div in the source code, it will even do a better because the division value can be a constant, no need to do "1 shl bits" to get the divisor...

    I just found out that at least Delphi (haven't tested FPC) emits SAR when you use the div operator with a power of 2 divisor...doesn't solve the case when the number of bits to shift should vary....

Page 3 of 3 FirstFirst 123

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •