Bug in TColorManager.RowConvertIndexed8 in 64bit version

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #37784
    HeDiBo
    Participant

    I had a nasty bug in acPNG when running as 64 bit program.

    Delphi XE4 for WIN32 generates this code:

    Code:
    acPNG.pas.2889: MaxInSample := (1 shl SourceBPS) – 1;
    008A5F03 8A4DE7 mov cl,[ebp-$19]
    008A5F06 B001 mov al,$01
    008A5F08 D2E0 shl al,cl
    008A5F0A 48 dec eax
    008A5F0B 8845E9 mov [ebp-$17],al

    But when generating for WIN64 it generates this code:

    Code:
    acPNG.pas.2889: MaxInSample := (1 shl SourceBPS) – 1;
    0000000000B37A45 B001 mov al,$01
    0000000000B37A47 480FB64D27 movzx rcx,byte ptr [rbp+$27]
    0000000000B37A4C 80E107 and cl,$07
    0000000000B37A4F D3E0 shl eax,cl
    0000000000B37A51 80E801 sub al,$01
    0000000000B37A54 884529 mov [rbp+$29],al

    I think that's not correct (the pascal source does not limit the shift to max 7 bits). But there we have it. Maybe you can figure out how to make this module behave with WIN64

    #57728
    Support
    Keymaster

    Hello!

    Max value of SourceBPS is 8 there, min value is 0

    I have checked the code in x32 and x64, all results were equal and there was not overflow or range errors.

    Do you have problems with this code? Maybe you can give me the Png-file which can't be processed? I will test why it happens.

    #57745
    HeDiBo
    Participant
    'Support' wrote:

    Hello!

    Max value of SourceBPS is 8 there, min value is 0

    I have checked the code in x32 and x64, all results were equal and there was not overflow or range errors.

    Do you have problems with this code? Maybe you can give me the Png-file which can't be processed? I will test why it happens.

    I didn't say there was an overflow or range error. In my 64 bit delphi (Delphi XE4) the code generated is simply wrong.

    This is the stack trace:

    acPNG.MulDiv16(0,0,0)

    acPNG.TColorManager.RowConvertIndexed8((…),$5710618,40,128)

    acPNG.TColorManager.ConvertRow((…),$5710618,40,128)

    acPNG.TPNGGraphic.LoadIDAT((no value))

    acPNG.TPNGGraphic.LoadFromStream($82D5BD0)

    sGraphUtils.LoadBmpFromPngStream($53C8E40,$82D5BD0)

    acAlphaImageList.TsAlphaImageList.GenerateStdList

    acAlphaImageList.TsAlphaImageList.Loaded

    System.Classes.NotifyGlobalLoading

    System.Classes.InitInheritedComponent($531DEB0,TClass($6F1738))

    Vcl.Forms.TCustomForm.Create($53B74E0)

    Vcl.Forms.TApplication.CreateForm(TComponentClass($927278),(no value))

    ac64BitBug.ac64BitBug

    :00007FFCDD051FE4 ; C:WINDOWSSystem32KERNEL32.DLL

    :00007FFCDD35EFC1 ;

    This is a sample project, As soon as image 1 is loaded, the DIvision by Zero exception occurs.

    [attachment=8702:ac64BitBug.zip]

    #57747
    Support
    Keymaster

    Thank you for the demo.

    Really, I see that old x64 compilers has this bug.

    I will search a better solution.

    This solution is the best, I think (2 shl (n-1)-1), but I will check it: https://stackoverflo…on-in-delphi-xe

    #57748
    HeDiBo
    Participant
    'Support' wrote:

    Thank you for the demo.

    Really, I see that old x64 compilers has this bug.

    I will search a better solution.

    This solution is the best, I think (2 shl (n-1)-1), but I will check it: https://stackoverflo…on-in-delphi-xe

    Thanks!

    Will you report here when you think you programmed around this Delphi bug? I'll try it again then.

    #57762
    HeDiBo
    Participant

    In AC 13.04 the 64 bit code changed to:

    Code:
    acPNG.pas.2891: MaxInSample := byte(integer(1 shl SourceBPS) – 1);
    000000000084CEC9 C7C001000000 mov eax,$00000001
    000000000084CECF 480FB64D27 movzx rcx,byte ptr [rbp+$27]
    000000000084CED4 80E11F and cl,$1f
    000000000084CED7 D3E0 shl eax,cl
    000000000084CED9 80E801 sub al,$01
    000000000084CEDC 884529 mov [rbp+$29],al

    And that seems to work a3.gif

    PS. Is there anyway you can eliminate the generated tabs in the code text? They're always wrong 😰

    #57757
    Support
    Keymaster

    This is how it looks in the my editor.

    [attachment=8714:Code.png]

    Please, show how it looks on your side.

    #57799
    HeDiBo
    Participant
    'Support' wrote:

    This is how it looks in the my editor.

    [attachment=8714:Code.png]

    Please, show how it looks on your side.

    Sorry, I didn't mean in the Delphi editor, but in the [code}..[/code] part on this website.

    #57833
    Support
    Keymaster

    Hello!

    I understand now, but can't change it in the form engine, unfortunately.

    #57961
    HeDiBo
    Participant
    'Support' wrote:

    Thank you for the demo.

    Really, I see that old x64 compilers has this bug.

    I will search a better solution.

    This solution is the best, I think (2 shl (n-1)-1), but I will check it: https://stackoverflo…on-in-delphi-xe

    The problem is solved. But the change in the source code is not marked with a comment, explaining the need for the used construct. That's important to prevent forgetting where it's for or for a third person maintaining the code in the future.

Viewing 10 posts - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.