GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — SRT-Division-Simulator/srt.cpp at master · emonrm/SRT-Division-Simulator Expand file tree/ Copy pathsrt.cppMore file actions 419 lines (367 loc) · 14.3 KB / Copy pathsrt.cppFile metadata and controls419 lines (367 loc) · 14.3 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 /* * Author: Mahbubur Rahman */ #include <iostream> #include <cstdlib> #include <string.h> #include <fstream> #include <iomanip> #include <sstream> using namespace std; #define debug false // only shows the results after applying SRT method #define simulation true // enable to see all the simulaiton steps inside SRT method #define dout debug && cout #define sout simulation && cout int total_cost = 0; // counts the total execution time in delta_t /////////////////////// Compound result structure /////////////////// //self explanatory class result{ public: string remainder, quotient; int execution_time; }; ///////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////// print helper functions//////////////////////////////////////////////////////////////////////////////////////////////// void println(){dout << endl;} //specilized print function: decreses visual representation complexity for showing args in console void println (string str){if(str[0] == '1'){str.insert(1, ".");}else if (str[0] == '0') {str[0] = '.';}else{}dout << str << endl;} //specilized print function: decreses visual representation complexity for showing args in console void print(string str){if(str[0] == '1'){str.insert(1, ".");}else if (str[0] == '0'){str[0] = '.';}else{}dout << " " << str << " ";} //prints in special format to show simulaiton steps void print_simulation_step(string description, string str, int b = 0, bool ir = false, bool st = false){ int w = 5; int x = 15; if((str[0] == '1') && !st){str.insert(1, "."); w--;}else if (str[0] == '0') {str[0] = '.';}else{} if (!b) str.insert(str.length()/2 + 1, " "); if(b == 1) {str = "\33[4m" + str + "\33[0m";} sout << std::setw(25) << std::left << description; sout << std::setw(w) << std::left << ":"; sout << str << std::setw(x) << std::right; if ((!b && !ir) || (b == 2 && ir)) sout << total_cost << " Δt"; sout << endl; } //print any label in middle of the window void print_simulation_step(string step, int w){sout << std::setw(w) << std::right << "\33[4m" <<step << "\33[0m" << endl;} //just a new line in simulaiton step void print_simulation_step(){sout << endl;} //print the result in proper format void println(string aq, string b, result res){ dout<<"A = "<<aq<<"\tB = "<<b<<"\nRemainder = "<<res.remainder<<"\tQuotient = "<<res.quotient<<"\tT = "<<res.execution_time<<" Δt."; dout << endl << endl; } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////// check divide overflow function //////////////////////// // arg1 is value of A, arg2 is the value of B // returns 1 if devide by zero overflow occurs // returns 2 if a >= b overflow occurs, // 0 is no overflow int arg_check_overflow(const string & arg1, const string & arg2){ int overflow = 1; //assuming devide by zero overflow will occur // check if B is zero for (int i = 1; i < arg2.length(); i++){ if(arg2[i] == '1'){ overflow = 0; // B has at least one '1' bit break; } } if (overflow){ return overflow; } // return divide by zero overflow if occurs; //check if A >= B //Assuming this overflow will occur overflow = 2; for(int j = 1; j < arg2.length(); j++){ // if no condition is satisfied inside forloop, A = B if((arg1[j] != arg2[j]) && (arg2[j] == '1')){ // confirms that B > A overflow = 0; break; } else if((arg1[j] != arg2[j]) && (arg2[j] == '0')){ // confirms that A > B break; } } return overflow; } /////////////////////////////////////////////////////////////////////////////// ////////////////////// Normalize Args///////////////////////// // arg: inplace normalization pointer for argmuent // factor: number of bits normalized // mode: defines whether normalizing or adjusting. default: normalization void arg_normalize(string & arg, int & factor, int mode = 0){ // normalize upto normalization factor if (mode == 1){ int j = 1; while (j <= factor) { for(int i = 1; i < arg.length()-1; i++){ arg[i] = arg[i+1]; } arg[arg.length()-1] = '*'; j++; total_cost += 3; // each shift for adjustment costs 3dt } } // normalize and find the normalization factor else { while (arg[1] == '0'){ for (int i = 1; i < arg.length()-1; i ++){ arg[i] = arg[i+1]; } arg[arg.length()-1] = '0'; factor++; total_cost += 3; // each shift takes 3 dt time } } } ////////////////////////////////////////////////////////////// ////////////////////// Compliment function /////////////////// // arg1: unsigned value // arg2: 2's compliment void arg_compliment(const string & arg1, string & arg2){ //string arg2 = arg1; total_cost += (arg1.length() -1); // cost for 2's complitent , n * dt , n : numer of bits in arg1 arg2 = arg1; arg2[0] = '1'; // represents negetive sign for (int i = 1; i < arg1.length(); i ++){ if(arg1[i] == '0') arg2[i] = '1'; else arg2 [i] = '0'; } // if the least significant bit is 0, just change it to 1 and return if (arg2[arg1.length()-1] == '0'){ arg2[arg1.length()-1] = '1'; return; } // least significant bit is not 0 // can't change the zero index, that represents the sign bit, j must be > 0. // normalizaiton also makes sure that adding 1 does not overflow for (int j = arg1.length()-1; j > 0; j --){ if (arg2[j] == '0'){ arg2[j] = '1'; break; } arg2[j] = '0'; } } ////////////////////////////////////////////////////////////// /////////////////////// Skip/Shift over zeroes ////////////// //arg: aq //count: shift_count , will be decresing //ch: skip over 0 or 1 int arg_skip_over(string & arg, int & count, char ch){ int flag = 0; int skiped = 0; while (arg[1] == ch){ skiped = 1; for (int i = 1; i < arg.length() -1; i++){ arg[i] = arg[i+1]; } arg[arg.length()-1] = ch; count--; total_cost += 3; // check if shift_count is already n+1 if (count == 0) { flag = 1; break; } // we are done as far as SRT methods step count's concern } if(skiped){ string des = "Shift Over "; stringstream ss; ss << ch; string temp; ss >> temp; des = des + temp + "'s"; print_simulation_step(des, arg); } return (flag == 1)? 0 : 1 ; } ///////////////////////////////////////////////////////////// //////////////////////// Add function (if-else-logic) /////////////////////// //arg1: content of aq //arg2: b or -b /* addition logic Cin A B R Cout 0 0 0 0 0 0 0 1 1 0 0 1 0 1 0 0 1 1 0 1 1 0 0 1 0 1 0 1 0 1 1 1 0 0 1 1 1 1 1 1 */ void arg_add(string & arg1, const string & arg2){ char carry = '0'; for (int i = arg2.length(); i >= 0; i--){ if (arg1[i] == '*') continue; // we don't perform addition on bit marked as * if(carry == '0' && arg1[i] == '0' && arg2[i] == '0'){ arg1[i] = '0'; carry = '0'; } else if(carry == '0' && arg1[i] == '0' && arg2[i] == '1'){ arg1[i] = '1'; carry = '0'; } else if(carry == '0' && arg1[i] == '1' && arg2[i] == '0'){ arg1[i] = '1'; carry = '0'; } else if(carry == '0' && arg1[i] == '1' && arg2[i] == '1'){ arg1[i] = '0'; carry = '1'; } else if(carry == '1' && arg1[i] == '0' && arg2[i] == '0'){ arg1[i] = '1'; carry = '0'; } else if(carry == '1' && arg1[i] == '0' && arg2[i] == '1'){ arg1[i] = '0'; carry = '1'; } else if(carry == '1' && arg1[i] == '1' && arg2[i] == '0'){ arg1[i] = '0'; carry = '1'; } else if(carry == '1' && arg1[i] == '1' && arg2[i] == '1'){ arg1[i] = '1'; carry = '1'; } else{ /* do nothing, could also check for * marker here but not neccessarily.*/ } } // discard the final C_out // cost calculation for one addition (carry select) int m = 2; // 2 bit full adder int N = arg2.length() - 1; // total number of bits to add int m_t = 6; // total time for m bit full adder int temp = (((N / m) - 1) * 2) + m_t; // generalized equation for cost calculation in addition total_cost += temp; } ///////////////////////////////////////////////////////////// //////////////////////// shift-left-replace-q0 ////////////// // arg: aq //ch: assign 0 or 1 to Q_0 void arg_shl_new_q(string & arg, char ch){ for (int i = 1; i < arg.length() -1; i++){ arg[i] = arg[i+1]; } arg[arg.length()-1] = ch; total_cost += 3; // one left shift operation costs 3 dt } ////////////////////////////////////////////////////////////// //////////////////////// SRT Method ////////////////////////// result division_srt(string aq, string b){ result div_result; // result will be placed here and send all together int adjustment_factor = 0; // calculate how many bits to normalize int shift_count = b.length(); // algorithm ends after exactly no-of-bits(b) + 1 left shifts string comp_b; // holds the 2's compliment of normalized b bool pos_result = true; // flag to keep track whether previsious result was negative or not aq[0] = b[0] = '0'; // removing the dot (.) and will be used as carry bit //////////// SRT algorithm starts //////////////////////////////////////////////////////////////////////////////////// print_simulation_step(); //blank line print_simulation_step(); print_simulation_step("Initializations", 20); print_simulation_step("A Q", aq, 0, true); print_simulation_step("B", b, 2); arg_normalize(b , adjustment_factor); // normalize b print_simulation_step("Normalize B", b, 2, true); arg_compliment(b, comp_b); // calculates 2's compliment of normalized b print_simulation_step("2's Com. (Normalize B)", comp_b, 2, true); print_simulation_step("SRT Steps", 25); arg_normalize(aq, adjustment_factor, 1); //adjust AQ, and insert * in LSBs print_simulation_step("Adjust AQ", aq); arg_skip_over(aq, shift_count, '0'); //shift over 0's and insert 0's in LSBs. left shift_count starts from here ! //simulation step is inside the function // check if shift_count is equal n + 1, n = number of bit in b while (shift_count > 0){ // substract B if (pos_result){ arg_add(aq, comp_b); print_simulation_step("Subtract B", comp_b, 1); } // check if result is positive or negetive if (aq[0] == '0'){ print_simulation_step("Positive result", aq); arg_shl_new_q(aq, '1'); // result positive, shift AQ left, q_0 = 1 shift_count--; print_simulation_step("Shift AQ left, q_0 <- 1", aq); // check if we already did n + 1 left shifts if(shift_count == 0){ break; } // skip over zeroes if( ! arg_skip_over(aq, shift_count, '0')){ break; } if (! pos_result){ pos_result = true; } } else{ pos_result = false; print_simulation_step("Negative result", aq); arg_shl_new_q(aq, '0'); // result negetive, shift AQ left, q_0 = 0 shift_count--; print_simulation_step("Shift AQ left, q_0 <- 0", aq); // check if we already did n + 1 left shifts if(shift_count == 0){ break; } // skip over 1's if( ! arg_skip_over(aq, shift_count, '1')){ break; } arg_add(aq, b); // add B to A print_simulation_step("Add B", b, 1); } } //////////////////////////////Remainder Correction/////////////////////////////// // Remainder is negative, correct it. bool i_r = false; if(aq[0] == '1'){ i_r = true; print_simulation_step("Remainder Correction", 20); // Right Shift A once for(int i = b.length()-1; i >0 ; i--){ aq[i] = aq[i-1]; } total_cost += 3; // one right shift while correcting the remainder, costs 3 dt print_simulation_step("RSH A", aq); arg_add(aq, b); // Add B to A print_simulation_step("Add B", b, 1); } //////Now AQ has the Remainder & Quotient, process the result///////////////////// //print_simulation_step("A Q", aq, 0, true); // original code print_simulation_step("A Q", aq, 0, i_r? false:true); aq[0] = b[0] = '.'; // all the calculaitons are done, change back to original form string quo = "." + aq.substr(b.length(), aq.length()); // get the quotient from Q int r_factor = 1; for (; (aq[r_factor] != '*') && (r_factor < b.length()) ; r_factor++); // find the position of * in A string rmdr = aq.substr(0, r_factor); // get temporary Remainder value // Convert to actual value of Remainder for(int i = 0; i < aq.length() - r_factor; i++){ rmdr.insert(1, "0"); // insert as many zeroes as needed } print_simulation_step("Results", 25); print_simulation_step("Remainder", rmdr, 2); print_simulation_step("Quotient", quo, 2); stringstream ss; ss << total_cost; string temp; ss >> temp; print_simulation_step("Total Cost", temp + " Δt", 2, false, true); print_simulation_step(); print_simulation_step(); //////////////////////////// Load the result into result object//////////////////// div_result.remainder = rmdr; div_result.quotient = quo; div_result.execution_time = total_cost; total_cost = 0; // set the total cost count to zero again return div_result; } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////// MAIN FUNCTION ////////////////////////////////////////////////////////////////// int main(int argc, char * argv[]){ string arg_aq, arg_b; int overflow_type = 0; ifstream file("input.txt"); if (!file.is_open()) {cout << "Can't open file !" << endl; exit(EXIT_FAILURE);} int iteration_count = 1; while (! file.eof()){ file >> arg_aq >> arg_b; // read input args from file cout << "\n(" << iteration_count++ << ")\n"; ////////////////Devide overflow testing///////////////// overflow_type = arg_check_overflow(arg_aq, arg_b); if (overflow_type == 1){ cout << "\nA Q = " << arg_aq << " B = " << arg_b << endl; cout << "Divide by Zero overflow occured! \n" << endl; continue; } else if(overflow_type == 2){ cout << "\nA Q = " << arg_aq << " B = " << arg_b << endl; cout << "A >= B overflow occured! \n" << endl; continue; } /////////////////////////////////////////////////////////// result res = division_srt(arg_aq, arg_b); // store the result in res. println(arg_aq, arg_b, res); } file.close(); return 0; } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |