From nobody Thu Aug 21 10:43:32 2025 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4c70Kc5Xk0z64gl1; Thu, 21 Aug 2025 10:43:32 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R12" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4c70Kc49Xrz3Svc; Thu, 21 Aug 2025 10:43:32 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1755773012; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=P69n9WS9OKLxlvbxLB3gJAJ39UyqnCJVYm3SrPpHdb8=; b=kOLloYXW+2uRMtPYK/CyejwYp/M9QNBgKsLUcvv4r/bif5VROPbYZOYbhE48UL1V3+YLIO ASry9nmDXtJzjrjoxFF+KXujztl8ji158Fefdzk1ix7vM9w+ksVhqya9URkVALfuW12NjB sdsXVwyKsOStRxEL3O+PqNx44Lm4twWAMS5W16XyupOjF0eCvHR5OmJxMD4XltZum4B8/0 5de2n/M/cKzzUJdqPBIPwvuY0ogMCUeNwmixTIYEdDUlhegcWJigSmjcMhg5Gnug3/tvD/ NMBGWO+K01lRoEsTzXc1muicWWt60/I9BoQTh35ZBjem/GqEHHGAdMfUYW+PBQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1755773012; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=P69n9WS9OKLxlvbxLB3gJAJ39UyqnCJVYm3SrPpHdb8=; b=t+vEDNcyrBceB8XLOfFqbLlgf8bMUzUq3eKTTnRK38aDRx1bfJrHQnYxRP709tiWV8j+P9 OZ6yHijR1/GssC5wS6Q/U5z+gZxL6XUmdzDida/FF4szWtcXv/A0vatDu4TIUe8m0lf8gA x3Ybyridoifd36hMmvXHTiaHLoAV5befw2UiUfc+7k0dsKE9FPFNWvlL/FTRHkgJxils7i dTXEWDHYWI9auzjGJ6HUUy6emyD44RQajiJhZvSEI90cQW5KcfVTp3diPgFCojVWrtPOqQ ZEHelcOnaVBh2LNGQ2bO8nFlBOfVtNh3XVicFrAcVFxA32kjZ7Rj2SQLLXb7eA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1755773012; a=rsa-sha256; cv=none; b=t0cFdfhchXJ4gGgOoGrCPaHQnWKZilCOaxvcZDIM04L25g25SpwP+cPnEldBNJQNbyZHzn HaiXWPCr2vs5mQyWFg9joDJr5QF7Np7ALpg0XNMo2pJs2ACAJHh93WLt9cyuuGTkEQ/iZw 4VSD4MebldiJL/l+Fa0PgSHvc4Lbf+jAGJz3oTKunHhsY3kXUS96BLrPBAAL7hL0AJaXNv Yfo8ZpITp67Ze6kJmBLqzZGjNEKj+NF27CE9XeD/W1Vu9LYwucjeG9iW0oxm5wfAOS4hMQ 49t4fBF5orCpgndTBAauTlevpZvGpRm8imFemJGEXAQq9oWDK/KfCWSjFtmAQg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4c70Kc3mTHzwcB; Thu, 21 Aug 2025 10:43:32 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 57LAhWd1075703; Thu, 21 Aug 2025 10:43:32 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 57LAhWc7075700; Thu, 21 Aug 2025 10:43:32 GMT (envelope-from git) Date: Thu, 21 Aug 2025 10:43:32 GMT Message-Id: <202508211043.57LAhWc7075700@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Dimitry Andric Subject: git: 94b09b2f3440 - stable/13 - [libm] Fix undefined behavior of a left shifted of a signed integer List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: dim X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: 94b09b2f3440b16b58dd744fcaa73229f6503c10 Auto-Submitted: auto-generated The branch stable/13 has been updated by dim: URL: https://cgit.FreeBSD.org/src/commit/?id=94b09b2f3440b16b58dd744fcaa73229f6503c10 commit 94b09b2f3440b16b58dd744fcaa73229f6503c10 Author: Steve Kargl AuthorDate: 2025-08-12 04:26:29 +0000 Commit: Dimitry Andric CommitDate: 2025-08-21 10:41:41 +0000 [libm] Fix undefined behavior of a left shifted of a signed integer The patch fixes a few instances of left shifts on signed integer entities. A 'static inline' helper function 'subnormal_ilogb()' has been added to math_private.h. This function is then used e_fmod.c, s_ilogb(), and s_remquo.c. The change in s_remquo.c has only been compile tested. The change to e_fmod.c has been test on over 3 billion pairs of subnormal numbers where testing included x > y and x < y pairs. The test compared the output from fmod() with the output from mpfr_fmod() from MPFR. There were no difference. The change to s_ilogb() has had limited testing where its output was compared against frexp(). In this testing, no differences in output were detected. PR: 288778 MFC after: 1 week (cherry picked from commit d180086e6eae2e152e803ed6cf13775a7c006dc7) --- lib/msun/src/e_fmod.c | 30 ++++++++++++------------------ lib/msun/src/math_private.h | 21 +++++++++++++++++++++ lib/msun/src/s_ilogb.c | 13 +++++-------- lib/msun/src/s_remquo.c | 30 ++++++++++++------------------ 4 files changed, 50 insertions(+), 44 deletions(-) diff --git a/lib/msun/src/e_fmod.c b/lib/msun/src/e_fmod.c index fdfb56c2a475..973ab2ef30c4 100644 --- a/lib/msun/src/e_fmod.c +++ b/lib/msun/src/e_fmod.c @@ -28,14 +28,14 @@ static const double one = 1.0, Zero[] = {0.0, -0.0,}; double fmod(double x, double y) { - int32_t n,hx,hy,hz,ix,iy,sx,i; - u_int32_t lx,ly,lz; + int32_t hx, hy, hz, ix, iy, n, sx; + u_int32_t lx, ly, lz; EXTRACT_WORDS(hx,lx,x); EXTRACT_WORDS(hy,ly,y); sx = hx&0x80000000; /* sign of x */ - hx ^=sx; /* |x| */ - hy &= 0x7fffffff; /* |y| */ + hx ^= sx; /* |x| */ + hy &= 0x7fffffff; /* |y| */ /* purge off exception values */ if((hy|ly)==0||(hx>=0x7ff00000)|| /* y=0,or x not finite */ @@ -48,22 +48,16 @@ fmod(double x, double y) } /* determine ix = ilogb(x) */ - if(hx<0x00100000) { /* subnormal x */ - if(hx==0) { - for (ix = -1043, i=lx; i>0; i<<=1) ix -=1; - } else { - for (ix = -1022,i=(hx<<11); i>0; i<<=1) ix -=1; - } - } else ix = (hx>>20)-1023; + if(hx<0x00100000) + ix = subnormal_ilogb(hx, lx); + else + ix = (hx>>20)-1023; /* determine iy = ilogb(y) */ - if(hy<0x00100000) { /* subnormal y */ - if(hy==0) { - for (iy = -1043, i=ly; i>0; i<<=1) iy -=1; - } else { - for (iy = -1022,i=(hy<<11); i>0; i<<=1) iy -=1; - } - } else iy = (hy>>20)-1023; + if(hy<0x00100000) + iy = subnormal_ilogb(hy, ly); + else + iy = (hy>>20)-1023; /* set up {hx,lx}, {hy,ly} and align y to x */ if(ix >= -1022) diff --git a/lib/msun/src/math_private.h b/lib/msun/src/math_private.h index b71a8d03c0a1..8023cdf05857 100644 --- a/lib/msun/src/math_private.h +++ b/lib/msun/src/math_private.h @@ -740,6 +740,27 @@ irintl(long double x) (ar) = (x) - (ai); \ } while (0) +/* + * For a double entity split into high and low parts, compute ilogb. + */ +static inline int32_t +subnormal_ilogb(int32_t hi, int32_t lo) +{ + int32_t j; + uint32_t i; + + j = -1022; + if (hi == 0) { + j -= 21; + i = (uint32_t)lo; + } else + i = (uint32_t)hi << 11; + + for (; i < 0x7fffffff; i <<= 1) j -= 1; + + return (j); +} + #ifdef DEBUG #if defined(__amd64__) || defined(__i386__) #define breakpoint() asm("int $3") diff --git a/lib/msun/src/s_ilogb.c b/lib/msun/src/s_ilogb.c index 0b076edbd9b5..a958182a2a35 100644 --- a/lib/msun/src/s_ilogb.c +++ b/lib/msun/src/s_ilogb.c @@ -23,21 +23,18 @@ #include "math.h" #include "math_private.h" - int ilogb(double x) +int +ilogb(double x) { - int32_t hx,lx,ix; + int32_t hx, ix, lx; EXTRACT_WORDS(hx,lx,x); hx &= 0x7fffffff; if(hx<0x00100000) { if((hx|lx)==0) return FP_ILOGB0; - else /* subnormal x */ - if(hx==0) { - for (ix = -1043; lx>0; lx<<=1) ix -=1; - } else { - for (ix = -1022,hx<<=11; hx>0; hx<<=1) ix -=1; - } + else + ix = subnormal_ilogb(hx, lx); return ix; } else if (hx<0x7ff00000) return (hx>>20)-1023; diff --git a/lib/msun/src/s_remquo.c b/lib/msun/src/s_remquo.c index e3aac25230e0..fba788ed5104 100644 --- a/lib/msun/src/s_remquo.c +++ b/lib/msun/src/s_remquo.c @@ -5,7 +5,7 @@ * * Developed at SunSoft, a Sun Microsystems, Inc. business. * Permission to use, copy, modify, and distribute this - * software is freely granted, provided that this notice + * software is freely granted, provided that this notice * is preserved. * ==================================================== */ @@ -29,7 +29,7 @@ static const double Zero[] = {0.0, -0.0,}; double remquo(double x, double y, int *quo) { - int32_t n,hx,hy,hz,ix,iy,sx,i; + int32_t hx,hy,hz,ix,iy,n,sx; u_int32_t lx,ly,lz,q,sxy; EXTRACT_WORDS(hx,lx,x); @@ -55,25 +55,19 @@ remquo(double x, double y, int *quo) } /* determine ix = ilogb(x) */ - if(hx<0x00100000) { /* subnormal x */ - if(hx==0) { - for (ix = -1043, i=lx; i>0; i<<=1) ix -=1; - } else { - for (ix = -1022,i=(hx<<11); i>0; i<<=1) ix -=1; - } - } else ix = (hx>>20)-1023; + if(hx<0x00100000) + ix = subnormal_ilogb(hx, lx); + else + ix = (hx>>20)-1023; /* determine iy = ilogb(y) */ - if(hy<0x00100000) { /* subnormal y */ - if(hy==0) { - for (iy = -1043, i=ly; i>0; i<<=1) iy -=1; - } else { - for (iy = -1022,i=(hy<<11); i>0; i<<=1) iy -=1; - } - } else iy = (hy>>20)-1023; + if(hy<0x00100000) + iy = subnormal_ilogb(hy, ly); + else + iy = (hy>>20)-1023; /* set up {hx,lx}, {hy,ly} and align y to x */ - if(ix >= -1022) + if(ix >= -1022) hx = 0x00100000|(0x000fffff&hx); else { /* subnormal x, shift x to normal */ n = -1022-ix; @@ -85,7 +79,7 @@ remquo(double x, double y, int *quo) lx = 0; } } - if(iy >= -1022) + if(iy >= -1022) hy = 0x00100000|(0x000fffff&hy); else { /* subnormal y, shift y to normal */ n = -1022-iy;