1
/* Test file for mpfr_custom_*
3
Copyright 2005 Free Software Foundation.
5
This file is part of the MPFR Library.
7
The MPFR Library is free software; you can redistribute it and/or modify
8
it under the terms of the GNU Lesser General Public License as published by
9
the Free Software Foundation; either version 2.1 of the License, or (at your
10
option) any later version.
12
The MPFR Library is distributed in the hope that it will be useful, but
13
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15
License for more details.
17
You should have received a copy of the GNU Lesser General Public License
18
along with the MPFR Library; see the file COPYING.LIB. If not, write to
19
the Free Software Foundation, Inc., 51 Franklin Place, Fifth Floor, Boston,
20
MA 02110-1301, USA. */
27
#include "mpfr-test.h"
29
#define BUFFER_SIZE 1000
30
#define PREC_TESTED 200
32
char Buffer[BUFFER_SIZE];
34
mp_prec_t p = PREC_TESTED;
36
#define ALIGNED(s) ( ((s)+sizeof (long)-1) / sizeof (long) * sizeof (long))
41
void *p = (void*) stack;
43
if (MPFR_UNLIKELY (stack > &Buffer[BUFFER_SIZE]))
45
printf ("Stack overflow.\n");
51
/* Alloc a new mpfr_t on the main stack */
53
new_mpfr (mp_prec_t p)
55
mpfr_ptr x = (mpfr_ptr) new (sizeof (mpfr_t));
56
void *mantissa = new (mpfr_custom_get_size (p));
57
mpfr_custom_init (mantissa, p);
58
mpfr_custom_init_set (x, 0, 0, p, mantissa);
62
/* Garbage the stack by keeping only x */
64
return_mpfr (mpfr_ptr x, char *old_stack)
66
void *mantissa = mpfr_custom_get_mantissa (x);
67
size_t size_mantissa = mpfr_custom_get_size (mpfr_get_prec (x));
70
memmove (old_stack, x, sizeof (mpfr_t));
71
memmove (old_stack + ALIGNED (sizeof (mpfr_t)), mantissa, size_mantissa);
72
newx = (mpfr_ptr) old_stack;
73
mpfr_custom_move (newx, old_stack + ALIGNED (sizeof (mpfr_t)));
74
stack = old_stack + ALIGNED (sizeof (mpfr_t)) + ALIGNED (size_mantissa);
87
mpfr_set_ui (x, 42, GMP_RNDN);
88
mpfr_set_ui (y, 17, GMP_RNDN);
89
mpfr_add (y, x, y, GMP_RNDN);
90
y = return_mpfr (y, org);
91
if (y != x || mpfr_cmp_ui (y, 59) != 0)
93
printf ("Compact (1) failed!\n");
99
/* We build the MPFR variable each time it is needed */
100
/* a[0] is the kind, a[1] is the exponent, &a[2] is the mantissa */
106
r = new (ALIGNED (2*sizeof (long)) + ALIGNED (mpfr_custom_get_size (p)));
107
MPFR_ASSERTN (r != NULL);
108
(mpfr_custom_init) (&r[2], p);
109
r[0] = (int) MPFR_NAN_KIND;
115
dummy_set_si (long si)
118
long * r = dummy_new ();
119
(mpfr_custom_init_set) (x, 0, 0, p, &r[2]);
120
mpfr_set_si (x, si, GMP_RNDN);
121
r[0] = mpfr_custom_get_kind (x);
122
r[1] = mpfr_custom_get_exp (x);
127
dummy_add (long *a, long *b)
130
long *r = dummy_new ();
131
mpfr_custom_init_set (x, 0, 0, p, &r[2]);
132
(mpfr_custom_init_set) (y, a[0], a[1], p, &a[2]);
133
mpfr_custom_init_set (z, b[0], b[1], p, &b[2]);
134
mpfr_add (x, y, z, GMP_RNDN);
135
r[0] = (mpfr_custom_get_kind) (x);
136
r[1] = (mpfr_custom_get_exp) (x);
141
dummy_compact (long *r, char *org_stack)
143
memmove (org_stack, r,
144
ALIGNED (2*sizeof (long)) + ALIGNED ((mpfr_custom_get_size) (p)));
145
return (long *) org_stack;
155
a = dummy_set_si (42);
156
b = dummy_set_si (17);
157
c = dummy_add (a, b);
158
c = dummy_compact (c, org);
159
(mpfr_custom_init_set) (x, c[0], c[1], p, &c[2]);
160
if (c != a || mpfr_cmp_ui (x, 59) != 0)
162
printf ("Compact (2) failed! c=%p a=%p\n", c, a);
173
/* We test iff long = mp_limb_t */
174
if (sizeof (long) == sizeof (mp_limb_t))