mirror of
git://sourceware.org/git/newlib-cygwin.git
synced 2025-01-19 12:59:21 +08:00
ca7b4bd236
cc Aldy Hernandez <aldyh@redhat.com> and Andrew MacLeod <amacleod@redhat.com>, they are author of new VRP analysis for GCC, just to make sure I didn't mis-understanding or mis-interpreting anything on GCC site. GCC 11 have better value range analysis, that give GCC more confidence to perform more aggressive optimization, but it cause scalbn/scalbnf get wrong result. Using scalbn to demostrate what happened on GCC 11, see comments with VRP prefix: ```c double scalbn (double x, int n) { /* VRP RESULT: n = [-INF, +INF] */ __int32_t k,hx,lx; ... k = (hx&0x7ff00000)>>20; /* VRP RESULT: k = [0, 2047] */ if (k==0) { /* VRP RESULT: k = 0 */ ... k = ((hx&0x7ff00000)>>20) - 54; if (n< -50000) return tiny*x; /*underflow*/ /* VRP RESULT: k = -54 */ } /* VRP RESULT: k = [-54, 2047] */ if (k==0x7ff) return x+x; /* NaN or Inf */ /* VRP RESULT: k = [-54, 2046] */ k = k+n; if (k > 0x7fe) return huge*copysign(huge,x); /* overflow */ /* VRP RESULT: k = [-INF, 2046] */ /* VRP RESULT: n = [-INF, 2100], because k + n <= 0x7fe is false, so: 1. -INF < [-54, 2046] + n <= 0x7fe(2046) < INF 2. -INF < [-54, 2046] + n <= 2046 < INF 3. -INF < n <= 2046 - [-54, 2046] < INF 4. -INF < n <= [0, 2100] < INF 5. n = [-INF, 2100] */ if (k > 0) /* normal result */ {SET_HIGH_WORD(x,(hx&0x800fffff)|(k<<20)); return x;} if (k <= -54) { /* VRP OPT: Evaluate n > 50000 as true...*/ if (n > 50000) /* in case integer overflow in n+k */ return huge*copysign(huge,x); /*overflow*/ else return tiny*copysign(tiny,x); /*underflow*/ } k += 54; /* subnormal result */ SET_HIGH_WORD(x,(hx&0x800fffff)|(k<<20)); return x*twom54; } ``` However give the input n = INT32_MAX, k = k+n will overflow, and then we expect got `huge*copysign(huge,x)`, but new VRP optimization think `n > 50000` is never be true, so optimize that into `tiny*copysign(tiny,x)`. so the solution here is to moving the overflow handle logic before `k = k + n`.
86 lines
2.1 KiB
C
86 lines
2.1 KiB
C
/* sf_scalbn.c -- float version of s_scalbn.c.
|
|
* Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
|
|
*/
|
|
|
|
/*
|
|
* ====================================================
|
|
* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
|
|
*
|
|
* Developed at SunPro, a Sun Microsystems, Inc. business.
|
|
* Permission to use, copy, modify, and distribute this
|
|
* software is freely granted, provided that this notice
|
|
* is preserved.
|
|
* ====================================================
|
|
*/
|
|
|
|
#include "fdlibm.h"
|
|
#include <limits.h>
|
|
#include <float.h>
|
|
|
|
#if INT_MAX > 50000
|
|
#define OVERFLOW_INT 50000
|
|
#else
|
|
#define OVERFLOW_INT 30000
|
|
#endif
|
|
|
|
#ifdef __STDC__
|
|
static const float
|
|
#else
|
|
static float
|
|
#endif
|
|
two25 = 3.355443200e+07, /* 0x4c000000 */
|
|
twom25 = 2.9802322388e-08, /* 0x33000000 */
|
|
huge = 1.0e+30,
|
|
tiny = 1.0e-30;
|
|
|
|
#ifdef __STDC__
|
|
float scalbnf (float x, int n)
|
|
#else
|
|
float scalbnf (x,n)
|
|
float x; int n;
|
|
#endif
|
|
{
|
|
__int32_t k,ix;
|
|
__uint32_t hx;
|
|
|
|
GET_FLOAT_WORD(ix,x);
|
|
hx = ix&0x7fffffff;
|
|
k = hx>>23; /* extract exponent */
|
|
if (FLT_UWORD_IS_ZERO(hx))
|
|
return x;
|
|
if (!FLT_UWORD_IS_FINITE(hx))
|
|
return x+x; /* NaN or Inf */
|
|
if (FLT_UWORD_IS_SUBNORMAL(hx)) {
|
|
x *= two25;
|
|
GET_FLOAT_WORD(ix,x);
|
|
k = ((ix&0x7f800000)>>23) - 25;
|
|
if (n< -50000) return tiny*x; /*underflow*/
|
|
}
|
|
if (n > OVERFLOW_INT) /* in case integer overflow in n+k */
|
|
return huge*copysignf(huge,x); /*overflow*/
|
|
k = k+n;
|
|
if (k > FLT_LARGEST_EXP) return huge*copysignf(huge,x); /* overflow */
|
|
if (k > 0) /* normal result */
|
|
{SET_FLOAT_WORD(x,(ix&0x807fffff)|(k<<23)); return x;}
|
|
if (k < FLT_SMALLEST_EXP)
|
|
return tiny*copysignf(tiny,x); /*underflow*/
|
|
k += 25; /* subnormal result */
|
|
SET_FLOAT_WORD(x,(ix&0x807fffff)|(k<<23));
|
|
return x*twom25;
|
|
}
|
|
|
|
#ifdef _DOUBLE_IS_32BITS
|
|
|
|
#ifdef __STDC__
|
|
double scalbn(double x, int n)
|
|
#else
|
|
double scalbn(x,n)
|
|
double x;
|
|
int n;
|
|
#endif
|
|
{
|
|
return (double) scalbnf((float) x, n);
|
|
}
|
|
|
|
#endif /* defined(_DOUBLE_IS_32BITS) */
|