-
Notifications
You must be signed in to change notification settings - Fork 126
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
Generate a sequence of instructions for divisions on Arm targets #126
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Add new command line option
+m
, which indicates the use of hardware integer multiplication and division instructions. - Extend
tests/driver.sh
for more integer division coverage.
@jserv I understand that we can add a However, I consider shecc can still generate multiplication instructions for ARM even if the Therefore, for the implementation of the |
Yes for Arm backend. Please proceed. |
The reference implementation mentioned in #46 assumes that the dividend and divisor are unsigned integers, but it may fail if at least one of them is a signed integer. Can I suppose that shecc will not encounter any signed integer division and just handle unsigned integer division? |
Given your aim to replace the current udiv/sdiv instruction, the proposed changes should also ensure division is covered for both signed and unsigned integers, mirroring the approach gcc took with its libgcc. |
By the way, I enjoy this article Fast(est) Double-Word Integer Division. |
I have a new question about the right shift operation. During these days, I test the following code: /* test.c */
int main() {
printf("%d\n", -2 >> 31);
return 0;
} In C99, It mentions that it is an implementation-defined behavior for using right shift operation on signed integers. If we use gcc and clang to compile and execute it, we can all obtain the output
I believe it may be a convention that many compilers use arithmetic right shift instructions for signed integers. But, when using shecc to test the right shift operation, we will get the output
Should we instruct shecc to generate arithmetic right shift instructions as well? |
@vacantron, can you comment this? |
Yes, the behavior should be consistent with GCC or Clang. Thank you for the report! |
I defer to @vacantron for his performance evaluation. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rebase the latest master
branch for bool specific fixes.
I encounter a new issue when rebasing:
Then, I checked the source code including my changes, and found that shecc has a potential problem:
It can be observed that the total bytes of all source files have exceeded 278528, as defined by This will cause the Should I enlarge the allocated size in this PR to prevent the potential problem? |
Yes, please do so. |
Division instruction doesn't exist in ARMv7-A, but the current implementation of shecc still generates it to compute the result for division or modulo operations. Thus, here adjusts arm-codegen so that shecc will generate a sequence of instructions to achieve signed integer division without relying on any division instruction. Additionally, extend test/driver.sh to test the divisions and add a new command line option '+m' to determine whether generating hardware multiplication and division instructions for the executable compiled by shecc: $ shecc [-o output] [+m] [--no-libc] [--dump-ir] <infile.c> Finally, the allocated size for 'SOURCE', used to store all bytes of the source files, has been enlarged to prevent access to invalid memory regions.
Because of #120 , the measurement of the performance is postponed. I prefer to apply this patch first. |
Thank @DrXiao for contributing! |
@jserv May I ask why this PR can be merged? Or will this PR be reopened and requested to be improved again in the future? |
The latter. |
Division instruction doesn't exist in ARMv7-A (#46 ), but the current implementation of shecc still generates it to compute the result for division or modulo operations.
Thus, here adjusts arm-codegen so that shecc will generate an instruction sequence to achieve integer division without relying on any division instruction.