This is an automated email from the ASF dual-hosted git repository. xiaoxiang pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 634baa5a2f309812d8f2cc54511254f20d7a2aa7 Author: chao an <anc...@xiaomi.com> AuthorDate: Wed Feb 8 16:15:56 2023 +0800 libs: workaround for Visual Studio(MSVC) Compiler Error C2124 D:\archer\code\nuttx\libs\libc\stdlib\lib_strtod.c: error C2124: divide or mod by zero Windows MSVC restrictions, MSVC doesn't allow division through a zero literal, but allows it through const variable set to zero Reference: https://docs.microsoft.com/en-us/cpp/error-messages/compiler-errors-1/compiler-error-c2124?view=msvc-170 Signed-off-by: chao an <anc...@xiaomi.com> --- include/nuttx/lib/math.h | 18 +++++++++++------- libs/libc/math/lib_gamma.c | 30 +++++++++++++++++++++--------- 2 files changed, 32 insertions(+), 16 deletions(-) diff --git a/include/nuttx/lib/math.h b/include/nuttx/lib/math.h index 3737c1715d..a5469c2e3f 100644 --- a/include/nuttx/lib/math.h +++ b/include/nuttx/lib/math.h @@ -74,15 +74,19 @@ /* General Constants ********************************************************/ -#define INFINITY (1.0/0.0) -#define NAN (0.0/0.0) -#define HUGE_VAL INFINITY +#ifndef _HUGE_ENUF +# define _HUGE_ENUF (1e+300) /* _HUGE_ENUF*_HUGE_ENUF must overflow */ +#endif + +#define INFINITY ((double)(_HUGE_ENUF * _HUGE_ENUF)) +#define NAN ((double)(INFINITY * 0.0F)) +#define HUGE_VAL INFINITY -#define INFINITY_F (1.0F/0.0F) -#define NAN_F (0.0F/0.0F) +#define INFINITY_F ((float)INFINITY) +#define NAN_F ((float)(INFINITY * 0.0F)) -#define INFINITY_L (1.0L/0.0L) -#define NAN_L (0.0L/0.0L) +#define INFINITY_L ((long double)INFINITY) +#define NAN_L ((long double)(INFINITY * 0.0F)) #define isnan(x) ((x) != (x)) #define isnanf(x) ((x) != (x)) diff --git a/libs/libc/math/lib_gamma.c b/libs/libc/math/lib_gamma.c index 60728e5c0e..a93c4629fb 100644 --- a/libs/libc/math/lib_gamma.c +++ b/libs/libc/math/lib_gamma.c @@ -32,9 +32,12 @@ * ****************************************************************************/ -/* "A Precision Approximation of the Gamma Function" - Cornelius Lanczos (1964) - * "Lanczos Implementation of the Gamma Function" - Paul Godfrey (2001) - * "An Analysis of the Lanczos Gamma Approximation" - Glendon Ralph Pugh (2004) +/* "A Precision Approximation of the Gamma Function" + * - Cornelius Lanczos (1964) + * "Lanczos Implementation of the Gamma Function" + * - Paul Godfrey (2001) + * "An Analysis of the Lanczos Gamma Approximation" + * - Glendon Ralph Pugh (2004) * * Approximation method: * @@ -133,9 +136,10 @@ static const double g_sden[N + 1] = static const double g_fact[] = { 1, 1, 2, 6, 24, 120, 720, 5040.0, 40320.0, 362880.0, 3628800.0, 39916800.0, - 479001600.0, 6227020800.0, 87178291200.0, 1307674368000.0, 20922789888000.0, - 355687428096000.0, 6402373705728000.0, 121645100408832000.0, - 2432902008176640000.0, 51090942171709440000.0, 1124000727777607680000.0, + 479001600.0, 6227020800.0, 87178291200.0, 1307674368000.0, + 20922789888000.0, 355687428096000.0, 6402373705728000.0, + 121645100408832000.0, 2432902008176640000.0, 51090942171709440000.0, + 1124000727777607680000.0, }; /* S(x) rational function for positive x */ @@ -151,6 +155,7 @@ static double sinpi(double x) int n; /* argument reduction: x = |x| mod 2 */ + /* spurious inexact when x is odd int */ x = x * 0.5; @@ -205,7 +210,7 @@ static double s(double x) } } - return num/den; + return num / den; } /**************************************************************************** @@ -219,6 +224,7 @@ double tgamma(double x) double f; uint64_t i; } u; + u.f = x; double absx; @@ -241,17 +247,19 @@ double tgamma(double x) if (ix < (0x3ff - 54) << 20) { /* |x| < 2^-54: tgamma(x) ~ 1/x, +-0 raises div-by-zero */ + return 1 / x; } /* integer arguments */ + /* raise inexact when non-integer */ if (x == floor(x)) { if (sign) { - return 0 / 0.0; + return NAN; } if (x <= sizeof g_fact / sizeof *g_fact) @@ -261,6 +269,7 @@ double tgamma(double x) } /* x >= 172: tgamma(x)=inf with overflow */ + /* x =< -184: tgamma(x)=+-0 with underflow */ if (ix >= 0x40670000) @@ -269,11 +278,13 @@ double tgamma(double x) if (sign) { - FORCE_EVAL((float)(0x1p-126 / x)); + FORCE_EVAL((float)(ldexp(1.0, -126) / x)); + if (floor(x) * 0.5 == floor(x * 0.5)) { return 0; } + return -0.0; } @@ -302,6 +313,7 @@ double tgamma(double x) if (x < 0) { /* reflection formula for negative x */ + /* sinpi(absx) is not 0, integers are already handled */ r = -pi / (sinpi(absx) * absx * r);