Menu

#1432 int8_t x uint8_t multiplication broken

closed-fixed
5
2013-05-25
2008-02-24
No

Multiplication of int8_t by uint8_t (and vice vers) is broken on Z80.

To see the bug remove the first #ifndef SDCC_z80 in the regression test support/regression/tests/onebyte.c

sdcc generates a call to the multiplication function for int8_t x int8_t, so the unsigned number is treated as a signed number which gives wrong results if it's greater than 127.

Philipp

Discussion

  • Philipp Klaus Krause

    • labels: 100692 --> Icode generator
     
  • Philipp Klaus Krause

    Logged In: YES
    user_id=564030
    Originator: YES

    While Inoticed this problem in the Z80 port it probably affects all ports that use support functions for 8-bit multiplication: There are just two multiplication support functions: One for signed, one for unsigned multiplication. The one for signed multiplication is used for mixed multiplications, too, which gives wrong results.

    I suggest the following fix:
    Promote operands of mixed char multiplications to int, do an int multiplication. This is slower, but gives the correct result and I expect mixed char multiplications through support functions to be rare anyway.

    Philipp

     
  • Maarten Brock

    Maarten Brock - 2008-07-13

    Logged In: YES
    user_id=888171
    Originator: NO

    Using either the signed or unsigned 8 bit multiplication for mixed operands can only give the wrong result if the result is 16 bits. If the result is only 8 bits it doesn't matter. See also genMultOneByte() in mcs51/gen.c.

    Maarten

     
  • Maarten Brock

    Maarten Brock - 2008-09-15
    • assigned_to: nobody --> maartenbrock
     
  • Maarten Brock

    Maarten Brock - 2008-09-16
    • milestone: --> fixed
    • status: open --> closed-fixed
     
  • Maarten Brock

    Maarten Brock - 2008-09-16

    Fixed in SDCC 2.8.4 #5241.

     

Log in to post a comment.