-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
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
BUG: don't use pow for integer power ufunc loops. #7496
Conversation
if (in2 & 1) { | ||
out *= in1; | ||
} | ||
in2 >>= 1; /* Shift exponent down by 1 bit */ |
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.
Please put the comment on the line before the code.
Could you put a test in maybe from the cited issue? |
out *= in1; | ||
} | ||
in2 >>= 1; /* Shift exponent down by 1 bit */ | ||
if (in2 == 0) { |
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.
Does this really speed things up at all? You are doing one check per iteration in order to save yourself a single multiplication in the last one, does it pay off.
If there is any performance to be gained there, you could alternatively peel off the first multiplication as an assignment, so that a single check is needed:
out = in2 & 1 ? in1 : 1;
in2 >>= 1;
while (in2 > 0) {
in1 *= in1;
if (in2 & 1) {
out *= in1;
}
in2 >>= 1;
}
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.
I have no idea really. This code is lightly adapted from the integer power code used for scalars.
Test added. I switched to using your version @jaimefrio. When I tested this, it either made no difference or a minuscule difference in run time with your version being the ever so slightly faster. |
But in fairness, eliminating one multiplication is a bigger deal when finding squares or cubes relative to 62 powers. |
Thanks Eric! |
Fixes gh-7405.