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 #31379 from charris/backport-31360 · numpy/numpy@467eef5

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • numpy/_core
    • src/npymath
      • npy_math_internal.h.src
    • tests
      • test_umath.py
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎numpy/_core/src/npymath/npy_math_internal.h.src‎

Copy file name to clipboardExpand all lines: numpy/_core/src/npymath/npy_math_internal.h.src
+10-1Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -650,12 +650,21 @@ npy_lcm@c@(@type@ a, @type@ b)
650650
*
651651
* #type = (npy_int, npy_long, npy_longlong)*2#
652652
* #c = (,l,ll)*2#
653+
* #utype = (npy_uint, npy_ulong, npy_ulonglong)*2#
653654
* #func=gcd*3,lcm*3#
654655
*/
655656
NPY_INPLACE @type@
656657
npy_@func@@c@(@type@ a, @type@ b)
657658
{
658-
return npy_@func@u@c@(a < 0 ? -a : a, b < 0 ? -b : b);
659+
/*
660+
* Cast to unsigned to avoid overflow when negating minimum signed value.
661+
* For negative values, cast to unsigned first, then apply negation in
662+
* unsigned arithmetic to avoid undefined behavior.
663+
* This ensures correct behavior across all architectures. #31359
664+
*/
665+
@utype@ a_abs = a < 0 ? (@utype@)(0) - (@utype@)a : (@utype@)a;
666+
@utype@ b_abs = b < 0 ? (@utype@)(0) - (@utype@)b : (@utype@)b;
667+
return (@type@)npy_@func@u@c@(a_abs, b_abs);
659668
}
660669
/**end repeat**/
661670

Collapse file

‎numpy/_core/tests/test_umath.py‎

Copy file name to clipboardExpand all lines: numpy/_core/tests/test_umath.py
+8-4Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4160,13 +4160,17 @@ def test_lcm_overflow(self):
41604160
assert_equal(np.lcm(a, b), 10 * big)
41614161

41624162
def test_gcd_overflow(self):
4163-
for dtype in (np.int32, np.int64):
4164-
# verify that we don't overflow when taking abs(x)
4165-
# not relevant for lcm, where the result is unrepresentable anyway
4166-
a = dtype(np.iinfo(dtype).min) # negative power of two
4163+
# verify that we don't overflow when taking abs(x) for INT_MIN
4164+
# this was undefined behavior that manifested on s390x with GCC 11.5
4165+
for dtype in (np.int8, np.int16, np.int32, np.int64):
4166+
a = dtype(np.iinfo(dtype).min) # INT_MIN
41674167
q = -(a // 4)
4168+
# Test with INT_MIN as first argument
41684169
assert_equal(np.gcd(a, q * 3), q)
41694170
assert_equal(np.gcd(a, -q * 3), q)
4171+
# Test with INT_MIN as second argument
4172+
assert_equal(np.gcd(q * 3, a), q)
4173+
assert_equal(np.gcd(-q * 3, a), q)
41704174

41714175
def test_decimal(self):
41724176
from decimal import Decimal

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.