-
-
Notifications
You must be signed in to change notification settings - Fork 11.1k
BUG: shift operator cycles, fixes #2449 #7473
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -772,6 +772,7 @@ BOOL__ones_like(char **args, npy_intp *dimensions, npy_intp *steps, void *NPY_UN | |
* npy_long, npy_ulong, npy_longlong, npy_ulonglong# | ||
* #ftype = npy_float, npy_float, npy_float, npy_float, npy_double, npy_double, | ||
* npy_double, npy_double, npy_double, npy_double# | ||
* #issigned = 1, 0, 1, 0, 1, 0, 1, 0, 1, 0# | ||
*/ | ||
|
||
#define @TYPE@_floor_divide @TYPE@_divide | ||
|
@@ -824,15 +825,15 @@ NPY_NO_EXPORT NPY_GCC_OPT_3 void | |
|
||
/**begin repeat1 | ||
* Arithmetic | ||
* #kind = add, subtract, multiply, bitwise_and, bitwise_or, bitwise_xor, | ||
* left_shift, right_shift# | ||
* #OP = +, -,*, &, |, ^, <<, >># | ||
* #kind = add, subtract, multiply, bitwise_and, bitwise_or, bitwise_xor# | ||
* #OP = +, -,*, &, |, ^# | ||
*/ | ||
|
||
NPY_NO_EXPORT NPY_GCC_OPT_3 void | ||
@TYPE@_@kind@(char **args, npy_intp *dimensions, npy_intp *steps, void *NPY_UNUSED(func)) | ||
@TYPE@_@kind@(char **args, npy_intp *dimensions, npy_intp *steps, | ||
void *NPY_UNUSED(func)) | ||
{ | ||
if(IS_BINARY_REDUCE) { | ||
if (IS_BINARY_REDUCE) { | ||
BINARY_REDUCE_LOOP(@type@) { | ||
io1 @OP@= *(@type@ *)ip2; | ||
} | ||
|
@@ -845,6 +846,109 @@ NPY_NO_EXPORT NPY_GCC_OPT_3 void | |
|
||
/**end repeat1**/ | ||
|
||
/* | ||
* Arithmetic bit shift operations. | ||
* | ||
* Intel hardware masks bit shift values, so large shifts wrap around | ||
* and can produce surprising results. The special handling ensures that | ||
* behavior is independent of compiler or hardware. | ||
* TODO: We could implement consistent behavior for negative shifts, | ||
* which is undefined in C. | ||
*/ | ||
|
||
#define LEFT_SHIFT_OP \ | ||
do { \ | ||
if (NPY_LIKELY(in2 < sizeof(@type@) * 8)) { \ | ||
*out = in1 << in2; \ | ||
} \ | ||
else { \ | ||
*out = 0; \ | ||
} \ | ||
} while (0) | ||
|
||
|
||
NPY_NO_EXPORT NPY_GCC_OPT_3 void | ||
@TYPE@_left_shift(char **args, npy_intp *dimensions, npy_intp *steps, | ||
void *NPY_UNUSED(func)) | ||
{ | ||
if (IS_BINARY_REDUCE) { | ||
BINARY_REDUCE_LOOP(@type@) { | ||
@type@ ip2_val = *(@type@ *)ip2; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could remove the reduce specialization, I suspect reduce using the shift operators would be rare and There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done in #13739 |
||
|
||
if (NPY_LIKELY(ip2_val < sizeof(@type@) * 8)) { | ||
io1 <<= ip2_val; | ||
} | ||
else { | ||
io1 = 0; | ||
} | ||
} | ||
*((@type@ *)iop1) = io1; | ||
} | ||
else { | ||
BINARY_LOOP_FAST(@type@, @type@, LEFT_SHIFT_OP); | ||
} | ||
} | ||
|
||
#undef LEFT_SHIFT_OP | ||
|
||
#define RIGHT_SHIFT_OP_SIGNED \ | ||
do { \ | ||
if (NPY_LIKELY(in2 < sizeof(@type@) * 8)) { \ | ||
*out = in1 >> in2; \ | ||
} \ | ||
else if (in1 < 0) { \ | ||
*out = -1; \ | ||
} \ | ||
else { \ | ||
*out = 0; \ | ||
} \ | ||
} while (0) | ||
|
||
#define RIGHT_SHIFT_OP_UNSIGNED \ | ||
do { \ | ||
if (NPY_LIKELY(in2 < sizeof(@type@) * 8)) { \ | ||
*out = in1 >> in2; \ | ||
} \ | ||
else { \ | ||
*out = 0; \ | ||
} \ | ||
} while (0) | ||
|
||
NPY_NO_EXPORT NPY_GCC_OPT_3 void | ||
@TYPE@_right_shift(char **args, npy_intp *dimensions, npy_intp *steps, | ||
void *NPY_UNUSED(func)) | ||
{ | ||
if (IS_BINARY_REDUCE) { | ||
BINARY_REDUCE_LOOP(@type@) { | ||
@type@ ip2_val = *(@type@ *)ip2; | ||
|
||
if (NPY_LIKELY(ip2_val < sizeof(@type@) * 8)) { | ||
io1 >>= ip2_val; | ||
} | ||
#if @issigned@ | ||
else if (io1 < 0) { | ||
io1 = -1; | ||
eric-wieser marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
#endif | ||
else { | ||
io1 = 0; | ||
} | ||
} | ||
*((@type@ *)iop1) = io1; | ||
} | ||
else { | ||
#if @issigned@ | ||
BINARY_LOOP_FAST(@type@, @type@, RIGHT_SHIFT_OP_SIGNED); | ||
#else | ||
BINARY_LOOP_FAST(@type@, @type@, RIGHT_SHIFT_OP_UNSIGNED); | ||
#endif | ||
} | ||
} | ||
|
||
#undef RIGHT_SHIFT_OP_SIGNED | ||
#undef RIGHT_SHIFT_OP_UNSIGNED | ||
|
||
|
||
/**begin repeat1 | ||
* #kind = equal, not_equal, greater, greater_equal, less, less_equal, | ||
* logical_and, logical_or# | ||
|
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 think there are number of bits macros defined somewhere... yep, in
npy_common.h
. The have the formNPY_BITSOF_LONGLONG
etc.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.
Probably already included here.
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, kill the macro ;)
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 actually buy speed over
...? ... : ...
?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.
A cleaner version modulo signedness might be
Although I don't think we actually need the
do ... while(0)
bit here.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.
Macro removed in #13739. Kept the if statements because I think they're clearer. Did not use
NPY_BITS_OF_LONG
and friends because they don't exist forNPY_BITS_OF_ULONG
etc.