From 02b1e895e27c0660b9fd5639da713de3605eeaa3 Mon Sep 17 00:00:00 2001 From: Petro Karashchenko Date: Thu, 5 Sep 2024 11:56:45 +0200 Subject: [PATCH] libs/libm/libm: apply epsilon relax factor only if epsilon is small Signed-off-by: Petro Karashchenko --- libs/libm/libm/lib_log.c | 15 +++++---------- libs/libm/libm/lib_logf.c | 19 +++++++------------ 2 files changed, 12 insertions(+), 22 deletions(-) diff --git a/libs/libm/libm/lib_log.c b/libs/libm/libm/lib_log.c index 9b8e23b97d..87a1744972 100644 --- a/libs/libm/libm/lib_log.c +++ b/libs/libm/libm/lib_log.c @@ -64,15 +64,15 @@ double log(double x) double y_old; double ey; double epsilon; - double relax_factor; + double rf; /* epsilon relax factor */ int iter; y = 0.0; y_old = 1.0; epsilon = DBL_EPSILON; - iter = 0; - relax_factor = 1.0; + iter = 0; + rf = 1.0; while (y > y_old + epsilon || y < y_old - epsilon) { @@ -90,18 +90,13 @@ double log(double x) y = -DBL_MAX_EXP_X; } - epsilon = (fabs(y) > 1.0) ? fabs(y) * DBL_EPSILON : DBL_EPSILON; + epsilon = ((fabs(y) > rf) ? fabs(y) : rf) * DBL_EPSILON; if (++iter >= LOG_MAX_ITER) { - relax_factor *= LOG_RELAX_MULTIPLIER; + rf *= LOG_RELAX_MULTIPLIER; iter = 0; } - - if (relax_factor > 1.0) - { - epsilon *= relax_factor; - } } if (y == DBL_MAX_EXP_X) diff --git a/libs/libm/libm/lib_logf.c b/libs/libm/libm/lib_logf.c index 35672915b4..73b0afbaf1 100644 --- a/libs/libm/libm/lib_logf.c +++ b/libs/libm/libm/lib_logf.c @@ -60,15 +60,15 @@ float logf(float x) float y_old; float ey; float epsilon; - float relax_factor; + float rf; /* epsilon relax factor */ int iter; - y = 0.0F; - y_old = 1.0F; + y = 0.0F; + y_old = 1.0F; epsilon = FLT_EPSILON; - iter = 0; - relax_factor = 1.0F; + iter = 0; + rf = 1.0F; while (y > y_old + epsilon || y < y_old - epsilon) { @@ -86,18 +86,13 @@ float logf(float x) y = -FLT_MAX_EXP_X; } - epsilon = (fabsf(y) > 1.0F) ? fabsf(y) * FLT_EPSILON : FLT_EPSILON; + epsilon = ((fabsf(y) > rf) ? fabsf(y) : rf) * FLT_EPSILON; if (++iter >= LOGF_MAX_ITER) { - relax_factor *= LOGF_RELAX_MULTIPLIER; + rf *= LOGF_RELAX_MULTIPLIER; iter = 0; } - - if (relax_factor > 1.0F) - { - epsilon *= relax_factor; - } } if (y == FLT_MAX_EXP_X)