summaryrefslogtreecommitdiff
path: root/patches/mpfr/3.1.1/110-get_decimal64.patch
blob: d73a3d7b4fd8e9422ef4c56023e26fe511e79646 (plain)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
diff -Naurd mpfr-3.1.1-a/PATCHES mpfr-3.1.1-b/PATCHES
--- mpfr-3.1.1-a/PATCHES	2012-08-30 09:28:51.000000000 +0000
+++ mpfr-3.1.1-b/PATCHES	2012-08-30 09:28:51.000000000 +0000
@@ -0,0 +1 @@
+get_decimal64
diff -Naurd mpfr-3.1.1-a/VERSION mpfr-3.1.1-b/VERSION
--- mpfr-3.1.1-a/VERSION	2012-07-03 15:01:13.000000000 +0000
+++ mpfr-3.1.1-b/VERSION	2012-08-30 09:28:51.000000000 +0000
@@ -1 +1 @@
-3.1.1
+3.1.1-p1
diff -Naurd mpfr-3.1.1-a/src/get_d64.c mpfr-3.1.1-b/src/get_d64.c
--- mpfr-3.1.1-a/src/get_d64.c	2012-07-03 15:01:18.000000000 +0000
+++ mpfr-3.1.1-b/src/get_d64.c	2012-08-30 09:28:51.000000000 +0000
@@ -32,6 +32,10 @@
 
 #ifdef MPFR_WANT_DECIMAL_FLOATS
 
