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

Subversion Repositories aemb

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

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

Line No. Rev Author Line
1 123 sybreon
/* $Id: literate.hh,v 1.3 2008-04-21 12:01:18 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
#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
    }
97 112 sybreon
  return EXIT_SUCCESS;
98
}
99
 
100
/*
101
   EUCLIDEAN TEST
102
   http://en.literateprograms.org/Euclidean_algorithm_(C)
103
 
104
   This tests for the following:
105
   - Modulo arithmetic
106
   - Goto
107
*/
108
 
109
int euclidGCD(int a, int b) {
110
  if (b > a) goto b_larger;
111
  while (1) {
112
    a = a % b;
113
    if (a == 0) return b;
114
  b_larger:
115
    b = b % a;
116
    if (b == 0) return a;
117
  }
118
}
119
 
120
int euclideanTest(int max)
121
{
122
  int n;
123
  int euclid;
124
  // Random Numbers
125
  int euclid_a[] = {
126
    1804289383, 1681692777, 1957747793, 719885386, 596516649,
127
    1025202362, 783368690, 2044897763, 1365180540, 304089172,
128
    35005211, 294702567, 336465782, 278722862
129
  };
130
  int euclid_b[] = {
131
    846930886, 1714636915, 424238335, 1649760492, 1189641421,
132
    1350490027, 1102520059, 1967513926, 1540383426, 1303455736,
133
    521595368, 1726956429, 861021530, 233665123
134
  };
135
 
136
  // GCD
137
  int euclid_lut[] = {
138
    1, 1, 1, 2, 1, 1, 1, 1, 6, 4, 1, 3, 2, 1
139
  };
140
 
141 122 sybreon
  for (n=0;n
142
    {
143
      euclid = euclidGCD(euclid_a[n],euclid_b[n]);
144
      if (euclid != euclid_lut[n])
145
        {
146
          return EXIT_FAILURE;
147
        }
148 112 sybreon
    }
149 122 sybreon
 
150 112 sybreon
  return EXIT_SUCCESS;
151
}
152
 
153
/**
154
   NEWTON-RHAPSON
155
   http://en.literateprograms.org/Newton-Raphson's_method_for_root_finding_(C)
156
 
157
   This tests for the following:
158
   - Multiplication & Division
159 122 sybreon
   - Barrel Shifts
160 112 sybreon
   - Floating point arithmetic
161
   - Integer to Float conversion
162
*/
163
 
164
float newtonSqrt(float n)
165
{
166
  float x = 0.0;
167
  float xn = 0.0;
168
  int iters = 0;
169
  int i;
170
  for (i = 0; i <= (int)n; ++i)
171
    {
172
      float val = i*i-n;
173
      if (val == 0.0)
174
        return i;
175
      if (val > 0.0)
176
        {
177
          xn = (i+(i-1))/2.0;
178
          break;
179
        }
180
    }
181 122 sybreon
  while (!(iters++ >= 10
182 112 sybreon
           || x == xn))
183
    {
184
      x = xn;
185
      xn = x - (x * x - n) / (2 * x);
186
    }
187
  return xn;
188
}
189
 
190
int newtonTest (int max) {
191
  int n;
192
  float newt;
193 122 sybreon
  // 32-bit LUT in IEEE754 hex representation
194 112 sybreon
  float newt_lut[] = {
195 123 sybreon
    0.000000000000000000000000,
196
    1.000000000000000000000000,
197
    1.414213538169860839843750,
198
    1.732050776481628417968750,
199
    2.000000000000000000000000,
200
    2.236068010330200195312500,
201
    2.449489831924438476562500,
202
    2.645751237869262695312500,
203
    2.828427076339721679687500,
204
    3.000000000000000000000000,
205
    3.162277698516845703125000,
206
    3.316624879837036132812500,
207
    3.464101552963256835937500,
208
    3.605551242828369140625000,
209
    3.741657495498657226562500
210 112 sybreon
  };
211
 
212 122 sybreon
  for (n=0;n
213
    {
214
      newt = newtonSqrt(n);
215
      if (newt != newt_lut[n])
216 123 sybreon
        {
217 122 sybreon
          return EXIT_FAILURE;
218
        }
219
    }
220
 
221 112 sybreon
  return EXIT_SUCCESS;
222
}
223
 
224
#endif
225
 
226
/*
227
$log$
228
*/

powered by: WebSVN 2.1.0

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