Import Upstream version 20180207
[hcoop/debian/mlton.git] / runtime / basis / Real / gdtoa.c
1 #include "platform.h"
2 #include "gdtoa/gdtoa.h"
3
4 /* This code is patterned on g_ffmt from the gdtoa sources. */
5 C_String_t Real32_gdtoa (Real32_t f, C_Int_t mode, C_Int_t ndig,
6 C_Int_t rounding, Ref(C_Int_t) decpt) {
7 FPI fpi = { 24, 1-127-24+1, 254-127-24+1, (int)rounding, 0, 6 };
8 ULong bits[1], L[1];
9 int ex, i;
10 char *result;
11
12 memcpy(L, &f, sizeof(Real32_t));
13 bits[0] = L[0] & 0x7fffff;
14 if ((ex = (L[0] >> 23) & 0xff) != 0)
15 bits[0] |= 0x800000;
16 else
17 ex = 1;
18 ex -= 0x7f + 23;
19 i = STRTOG_Normal;
20 result = gdtoa__gdtoa (&fpi, ex, bits, &i, (int)mode, (int)ndig, (int*)decpt, NULL);
21 return (C_String_t)result;
22 }
23
24 /* This code is patterned on g_dfmt from the gdtoa sources. */
25 C_String_t Real64_gdtoa (Real64_t d, C_Int_t mode, C_Int_t ndig,
26 C_Int_t rounding, Ref(C_Int_t) decpt) {
27 FPI fpi = { 53, 1-1023-53+1, 2046-1023-53+1, (int)rounding, 0, 14 };
28 ULong bits[2], L[2];
29 int ex, i;
30 char *result;
31 int x0, x1;
32
33 if (isBigEndian()) {
34 x0 = 0;
35 x1 = 1;
36 } else {
37 x0 = 1;
38 x1 = 0;
39 }
40 memcpy(L, &d, sizeof(Real64_t));
41 bits[0] = L[x1];
42 bits[1] = L[x0] & 0xfffff;
43 if ((ex = (L[x0] >> 20) & 0x7ff) != 0)
44 bits[1] |= 0x100000;
45 else
46 ex = 1;
47 ex -= 0x3ff + 52;
48 i = STRTOG_Normal;
49 result = gdtoa__gdtoa (&fpi, ex, bits, &i, mode, ndig, (int*)decpt, NULL);
50 return (C_String_t)result;
51 }