Skip to content

Commit

Permalink
libs/libm/libm: apply epsilon relax factor only if epsilon is small
Browse files Browse the repository at this point in the history
Signed-off-by: Petro Karashchenko <petro.karashchenko@gmail.com>
  • Loading branch information
pkarashchenko committed Sep 5, 2024
1 parent 06f59e6 commit 327d24c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 22 deletions.
15 changes: 5 additions & 10 deletions libs/libm/libm/lib_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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)
Expand Down
19 changes: 7 additions & 12 deletions libs/libm/libm/lib_logf.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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)
Expand Down

0 comments on commit 327d24c

Please sign in to comment.