arith06.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  arith06.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#include <stdlib.h>
39#include "systemc.h"
40#include "isaac.h"
41
42QTIsaac<8> rng;		// Platform independent random number generator.
43
44int
45sc_main( int argc, char* argv[] )
46{
47    signed int vali[5] = { 0, 1, -1, 7, -8 };
48    signed int valj[5] = { 0, 1, -1, 7, -8 };
49
50    for (int i = 3; i < 32; ++i) {
51        for (int j = 3; j < 32; ++j) {
52            cout << "i = " << i << ", j = " << j << endl;
53
54            sc_signed x(i);
55            sc_signed y(j);
56            sc_signed z(64);
57
58            vali[3] = (1 << (i - 1)) - 1;
59            vali[4] = - (1 << (i - 1));
60
61            valj[3] = (1 << (j - 1)) - 1;
62            valj[4] = - (1 << (j - 1));
63
64            for (int ii = 0; ii < 100; ++ii) {
65                for (int jj = 0; jj < 100; ++jj) {
66                    signed int qi = (ii < 5) ? vali[ii] : (rng.rand() & ((1 << i) - 1));
67                    signed int qj = (jj < 5) ? valj[jj] : (rng.rand() & ((1 << j) - 1));
68
69                    if (qi & (1 << (i - 1))) {
70                        qi = (qi << (32 - i)) >> (32 - i);
71                    }
72                    if (qj & (1 << (j - 1))) {
73                        qj = (qj << (32 - j)) >> (32 - j);
74                    }
75
76                    x = qi;
77                    sc_assert( x == qi );
78                    y = qj;
79                    sc_assert( y == qj );
80                    sc_assert((x == qj) == (qi == qj));
81                    sc_assert((x == qj) == (qj == x));
82                    sc_assert((x != qj) == (qi != qj));
83                    sc_assert((x != qj) == (qj != x));
84                    sc_assert((x < qj) == (qi < qj));
85                    sc_assert((x < qj) == (qj > x));
86                    sc_assert((x <= qj) == (qi <= qj));
87                    sc_assert((x <= qj) == (qj >= x));
88                    sc_assert((x > qj) == (qi > qj));
89                    sc_assert((x > qj) == (qj < x));
90                    sc_assert((x >= qj) == (qi >= qj));
91                    sc_assert((x >= qj) == (qj <= x));
92                    z = x + qj;
93                    sc_assert( static_cast<sc_bigint<32> >( z.range(31,0) ) ==
94			    (qi + qj) );
95                    z = qi + y;
96                    sc_assert( static_cast<sc_bigint<32> >( z.range(31,0) ) ==
97			    (qi + qj) );
98                    z = x - qj;
99                    sc_assert( static_cast<sc_bigint<32> >( z.range(31,0) ) ==
100			    (qi - qj) );
101                    z = qi - y;
102                    sc_assert( static_cast<sc_bigint<32> >( z.range(31,0) ) ==
103			    (qi - qj) );
104                    z = x * qj;
105                    sc_assert( static_cast<sc_bigint<32> >( z.range(31,0) ) ==
106			    (qi * qj) );
107                    z = qi * y;
108                    sc_assert( static_cast<sc_bigint<32> >( z.range(31,0) ) ==
109			    (qi * qj) );
110                    if (qj != 0) {
111                        z = x / qj;
112                        sc_assert( static_cast<sc_bigint<32> >( z.range(31,0) ) ==
113				(qi / qj) );
114                        z = qi / y;
115                        sc_assert( static_cast<sc_bigint<32> >( z.range(31,0) ) ==
116				(qi / qj) );
117                        z = x % qj;
118                        sc_assert( static_cast<sc_bigint<32> >( z.range(31,0) ) ==
119				(qi % qj) );
120                        z = qi % y;
121                        sc_assert( static_cast<sc_bigint<32> >( z.range(31,0) ) ==
122				(qi % qj) );
123                    }
124                    z = x & qj;
125                    sc_assert( static_cast<sc_bigint<32> >( z.range(31,0) ) ==
126			    (qi & qj) );
127                    z = qi & y;
128                    sc_assert( static_cast<sc_bigint<32> >( z.range(31,0) ) ==
129			    (qi & qj) );
130                    z = x | qj;
131                    sc_assert( static_cast<sc_bigint<32> >( z.range(31,0) ) ==
132			    (qi | qj) );
133                    z = qi | y;
134                    sc_assert( static_cast<sc_bigint<32> >( z.range(31,0) ) ==
135			    (qi | qj) );
136                    z = x ^ qj;
137                    sc_assert( static_cast<sc_bigint<32> >( z.range(31,0) ) ==
138			    (qi ^ qj) );
139                    z = qi ^ y;
140                    sc_assert( static_cast<sc_bigint<32> >( z.range(31,0) ) ==
141			    (qi ^ qj) );
142                }
143            }
144        }
145    }
146    return 0;
147}
148