arith08.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  arith08.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 "systemc.h"
39#include "isaac.h"
40
41QTIsaac<8> rng;		// Platform independent random number generator.
42
43#define TEST(A,B) \
44{ \
45    if ( A != B ) \
46        cout << #A << " (" << A << ") != " << #B << "(" << B << ")" << endl; \
47}
48
49int
50sc_main(int, char**)
51{
52    unsigned vali[5] = { 0, 1, (unsigned)-1, 7, (unsigned)-8 };
53    signed int valj[5] = { 0, 1, -1, 7, -8 };
54
55    for (int i = 3; i < 30; ++i) {
56        for (int j = 3; j < 30; ++j) {
57            cout << "i = " << i << ", j = " << j << endl;
58            sc_unsigned x(i);
59            sc_signed y(j);
60            sc_signed z(64);
61
62            vali[3] = (1 << (i - 1)) - 1;
63            vali[4] = - (1 << (i - 1));
64
65            valj[3] = (1 << (j - 1)) - 1;
66            valj[4] = - (1 << (j - 1));
67
68            for (int ii = 0; ii < 100; ++ii) {
69                for (int jj = 0; jj < 100; ++jj) {
70                    unsigned qi = (ii < 5) ? vali[ii] :
71		                             (rng.rand() & ((1 << i) - 1));
72                    signed int qj = (jj < 5) ? valj[jj] :
73		                               (rng.rand() & ((1 << j) - 1));
74
75                    if (qi & (1 << (i - 1))) {
76                        qi = (qi << (32 - i)) >> (32 - i);
77                    }
78                    if (qj & (1 << (j - 1))) {
79                        qj = (qj << (32 - j)) >> (32 - j);
80                    }
81
82                    x = qi;
83                    y = qj;
84
85                    sc_signed ty(x);
86                    TEST(x,ty );
87                    sc_assert( ty >= 0 );
88                    sc_assert((! x[i-1]) || (ty.length() == i+1) );
89
90                    z = x + y;
91                    TEST(static_cast<sc_bigint<32> >( z.range(31,0) ),
92			    int(int(qi) + qj) );
93                    z = x - y;
94                    TEST(static_cast<sc_bigint<32> >( z.range(31,0) ),
95			    int(int(qi) - qj) );
96                    z = x * y;
97                    TEST(static_cast<sc_bigint<32> >( z.range(31,0) ),
98			    int(int(qi) * qj) );
99                    sc_unsigned xx(i);
100                    xx = x;
101                    xx *= y;
102                    sc_unsigned xx2(i);
103                    xx2 = z.range(i - 1, 0);
104                    TEST(xx,xx2);
105
106                    if (y != 0) {
107                        z = x / y;
108                        TEST(static_cast<sc_bigint<32> >( z.range(31,0) ), int(int(qi) / qj) );
109                        z = x % y;
110                        TEST(static_cast<sc_bigint<32> >( z.range(31,0) ), int(int(qi) % qj) );
111                    }
112                    z = x & y;
113                    TEST(static_cast<sc_bigint<32> >( z.range(31,0) ), int(int(qi) & qj) );
114                    z = x | y;
115                    TEST(static_cast<sc_bigint<32> >( z.range(31,0) ), int(int(qi) | qj) );
116                    z = x ^ y;
117                    TEST(static_cast<sc_bigint<32> >( z.range(31,0) ), int(int(qi) ^ qj) );
118                }
119            }
120        }
121    }
122    return 0;
123}
124