arith09.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 arith09.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 unsigned int vali[5] = { 0, 1, (unsigned)-1, 7, (unsigned)-8 }; 48 unsigned int valj[5] = { 0, 1, (unsigned)-1, 7, (unsigned)-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_unsigned x(i); 55 sc_unsigned y(j); 56 sc_unsigned z(64), q(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 unsigned qi = (ii < 5) ? vali[ii] : 67 (rng.rand() & ((1 << i) - 1)); 68 unsigned qj = (jj < 5) ? valj[jj] : 69 (rng.rand() & ((1 << j) - 1)); 70 71 if (qi & (1 << (i - 1))) { 72 qi = (qi << (32 - i)) >> (32 - i); 73 } 74 if (qj & (1 << (j - 1))) { 75 qj = (qj << (32 - j)) >> (32 - j); 76 } 77 78 x = qi; 79 y = qj; 80 z = x * y; 81 sc_assert( (sc_unsigned)( z.range(31,0) ) == 82 (qi * qj) ); 83 if ( i < 17 ) 84 { 85 if ( j < 17 ) // (i < 17) 86 { 87 q = (x.range(i-1,0) * y.range(j-1,0)); 88 } 89 else // (j >= 17) (i < 17) 90 { 91 q = (x.range(i-1,0) * y.range(15,0)) + 92 ( x.range(i-1,0) * y.range(j-1,16) ) * 65536; 93 } 94 } 95 else 96 { 97 if ( j < 17 ) // (i >= 17) 98 { 99 q = (x.range(15,0) * y.range(j-1,0)) + 100 (x.range(i-1,16) * y.range(j-1,0)) * 65536; 101 } 102 else // (j >= 17) (i >= 17) 103 { 104 q = (x.range(15,0) * y.range(15,0)) + 105 (x.range(i-1,16) * y.range(15,0) + 106 x.range(15,0) * y.range(j-1,16)) * 65536 + 107 ((x.range(i-1,16) * y.range(j-1,16) * 65536) 108 * 65536); 109 } 110 } 111 sc_assert(z == q); 112 113 if (y != 0) { 114 z /= y; 115 sc_assert(z == x); 116 } 117 } 118 } 119 } 120 } 121 return 0; 122} 123