summaryrefslogtreecommitdiff
path: root/mbench/mfv5.cc
blob: 459ba6567270e098e1f1252430e4e7b1e12202d8 (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
/*
Copyright 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
Contributed by Patrick Pelissier, INRIA.

This file is part of the MPFR Library.

The MPFR Library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or (at your
option) any later version.

The MPFR Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
License for more details.

You should have received a copy of the GNU Lesser General Public License
along with the MPFR Library; see the file COPYING.LIB.  If not, write to
the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
MA 02110-1301, USA. */

#include <mpfr.h>

#include "timp.h"

#include "mfv5.h"

#define USAGE                                                           \
 "Bench functions for Pentium (V5++).\n"                                \
 __FILE__" " __DATE__" " __TIME__" GCC "__VERSION__ "\n"                \
 "Usage: mfv5 [-pPREC] [-sSEED] [-mSIZE] [-iPRIO] [-lLIST] tests ...\n"

using namespace std;

registered_test *first_registered_test = 0;

bool
do_test (const char *n, const vector<string> &base, const option_test &opt) {
  registered_test *p;
  int all = strcmp (n, "all") == 0;
  bool ret = false;

  TIMP_OVERHEAD ();

  p = first_registered_test;
  while (p) {
    if (all || p->is (n))
      ret = p->test (base, opt) || ret;
    p = p->next ();
  }
  return ret;
}

void
list_test (void) {
  registered_test *p;

  p = first_registered_test;
  while (p) {
    cout << p->get_name () << endl;
    p = p->next ();
  }
  cout << "all\n";
  return;
}

void
build_base (vector<string> &base, const option_test &opt)
{
  unsigned long i, n = opt.stat;
  mpfr_t x;
  gmp_randstate_t state;
  const char *str;
  mp_exp_t e;
  char *buffer;

  mpfr_init2 (x, opt.prec);
  gmp_randinit_lc_2exp_size (state, 128);
  gmp_randseed_ui (state, opt.seed);

  for (i = 0 ; i < n ; i++) {
    mpfr_urandomb (x, state);
    mpfr_mul_2si  (x, x, (rand()%GMP_NUMB_BITS)-(GMP_NUMB_BITS/2), GMP_RNDN);
    str = mpfr_get_str (NULL, &e, 10, 0, x, GMP_RNDN);
    if (str == 0)
      abort ();
    buffer = (char *) malloc (strlen(str)+50);
    if (buffer == 0)
      abort ();
    sprintf (buffer, "%sE%ld", str, (unsigned long) e-strlen(str)+1);
    base.push_back (buffer);
    if (opt.verbose)
      cout << "[" << i << "] = " << buffer << endl;
    free (buffer);
    mpfr_free_str ((char*)str);
  }

  gmp_randclear(state);
  mpfr_clear (x);
}


int main (int argc, const char *argv[])
{
  option_test options;
  vector<string> base;
  int i, j, cont, prio;

  /* Parse option */
  prio = 19;
  for(i = 1 ; i < argc ; i++)
    {
      if (argv[i][0] == '-')
	{
	  switch (argv[i][1])
	    {
	    case 'h':
	      cout << USAGE;
	      exit (0);
	      break;
	    case 'p':
	      options.prec = atol (argv[i]+2);
	      break;
	    case 's':
	      options.seed = atol (argv[i]+2);
	      break;
 	    case 'm':
	      options.stat = atol (argv[i]+2);
	      break;
	    case 'v':
	      options.verbose = true;
	      break;
	    case 'i':
	      prio = atol (argv[i]+2);
	      break;
	    case 'l':
	      list_test ();
	      exit (0);
	      break;
	    default:
	      cerr <<  "Unkwown option:" << argv[i] << endl;
	      exit (1);
	      break;
	    }
	}
    }

  if (options.verbose)
    cout << USAGE;

  /* Set low priority */
  setpriority(PRIO_PROCESS, 0, prio);
  
  /* Build Used Base */
  if (options.verbose)
    cout << "Building DATA Base\n";
  mp_set_memory_functions (NULL, NULL, NULL);
  build_base (base, options);

  /* Do test */
  for (j = 1, cont = 5 ; cont ; j++, cont--) {
    cout << "Pass " << j << endl;
    for(i = 1 ; i < argc ; i++)
      if (argv[i][0] != '-')
	if (do_test (argv[i], base, options))
	  cont = 5;
  }

  /* Final */
  if (options.verbose)
    cout << "Used precision: " << options.prec << endl;

  return 0;
}