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  arith10.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(65), q(65);
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                    y = qj;
78                    z = x * y;
79                    sc_assert( static_cast<sc_bigint<32> >( z.range(31,0) ) ==
80                            (qi * qj) );
81                    bool s;
82                    s = ((x < 0) != (y < 0));
83                    sc_signed x2(i+1);
84                    x2 = x;
85                    if (x < 0) {
86                        x2 = - x;
87                    }
88                    sc_signed y2(j+1);
89                    y2 = y;
90                    if (y < 0) {
91                        y2 = - y;
92                    }
93
94                    sc_unsigned xhi(16), xlo(16);
95                    sc_unsigned yhi(16), ylo(16);
96		    sc_unsigned zero(16);
97		    zero = 0;
98                    xlo = i > 14 ? x2.range(15,0) : x2.range(i,0);
99                    xhi = i > 15 ? x2.range(i,16) : zero;
100                    ylo = j > 14 ? y2.range(15,0) : y2.range(j,0);
101                    yhi = j > 15 ? y2.range(j,16) : zero;
102                    q = (xlo * ylo) +
103                        (xhi * ylo + xlo * yhi) * 65536 +
104                        ((xhi * yhi) * 65536) * 65536;
105                    if (s)
106                        q = - q;
107                    if (z.range(63,0) != q.range(63,0)) {
108                        cout << "xlo = " << xlo << endl;
109                        cout << "xhi = " << xhi << endl;
110                        cout << "ylo = " << ylo << endl;
111                        cout << "yhi = " << yhi << endl;
112                        sc_assert(false);
113                    }
114                }
115            }
116        }
117    }
118    return 0;
119}
120