+#ifndef DEC64_MAX
+# define DEC64_MAX 9.999999999999999E384dd
+#endif
+
 #ifdef DPD_FORMAT
 static int T[1000] = {
   0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 32,
@@ -142,26 +146,14 @@
 static _Decimal64
 get_decimal64_min (int negative)
 {
-  union ieee_double_extract x;
-
-  x.s.sig = (negative) ? 1 : 0;
-  x.s.exp = 0;
-  x.s.manh = 0;
-  x.s.manl = 1;
-  return x.d;
+  return negative ? - 1E-398dd : 1E-398dd;
 }
 
 /* construct the decimal64 largest finite number with given sign */
 static _Decimal64
 get_decimal64_max (int negative)
 {
-  union ieee_double_extract x;
-
-  x.s.sig = (negative) ? 1 : 0;
-  x.s.exp = 1919;
-  x.s.manh = 1048575; /* 2^20-1 */
-  x.s.manl = ~0;
-  return x.d;
+  return negative ? - DEC64_MAX : DEC64_MAX;
 }
 
 /* one-to-one conversion:
@@ -334,7 +326,8 @@
   /* the largest decimal64 number is just below 10^(385) < 2^1279 */
   else if (MPFR_UNLIKELY (e > 1279)) /* then src >= 2^1279 */
     {
-      if (MPFR_RNDZ || (rnd_mode == MPFR_RNDU && negative != 0)
+      if (rnd_mode == MPFR_RNDZ
+          || (rnd_mode == MPFR_RNDU && negative != 0)
           || (rnd_mode == MPFR_RNDD && negative == 0))
         return get_decimal64_max (negative);
       else
@@ -354,6 +347,15 @@
              which corresponds to s=[0.]1000...000 and e=-397 */
           if (e < -397)
             {
+              if (rnd_mode == MPFR_RNDN && e == -398)
+                {
+                  /* If 0.5E-398 < |src| < 1E-398 (smallest subnormal),
+                     src should round to +/- 1E-398 in MPFR_RNDN. */
+                  mpfr_get_str (s, &e, 10, 1, src, MPFR_RNDA);
+                  return e == -398 && s[negative] <= '5' ?
+                    get_decimal64_zero (negative) :
+                    get_decimal64_min (negative);
+                }
               if (rnd_mode == MPFR_RNDZ || rnd_mode == MPFR_RNDN
                   || (rnd_mode == MPFR_RNDD && negative == 0)
                   || (rnd_mode == MPFR_RNDU && negative != 0))
@@ -379,7 +381,8 @@
          which corresponds to s=[0.]9999...999 and e=385 */
       else if (e > 385)
         {
-          if (MPFR_RNDZ || (rnd_mode == MPFR_RNDU && negative != 0)
+          if (rnd_mode == MPFR_RNDZ
+              || (rnd_mode == MPFR_RNDU && negative != 0)
               || (rnd_mode == MPFR_RNDD && negative == 0))
             return get_decimal64_max (negative);
           else
diff -Naurd mpfr-3.1.1-a/src/mpfr.h mpfr-3.1.1-b/src/mpfr.h
--- mpfr-3.1.1-a/src/mpfr.h	2012-07-03 15:01:19.000000000 +0000
+++ mpfr-3.1.1-b/src/mpfr.h	2012-08-30 09:28:51.000000000 +0000
@@ -27,7 +27,7 @@
 #define MPFR_VERSION_MAJOR 3
 #define MPFR_VERSION_MINOR 1
 #define MPFR_VERSION_PATCHLEVEL 1
-#define MPFR_VERSION_STRING "3.1.1"
+#define MPFR_VERSION_STRING "3.1.1-p1"
 
 /* Macros dealing with MPFR VERSION */
 #define MPFR_VERSION_NUM(a,b,c) (((a) << 16L) | ((b) << 8) | (c))
diff -Naurd mpfr-3.1.1-a/src/version.c mpfr-3.1.1-b/src/version.c
--- mpfr-3.1.1-a/src/version.c	2012-07-03 15:01:18.000000000 +0000
+++ mpfr-3.1.1-b/src/version.c	2012-08-30 09:28:51.000000000 +0000
@@ -25,5 +25,5 @@
 const char *
 mpfr_get_version (void)
 {
-  return "3.1.1";
+  return "3.1.1-p1";
 }
diff -Naurd mpfr-3.1.1-a/tests/tget_set_d64.c mpfr-3.1.1-b/tests/tget_set_d64.c
--- mpfr-3.1.1-a/tests/tget_set_d64.c	2012-07-03 15:01:24.000000000 +0000
+++ mpfr-3.1.1-b/tests/tget_set_d64.c	2012-08-30 09:28:51.000000000 +0000
@@ -25,6 +25,10 @@
 #include <stdlib.h> /* for exit */
 #include "mpfr-test.h"
 
+#ifndef DEC64_MAX
+# define DEC64_MAX 9.999999999999999E384dd
+#endif
+
 /* #define DEBUG */
 
 static void
@@ -149,6 +153,15 @@
   mpfr_set_str (x, "9.999999999999999E384", 10, MPFR_RNDZ);
   mpfr_set (y, x, MPFR_RNDZ);
   d = mpfr_get_decimal64 (x, MPFR_RNDU);
+  ASSERT_ALWAYS (d == DEC64_MAX);
+  mpfr_set_ui (x, 0, MPFR_RNDZ);
+  mpfr_set_decimal64 (x, d, MPFR_RNDZ);
+  ASSERT_ALWAYS (mpfr_cmp (x, y) == 0);
+
+  mpfr_set_str (x, "-9.999999999999999E384", 10, MPFR_RNDZ);
+  mpfr_set (y, x, MPFR_RNDZ);
+  d = mpfr_get_decimal64 (x, MPFR_RNDA);
+  ASSERT_ALWAYS (d == -DEC64_MAX);
   mpfr_set_ui (x, 0, MPFR_RNDZ);
   mpfr_set_decimal64 (x, d, MPFR_RNDZ);
   ASSERT_ALWAYS (mpfr_cmp (x, y) == 0);
@@ -225,6 +238,83 @@
   mpfr_clear (x);
 }
 
+static void
+check_overflow (void)
+{
+  mpfr_t x;
+  int err = 0, neg, rnd;
+
+  mpfr_init2 (x, 96);
+  for (neg = 0; neg < 2; neg++)
+    RND_LOOP (rnd)
+      {
+        _Decimal64 d, e;
+        mpfr_rnd_t r = (mpfr_rnd_t) rnd;
+        int sign = neg ? -1 : 1;
+
+        e = sign * (MPFR_IS_LIKE_RNDZ (r, neg) ? 1 : 2) * DEC64_MAX;
+        /* This tests the binary exponent e > 1279 case of get_d64.c */
+        mpfr_set_si_2exp (x, sign, 9999, MPFR_RNDN);
+        d = mpfr_get_decimal64 (x, r);
+        if (d != e)
+          {
+            printf ("Error 1 in check_overflow for %s, %s\n",
+                    neg ? "negative" : "positive",
+                    mpfr_print_rnd_mode (r));
+            err = 1;
+          }
+        /* This tests the decimal exponent e > 385 case of get_d64.c */
+        mpfr_set_si_2exp (x, sign * 31, 1274, MPFR_RNDN);
+        d = mpfr_get_decimal64 (x, r);
+        if (d != e)
+          {
+            printf ("Error 2 in check_overflow for %s, %s\n",
+                    neg ? "negative" : "positive",
+                    mpfr_print_rnd_mode (r));
+            err = 1;
+          }
+        /* This tests the last else (-382 <= e <= 385) of get_d64.c */
+        mpfr_set_decimal64 (x, e, MPFR_RNDA);
+        d = mpfr_get_decimal64 (x, r);
+        if (d != e)
+          {
+            printf ("Error 3 in check_overflow for %s, %s\n",
+                    neg ? "negative" : "positive",
+                    mpfr_print_rnd_mode (r));
+            err = 1;
+          }
+      }
+  mpfr_clear (x);
+  if (err)
+    exit (1);
+}
+
+static void
+check_tiny (void)
+{
+  mpfr_t x;
+  _Decimal64 d;
+
+  /* If 0.5E-398 < |x| < 1E-398 (smallest subnormal), x should round
+     to +/- 1E-398 in MPFR_RNDN. Note: the midpoint 0.5E-398 between
+     0 and 1E-398 is not a representable binary number, so that there
+     are no tests for it. */
+  mpfr_init2 (x, 128);
+  mpfr_set_str (x, "1E-398", 10, MPFR_RNDZ);
+  d = mpfr_get_decimal64 (x, MPFR_RNDN);
+  MPFR_ASSERTN (d == 1.0E-398dd);
+  mpfr_neg (x, x, MPFR_RNDN);
+  d = mpfr_get_decimal64 (x, MPFR_RNDN);
+  MPFR_ASSERTN (d == -1.0E-398dd);
+  mpfr_set_str (x, "0.5E-398", 10, MPFR_RNDU);
+  d = mpfr_get_decimal64 (x, MPFR_RNDN);
+  MPFR_ASSERTN (d == 1.0E-398dd);
+  mpfr_neg (x, x, MPFR_RNDN);
+  d = mpfr_get_decimal64 (x, MPFR_RNDN);
+  MPFR_ASSERTN (d == -1.0E-398dd);
+  mpfr_clear (x);
+}
+
 int
 main (void)
 {
@@ -241,6 +331,8 @@
   check_inf_nan ();
   check_random ();
   check_native ();
+  check_overflow ();
+  check_tiny ();
 
   tests_end_mpfr ();
   return 0;