Skip to content

feat: add C implementation for math/base/special/gcd #1703

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

Merged
merged 19 commits into from
Apr 20, 2024

Conversation

aman-095
Copy link
Member

@aman-095 aman-095 commented Mar 5, 2024

Resolves #1701.

Description

This pull request adds native C implementation for
@stdlib/math/base/special/gcd.

Related Issues

Does this pull request have any related issues?

This pull request:

Questions

Any questions for reviewers of this pull request?

The above implementation for gcd uses 64-bit signed integers as input and returns double-precision floating-point number as output. I have changed the test.native.js for the last case of test.js where we cannot have value >= 2^63 - 1, and removed the cases of infinity, NaN, as they won't work and hence are removed. Also, the case in test.js where when passed any non-integer should give NaN as output is removed as here when passed non-integer it would type cast it to int and answer then will not be NaN.

Other

Any other information relevant to this pull request? This may include screenshots, references, and/or implementation notes.

No.

Checklist

Please ensure the following tasks are completed before submitting this pull request.


@stdlib-js/reviewers

@aman-095
Copy link
Member Author

aman-095 commented Mar 5, 2024

Also, if we use double-precision floating-point numbers as input and the same as output we will have issues in syncing the javascript implementation with the C implementation as the right shift, and left shift operators used won't work for double-precision floating-point numbers.
CC @kgryte @Planeshifter @Pranavchiku

@Pranavchiku Pranavchiku added Feature Issue or pull request for adding a new feature. Needs Discussion Needs further discussion. Math Issue or pull request specific to math functionality. C Issue involves or relates to C. labels Mar 5, 2024
@aman-095 aman-095 changed the title feat: Add C implementation for @stdlib/math/base/special/gcd feat: Add C implementation for math/base/special/gcd Mar 9, 2024
@aman-095 aman-095 changed the title feat: Add C implementation for math/base/special/gcd feat: add C implementation for math/base/special/gcd Mar 9, 2024
@kgryte
Copy link
Member

kgryte commented Mar 17, 2024

@aman-095 After looking at this again, I think we should do doubles in and doubles out in order to support a larger range of values, as done in JS.

Re: bit shift. I suggest just casting the double to an integer of the appropriate type. Once cast, a bitwise implementation should be readily doable in C.

@kgryte kgryte added Needs Changes Pull request which needs changes before being merged. and removed Needs Discussion Needs further discussion. labels Mar 17, 2024
@aman-095
Copy link
Member Author

@kgryte the test.js file contains a case with TWO_100. If we use typecast then the maximum we can do is typecast double to int64_t. But TWO_100 cannot be typecasted as it is out of the scope of int64_t.

@kgryte
Copy link
Member

kgryte commented Mar 25, 2024

@aman-095 Yeah, that's my point. We should update the implementation to work on doubles, rather than int64_t. Instead of int32 max, we can perform bitwise GCD on values less than int64 max. Should be able to cast from an integer double less than int64 max to an int64_t.

Signed-off-by: Athan <kgryte@gmail.com>
Signed-off-by: Athan <kgryte@gmail.com>
Signed-off-by: Athan <kgryte@gmail.com>
Copy link
Member

@kgryte kgryte left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the updates, @aman-095. Left another round of comments. Some small changes and I think we're getting there.

@kgryte kgryte removed the Needs Changes Pull request which needs changes before being merged. label Apr 20, 2024
Copy link
Member

@kgryte kgryte left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks, @aman-095!

@kgryte kgryte merged commit 753fbf2 into stdlib-js:develop Apr 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C Issue involves or relates to C. Feature Issue or pull request for adding a new feature. Math Issue or pull request specific to math functionality.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feat: add C implementation for math/base/special/gcd
3 participants