OpenCores
URL https://opencores.org/ocsvn/aemb/aemb/trunk

Subversion Repositories aemb

[/] [aemb/] [trunk/] [sw/] [cc/] [literate.hh] - Blame information for rev 207

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 141 sybreon
/* $Id: literate.hh,v 1.5 2008-04-27 16:04:42 sybreon Exp $
2 112 sybreon
**
3
** AEMB Function Verification C++ Testbench
4
** Copyright (C) 2004-2008 Shawn Tan 
5
**
6
** This file is part of AEMB.
7
**
8
** AEMB is free software: you can redistribute it and/or modify it
9
** under the terms of the GNU General Public License as published by
10
** the Free Software Foundation, either version 3 of the License, or
11
** (at your option) any later version.
12
**
13
** AEMB is distributed in the hope that it will be useful, but WITHOUT
14
** ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15
** or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
16
** License for more details.
17
**
18
** You should have received a copy of the GNU General Public License
19
** along with AEMB.  If not, see .
20
*/
21
/**
22
  AEMB Software Verification
23
  @file literate.hh
24
 
25
  Algorithms listed here are extracted from literateprograms.org and
26
  modified for AEMB testing.
27
*/
28
 
29 137 sybreon
#include 
30 122 sybreon
#include "simboard.hh"
31 112 sybreon
 
32
#ifndef LITERATE_HH
33
#define LITERATE_HH
34
 
35
/*
36
FIBONACCI TEST
37
http://en.literateprograms.org/Fibonacci_numbers_(C)
38
 
39
  This tests for the following:
40
  - Recursion & Iteration
41
  - 32/16/8-bit data handling
42
*/
43
 
44
unsigned int fibSlow(unsigned int n)
45
{
46
  return n < 2 ? n : fibSlow(n-1) + fibSlow(n-2);
47
}
48
 
49
unsigned int fibFast(unsigned int n)
50
{
51
  unsigned int a[3];
52
  unsigned int *p=a;
53
  unsigned int i;
54
 
55 122 sybreon
  for(i=0; i<=n; ++i)
56
    {
57
      if(i<2) *p=i;
58
      else
59
        {
60
          if(p==a) *p=*(a+1)+*(a+2);
61
          else if(p==a+1) *p=*a+*(a+2);
62
          else *p=*a+*(a+1);
63
        }
64
      if(++p>a+2) p=a;
65 112 sybreon
    }
66
 
67
  return p==a?*(p+2):*(p-1);
68
}
69
 
70
int fibonacciTest(int max) {
71
  unsigned int n;
72
  unsigned int fast, slow;
73
  // 32-bit LUT
74
  unsigned int fib_lut32[] = {
75
    0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233
76
  };
77
  // 16-bit LUT
78
  unsigned short fib_lut16[] = {
79
    0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233
80
  };
81
  // 8-bit LUT
82
  unsigned char fib_lut8[] = {
83
    0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233
84
  };
85
 
86 122 sybreon
  for (n=0;n
87
    {
88
      slow = fibSlow(n);
89
      fast = fibFast(n);
90
      if ((slow != fast) ||
91
          (fast != fib_lut32[n]) ||
92
          (fast != fib_lut16[n]) ||
93
          (fast != fib_lut8[n])) {
94
        return EXIT_FAILURE;
95
      }
96 137 sybreon
 
97 122 sybreon
    }
98 112 sybreon
  return EXIT_SUCCESS;
99
}
100
 
101
/*
102
   EUCLIDEAN TEST
103
   http://en.literateprograms.org/Euclidean_algorithm_(C)
104
 
105
   This tests for the following:
106
   - Modulo arithmetic
107
   - Goto
108
*/
109
 
110
int euclidGCD(int a, int b) {
111
  if (b > a) goto b_larger;
112
  while (1) {
113
    a = a % b;
114
    if (a == 0) return b;
115
  b_larger:
116
    b = b % a;
117
    if (b == 0) return a;
118
  }
119
}
120
 
121
int euclideanTest(int max)
122
{
123
  int n;
124
  int euclid;
125
  // Random Numbers
126
  int euclid_a[] = {
127
    1804289383, 1681692777, 1957747793, 719885386, 596516649,
128
    1025202362, 783368690, 2044897763, 1365180540, 304089172,
129
    35005211, 294702567, 336465782, 278722862
130
  };
131
  int euclid_b[] = {
132
    846930886, 1714636915, 424238335, 1649760492, 1189641421,
133
    1350490027, 1102520059, 1967513926, 1540383426, 1303455736,
134
    521595368, 1726956429, 861021530, 233665123
135
  };
136
 
137
  // GCD
138
  int euclid_lut[] = {
139
    1, 1, 1, 2, 1, 1, 1, 1, 6, 4, 1, 3, 2, 1
140
  };
141
 
142 122 sybreon
  for (n=0;n
143
    {
144
      euclid = euclidGCD(euclid_a[n],euclid_b[n]);
145
      if (euclid != euclid_lut[n])
146
        {
147
          return EXIT_FAILURE;
148
        }
149 112 sybreon
    }
150 122 sybreon
 
151 112 sybreon
  return EXIT_SUCCESS;
152
}
153
 
154
/**
155
   NEWTON-RHAPSON
156
   http://en.literateprograms.org/Newton-Raphson's_method_for_root_finding_(C)
157
 
158
   This tests for the following:
159
   - Multiplication & Division
160 122 sybreon
   - Barrel Shifts
161 112 sybreon
   - Floating point arithmetic
162
   - Integer to Float conversion
163
*/
164
 
165
float newtonSqrt(float n)
166
{
167
  float x = 0.0;
168
  float xn = 0.0;
169
  int iters = 0;
170
  int i;
171
  for (i = 0; i <= (int)n; ++i)
172
    {
173
      float val = i*i-n;
174
      if (val == 0.0)
175
        return i;
176
      if (val > 0.0)
177
        {
178
          xn = (i+(i-1))/2.0;
179
          break;
180
        }
181
    }
182 122 sybreon
  while (!(iters++ >= 10
183 112 sybreon
           || x == xn))
184
    {
185
      x = xn;
186
      xn = x - (x * x - n) / (2 * x);
187
    }
188
  return xn;
189
}
190
 
191
int newtonTest (int max) {
192
  int n;
193
  float newt;
194 122 sybreon
  // 32-bit LUT in IEEE754 hex representation
195 112 sybreon
  float newt_lut[] = {
196 123 sybreon
    0.000000000000000000000000,
197
    1.000000000000000000000000,
198
    1.414213538169860839843750,
199
    1.732050776481628417968750,
200
    2.000000000000000000000000,
201
    2.236068010330200195312500,
202
    2.449489831924438476562500,
203
    2.645751237869262695312500,
204
    2.828427076339721679687500,
205
    3.000000000000000000000000,
206
    3.162277698516845703125000,
207
    3.316624879837036132812500,
208
    3.464101552963256835937500,
209
    3.605551242828369140625000,
210
    3.741657495498657226562500
211 112 sybreon
  };
212
 
213 122 sybreon
  for (n=0;n
214
    {
215
      newt = newtonSqrt(n);
216
      if (newt != newt_lut[n])
217 123 sybreon
        {
218 122 sybreon
          return EXIT_FAILURE;
219
        }
220
    }
221
 
222 112 sybreon
  return EXIT_SUCCESS;
223
}
224
 
225
#endif
226
 
227
/*
228 141 sybreon
$Log: not supported by cvs2svn $
229 112 sybreon
*/

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.