compacted duplicate code into inline functions

This commit is contained in:
hyunghwan.chung 2019-02-19 14:19:31 +00:00
parent 4a6428445b
commit fe6c693b86
2 changed files with 54 additions and 32 deletions

View File

@ -257,8 +257,5 @@ extend MyObject
tb := tc at: idx. tb := tc at: idx.
System log(System.Log.INFO, idx asString, (if (tb value) { ' PASS' } else { ' FAIL' }), S'\n'). System log(System.Log.INFO, idx asString, (if (tb value) { ' PASS' } else { ' FAIL' }), S'\n').
]. ].
6000p99 dump.
} }
} }

View File

@ -87,6 +87,42 @@ moo_oop_t moo_makefpdec (moo_t* moo, moo_oop_t value, moo_ooi_t scale)
return (moo_oop_t)fpdec; return (moo_oop_t)fpdec;
} }
static MOO_INLINE moo_oop_t multiply_by_tens (moo_t* moo, moo_oop_t v, moo_oow_t count)
{
moo_oow_t dec;
MOO_ASSERT (moo, count > 0);
do
{
dec = (count >= MOO_COUNTOF(pow_of_ten) - 1)? (MOO_COUNTOF(pow_of_ten) - 1): count;
v = moo_mulints(moo, v, MOO_SMOOI_TO_OOP(pow_of_ten[dec]));
if (!v) return MOO_NULL;
count -= dec;
}
while (count > 0);
return v;
}
static MOO_INLINE moo_oop_t divide_by_tens (moo_t* moo, moo_oop_t v, moo_oow_t count)
{
moo_oow_t dec;
MOO_ASSERT (moo, count > 0);
do
{
dec = (count >= MOO_COUNTOF(pow_of_ten) - 1)? (MOO_COUNTOF(pow_of_ten) - 1): count;
v = moo_divints(moo, v, MOO_SMOOI_TO_OOP(pow_of_ten[dec]), 0, MOO_NULL);
if (!v) return MOO_NULL;
count -= dec;
}
while (count > 0);
return v;
}
static moo_ooi_t equalize_scale (moo_t* moo, moo_oop_t* x, moo_oop_t* y) static moo_ooi_t equalize_scale (moo_t* moo, moo_oop_t* x, moo_oop_t* y)
{ {
moo_ooi_t xs, ys; moo_ooi_t xs, ys;
@ -123,14 +159,19 @@ static moo_ooi_t equalize_scale (moo_t* moo, moo_oop_t* x, moo_oop_t* y)
if (xs < ys) if (xs < ys)
{ {
#if 0
nv = xv; nv = xv;
while (xs < ys) while (xs < ys)
{ {
/* TODO: optmize this. less multiplications */
nv = moo_mulints(moo, nv, MOO_SMOOI_TO_OOP(10)); nv = moo_mulints(moo, nv, MOO_SMOOI_TO_OOP(10));
if (!nv) return -1; if (!nv) return -1;
xs++; xs++;
} }
#else
nv = multiply_by_tens(moo, xv, ys - xs);
if (!nv) return -1;
xs = ys;
#endif
nv = moo_makefpdec(moo, nv, xs); nv = moo_makefpdec(moo, nv, xs);
if (!nv) return -1; if (!nv) return -1;
@ -139,6 +180,7 @@ static moo_ooi_t equalize_scale (moo_t* moo, moo_oop_t* x, moo_oop_t* y)
} }
else if (xs > ys) else if (xs > ys)
{ {
#if 0
nv = yv; nv = yv;
while (ys < xs) while (ys < xs)
{ {
@ -146,6 +188,11 @@ static moo_ooi_t equalize_scale (moo_t* moo, moo_oop_t* x, moo_oop_t* y)
if (!nv) return -1; if (!nv) return -1;
ys++; ys++;
} }
#else
nv = multiply_by_tens(moo, yv, xs - ys);
if (!nv) return -1;
ys = xs;
#endif
nv = moo_makefpdec(moo, nv, ys); nv = moo_makefpdec(moo, nv, ys);
if (!nv) return -1; if (!nv) return -1;
@ -168,22 +215,14 @@ moo_oop_t moo_truncfpdecval (moo_t* moo, moo_oop_t iv, moo_ooi_t cs, moo_ooi_t n
#if 0 #if 0
do do
{ {
/* TODO: optimization... less divisions */
iv = moo_divints(moo, iv, MOO_SMOOI_TO_OOP(10), 0, MOO_NULL); iv = moo_divints(moo, iv, MOO_SMOOI_TO_OOP(10), 0, MOO_NULL);
if (!iv) return MOO_NULL; if (!iv) return MOO_NULL;
cs--; cs--;
} }
while (cs > ns); while (cs > ns);
#else #else
moo_oow_t dec, rem = cs - ns; iv = divide_by_tens(moo, iv, cs - ns);
do /* if (!iv) return MOO_NULL; */
{
dec = (rem >= MOO_COUNTOF(pow_of_ten) - 1)? (MOO_COUNTOF(pow_of_ten) - 1): rem;
iv = moo_divints(moo, iv, MOO_SMOOI_TO_OOP(pow_of_ten[dec]), 0, MOO_NULL);
if (!iv) return MOO_NULL;
rem -= dec;
}
while (rem > 0);
#endif #endif
} }
@ -228,15 +267,8 @@ moo_oop_t moo_truncfpdec (moo_t* moo, moo_oop_t x, moo_ooi_t ns)
} }
while (cs > ns); while (cs > ns);
#else #else
moo_oow_t dec, rem = cs - ns; xv = divide_by_tens (moo, xv, cs - ns);
do if (!xv) return MOO_NULL;
{
dec = (rem >= MOO_COUNTOF(pow_of_ten) - 1)? (MOO_COUNTOF(pow_of_ten) - 1): rem;
xv = moo_divints(moo, xv, MOO_SMOOI_TO_OOP(pow_of_ten[dec]), 0, MOO_NULL);
if (!xv) return MOO_NULL;
rem -= dec;
}
while (rem > 0);
#endif #endif
} }
else /*if (cs < ns)*/ else /*if (cs < ns)*/
@ -250,15 +282,8 @@ moo_oop_t moo_truncfpdec (moo_t* moo, moo_oop_t x, moo_ooi_t ns)
} }
while (cs < ns); while (cs < ns);
#else #else
moo_oow_t dec, rem = ns - cs; xv = multiply_by_tens (moo, xv, ns - cs);
do if (!xv) return MOO_NULL;
{
dec = (rem >= MOO_COUNTOF(pow_of_ten) - 1)? (MOO_COUNTOF(pow_of_ten) - 1): rem;
xv = moo_mulints(moo, xv, MOO_SMOOI_TO_OOP(pow_of_ten[dec]));
if (!xv) return MOO_NULL;
rem -= dec;
}
while (rem > 0);
#endif #endif
} }