arith05.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  arith05.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
44void
45check_string( const sc_signed& z, int v )
46{
47    std::string buf( z.to_string( SC_BIN ) );
48    if (z < 0) {
49        sc_assert(buf[2] == '1');
50    } else {
51        sc_assert(buf[2] == '0');
52    }
53}
54
55int
56sc_main( int argc, char* argv[] )
57{
58    unsigned vali[5] = { 0, 1, (unsigned)-1, 7, (unsigned)-8 };
59    unsigned valj[5] = { 0, 1, (unsigned)-1, 7, (unsigned)-8 };
60
61    for (unsigned i = 3; i < 32; ++i) {
62        for (unsigned j = 3; j < 32; ++j) {
63            cout << "i = " << i << ", j = " << j << endl;
64
65            sc_unsigned x(i);
66            sc_unsigned y(j);
67            sc_unsigned z(64);
68
69            vali[3] = (1 << (i - 1)) - 1;
70            vali[4] = - (1 << (i - 1));
71
72            valj[3] = (1 << (j - 1)) - 1;
73            valj[4] = - (1 << (j - 1));
74
75            for (unsigned ii = 0; ii < 100; ++ii) {
76                for (unsigned jj = 0; jj < 100; ++jj) {
77                    unsigned qi = (ii < 5) ? vali[ii] : (rng.rand() & ((1 << i) - 1));
78                    unsigned qj = (jj < 5) ? valj[jj] : (rng.rand() & ((1 << j) - 1));
79                    unsigned tqi;
80
81                    if (qi & (1 << (i - 1))) {
82                        qi = (qi << (32 - i)) >> (32 - i);
83                    }
84                    if (qj & (1 << (j - 1))) {
85                        qj = (qj << (32 - j)) >> (32 - j);
86                    }
87
88                    x = qi;
89                    tqi = qi;
90                    sc_assert( x == qi );
91                    y = qj;
92                    sc_assert( y == qj );
93                    sc_assert((x == qj) == (qi == qj));
94                    sc_assert((x == qj) == (qj == x));
95                    sc_assert((x != qj) == (qi != qj));
96                    sc_assert((x != qj) == (qj != x));
97                    sc_assert((x < qj) == (qi < qj));
98                    sc_assert((x < qj) == (qj > x));
99                    sc_assert((x <= qj) == (qi <= qj));
100                    sc_assert((x <= qj) == (qj >= x));
101                    sc_assert((x > qj) == (qi > qj));
102                    sc_assert((x > qj) == (qj < x));
103                    sc_assert((x >= qj) == (qi >= qj));
104                    sc_assert((x >= qj) == (qj <= x));
105
106                    x += y;
107                    tqi += qj;
108                    tqi = (tqi << (32 - i)) >> (32 - i);
109                    sc_assert( x == tqi );
110
111                    x = qi;
112                    tqi = qi;
113                    x -= y;
114                    tqi -= qj;
115                    tqi = (tqi << (32 - i)) >> (32 - i);
116                    sc_assert( x == tqi );
117
118                    x = qi;
119                    tqi = qi;
120                    x *= y;
121                    tqi *= qj;
122                    tqi = (tqi << (32 - i)) >> (32 - i);
123                    sc_assert( x == tqi );
124
125                    if (y != 0) {
126                        x = qi;
127                        tqi = qi;
128                        x /= y;
129                        tqi /= qj;
130                        tqi = (tqi << (32 - i)) >> (32 - i);
131                        sc_assert( x == tqi );
132
133                        x = qi;
134                        tqi = qi;
135                        x %= y;
136                        tqi %= qj;
137                        tqi = (tqi << (32 - i)) >> (32 - i);
138                        sc_assert( x == tqi );
139                    }
140
141                    x = qi;
142                    tqi = qi;
143                    x &= y;
144                    tqi &= qj;
145                    tqi = (tqi << (32 - i)) >> (32 - i);
146                    sc_assert( x == tqi );
147
148                    x = qi;
149                    tqi = qi;
150                    x |= y;
151                    tqi |= qj;
152                    tqi = (tqi << (32 - i)) >> (32 - i);
153                    sc_assert( x == tqi );
154
155                    x = qi;
156                    tqi = qi;
157                    x ^= y;
158                    tqi ^= qj;
159                    tqi = (tqi << (32 - i)) >> (32 - i);
160                    sc_assert( x == tqi );
161
162                    if (jj < i - 1) {
163                        x = qi;
164                        tqi = qi;
165                        x <<= jj;
166                        tqi <<= jj;
167                        tqi = (tqi << (32 - i)) >> (32 - i);
168                        sc_assert( x == tqi );
169
170                        x = qi;
171                        tqi = qi;
172                        x >>= jj;
173                        tqi >>= jj;
174                        tqi = (tqi << (32 - i)) >> (32 - i);
175                        sc_assert( x == tqi );
176                    }
177                }
178            }
179        }
180    }
181    return 0;
182}
183