summaryrefslogtreecommitdiff
path: root/testing/mpfr/mpfr-3.1.1-p2.patch
blob: 43dfcbfe32ee6355266ff56324bca51bbc6a1aa9 (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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
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;
diff -Naurd mpfr-3.1.1-a/PATCHES mpfr-3.1.1-b/PATCHES
--- mpfr-3.1.1-a/PATCHES	2012-08-30 09:35:12.000000000 +0000
+++ mpfr-3.1.1-b/PATCHES	2012-08-30 09:35:12.000000000 +0000
@@ -0,0 +1 @@
+strtofr-ternary-value
diff -Naurd mpfr-3.1.1-a/VERSION mpfr-3.1.1-b/VERSION
--- mpfr-3.1.1-a/VERSION	2012-08-30 09:28:51.000000000 +0000
+++ mpfr-3.1.1-b/VERSION	2012-08-30 09:35:12.000000000 +0000
@@ -1 +1 @@
-3.1.1-p1
+3.1.1-p2
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-08-30 09:28:51.000000000 +0000
+++ mpfr-3.1.1-b/src/mpfr.h	2012-08-30 09:35:12.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-p1"
+#define MPFR_VERSION_STRING "3.1.1-p2"
 
 /* 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/strtofr.c mpfr-3.1.1-b/src/strtofr.c
--- mpfr-3.1.1-a/src/strtofr.c	2012-07-03 15:01:16.000000000 +0000
+++ mpfr-3.1.1-b/src/strtofr.c	2012-08-30 09:35:12.000000000 +0000
@@ -667,6 +667,20 @@
           /* (z, exp_z) = base^(exp_base-pstr_size) */
           z = result + 2*ysize + 1;
           err = mpfr_mpn_exp (z, &exp_z, pstr->base, exp_z, ysize);
+          /* Since we want y/z rounded toward zero, we must get an upper
+             bound of z. If err >= 0, the error on z is bounded by 2^err. */
+          if (err >= 0)
+            {
+              mp_limb_t cy;
+              unsigned long h = err / GMP_NUMB_BITS;
+              unsigned long l = err - h * GMP_NUMB_BITS;
+
+              if (h >= ysize) /* not enough precision in z */
+                goto next_loop;
+              cy = mpn_add_1 (z, z, ysize - h, MPFR_LIMB_ONE << l);
+              if (cy != 0) /* the code below requires z on ysize limbs */
+                goto next_loop;
+            }
           exact = exact && (err == -1);
           if (err == -2)
             goto underflow; /* FIXME: Sure? */
@@ -730,6 +744,7 @@
                                        MPFR_RNDN, rnd, MPFR_PREC(x)))
         break;
 
+    next_loop:
       /* update the prec for next loop */
       MPFR_ZIV_NEXT (loop, prec);
     } /* loop */
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-08-30 09:28:51.000000000 +0000
+++ mpfr-3.1.1-b/src/version.c	2012-08-30 09:35:12.000000000 +0000
@@ -25,5 +25,5 @@
 const char *
 mpfr_get_version (void)
 {
-  return "3.1.1-p1";
+  return "3.1.1-p2";
 }
diff -Naurd mpfr-3.1.1-a/tests/tstrtofr.c mpfr-3.1.1-b/tests/tstrtofr.c
--- mpfr-3.1.1-a/tests/tstrtofr.c	2012-07-03 15:01:24.000000000 +0000
+++ mpfr-3.1.1-b/tests/tstrtofr.c	2012-08-30 09:35:12.000000000 +0000
@@ -1105,6 +1105,92 @@
   mpfr_clear (y);
 }
 
+/* From a bug reported by Joseph S. Myers
+   https://sympa.inria.fr/sympa/arc/mpfr/2012-08/msg00005.html */
+static void
+bug20120814 (void)
+{
+  mpfr_exp_t emin = -30, e;
+  mpfr_t x, y;
+  int r;
+  char s[64], *p;
+
+  mpfr_init2 (x, 2);
+  mpfr_set_ui_2exp (x, 3, emin - 2, MPFR_RNDN);
+  mpfr_get_str (s + 1, &e, 10, 19, x, MPFR_RNDD);
+  s[0] = s[1];
+  s[1] = '.';
+  for (p = s; *p != 0; p++) ;
+  *p = 'e';
+  sprintf (p + 1, "%d", (int) e - 1);
+
+  mpfr_init2 (y, 4);
+  r = mpfr_strtofr (y, s, NULL, 0, MPFR_RNDN);
+  if (r <= 0 || ! mpfr_equal_p (x, y))
+    {
+      printf ("Error in bug20120814\n");
+      printf ("mpfr_strtofr failed on string \"%s\"\n", s);
+      printf ("Expected inex > 0 and y = 0.1100E%d\n", (int) emin);
+      printf ("Got inex = %-6d and y = ", r);
+      mpfr_dump (y);
+      exit (1);
+    }
+
+  mpfr_clear (x);
+  mpfr_clear (y);
+}
+
+static void
+bug20120829 (void)
+{
+  mpfr_t x1, x2, e;
+  int inex1, inex2, i, r;
+  char s[48] = "1e-1";
+
+  mpfr_init2 (e, 128);
+  mpfr_inits2 (4, x1, x2, (mpfr_ptr) 0);
+
+  inex1 = mpfr_set_si (e, -1, MPFR_RNDN);
+  MPFR_ASSERTN (inex1 == 0);
+
+  for (i = 1; i <= sizeof(s) - 5; i++)
+    {
+      s[3+i] = '0';
+      s[4+i] = 0;
+      inex1 = mpfr_mul_ui (e, e, 10, MPFR_RNDN);
+      MPFR_ASSERTN (inex1 == 0);
+      RND_LOOP(r)
+        {
+          mpfr_rnd_t rnd = (mpfr_rnd_t) r;
+
+          inex1 = mpfr_exp10 (x1, e, rnd);
+          inex1 = SIGN (inex1);
+          inex2 = mpfr_strtofr (x2, s, NULL, 0, rnd);
+          inex2 = SIGN (inex2);
+          /* On 32-bit machines, for i = 7, r8389, r8391 and r8394 do:
+             strtofr.c:...: MPFR assertion failed: cy == 0
+             r8396 is OK.
+             On 64-bit machines, for i = 15,
+             r8389 does: strtofr.c:678: MPFR assertion failed: err < (64 - 0)
+             r8391 does: strtofr.c:680: MPFR assertion failed: h < ysize
+             r8394 and r8396 are OK.
+          */
+          if (! mpfr_equal_p (x1, x2) || inex1 != inex2)
+            {
+              printf ("Error in bug20120829 for i = %d, rnd = %s\n",
+                      i, mpfr_print_rnd_mode (rnd));
+              printf ("Expected inex = %d, x = ", inex1);
+              mpfr_dump (x1);
+              printf ("Got      inex = %d, x = ", inex2);
+              mpfr_dump (x2);
+              exit (1);
+            }
+        }
+    }
+
+  mpfr_clears (e, x1, x2, (mpfr_ptr) 0);
+}
+
 int
 main (int argc, char *argv[])
 {
@@ -1117,6 +1203,8 @@
   check_retval ();
   bug20081028 ();
   test20100310 ();
+  bug20120814 ();
+  bug20120829 ();
 
   tests_end_mpfr ();
   return 0;