blv.cpp revision 12855:588919e0e4aa
1/*****************************************************************************
2
3  Licensed to Accellera Systems Initiative Inc. (Accellera) under one or
4  more contributor license agreements.  See the NOTICE file distributed
5  with this work for additional information regarding copyright ownership.
6  Accellera licenses this file to you under the Apache License, Version 2.0
7  (the "License"); you may not use this file except in compliance with the
8  License.  You may obtain a copy of the License at
9
10    http://www.apache.org/licenses/LICENSE-2.0
11
12  Unless required by applicable law or agreed to in writing, software
13  distributed under the License is distributed on an "AS IS" BASIS,
14  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
15  implied.  See the License for the specific language governing
16  permissions and limitations under the License.
17
18 *****************************************************************************/
19
20/*****************************************************************************
21
22  blv.cpp --
23
24  Original Author: Martin Janssen, Synopsys, Inc., 2002-02-15
25
26 *****************************************************************************/
27
28/*****************************************************************************
29
30  MODIFICATION LOG - modifiers, enter your name, affiliation, date and
31  changes you are making here.
32
33      Name, Affiliation, Date:
34  Description of Modification:
35
36 *****************************************************************************/
37
38//----------------------------------------------------------
39// test the basic functionality of sc_bv<> and sc_lv<>
40//----------------------------------------------------------
41#include "systemc.h"
42#include <time.h>
43#include "isaac.h"
44
45QTIsaac<8> rng;
46
47template<class T>
48void compare(int W)
49{ // functionality verification
50  sc_bv_base x(W);
51  T st;
52  // initialize
53  for(int i=0; i<W; i++)
54  {
55    bool la = (rng.rand()&1) == 0;
56    x[i] = la;
57    st[i] = la;
58  }
59  if(st.to_string()!=x.to_string())
60    cout<<"\nERROR: x= "<<x<<" st= "<<st<<"\n"<<
61    "st="<<st<<"\nx="<<x<< endl;
62  if( (int) st.xor_reduce()!= x.xor_reduce())
63    cout<<"\nERROR: st.xor_reduce="<<(short)st.xor_reduce()<<"; x.xor_reduce="<<
64    (short)x.xor_reduce()<<"\n"<<
65    "st="<<st<<"\nx="<<x<< endl;
66  if( (int) x.or_reduce()!=st.or_reduce())
67     cout<<"\nERROR: st.or_reduce="<<(short)st.or_reduce()<<
68     "; x.or_reduce="<<(short)x.or_reduce()<<"\n"<<
69    "st="<<st<<"\nx="<<x<< endl;
70  if( (int) x.and_reduce()!=st.and_reduce())
71    cout<<"\nERROR: st.and_reduce="<<(short)st.and_reduce()<<
72    "; x.and_reduce="<<(short)x.and_reduce()<<"\n"<<
73    "st="<<st<<"\nx="<<x<< endl;
74
75  if((st,st).to_string()!=(x,x).to_string())
76    cout<<"\nERROR: st,st="<<(st,st)<<
77    "; x,x="<<(x,x)<<"\n"<<
78    "st="<<st<<"\nx="<<x<< endl;
79  int first = (int) ( (double) W * ((double)rng.rand() / (double)0x7fffffff));
80  int second = (int) ( (double) W * ((double)rng.rand() / (double)0x7fffffff));
81  if(st.range(first,second).to_string()!=x.range(first,second).to_string())
82    cout<<"st.range("<<first<<","<<second<<")="<<st.range(first,second)<<
83    "; x.range("<<first<<","<<second<<")="<<x.range(first,second)<<"\n"<<
84    "st="<<st<<"\nx="<<x<< endl;
85
86  sc_bv_base bv(2*W);
87  sc_bv_base xv(2*W);
88  bv = (st,st);
89  xv = (x,x);
90  if(bv.to_string()!=xv.to_string())
91    cout<<"\nERROR: bv(st,st)="<<bv<<"; xv(x,x)="<<xv<<"\n"<<
92    "st="<<st<<"\nx="<<x<< endl;
93  int Len=0;
94  if(first>second)
95    Len = first-second;
96  else
97    Len = second-first;
98  sc_bv_base br(Len+1);
99  sc_bv_base xr(Len+1);
100  br = st.range(first,second);
101  xr = x.range(first,second);
102  if(br.to_string()!=xr.to_string())
103  {
104    cout<<"\nERROR: br("<<first<<","<<second<<")!= xr("<<first<<","<<second
105        <<")" << endl;
106    br = st.range(first,second);
107    xr = x.range(first,second);
108    cout<<"br="<<br.to_string()<<"  xr="<<xr.to_string()<<
109    "st.range="<<st.range(first,second)<<"  x.range="<<x.range(first,second)<<
110    "st="<<st<<"\nx="<<x<< endl;
111  }
112
113  // verify assignments
114  long ra = rng.rand();
115  x  = ra;
116  st = ra;
117  if(st.to_string()!=x.to_string())
118    cout<<"\nERROR (assignment): x= "<<x<<" st= "<<st<<" original long="<<ra
119        << endl;
120}
121
122int sc_main(int, char**)
123{
124  const int N = 2000;
125  int Seed = rng.rand();
126  cout<<"\nverifying sc_bv<"<<N<<">" << endl;
127  try{
128    for(int i=0; i<1000; i++)
129      compare<sc_bv<N> >(N);
130  }
131  catch(...)
132  {
133    cout<<"Test failed due to exception in sc_bv. Seed = "<<Seed<< endl;
134    throw;
135  }
136  try{
137    cout<<"\nverifying sc_lv<"<<N<<">" << endl;
138    for(int i=0; i<1000; i++)
139      compare<sc_lv<N> >(N);
140  }
141  catch(...)
142  {
143    cout<<"Test failed due to exception in sc_lv. Seed = "<<Seed<< endl;
144    throw;
145  }
146
147  return 0;
148}
149