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

feat: add C implementation for math/base/special/minmax #6296

Open
wants to merge 9 commits into
base: develop
Choose a base branch
from

Conversation

Neerajpathak07
Copy link
Contributor

Progresses #649,
Resolves #2106.

Description

What is the purpose of this pull request?

This pull request:

  • adds C implementation for math/base/special/minmax
#include <stdint.h>

double min;
double max;

stdlib_base_minmax( 4.0, -5.0, &min, &max );

Related Issues

Does this pull request have any related issues?

This pull request:

Questions

Any questions for reviewers of this pull request?

No.

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

@stdlib-bot stdlib-bot added the Math Issue or pull request specific to math functionality. label Mar 22, 2025
@stdlib-bot
Copy link
Contributor

stdlib-bot commented Mar 22, 2025

Coverage Report

Package Statements Branches Functions Lines
math/base/special/minmax 253 / 253
+ 100.00 %
17 / 17
+ 100.00 %
3 / 3
+ 100.00 %
253 / 253
+ 100.00 %

The above coverage report was generated for the changes in this PR.

* @param info callback data
* @return Node-API value
*/
static napi_value addon( napi_env env, napi_callback_info info ) {
Copy link
Contributor Author

@Neerajpathak07 Neerajpathak07 Mar 22, 2025

Choose a reason for hiding this comment

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

Have kept the addon logic as this for now and going through https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/napi to have an idea for a utility for void functions.
for this file I have refactored this code-block.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Turns out we can convert it a very simplified fashion like:-

static napi_value addon( napi_env env, napi_callback_info info ) {
        STDLIB_NAPI_ARGV( env, info, argv, argc, 3 );
	STDLIB_NAPI_ARGV_DOUBLE( env, x, argv, 0 );
	STDLIB_NAPI_ARGV_DOUBLE( env, y, argv, 1 );
	STDLIB_NAPI_ARGV_FLOAT64ARRAY( env, z, zlen, argv, 2 );
        
        double min;
	double max;
	stdlib_base_minmax( x, y, &min, &max );

	double *op = (double *)z;
	op[ 0 ] = min;
	op[ 1 ] = max;

	return NULL;
}

just searching for an utility that can accumulate void function.

Copy link
Contributor Author

@Neerajpathak07 Neerajpathak07 Mar 22, 2025

Choose a reason for hiding this comment

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

something similar was also done in rempio2

Copy link
Member

Choose a reason for hiding this comment

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

@Neerajpathak07 You can do something like this:

In addon.c, you can have:

#include "stdlib/math/base/special/minmax.h"
#include "stdlib/napi/export.h"
#include "stdlib/napi/argv.h"
#include "stdlib/napi/argv_double.h"
#include "stdlib/napi/argv_float64array.h"
#include <node_api.h>

/**
* Receives JavaScript callback invocation data.
*
* @param env    environment under which the function is invoked
* @param info   callback data
* @return       Node-API value
*/
static napi_value addon( napi_env env, napi_callback_info info ) {
    STDLIB_NAPI_ARGV( env, info, argv, argc, 3 );
    STDLIB_NAPI_ARGV_DOUBLE( env, x, argv, 0 );
    STDLIB_NAPI_ARGV_DOUBLE( env, y, argv, 1 );
    STDLIB_NAPI_ARGV_FLOAT64ARRAY( env, out, outlen, argv, 2 );
    stdlib_base_minmax( x, y, &out[ 0 ], &out[ 1 ] );
    return NULL;
}

STDLIB_NAPI_MODULE_EXPORT_FCN( addon )

And, for native.js:

function minmax( x, y ) {
	var out = new Float64Array( 2 );
	addon( x, y, out );
	return [ out[ 0 ], out[ 1 ] ];
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@gunjjoshi Bingo!!
I had drafted a similar logic earlier but was confused on how to go about the void function. But you just clarified it thank you soo much!!

@Neerajpathak07 Neerajpathak07 marked this pull request as ready for review March 22, 2025 18:26
@stdlib-bot stdlib-bot added the Needs Review A pull request which needs code review. label Mar 22, 2025
@Neerajpathak07
Copy link
Contributor Author

@gunjjoshi LMK your opinion on this!!

@Neerajpathak07
Copy link
Contributor Author

Not sure though why only C examples CI tests are failing for #include <node_api.h> and not for any other files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Math Issue or pull request specific to math functionality. Needs Review A pull request which needs code review.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[RFC]: Add C implementation for @stdlib/math/base/special/minmax
3 participants