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

Division #9

Closed
apt1002 opened this issue Aug 19, 2020 · 2 comments
Closed

Division #9

apt1002 opened this issue Aug 19, 2020 · 2 comments
Labels
enhancement New feature or request

Comments

@apt1002
Copy link
Owner

apt1002 commented Aug 19, 2020

x64_64 provides the ability to assemble an unsigned long division instruction. Expose it as a Mijit instruction.

The main task is to define the behaviour of the Mijit instruction in exceptional cases. For example, do we like Undefined Behaviour? Do we need a way to throw Exceptions? It eventually needs to abstract a variety of different native machine codes without excessive boiler-plate.

Also, we should provide signed division, which is harder still.

@apt1002 apt1002 added the enhancement New feature or request label Aug 24, 2020
@apt1002
Copy link
Owner Author

apt1002 commented Sep 9, 2020

I think there are going to be other ways in which undefined behaviour arises in Mijit (see e.g. #14) so perhaps it's fine here too.

@apt1002
Copy link
Owner Author

apt1002 commented Oct 5, 2021

This is now done. We provided unsigned and signed division (UDiv and SDiv) as new BinaryOps. The semantics cover the cases where Arm and x86 do the same thing; everything else is missing or undefined behaviour.

Unsigned division is uncontroversial. The quotient is rounded down, and only division by zero is undefined behaviour. UDiv divides a one-word integer (32- or 64-bit) by another; there is no 128-bit by 64-bit division because it would be difficult to implement on Arm.

The quotient of a signed division is rounded towards zero, which is usually not what you want, but that's what the hardware does (both x86 and Arm). Again it's 64-bit by 64-bit division. There is one case in which signed division gives an answer that cannot be represented: -2⁶³ / -1. This is undefined behaviour in Mijit, because x86 and Arm handle it differently.

Mijit does not provide an instruction to compute the remainder of a division. The remainder can easily and relatively cheaply be computed using a division, a multiplication and a subtraction. This matches Arm's design.

Experience of implementing Beetle's five division and remainder instructions in terms of Mijit's new instructions suggests that UDiv is really all you need. SDiv is quite unlikely to be useful unless you are sure you want it's strange rounding mode and you can avoid it's undefined behaviour. For now we provide both UDiv and SDiv.

@apt1002 apt1002 closed this as completed Oct 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant