arith02.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 arith02.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 signed int vali[5] = { 0, 1, -1, 7, -8 }; 59 signed int valj[5] = { 0, 1, -1, 7, -8 }; 60 61 for (int i = 3; i < 32; ++i) { 62 for (int j = 3; j < 32; ++j) { 63 cout << "i = " << i << ", j = " << j << endl; 64 65 sc_signed x(i); 66 sc_signed y(j); 67 sc_signed 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 /* 76 // old code - takes too much time. 77 for (int ii = 0; ii < 100; ++ii) { 78 for (int jj = 0; jj < 100; ++jj) { 79 */ 80 for (int ii = 0; ii < 10; ++ii) { 81 for (int jj = 0; jj < 10; ++jj) { 82 signed int qi = (ii < 5) ? vali[ii] : (rng.rand() & ((1 << i) - 1)); 83 signed int qj = (jj < 5) ? valj[jj] : (rng.rand() & ((1 << j) - 1)); 84 85 if (qi & (1 << (i - 1))) { 86 qi = (qi << (32 - i)) >> (32 - i); 87 } 88 if (qj & (1 << (j - 1))) { 89 qj = (qj << (32 - j)) >> (32 - j); 90 } 91 92 x = qi; 93 sc_assert( x == qi ); 94 y = qj; 95 sc_assert( y == qj ); 96 sc_assert((x == qj) == (qi == qj)); 97 sc_assert((x == qj) == (qj == x)); 98 sc_assert((x != qj) == (qi != qj)); 99 sc_assert((x != qj) == (qj != x)); 100 sc_assert((x < qj) == (qi < qj)); 101 sc_assert((x < qj) == (qj > x)); 102 sc_assert((x <= qj) == (qi <= qj)); 103 sc_assert((x <= qj) == (qj >= x)); 104 sc_assert((x > qj) == (qi > qj)); 105 sc_assert((x > qj) == (qj < x)); 106 sc_assert((x >= qj) == (qi >= qj)); 107 sc_assert((x >= qj) == (qj <= x)); 108 z = x + y; 109 sc_assert( static_cast<sc_bigint<32> >( z.range(31,0) ) == 110 (qi + qj) ); 111 check_string( z, qi + qj ); 112 z = x - y; 113 sc_assert( static_cast<sc_bigint<32> >( z.range(31,0) ) == 114 (qi - qj) ); 115 check_string( z, qi - qj ); 116 z = x * y; 117 sc_assert( static_cast<sc_bigint<32> >( z.range(31,0) ) == 118 (qi * qj) ); 119 check_string( z, qi * qj ); 120 if (y != 0) { 121 z = x / y; 122 sc_assert( static_cast<sc_bigint<32> >( z.range(31,0) ) == 123 (qi / qj) ); 124 check_string( z, qi / qj ); 125 z = x % y; 126 sc_assert( static_cast<sc_bigint<32> >( z.range(31,0) ) == 127 (qi % qj) ); 128 check_string( z, qi % qj ); 129 } 130 z = x & y; 131 sc_assert( static_cast<sc_bigint<32> >( z.range(31,0) ) == 132 (qi & qj) ); 133 check_string( z, qi & qj ); 134 z = x | y; 135 sc_assert( static_cast<sc_bigint<32> >( z.range(31,0) ) == 136 (qi | qj) ); 137 check_string( z, qi | qj ); 138 z = x ^ y; 139 sc_assert( static_cast<sc_bigint<32> >( z.range(31,0) ) == 140 (qi ^ qj) ); 141 check_string( z, qi ^ qj ); 142 if (jj < i - 1) { 143 z = x << jj; 144 for (int r = 0; r < i; ++r) { 145 sc_assert( (bool) z[r] == !!((qi << jj) & (1 << r)) ); 146 } 147 z = x >> jj; 148 for (int r = 0; r < i; ++r) { 149 sc_assert( (bool) z[r] == !!((qi >> jj) & (1 << r)) ); 150 } 151 } 152 } 153 } 154 } 155 } 156 return 0; 157} 158