-
Notifications
You must be signed in to change notification settings - Fork 8.3k
sys: util: Add SIGN, gcd and lcm #98875
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
base: main
Are you sure you want to change the base?
Conversation
| * @param a First integer | ||
| * @param b Second integer | ||
| * | ||
| * @return The greatest common divisor of a and b, always returns a positive value. |
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.
This statement is not quite true. If both a and b are INT_MIN, then the result will be INT_MIN -- a negative value.
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.
By mathematical definition, the greatest common divisor (GCD) of two integers is always a non-negative integer. But you are right, the old implementation didn't treat this edge case when both a and b are INT_MIN. We fixed this in the current push. Thanks !
|
@ngphibang I've added a PR in the hal_stm32 (zephyrproject-rtos/hal_stm32#327) in order to avoid the duplication of SIGN macro. Since this is part of the hal_stm32 repo, you'll need to update the west.yaml in this PR in order to have twister tests working fine by using the hal_stm32 PR. You can do that by adding a commit in this PR to update the manifest. aka below modification in the west.yml Once the PR in hal_stm32 will be merged you'll be able to update again the manifest to point to the new revision of the hal_stm32. I was first thinking about creating a PR myself for the west.yml to update the hal_stm32 revision as I usually do, but I am afraid that our PR will depend on each others so it seems easier if your PR has the west.yml directly in it. |
josuah
left a comment
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.
Good idea to split dependencies out of the main PR, which will hopefully make the main PR quicker to merge.
|
For this PR, you might want to get some inspiration from #96478 ? |
|
Yeah prob a good idea to do single evaluation macros whenever possible, for generics, this needs to wait for #97061 |
Thanks, If I understand well, should the macro LCM be implemented like this: so that a and b is evaluated once ? Thanks. |
|
That's right, I'd make it lowercase in that case since then it can only be used in functions and then the multiple evaluation version can still be there in uppercase. That's how we did min/max and how Linux does it too. |
Thanks. However, in this case, I don’t think we should implement the version with multiple evaluation because the functions lcm_s, gcd_s, lcm_u, and gcd_u will be declared as ALWAYS_INLINE. After the last comment in 96478#issuecomment-3368956442 lcm and gcd macros will always be single-evaluation macros. The most challenging part here is the acceptance of _Generic in Zephyr. Nevertheless, I’ve noticed that _Generic is already used in the C_PRINTF() macros, so at this point I don’t think this should have any problem. As lcm and gcd are single evaluated macro, so these macros be in lower case only ? @josuah @fabiobaltieri Thanks. |
Yeah but I think that function is optional? Not too sure, anyway mandatory C17 support has been discussed and voted by TSC last week and it looks like it was going to get in, will keep you posted.
Yeah, from the min/max discussion, the reasoning about doing the macro in lowercase is that the way it's implemented it effectively behave as a function, should probably write it down in the coding style somewhere because I think it makes a lot of sense. Like you can't use in a BUILD_ASSERT or anything that must be evaluated statically. Doing only the single evaluation I think is fine, avoid having people picking the wrong implementation unintentionally, if the macro one is needed it can always be introduced at a later stage. |
|
The following west manifest projects have changed revision in this Pull Request:
⛔ DNM label due to: 1 project with PR revision Note: This message is automatically posted and updated by the Manifest GitHub Action. |
Add a macro to determine the sign of a value. Signed-off-by: Phi Bang Nguyen <[email protected]> Signed-off-by: Trung Hieu Le <[email protected]>
Add helpers to compute Greates Common Divisor (GCD) and Least Common Multiple (LCM). Signed-off-by: Phi Bang Nguyen <[email protected]> Signed-off-by: Trung Hieu Le <[email protected]>
Update the revision to include SIGN macro fix Signed-off-by: Phi Bang Nguyen <[email protected]>
Thanks @avolmat-st. I added the west.yml revision commit into this PR. However, this PR may take some time to be merged, so does it block your PR ? |
|
No issue @ngphibang. It is not blocking the hal_stm32 PR which has actually already been merged and does not block other hal_stm32 related PRs. |



Add some helpers to
util.hwhich are helpul for example, used in #98514