Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

80x86 Co Pro: Issues with IDIV #127

Closed
hoglet67 opened this issue Sep 6, 2021 · 6 comments
Closed

80x86 Co Pro: Issues with IDIV #127

hoglet67 opened this issue Sep 6, 2021 · 6 comments

Comments

@hoglet67
Copy link
Owner

hoglet67 commented Sep 6, 2021

From Dominic:

Just looking over warnings for various code checkers . Does anyone know an x86 checker like dormann ? I think cpu80186.c line 1124 has a bug. it looks as though it is written for a 16 bit divide where as it should be an 8 bit divide. line 1143 I think needs to be reinstated bit with (s2 < 0x80)? s2 :....

//s2 = (s2 < 0x8000) ? s2 : ((~s2 + 1) & 0xffff); //always true

This would be a long-standing fake86 bug.

@hoglet67
Copy link
Owner Author

hoglet67 commented Sep 6, 2021

There is also a bug in this line:

sign = (((s1 ^ s2) & 0x8000) != 0);

I wonder if the best solution is to just sign-extend the divisor to 16 bits.

i.e. instead of

  s2 = divisor

have

  s2 = divisor;
  if (s2 & 0x80) {
    s2 |= 0xff00;
  }

Then both s1 and s2 are 16 bits, and the code should be the same as the 16-bit version.

@dp111
Copy link
Collaborator

dp111 commented Sep 6, 2021 via email

@hoglet67
Copy link
Owner Author

hoglet67 commented Oct 30, 2021

There may be a second issue with idiv.

The documentation states:

Non-integral results are truncated (chopped) towards 0.

I believe the implication of this is that the sign of the remainder should depend only on the sign of the dividend:

 14 / 3 : quotient =  4 remainder =   2    (checking:  3* 4+2= 14)  
 14 /-3 : quotient = -4 remainder =   2    (checking: -3*-4+2= 14)
-14 / 3 : quotient = -4 remainder =  -2    (checking: -3* 4-2=-14)
-14 /-3 : quotient =  4 remainder =  -2    (checking:  3*-4-2=-14)

I don't believe the current code is correct in this respect...

  if (sign)
  {
    d1 = (~d1 + 1) & 0xff;
    d2 = (~d2 + 1) & 0xff;
  }

It looks like the current code always makes the sign of the quotient (d1) and remainder (d2) the same.

I really need to try write some test cases, and maybe get someone to run them on a real 80186 Co Pro.

@hoglet67
Copy link
Owner Author

Here's a small test case for IDIV8:
capture25
Here's the (correct) result from the Matchbox Co Pro:
capture26
Here's the (incorrect) result from PiTubeDirect Alpha4:
capture27
And here's the result after the fix:
capture28

@hoglet67
Copy link
Owner Author

Here's a small test case for IDIV16:
capture30
Here's the (correct) result from the Matchbox Co Pro:
capture31
Here's the (incorrect) result from PiTubeDirect Alpha4:
capture32
And here's the result after the fix:
capture33

@hoglet67 hoglet67 changed the title 80x86 Co Pro: Possible issue with IDIV8 80x86 Co Pro: Possible issue with IDIV Oct 31, 2021
@hoglet67 hoglet67 changed the title 80x86 Co Pro: Possible issue with IDIV 80x86 Co Pro: Issues with IDIV Oct 31, 2021
hoglet67 added a commit that referenced this issue Oct 31, 2021
Change-Id: Id1be16e5a55197de45b8e8614748d5b9b77ae6a7
@hoglet67
Copy link
Owner Author

Dominic, if you get the chance, could you run the updated code through cppcheck?

hoglet67 added a commit that referenced this issue Nov 14, 2021
Change-Id: I27e3b47a89c990add1764afbc450f2734176682f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants