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

[RFC]: add C implementation for @stdlib/math/base/special/fast/uint32-log2 #1931

Closed
3 tasks done
performant23 opened this issue Mar 17, 2024 · 6 comments · Fixed by #1946
Closed
3 tasks done

[RFC]: add C implementation for @stdlib/math/base/special/fast/uint32-log2 #1931

performant23 opened this issue Mar 17, 2024 · 6 comments · Fixed by #1946
Assignees
Labels
Accepted RFC feature request which has been accepted. C Issue involves or relates to C. difficulty: 2 May require some initial design or R&D, but should be straightforward to resolve and/or implement. Good First Issue A good first issue for new contributors! Math Issue or pull request specific to math functionality. priority: Normal Normal priority concern or feature request. RFC Request for comments. Feature requests and proposed changes.

Comments

@performant23
Copy link
Contributor

performant23 commented Mar 17, 2024

Description

This RFC proposes adding native C implementation for @stdlib/math/base/special/fast/uint32-log2:

uint32_t stdlib_base_fast_uint32_log2 (uint32_t x);

Related Issues

N/A

Questions

No.

Other

No.

Checklist

  • I have read and understood the Code of Conduct.
  • Searched for existing issues and pull requests.
  • The issue name begins with RFC:.
@performant23
Copy link
Contributor Author

Hello, had started working on this issue yesterday. Will open a PR once my last one #1918 is addressed. Thanks!

@performant23
Copy link
Contributor Author

performant23 commented Mar 17, 2024

Also, just to clarify, there are variable declarations for constants and they're used later in lib/main.js as:

// 4294901760 => 0xFFFF0000 => 11111111111111110000000000000000
var B4 = 0xFFFF0000 >>> 0; // asm type annotation
.
.
.
if ( y & B4 ) {
do something;
}

Is it required that we do the same for the C implementation using // CONSTANTS // section:

const uint32_t B4 = 0xFFFF0000u; // asm type annotation

or can we use it directly in conditionals:

if (y & 0xFFFF0000u){
do something...
}

cc @kgryte, @Planeshifter

Presently I've implemented it using the // CONSTANTS // section for the declarations.

@performant23
Copy link
Contributor Author

And also, since the implementation is relatively more complex and subtle, just thought it would be a good idea to flesh out the implementation in parts I have doubts about. I wanted to confirm about the use of minstd in our C implementation. Since the single function in JS is broken into multiple parts for C and directly addresses memory, how would generating the random numbers work basically?

So far, I have come up with this using the implementation and examples in those.

struct BasePRNGObject *obj = stdlib_base_random_minstd_shuffle_allocate(<seed>); 

uint64_t randNum;
int8_t status = obj->prng->next(obj, &randNum); (//We can later typecast randNum to int32_t.)
.
.
.

stdlib_base_random_minstd_shuffle_free(obj);

In this case, how would we handle failures in allocation and the failures in generating the random numbers?

@kgryte
Copy link
Member

kgryte commented Mar 17, 2024

Re: variable declarations. Yes, prefer static const declarations over inlining.

Re: PRNGs. Not sure why this is necessary. Why do you need to use a stdlib PRNG for adding a C implementation?

@performant23
Copy link
Contributor Author

Thanks for the clarifications, @kgryte! So currently, the PRNGs are used in benchmarking to generate random 32-bit unsigned integers. Thought we'd use their corresponding C exports for generation in benchmark/c/native/benchmark.c

@kgryte
Copy link
Member

kgryte commented Mar 17, 2024

For random values, instead of rand_double (you can leave that function there!), just use rand() directly and cast to uint32_t.

@kgryte kgryte added RFC Request for comments. Feature requests and proposed changes. Math Issue or pull request specific to math functionality. Accepted RFC feature request which has been accepted. Good First Issue A good first issue for new contributors! priority: Normal Normal priority concern or feature request. C Issue involves or relates to C. difficulty: 2 May require some initial design or R&D, but should be straightforward to resolve and/or implement. labels Mar 17, 2024
kgryte added a commit that referenced this issue Mar 23, 2024
PR-URL: 	#1946
Closes: #1931 
Ref: #649
Co-authored-by: Athan Reines <[email protected]>
Reviewed-by: Athan Reines <[email protected]>
Reviewed-by: Pranav Goswami <[email protected]>
Signed-off-by: Rutam <[email protected]>
Signed-off-by: Pranav Goswami <[email protected]>
Signed-off-by: Athan Reines <[email protected]>
Co-authored-by: Pranav Goswami <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Accepted RFC feature request which has been accepted. C Issue involves or relates to C. difficulty: 2 May require some initial design or R&D, but should be straightforward to resolve and/or implement. Good First Issue A good first issue for new contributors! Math Issue or pull request specific to math functionality. priority: Normal Normal priority concern or feature request. RFC Request for comments. Feature requests and proposed changes.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants