GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Merge pull request #31401 from charris/backport-31396 · numpy/numpy@a42bd48

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • numpy/_core
    • config.h.in
    • meson.build
    • src/common
      • npy_extint128.h
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎numpy/_core/config.h.in‎

Copy file name to clipboardExpand all lines: numpy/_core/config.h.in
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
#mesondefine HAVE___BUILTIN_BSWAP64
3333
#mesondefine HAVE___BUILTIN_EXPECT
3434
#mesondefine HAVE___BUILTIN_MUL_OVERFLOW
35+
#mesondefine HAVE___BUILTIN_ADD_OVERFLOW
36+
#mesondefine HAVE___BUILTIN_SUB_OVERFLOW
3537
#mesondefine HAVE___BUILTIN_PREFETCH
3638

3739
#mesondefine HAVE_ATTRIBUTE_OPTIMIZE_UNROLL_LOOPS
Collapse file

‎numpy/_core/meson.build‎

Copy file name to clipboardExpand all lines: numpy/_core/meson.build
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,8 @@ optional_intrinsics = [
401401
['__builtin_expect', '5, 0', [], []],
402402
# Test `long long` for arm+clang 13 (gh-22811, but we use all versions):
403403
['__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', [], []],
404406
['__builtin_prefetch', '(float*)0, 0, 3', [], []],
405407
]
406408
foreach intrin: optional_intrinsics
Collapse file

‎numpy/_core/src/common/npy_extint128.h‎

Copy file name to clipboardExpand all lines: numpy/_core/src/common/npy_extint128.h
+48-3Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,52 +8,97 @@ typedef struct {
88
} npy_extint128_t;
99

1010

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+
1125
/* Integer addition with overflow checking */
1226
static inline npy_int64
1327
safe_add(npy_int64 a, npy_int64 b, char *overflow_flag)
1428
{
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
1536
if (a > 0 && b > NPY_MAX_INT64 - a) {
1637
*overflow_flag = 1;
38+
return 0;
1739
}
18-
else if (a < 0 && b < NPY_MIN_INT64 - a) {
40+
if (a < 0 && b < NPY_MIN_INT64 - a) {
1941
*overflow_flag = 1;
42+
return 0;
2043
}
2144
return a + b;
45+
#endif
2246
}
2347

2448

2549
/* Integer subtraction with overflow checking */
2650
static inline npy_int64
2751
safe_sub(npy_int64 a, npy_int64 b, char *overflow_flag)
2852
{
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
2960
if (a >= 0 && b < a - NPY_MAX_INT64) {
3061
*overflow_flag = 1;
62+
return 0;
3163
}
32-
else if (a < 0 && b > a - NPY_MIN_INT64) {
64+
if (a < 0 && b > a - NPY_MIN_INT64) {
3365
*overflow_flag = 1;
66+
return 0;
3467
}
3568
return a - b;
69+
#endif
3670
}
3771

3872

3973
/* Integer multiplication with overflow checking */
4074
static inline npy_int64
4175
safe_mul(npy_int64 a, npy_int64 b, char *overflow_flag)
4276
{
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
4384
if (a > 0) {
4485
if (b > NPY_MAX_INT64 / a || b < NPY_MIN_INT64 / a) {
4586
*overflow_flag = 1;
87+
return 0;
4688
}
4789
}
4890
else if (a < 0) {
4991
if (b > 0 && a < NPY_MIN_INT64 / b) {
5092
*overflow_flag = 1;
93+
return 0;
5194
}
52-
else if (b < 0 && a < NPY_MAX_INT64 / b) {
95+
if (b < 0 && a < NPY_MAX_INT64 / b) {
5396
*overflow_flag = 1;
97+
return 0;
5498
}
5599
}
56100
return a * b;
101+
#endif
57102
}
58103

59104

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.