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

Subversion Repositories aemb

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

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

Line No. Rev Author Line
1 112 sybreon
/* $Id: literate.hh,v 1.1 2008-04-11 15:31:47 sybreon Exp $
2
**
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
 
31
#ifndef LITERATE_HH
32
#define LITERATE_HH
33
 
34
/*
35
FIBONACCI TEST
36
http://en.literateprograms.org/Fibonacci_numbers_(C)
37
 
38
  This tests for the following:
39
  - Recursion & Iteration
40
  - 32/16/8-bit data handling
41
*/
42
 
43
unsigned int fibSlow(unsigned int n)
44
{
45
  return n < 2 ? n : fibSlow(n-1) + fibSlow(n-2);
46
}
47
 
48
unsigned int fibFast(unsigned int n)
49
{
50
  unsigned int a[3];
51
  unsigned int *p=a;
52
  unsigned int i;
53
 
54
  for(i=0; i<=n; ++i) {
55
    if(i<2) *p=i;
56
    else {
57
      if(p==a) *p=*(a+1)+*(a+2);
58
      else if(p==a+1) *p=*a+*(a+2);
59
      else *p=*a+*(a+1);
60
    }
61
    if(++p>a+2) p=a;
62
  }
63
 
64
  return p==a?*(p+2):*(p-1);
65
}
66
 
67
int fibonacciTest(int max) {
68
  unsigned int n;
69
  unsigned int fast, slow;
70
  // 32-bit LUT
71
  unsigned int fib_lut32[] = {
72
    0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233
73
  };
74
  // 16-bit LUT
75
  unsigned short fib_lut16[] = {
76
    0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233
77
  };
78
  // 8-bit LUT
79
  unsigned char fib_lut8[] = {
80
    0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233
81
  };
82
 
83
  for (n=0;n
84
    slow = fibSlow(n);
85
    fast = fibFast(n);
86
    if ((slow != fast) ||
87
        (fast != fib_lut32[n]) ||
88
        (fast != fib_lut16[n]) ||
89
        (fast != fib_lut8[n])) {
90
      return EXIT_FAILURE;
91
    }
92
  }
93
  return EXIT_SUCCESS;
94
}
95
 
96
/*
97
   EUCLIDEAN TEST
98
   http://en.literateprograms.org/Euclidean_algorithm_(C)
99
 
100
   This tests for the following:
101
   - Modulo arithmetic
102
   - Goto
103
*/
104
 
105
int euclidGCD(int a, int b) {
106
  if (b > a) goto b_larger;
107
  while (1) {
108
    a = a % b;
109
    if (a == 0) return b;
110
  b_larger:
111
    b = b % a;
112
    if (b == 0) return a;
113
  }
114
}
115
 
116
int euclideanTest(int max)
117
{
118
  int n;
119
  int euclid;
120
  // Random Numbers
121
  int euclid_a[] = {
122
    1804289383, 1681692777, 1957747793, 719885386, 596516649,
123
    1025202362, 783368690, 2044897763, 1365180540, 304089172,
124
    35005211, 294702567, 336465782, 278722862
125
  };
126
  int euclid_b[] = {
127
    846930886, 1714636915, 424238335, 1649760492, 1189641421,
128
    1350490027, 1102520059, 1967513926, 1540383426, 1303455736,
129
    521595368, 1726956429, 861021530, 233665123
130
  };
131
 
132
  // GCD
133
  int euclid_lut[] = {
134
    1, 1, 1, 2, 1, 1, 1, 1, 6, 4, 1, 3, 2, 1
135
  };
136
 
137
  for (n=0;n
138
    euclid = euclidGCD(euclid_a[n],euclid_b[n]);
139
    if (euclid != euclid_lut[n]) {
140
      return EXIT_FAILURE;
141
    }
142
  }
143
  return EXIT_SUCCESS;
144
}
145
 
146
/**
147
   NEWTON-RHAPSON
148
   http://en.literateprograms.org/Newton-Raphson's_method_for_root_finding_(C)
149
 
150
   This tests for the following:
151
   - Multiplication & Division
152
   - Floating point arithmetic
153
   - Integer to Float conversion
154
*/
155
 
156
float newtonSqrt(float n)
157
{
158
  float x = 0.0;
159
  float xn = 0.0;
160
  int iters = 0;
161
  int i;
162
  for (i = 0; i <= (int)n; ++i)
163
    {
164
      float val = i*i-n;
165
      if (val == 0.0)
166
        return i;
167
      if (val > 0.0)
168
        {
169
          xn = (i+(i-1))/2.0;
170
          break;
171
        }
172
    }
173
  while (!(iters++ >= 100
174
           || x == xn))
175
    {
176
      x = xn;
177
      xn = x - (x * x - n) / (2 * x);
178
    }
179
  return xn;
180
}
181
 
182
int newtonTest (int max) {
183
  int n;
184
  float newt;
185
  // 32-bit LUT
186
  float newt_lut[] = {
187
    0.000000000000000000000000,
188
    1.000000000000000000000000,
189
    1.414213538169860839843750,
190
    1.732050776481628417968750,
191
    2.000000000000000000000000,
192
    2.236068010330200195312500,
193
    2.449489831924438476562500,
194
    2.645751237869262695312500,
195
    2.828427076339721679687500,
196
    3.000000000000000000000000,
197
    3.162277698516845703125000,
198
    3.316624879837036132812500,
199
    3.464101552963256835937500,
200
    3.605551242828369140625000,
201
    3.741657495498657226562500
202
  };
203
 
204
  for (n=0;n
205
    newt = newtonSqrt(n);
206
    if (newt != newt_lut[n]) {
207
      return EXIT_FAILURE;
208
    }
209
  }
210
  return EXIT_SUCCESS;
211
}
212
 
213
#endif
214
 
215
/*
216
$log$
217
*/

powered by: WebSVN 2.1.0

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