GeezerVison is showing github.com — Merge pull request #31401 from charris/backport-31396 · numpy/numpy@a42bd48
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: numpy/_core/config.h.in +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 32 | 32 | #mesondefine HAVE___BUILTIN_BSWAP64 | 33 | 33 | #mesondefine HAVE___BUILTIN_EXPECT | 34 | 34 | #mesondefine HAVE___BUILTIN_MUL_OVERFLOW | | 35 | +#mesondefine HAVE___BUILTIN_ADD_OVERFLOW | | 36 | +#mesondefine HAVE___BUILTIN_SUB_OVERFLOW | 35 | 37 | #mesondefine HAVE___BUILTIN_PREFETCH | 36 | 38 |
| 37 | 39 | #mesondefine HAVE_ATTRIBUTE_OPTIMIZE_UNROLL_LOOPS | |
Collapse file Copy file name to clipboardExpand all lines: numpy/_core/meson.build +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -401,6 +401,8 @@ optional_intrinsics = [ | 401 | 401 | ['__builtin_expect', '5, 0', [], []], | 402 | 402 | # Test `long long` for arm+clang 13 (gh-22811, but we use all versions): | 403 | 403 | ['__builtin_mul_overflow', '(long long)5, 5, (int*)5', [], []], | | 404 | + ['__builtin_add_overflow', '(long long)5, 5, (int*)5', [], []], | | 405 | + ['__builtin_sub_overflow', '(long long)5, 5, (int*)5', [], []], | 404 | 406 | ['__builtin_prefetch', '(float*)0, 0, 3', [], []], | 405 | 407 | ] | 406 | 408 | foreach intrin: optional_intrinsics | |
Collapse file Copy file name to clipboardExpand all lines: numpy/_core/src/common/npy_extint128.h +48-3Lines changed: 48 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -8,52 +8,97 @@ typedef struct { | 8 | 8 | } npy_extint128_t; | 9 | 9 |
| 10 | 10 |
| | 11 | +/* | | 12 | + * Integer add/sub/mul with overflow checking. | | 13 | + * | | 14 | + * On overflow, *overflow_flag is set to 1 and the return value is | | 15 | + * unspecified (callers must not use it). The arithmetic itself is | | 16 | + * never performed on values that would overflow signed `npy_int64`, | | 17 | + * so these helpers are free of the signed-overflow undefined behavior | | 18 | + * that an unguarded `a + b` / `a - b` / `a * b` would have. | | 19 | + * | | 20 | + * `__builtin_{add,sub,mul}_overflow` are probed independently by the | | 21 | + * meson build (see `numpy/_core/meson.build`); fall back to a | | 22 | + * branch-and-skip implementation otherwise. | | 23 | + */ | | 24 | + | 11 | 25 | /* Integer addition with overflow checking */ | 12 | 26 | static inline npy_int64 | 13 | 27 | safe_add(npy_int64 a, npy_int64 b, char *overflow_flag) | 14 | 28 | { | | 29 | +#ifdef HAVE___BUILTIN_ADD_OVERFLOW | | 30 | + npy_int64 result; | | 31 | + if (__builtin_add_overflow(a, b, &result)) { | | 32 | + *overflow_flag = 1; | | 33 | + } | | 34 | + return result; | | 35 | +#else | 15 | 36 | if (a > 0 && b > NPY_MAX_INT64 - a) { | 16 | 37 | *overflow_flag = 1; | | 38 | + return 0; | 17 | 39 | } | 18 | | - else if (a < 0 && b < NPY_MIN_INT64 - a) { | | 40 | + if (a < 0 && b < NPY_MIN_INT64 - a) { | 19 | 41 | *overflow_flag = 1; | | 42 | + return 0; | 20 | 43 | } | 21 | 44 | return a + b; | | 45 | +#endif | 22 | 46 | } | 23 | 47 |
| 24 | 48 |
| 25 | 49 | /* Integer subtraction with overflow checking */ | 26 | 50 | static inline npy_int64 | 27 | 51 | safe_sub(npy_int64 a, npy_int64 b, char *overflow_flag) | 28 | 52 | { | | 53 | +#ifdef HAVE___BUILTIN_SUB_OVERFLOW | | 54 | + npy_int64 result; | | 55 | + if (__builtin_sub_overflow(a, b, &result)) { | | 56 | + *overflow_flag = 1; | | 57 | + } | | 58 | + return result; | | 59 | +#else | 29 | 60 | if (a >= 0 && b < a - NPY_MAX_INT64) { | 30 | 61 | *overflow_flag = 1; | | 62 | + return 0; | 31 | 63 | } | 32 | | - else if (a < 0 && b > a - NPY_MIN_INT64) { | | 64 | + if (a < 0 && b > a - NPY_MIN_INT64) { | 33 | 65 | *overflow_flag = 1; | | 66 | + return 0; | 34 | 67 | } | 35 | 68 | return a - b; | | 69 | +#endif | 36 | 70 | } | 37 | 71 |
| 38 | 72 |
| 39 | 73 | /* Integer multiplication with overflow checking */ | 40 | 74 | static inline npy_int64 | 41 | 75 | safe_mul(npy_int64 a, npy_int64 b, char *overflow_flag) | 42 | 76 | { | | 77 | +#ifdef HAVE___BUILTIN_MUL_OVERFLOW | | 78 | + npy_int64 result; | | 79 | + if (__builtin_mul_overflow(a, b, &result)) { | | 80 | + *overflow_flag = 1; | | 81 | + } | | 82 | + return result; | | 83 | +#else | 43 | 84 | if (a > 0) { | 44 | 85 | if (b > NPY_MAX_INT64 / a || b < NPY_MIN_INT64 / a) { | 45 | 86 | *overflow_flag = 1; | | 87 | + return 0; | 46 | 88 | } | 47 | 89 | } | 48 | 90 | else if (a < 0) { | 49 | 91 | if (b > 0 && a < NPY_MIN_INT64 / b) { | 50 | 92 | *overflow_flag = 1; | | 93 | + return 0; | 51 | 94 | } | 52 | | - else if (b < 0 && a < NPY_MAX_INT64 / b) { | | 95 | + if (b < 0 && a < NPY_MAX_INT64 / b) { | 53 | 96 | *overflow_flag = 1; | | 97 | + return 0; | 54 | 98 | } | 55 | 99 | } | 56 | 100 | return a * b; | | 101 | +#endif | 57 | 102 | } | 58 | 103 |
| 59 | 104 |
| |
|