1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
#include "platform.h"
#include "gdtoa/gdtoa.h"
#ifndef DEBUG
#define DEBUG FALSE
#endif
/* This code is patterned on g_dfmt from the gdtoa sources. */
C_String_t Real32_gdtoa (Real32_t f, C_Int_t mode, C_Int_t ndig,
C_Int_t rounding, Ref(C_Int_t) decpt) {
ULong bits[1];
int ex;
FPI fpi = { 24, 1-127-24+1, 254-127-24+1, (int)rounding, 0 };
int i;
ULong L[1];
char *result;
memcpy(L, &f, sizeof(Real32_t));
bits[0] = L[0] & 0x7fffff;
if (0 != (ex = (L[0] >> 23) & 0xff))
bits[0] |= 0x800000;
else
ex = 1;
ex -= 0x7f + 23;
i = STRTOG_Normal;
result = gdtoa__gdtoa (&fpi, ex, bits, &i, (int)mode, (int)ndig, (int*)decpt, NULL);
if (DEBUG)
fprintf (stderr, "%s = gdtoa (%g, %d, %d, %d) decpt = %d\n",
result, (double)f, (int)mode, (int)ndig, (int)rounding, *((int*)decpt));
return (C_String_t)result;
}
C_String_t Real64_gdtoa (Real64_t d, C_Int_t mode, C_Int_t ndig,
C_Int_t rounding, Ref(C_Int_t) decpt) {
ULong bits[2];
int ex;
FPI fpi = { 53, 1-1023-53+1, 2046-1023-53+1, (int)rounding, 0 };
int i;
ULong L[2];
char *result;
int x0, x1;
if (isBigEndian()) {
x0 = 0;
x1 = 1;
} else {
x0 = 1;
x1 = 0;
}
memcpy(L, &d, sizeof(Real64_t));
bits[0] = L[x1];
bits[1] = L[x0] & 0xfffff;
if (0 != (ex = (L[x0] >> 20) & 0x7ff))
bits[1] |= 0x100000;
else
ex = 1;
ex -= 0x3ff + 52;
i = STRTOG_Normal;
result = gdtoa__gdtoa (&fpi, ex, bits, &i, mode, ndig, (int*)decpt, NULL);
if (DEBUG)
fprintf (stderr, "%s = gdtoa (%g, %d, %d, %d) decpt = %d\n",
result, d, (int)mode, (int)ndig, (int)rounding, *((int*)decpt));
return (C_String_t)result;
}
|