[Gmp-commit] /var/hg/gmp: 2 new changesets

mercurial at gmplib.org mercurial at gmplib.org
Thu Jul 20 12:45:00 CEST 2023


details:   /var/hg/gmp/rev/968d773832ab
changeset: 18396:968d773832ab
user:      Niels Möller <nisse at lysator.liu.se>
date:      Thu Jul 20 12:39:03 2023 +0200
description:
mini-gmp: New macro gmp_umullo_limb, to avoid promotion to signed int..

details:   /var/hg/gmp/rev/6741673d1679
changeset: 18397:6741673d1679
user:      Niels Möller <nisse at lysator.liu.se>
date:      Thu Jul 20 12:44:33 2023 +0200
description:
Trivial merge.

diffstat:

 config.guess        |  18 +++++++++---------
 mini-gmp/ChangeLog  |   9 +++++++++
 mini-gmp/mini-gmp.c |  11 +++++++++--
 3 files changed, 27 insertions(+), 11 deletions(-)

diffs (85 lines):

diff -r 73d9ef70d14f -r 6741673d1679 config.guess
--- a/config.guess	Wed Jul 19 11:44:37 2023 +0200
+++ b/config.guess	Thu Jul 20 12:44:33 2023 +0200
@@ -700,23 +700,23 @@
   for prtconfopt in "" "-vp"; do
     if test -z "$exact_cpu"; then
       if $SHELL -c "/usr/sbin/prtconf $prtconfopt" 2>/dev/null >$dummy; then
-	if grep 'SUNW,UltraSPARC-T5' $dummy >/dev/null; then
+	if egrep '(SUNW|ORCL),(Ultra)?SPARC-T5' $dummy >/dev/null; then
 	  exact_cpu=ultrasparct5
-	elif grep 'SUNW,UltraSPARC-T4' $dummy >/dev/null; then
+	elif egrep '(SUNW|ORCL),(Ultra)?SPARC-T4' $dummy >/dev/null; then
 	  exact_cpu=ultrasparct4
-	elif grep 'SUNW,UltraSPARC-T3' $dummy >/dev/null; then
+	elif egrep '(SUNW|ORCL),(Ultra)?SPARC-T3' $dummy >/dev/null; then
 	  exact_cpu=ultrasparct3
-	elif grep 'SUNW,UltraSPARC-T2' $dummy >/dev/null; then
+	elif egrep '(SUNW|ORCL),(Ultra)?SPARC-T2' $dummy >/dev/null; then
 	  exact_cpu=ultrasparct2
-	elif grep 'SUNW,UltraSPARC-T1' $dummy >/dev/null; then
+	elif egrep '(SUNW|ORCL),(Ultra)?SPARC-T1' $dummy >/dev/null; then
 	  exact_cpu=ultrasparct1
-	elif grep 'SUNW,UltraSPARC-III' $dummy >/dev/null; then
+	elif egrep '(SUNW|ORCL),(Ultra)?SPARC-III' $dummy >/dev/null; then
 	  exact_cpu=ultrasparc3
-	elif grep 'SUNW,UltraSPARC-IIi' $dummy >/dev/null; then
+	elif egrep '(SUNW|ORCL),(Ultra)?SPARC-IIi' $dummy >/dev/null; then
 	  exact_cpu=ultrasparc2i
-	elif grep 'SUNW,UltraSPARC-II' $dummy >/dev/null; then
+	elif egrep '(SUNW|ORCL),(Ultra)?SPARC-II' $dummy >/dev/null; then
 	  exact_cpu=ultrasparc2
-	elif grep 'SUNW,UltraSPARC' $dummy >/dev/null; then
+	elif egrep '(SUNW|ORCL),(Ultra)?SPARC' $dummy >/dev/null; then
 	  exact_cpu=ultrasparc
 	elif grep 'Ross,RT62.' $dummy >/dev/null; then
 	  # RT620, RT625, RT626 hypersparcs (v8).
diff -r 73d9ef70d14f -r 6741673d1679 mini-gmp/ChangeLog
--- a/mini-gmp/ChangeLog	Wed Jul 19 11:44:37 2023 +0200
+++ b/mini-gmp/ChangeLog	Thu Jul 20 12:44:33 2023 +0200
@@ -1,3 +1,12 @@
+2023-07-20  Niels Möller  <nisse at lysator.liu.se>
+
+	* mini-gmp.c (gmp_umullo_limb): New macro, to avoid unintended
+	promotion to signed int, in case mp_limb_t is a size smaller than
+	int. In particular, mp_limb_t configured to be a 16-bit unsigned
+	short, with 32-bit int, leads to undefined signed overflow.
+	Problem reported by Vincent Lefevre.
+	(gmp_udiv_qrnnd_preinv, gmp_udiv_qr_3by2): Use new macro.
+
 2022-09-08 Marco Bodrato <bodrato at mail.dm.unipi.it>
 
 	* tests/t-powm.c: Test some corner cases (e.g. 1^0 mod 1).
diff -r 73d9ef70d14f -r 6741673d1679 mini-gmp/mini-gmp.c
--- a/mini-gmp/mini-gmp.c	Wed Jul 19 11:44:37 2023 +0200
+++ b/mini-gmp/mini-gmp.c	Thu Jul 20 12:44:33 2023 +0200
@@ -172,12 +172,19 @@
     }									\
   } while (0)
 
+/* If mp_limb_t is of size smaller than int, plain u*v implies
+   automatic promotion to *signed* int, and then multiply may overflow
+   and cause undefined behavior. Explicitly cast to unsigned int for
+   that case. */
+#define gmp_umullo_limb(u, v) \
+  ((sizeof(mp_limb_t) >= sizeof(int)) ? (u)*(v) : (unsigned int)(u) * (v))
+
 #define gmp_udiv_qrnnd_preinv(q, r, nh, nl, d, di)			\
   do {									\
     mp_limb_t _qh, _ql, _r, _mask;					\
     gmp_umul_ppmm (_qh, _ql, (nh), (di));				\
     gmp_add_ssaaaa (_qh, _ql, _qh, _ql, (nh) + 1, (nl));		\
-    _r = (nl) - _qh * (d);						\
+    _r = (nl) - gmp_umullo_limb (_qh, (d));				\
     _mask = -(mp_limb_t) (_r > _ql); /* both > and >= are OK */		\
     _qh += _mask;							\
     _r += _mask & (d);							\
@@ -198,7 +205,7 @@
     gmp_add_ssaaaa ((q), _q0, (q), _q0, (n2), (n1));			\
 									\
     /* Compute the two most significant limbs of n - q'd */		\
-    (r1) = (n1) - (d1) * (q);						\
+    (r1) = (n1) - gmp_umullo_limb ((d1), (q));				\
     gmp_sub_ddmmss ((r1), (r0), (r1), (n0), (d1), (d0));		\
     gmp_umul_ppmm (_t1, _t0, (d0), (q));				\
     gmp_sub_ddmmss ((r1), (r0), (r1), (r0), _t1, _t0);			\


More information about the gmp-commit mailing list