~ubuntu-branches/debian/sid/genius/sid

« back to all changes in this revision

Viewing changes to mpfr/tests/tstckintc.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2006-08-21 12:57:45 UTC
  • Revision ID: james.westby@ubuntu.com-20060821125745-sl9ks8v7fq324bdf
Tags: upstream-0.7.6.1
ImportĀ upstreamĀ versionĀ 0.7.6.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Test file for mpfr_custom_*
 
2
 
 
3
Copyright 2005 Free Software Foundation.
 
4
 
 
5
This file is part of the MPFR Library.
 
6
 
 
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.
 
11
 
 
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.
 
16
 
 
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. */
 
21
 
 
22
#include <stdio.h>
 
23
#include <stdlib.h>
 
24
#include <limits.h>
 
25
#include <string.h>
 
26
 
 
27
#include "mpfr-test.h"
 
28
 
 
29
#define BUFFER_SIZE 1000
 
30
#define PREC_TESTED 200
 
31
 
 
32
char Buffer[BUFFER_SIZE];
 
33
char *stack = Buffer;
 
34
mp_prec_t p = PREC_TESTED;
 
35
 
 
36
#define ALIGNED(s) ( ((s)+sizeof (long)-1) / sizeof (long) * sizeof (long))
 
37
 
 
38
static void *
 
39
new (size_t s)
 
40
{
 
41
  void *p = (void*) stack;
 
42
  stack += ALIGNED (s);
 
43
  if (MPFR_UNLIKELY (stack > &Buffer[BUFFER_SIZE]))
 
44
    {
 
45
      printf ("Stack overflow.\n");
 
46
      exit (1);
 
47
    }
 
48
  return p;
 
49
}
 
50
 
 
51
 /* Alloc a new mpfr_t on the main stack */
 
52
static mpfr_ptr
 
53
new_mpfr (mp_prec_t p)
 
54
{
 
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);
 
59
  return x;
 
60
}
 
61
 
 
62
 /* Garbage the stack by keeping only x  */
 
63
static mpfr_ptr
 
64
return_mpfr (mpfr_ptr x, char *old_stack)
 
65
{
 
66
  void *mantissa       = mpfr_custom_get_mantissa (x);
 
67
  size_t size_mantissa = mpfr_custom_get_size (mpfr_get_prec (x));
 
68
  mpfr_ptr newx;
 
69
 
 
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);
 
75
  return newx;
 
76
}
 
77
 
 
78
static void
 
79
test1 (void)
 
80
{
 
81
  mpfr_ptr x, y;
 
82
  char *org;
 
83
 
 
84
  org = stack;
 
85
  x = new_mpfr (p);
 
86
  y = new_mpfr (p);
 
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)
 
92
    {
 
93
      printf ("Compact (1) failed!\n");
 
94
      exit (1);
 
95
    }
 
96
  stack = org;
 
97
}
 
98
 
 
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 */
 
101
static long *
 
102
dummy_new (void)
 
103
{
 
104
  long *r;
 
105
 
 
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;
 
110
  r[1] = 0;
 
111
  return r;
 
112
}
 
113
 
 
114
static long *
 
115
dummy_set_si (long si)
 
116
{
 
117
  mpfr_t x;
 
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);
 
123
  return r;
 
124
}
 
125
 
 
126
static long *
 
127
dummy_add (long *a, long *b)
 
128
{
 
129
  mpfr_t x, y, z;
 
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);
 
137
  return r;
 
138
}
 
139
 
 
140
static long *
 
141
dummy_compact (long *r, char *org_stack)
 
142
{
 
143
  memmove (org_stack, r,
 
144
           ALIGNED (2*sizeof (long)) + ALIGNED ((mpfr_custom_get_size) (p)));
 
145
  return (long *) org_stack;
 
146
}
 
147
 
 
148
static void
 
149
test2 (void)
 
150
{
 
151
  mpfr_t x;
 
152
  char *org = stack;
 
153
  long *a, *b, *c;
 
154
 
 
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)
 
161
    {
 
162
      printf ("Compact (2) failed! c=%p a=%p\n", c, a);
 
163
      mpfr_dump (x);
 
164
      exit (1);
 
165
    }
 
166
  stack = org;
 
167
}
 
168
 
 
169
int
 
170
main (void)
 
171
{
 
172
  tests_start_mpfr ();
 
173
  /* We test iff long = mp_limb_t */
 
174
  if (sizeof (long) == sizeof (mp_limb_t))
 
175
    {
 
176
      test1 ();
 
177
      test2 ();
 
178
    }
 
179
  tests_end_mpfr ();
 
180
  return 0;
 
181
}