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

MAINT: Make the NPY_CPU_DISPATCH_CALL macros expressions not statements #17201

Merged
merged 1 commit into from
Sep 1, 2020

Conversation

eric-wieser
Copy link
Member

@eric-wieser eric-wieser commented Aug 31, 2020

This means they can return values, and need semicolons.

Not tested locally, but the constructs were tested in godbolt.

ping @seiko2plus

@eric-wieser eric-wieser requested a review from Qiyu8 August 31, 2020 15:37
…ents

This means they can return values, and need semicolons
@seiko2plus
Copy link
Member

@eric-wieser, flexibility and unify one behavior among all dispatching macros are the two reasons that forced me to use normal if-condition over the ternary condition. can I know your reasons?

@eric-wieser
Copy link
Member Author

can I know your reasons?

The main motivation here is that expressions are great, and making the result of the call an expression means I can use it like any other function call.

Note that I haven't touched the generated headers - those were flexible enough that I could just change the NPY_CPU ones.

Comment on lines +591 to +592
NPY_CPU_DISPATCH_CALL(highest_func = _umath_tests_dispatch_func, ());
NPY_CPU_DISPATCH_CALL(highest_var = _umath_tests_dispatch_var);
Copy link
Member Author

Choose a reason for hiding this comment

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

Note that these could now be rewritten

Suggested change
NPY_CPU_DISPATCH_CALL(highest_func = _umath_tests_dispatch_func, ());
NPY_CPU_DISPATCH_CALL(highest_var = _umath_tests_dispatch_var);
highest_func = NPY_CPU_DISPATCH_CALL(_umath_tests_dispatch_func)();
highest_var = NPY_CPU_DISPATCH_CALL(_umath_tests_dispatch_var);

Since this is a test, I decided to leave them as they are for now.

Copy link
Member

Choose a reason for hiding this comment

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

You can just change one of them so we can test both behaviors.

@seiko2plus
Copy link
Member

The main motivation here is that expressions are great, and making the result of the call an expression means I can use it like any other function call.

I will always vote for flexibility/hackability especially when you don't know "how deep the rabbit hole goes".
I see another thing here that may be reducing the flexibility will force the contributors towards safety, and I guess
this patch wouldn't harm that much we can always add new macros to serve different needs or even handle it locally in the case of #16960.

those were flexible enough that I could just change the NPY_CPU one

We can speed up the multiple calls of NPY_CPU_HAVE by inlining npy_cpu_have and extern the local variable npy__cpu_have which will give a good hint to the compiler without the need to change the NPY_CPU(CHK) one.

NPY_VISIBILITY_HIDDEN int
npy_cpu_have(int feature_id);
#define NPY_CPU_HAVE(FEATURE_NAME) \
npy_cpu_have(NPY_CPU_FEATURE_##FEATURE_NAME)

static unsigned char npy__cpu_have[NPY_CPU_FEATURE_MAX];

/**
* Macro NPY_CPU_DISPATCH_CALL_XB(LEFT, ...)
*
* Same as `NPY_CPU_DISPATCH_DECLARE` but exclude the baseline declration even
* if it was provided within the configration statments.
* Same as `NPY_CPU_DISPATCH_DECLARE` but exclude the baseline declaration even
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* Same as `NPY_CPU_DISPATCH_DECLARE` but exclude the baseline declaration even
* Same as `NPY_CPU_DISPATCH_CALL` but exclude the baseline call even

@Qiyu8
Copy link
Member

Qiyu8 commented Sep 1, 2020

I think this patch increases readability at the expense of a bit of unity, I'm confused the first time I saw NPY_CPU_DISPATCH_CALL and need to figure out it's expanded form, Glad to see this function-like style.

NPY__CPU_DISPATCH_CALL(NPY_CPU_HAVE, NPY_CPU_DISPATCH_CALL_CB_, __VA_ARGS__) \
NPY__CPU_DISPATCH_BASELINE_CALL(NPY_CPU_DISPATCH_CALL_BASE_CB_, __VA_ARGS__)
// Preprocessor callbacks
#define NPY_CPU_DISPATCH_CALL_CB_(TESTED_FEATURES, TARGET_NAME, LEFT, ...) \
else if (TESTED_FEATURES) { NPY_CAT(NPY_CAT(LEFT, _), TARGET_NAME) __VA_ARGS__; }
(TESTED_FEATURES) ? (NPY_CAT(NPY_CAT(LEFT, _), TARGET_NAME) __VA_ARGS__) :
Copy link
Member

Choose a reason for hiding this comment

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

the if statement is easier to read, why use the conditional expression?

Copy link
Member Author

Choose a reason for hiding this comment

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

Because the conditional expression makes this have a return value

@mattip mattip merged commit 263b293 into numpy:master Sep 1, 2020
@mattip
Copy link
Member

mattip commented Sep 1, 2020

Thanks @eric-wieser

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants