aboutsummaryrefslogtreecommitdiff
path: root/src/String/Float.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/String/Float.c')
-rw-r--r--src/String/Float.c122
1 files changed, 61 insertions, 61 deletions
diff --git a/src/String/Float.c b/src/String/Float.c
index 9c4e98c..a8ed44d 100644
--- a/src/String/Float.c
+++ b/src/String/Float.c
@@ -8,7 +8,7 @@
#include <string.h>
-#include "BInt.h"
+#include "Inline/Mpi.h"
/* Common defines */
@@ -31,11 +31,11 @@ struct Buffer
struct DragonState
{
- BInt r;
- BInt s;
- BInt mm;
- BInt mp;
- BInt tmp[5];
+ Mpi r;
+ Mpi s;
+ Mpi mm;
+ Mpi mp;
+ Mpi tmp[5];
long k;
int cutoff;
};
@@ -55,21 +55,21 @@ static void dragonFixup(struct DragonState *state,
/* Account for unqual gaps */
if (f == (((uint64_t)1) << 52))
{
- BIntLsh(&state->mp, 1, &state->mp);
- BIntLsh(&state->r, 1, &state->r);
- BIntLsh(&state->s, 1, &state->s);
+ MpiLsh(&state->mp, 1, &state->mp);
+ MpiLsh(&state->r, 1, &state->r);
+ MpiLsh(&state->s, 1, &state->s);
}
state->k = 0;
/* Burger/Dybvig approach */
#ifndef BH_TWEAK_SHORT_BINT
- state->k = BIntClz((f >> 32) & BINT_MASK);
- state->k += (state->k == 32) ? (BIntClz(f & BINT_MASK)) : (0);
+ state->k = MpiClz((f >> 32) & MPI_MASK);
+ state->k += (state->k == 32) ? (MpiClz(f & MPI_MASK)) : (0);
#else
- state->k = BIntClz((f >> 48) & BINT_MASK);
- state->k += (state->k == 16) ? (BIntClz((f >> 32) & BINT_MASK)) : (0);
- state->k += (state->k == 32) ? (BIntClz((f >> 16) & BINT_MASK)) : (0);
- state->k += (state->k == 48) ? (BIntClz(f & BINT_MASK)) : (0);
+ state->k = MpiClz((f >> 48) & MPI_MASK);
+ state->k += (state->k == 16) ? (MpiClz((f >> 32) & MPI_MASK)) : (0);
+ state->k += (state->k == 32) ? (MpiClz((f >> 16) & MPI_MASK)) : (0);
+ state->k += (state->k == 48) ? (MpiClz(f & MPI_MASK)) : (0);
#endif
/* 77 / 256 is an approximation for Log(2) or 0.30102999 */
@@ -82,18 +82,18 @@ static void dragonFixup(struct DragonState *state,
/* Scale numbers accordinaly */
if (state->k < 0)
{
- BIntPow10(&state->r, -state->k, &state->r, state->tmp);
- BIntPow10(&state->mm, -state->k, &state->mm, state->tmp);
- BIntPow10(&state->mp, -state->k, &state->mp, state->tmp);
+ MpiPow10(&state->r, -state->k, &state->r, state->tmp);
+ MpiPow10(&state->mm, -state->k, &state->mm, state->tmp);
+ MpiPow10(&state->mp, -state->k, &state->mp, state->tmp);
}
else if (state->k > 0)
- BIntPow10(&state->s, state->k, &state->s, state->tmp);
+ MpiPow10(&state->s, state->k, &state->s, state->tmp);
/* Scale S if we underestimated */
- if (BIntCompare(&state->r, &state->s) >= 0)
+ if (MpiCompare(&state->r, &state->s) >= 0)
{
state->k += 1;
- BIntMulDigit(&state->s, 10, &state->s);
+ MpiMulDigit(&state->s, 10, &state->s);
}
/* Find cutoff */
@@ -122,8 +122,8 @@ static void dragonRound(struct DragonState *state,
/* Check if rounding up required */
if (high == low)
{
- BIntLsh(&state->r, 1, &state->tmp[0]);
- i = BIntCompare(&state->tmp[0], &state->s);
+ MpiLsh(&state->r, 1, &state->tmp[0]);
+ i = MpiCompare(&state->tmp[0], &state->s);
if (i < 0) { low = 1; high = 0; }
else if (i > 0) { low = 0; high = 1; }
else low = (((s - '0') & 0x1) == 0);
@@ -175,22 +175,22 @@ static void dragon(double value,
/* Prepare dragon */
f = frexp(value, &e) * ((uint64_t)1 << 53);
#ifndef BH_TWEAK_SHORT_BINT
- state.r.data[0] = f & BINT_MASK;
- state.r.data[1] = (f >> 32) & BINT_MASK;
+ state.r.data[0] = f & MPI_MASK;
+ state.r.data[1] = (f >> 32) & MPI_MASK;
state.r.size = 2;
#else
- state.r.data[0] = f & BINT_MASK;
- state.r.data[1] = (f >> 16) & BINT_MASK;
- state.r.data[2] = (f >> 32) & BINT_MASK;
- state.r.data[3] = (f >> 48) & BINT_MASK;
+ state.r.data[0] = f & MPI_MASK;
+ state.r.data[1] = (f >> 16) & MPI_MASK;
+ state.r.data[2] = (f >> 32) & MPI_MASK;
+ state.r.data[3] = (f >> 48) & MPI_MASK;
state.r.size = 4;
#endif
- BIntTrim(&state.r);
+ MpiTrim(&state.r);
- BIntLsh(&state.r, MAX(e - 53, 0), &state.r);
- BIntLsh(&BInt1, MAX(0, -(e - 53)), &state.s);
- BIntLsh(&BInt1, MAX(e - 53, 0), &state.mm);
- BIntLsh(&BInt1, MAX(e - 53, 0), &state.mp);
+ MpiLsh(&state.r, MAX(e - 53, 0), &state.r);
+ MpiLsh(&BInt1, MAX(0, -(e - 53)), &state.s);
+ MpiLsh(&BInt1, MAX(e - 53, 0), &state.mm);
+ MpiLsh(&BInt1, MAX(e - 53, 0), &state.mp);
dragonFixup(&state, precision, mode, f, e);
/* Main digit generation loop */
@@ -198,8 +198,8 @@ static void dragon(double value,
while(1)
{
state.k -= 1;
- BIntMulDigit(&state.r, 10, &state.r);
- BIntDiv(&state.r, &state.s, &state.tmp[0], &state.r, &state.tmp[1]);
+ MpiMulDigit(&state.r, 10, &state.r);
+ MpiDiv(&state.r, &state.s, &state.tmp[0], &state.r, &state.tmp[1]);
s = '0';
if (state.tmp[0].size)
@@ -208,13 +208,13 @@ static void dragon(double value,
if (mode == NORMAL)
{
- BIntMulDigit(&state.mm, 10, &state.mm);
- BIntMulDigit(&state.mp, 10, &state.mp);
- BIntLsh(&state.r, 1, &state.tmp[1]);
- BIntLsh(&state.s, 1, &state.tmp[2]);
- BIntAdd(&state.tmp[1], &state.mp, &state.tmp[3]);
- low = BIntCompare(&state.tmp[1], &state.mm) < 0;
- high = BIntCompare(&state.tmp[3], &state.tmp[2]) > 0;
+ MpiMulDigit(&state.mm, 10, &state.mm);
+ MpiMulDigit(&state.mp, 10, &state.mp);
+ MpiLsh(&state.r, 1, &state.tmp[1]);
+ MpiLsh(&state.s, 1, &state.tmp[2]);
+ MpiAdd(&state.tmp[1], &state.mp, &state.tmp[3]);
+ low = MpiCompare(&state.tmp[1], &state.mm) < 0;
+ high = MpiCompare(&state.tmp[3], &state.tmp[2]) > 0;
if (low || high || state.k == state.cutoff || buffer->size >= BUFSIZE)
break;
}
@@ -617,7 +617,7 @@ double BH_StringToDouble(const char *string,
size_t *size)
{
int type, e, sign, i, count, shift;
- BInt r, s, tmp[5];
+ Mpi r, s, tmp[5];
char buffer[20];
double result;
uint64_t f;
@@ -668,43 +668,43 @@ double BH_StringToDouble(const char *string,
for (i = 0; i < count; i++)
{
tmp[0].data[0] = buffer[i] - '0';
- BIntMulDigit(&r, 10, &r);
- BIntAdd(&r, &tmp[0], &r);
+ MpiMulDigit(&r, 10, &r);
+ MpiAdd(&r, &tmp[0], &r);
}
if (e >= 0)
- BIntPow10(&r, e, &r, &tmp[0]);
+ MpiPow10(&r, e, &r, &tmp[0]);
else
- BIntPow10(&s, -e, &s, &tmp[0]);
+ MpiPow10(&s, -e, &s, &tmp[0]);
/* Calculate required shift */
shift = -52;
- if (BIntCompare(&r, &s) >= 0)
+ if (MpiCompare(&r, &s) >= 0)
{
- BIntDiv(&r, &s, &tmp[0], &tmp[1], &tmp[2]);
- shift += BIntLog2(&tmp[0]);
+ MpiDiv(&r, &s, &tmp[0], &tmp[1], &tmp[2]);
+ shift += MpiLog2(&tmp[0]);
}
else
{
- BIntDiv(&s, &r, &tmp[0], &tmp[1], &tmp[2]);
- shift += -(BIntLog2(&tmp[0]) + 1);
+ MpiDiv(&s, &r, &tmp[0], &tmp[1], &tmp[2]);
+ shift += -(MpiLog2(&tmp[0]) + 1);
}
/* Shift */
if (shift > 0)
- BIntLsh(&s, shift, &s);
+ MpiLsh(&s, shift, &s);
else if (shift < 0)
- BIntLsh(&r, -shift, &r);
+ MpiLsh(&r, -shift, &r);
/* Calculate final exponent and 53 bit integer */
- BIntDiv(&r, &s, &tmp[0], &tmp[1], &tmp[2]);
- BIntRsh(&s, 1, &s);
- if (BIntCompare(&tmp[1], &s) > 0 || (BIntCompare(&tmp[1], &s) == 0 && (tmp[0].data[0] & 0x1)))
+ MpiDiv(&r, &s, &tmp[0], &tmp[1], &tmp[2]);
+ MpiRsh(&s, 1, &s);
+ if (MpiCompare(&tmp[1], &s) > 0 || (MpiCompare(&tmp[1], &s) == 0 && (tmp[0].data[0] & 0x1)))
{
- BIntAdd(&tmp[0], &BInt1, &tmp[0]);
- if (BIntCompare(&tmp[0], &BInt53) >= 0)
+ MpiAdd(&tmp[0], &BInt1, &tmp[0]);
+ if (MpiCompare(&tmp[0], &BInt53) >= 0)
{
- BIntRsh(&tmp[0], 1, &tmp[0]);
+ MpiRsh(&tmp[0], 1, &tmp[0]);
shift++;
}
}