00001 /*!@file AppMedia/test-fastMath.C 00002 */ 00003 // //////////////////////////////////////////////////////////////////// // 00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2002 // 00005 // by the University of Southern California (USC) and the iLab at USC. // 00006 // See http://iLab.usc.edu for information about this project. // 00007 // //////////////////////////////////////////////////////////////////// // 00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00010 // in Visual Environments, and Applications'' by Christof Koch and // 00011 // Laurent Itti, California Institute of Technology, 2001 (patent // 00012 // pending; application number 09/912,225 filed July 23, 2001; see // 00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00014 // //////////////////////////////////////////////////////////////////// // 00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00016 // // 00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00018 // redistribute it and/or modify it under the terms of the GNU General // 00019 // Public License as published by the Free Software Foundation; either // 00020 // version 2 of the License, or (at your option) any later version. // 00021 // // 00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00025 // PURPOSE. See the GNU General Public License for more details. // 00026 // // 00027 // You should have received a copy of the GNU General Public License // 00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00030 // Boston, MA 02111-1307 USA. // 00031 // //////////////////////////////////////////////////////////////////// // 00032 // 00033 // Primary maintainer for this file: T. Nathan Mundhenk <mundhenk@usc.edu> 00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/AppMedia/test-fastMath.C $ 00035 // $Id: test-fastMath.C 10794 2009-02-08 06:21:09Z itti $ 00036 // 00037 00038 #include "Util/log.H" 00039 #include "Util/MathFunctions.H" 00040 #include "Util/Timer.H" 00041 #include <stdio.h> 00042 #include <stdlib.h> 00043 00044 int main(const int argc, const char** argv) 00045 { 00046 const int large_samples = (int)pow(2,8); 00047 const int loops = (int)pow(2,8); 00048 const int ticks_per_sec = 1000000; 00049 LINFO("Using %d large samples",large_samples); 00050 LINFO("Using %d loops",loops); 00051 double DLA[large_samples + 1]; 00052 float FLA[large_samples + 1]; 00053 int ILA[large_samples + 1]; 00054 00055 { 00056 for(int i = 0; i < large_samples; i++) 00057 { 00058 DLA[i] = pow(rand(),10*rand()); 00059 FLA[i] = (float)DLA[i]; 00060 ILA[i] = static_cast<int>(round(DLA[i])); 00061 } 00062 } 00063 00064 Timer tim(ticks_per_sec); 00065 LINFO("Ticks per second %d",tim.ticksPerSec()); 00066 tim.reset(); 00067 int t1,t2,t3; 00068 00069 // ###################################################################### 00070 { 00071 float store; 00072 t1 = tim.get(); 00073 for(int l = 0; l < loops; l++) 00074 { 00075 for(int k = 0; k < loops; k++) 00076 { 00077 store = 0.0f; 00078 for(int i = 0; i < large_samples; i++) 00079 { 00080 store += sqrt(FLA[i]); 00081 } 00082 } 00083 } 00084 t2 = tim.get(); 00085 t3 = t2 - t1; 00086 LINFO(">>> Time for float SQRT - %d mcs (result %f)",t3,store); 00087 } 00088 00089 // ###################################################################### 00090 { 00091 double store; 00092 t1 = tim.get(); 00093 for(int l = 0; l < loops; l++) 00094 { 00095 for(int k = 0; k < loops; k++) 00096 { 00097 store = 0.0F; 00098 for(int i = 0; i < large_samples; i++) 00099 { 00100 store += sqrt(DLA[i]); 00101 } 00102 } 00103 } 00104 t2 = tim.get(); 00105 t3 = t2 - t1; 00106 LINFO(">>> Time for double SQRT - %d mcs (result %f)",t3,store); 00107 } 00108 00109 LINFO("<<<NEXT>>>"); 00110 00111 // ###################################################################### 00112 { 00113 LINFO("fastSqrt_2 : Log 2 Approx Fast Square Root - Float"); 00114 const int samples = 7; 00115 00116 float A[samples]; 00117 float F[samples]; 00118 float N[samples]; 00119 00120 A[0] = 1.0f; 00121 A[1] = 2.0f; 00122 A[2] = 8.0f; 00123 A[3] = 100.0f; 00124 A[4] = M_PI; 00125 A[5] = 100000; 00126 A[6] = 1.0f/3.0f; 00127 00128 for(int i = 0; i < samples; i++) 00129 { 00130 F[i] = fastSqrt_2(A[i]); 00131 N[i] = sqrt(A[i]); 00132 LINFO("\t%d SQRT %f - Log 2 Fast %f Normal %f",i,A[i],F[i],N[i]); 00133 } 00134 00135 float store; 00136 t1 = tim.get(); 00137 for(int l = 0; l < loops; l++) 00138 { 00139 for(int k = 0; k < loops; k++) 00140 { 00141 store = 0.0f; 00142 for(int i = 0; i < large_samples; i++) 00143 { 00144 store += fastSqrt_2(FLA[i]); 00145 } 00146 } 00147 } 00148 t2 = tim.get(); 00149 t3 = t2 - t1; 00150 LINFO(">>> Time for Log 2 fast float SQRT - %d mcs (result %f)",t3,store); 00151 } 00152 00153 LINFO("<<<NEXT>>>"); 00154 00155 // ###################################################################### 00156 { 00157 LINFO("fastSqrt_Bab : Log 2 Babylonian Approx Fast Square Root - Float"); 00158 const int samples = 7; 00159 00160 float A[samples]; 00161 float F[samples]; 00162 float N[samples]; 00163 00164 A[0] = 1.0f; 00165 A[1] = 2.0f; 00166 A[2] = 8.0f; 00167 A[3] = 100.0f; 00168 A[4] = M_PI; 00169 A[5] = 100000; 00170 A[6] = 1.0f/3.0f; 00171 00172 for(int i = 0; i < samples; i++) 00173 { 00174 F[i] = fastSqrt_Bab(A[i]); 00175 N[i] = sqrt(A[i]); 00176 LINFO("\t%d SQRT %f - Log 2 Fast Babylonian %f Normal %f", 00177 i,A[i],F[i],N[i]); 00178 } 00179 00180 float store; 00181 t1 = tim.get(); 00182 for(int l = 0; l < loops; l++) 00183 { 00184 for(int k = 0; k < loops; k++) 00185 { 00186 store = 0.0f; 00187 for(int i = 0; i < large_samples; i++) 00188 { 00189 store += fastSqrt_Bab(FLA[i]); 00190 } 00191 } 00192 } 00193 t2 = tim.get(); 00194 t3 = t2 - t1; 00195 LINFO(">>> Time for Log 2 fast Babylonian float SQRT - %d mcs (result %f)",t3,store); 00196 } 00197 00198 LINFO("<<<NEXT>>>"); 00199 00200 // ###################################################################### 00201 { 00202 LINFO("fastSqrt_Q3 : Quake 3 Fast Square Root - Float"); 00203 const int samples = 7; 00204 00205 float A[samples]; 00206 float F[samples]; 00207 float N[samples]; 00208 00209 A[0] = 1.0f; 00210 A[1] = 2.0f; 00211 A[2] = 8.0f; 00212 A[3] = 100.0f; 00213 A[4] = M_PI; 00214 A[5] = 100000; 00215 A[6] = 1.0f/3.0f; 00216 00217 for(int i = 0; i < samples; i++) 00218 { 00219 F[i] = fastSqrt_Q3(A[i]); 00220 N[i] = sqrt(A[i]); 00221 LINFO("\t%d SQRT %f - Fast %f Normal %f",i,A[i],F[i],N[i]); 00222 } 00223 00224 float store; 00225 t1 = tim.get(); 00226 for(int l = 0; l < loops; l++) 00227 { 00228 for(int k = 0; k < loops; k++) 00229 { 00230 store = 0.0f; 00231 for(int i = 0; i < large_samples; i++) 00232 { 00233 store += fastSqrt_Q3(FLA[i]); 00234 } 00235 } 00236 } 00237 t2 = tim.get(); 00238 t3 = t2 - t1; 00239 LINFO(">>> Time for fast float SQRT - %d mcs (result %f)",t3,store); 00240 } 00241 00242 LINFO("<<<NEXT>>>"); 00243 00244 // ###################################################################### 00245 { 00246 LINFO("fastSqrt_Bab_2 : Log 2 Babylonian 2 Approx Fast Square Root - Float"); 00247 const int samples = 7; 00248 00249 float A[samples]; 00250 float F[samples]; 00251 float N[samples]; 00252 00253 A[0] = 1.0f; 00254 A[1] = 2.0f; 00255 A[2] = 8.0f; 00256 A[3] = 100.0f; 00257 A[4] = M_PI; 00258 A[5] = 100000; 00259 A[6] = 1.0f/3.0f; 00260 00261 for(int i = 0; i < samples; i++) 00262 { 00263 F[i] = fastSqrt_Bab_2(A[i]); 00264 N[i] = sqrt(A[i]); 00265 LINFO("\t%d SQRT %f - Log 2 Fast Babylonian %f Normal %f", 00266 i,A[i],F[i],N[i]); 00267 } 00268 00269 float store; 00270 t1 = tim.get(); 00271 for(int l = 0; l < loops; l++) 00272 { 00273 for(int k = 0; k < loops; k++) 00274 { 00275 store = 0.0f; 00276 for(int i = 0; i < large_samples; i++) 00277 { 00278 store += fastSqrt_Bab_2(FLA[i]); 00279 } 00280 } 00281 } 00282 t2 = tim.get(); 00283 t3 = t2 - t1; 00284 LINFO(">>> Time for Log 2 fast Babylonian float SQRT - %d mcs (result %f)",t3,store); 00285 } 00286 00287 LINFO("<<<NEXT>>>"); 00288 00289 // ###################################################################### 00290 { 00291 LINFO("fastSqrt_2 : Log 2 Approx Fast Square Root - Double"); 00292 const int samples = 7; 00293 00294 double A[samples]; 00295 double F[samples]; 00296 double N[samples]; 00297 00298 A[0] = 1.0F; 00299 A[1] = 2.0F; 00300 A[2] = 8.0F; 00301 A[3] = 100.0F; 00302 A[4] = M_PI; 00303 A[5] = 100000; 00304 A[6] = 1.0F/3.0F; 00305 00306 for(int i = 0; i < samples; i++) 00307 { 00308 F[i] = fastSqrt_2(A[i]); 00309 N[i] = sqrt(A[i]); 00310 LINFO("\t%d SQRT %f - Fast %f Normal %f",i,A[i],F[i],N[i]); 00311 } 00312 00313 double store; 00314 t1 = tim.get(); 00315 for(int l = 0; l < loops; l++) 00316 { 00317 for(int k = 0; k < loops; k++) 00318 { 00319 store = 0.0F; 00320 for(int i = 0; i < large_samples; i++) 00321 { 00322 store += fastSqrt_2(DLA[i]); 00323 } 00324 } 00325 } 00326 t2 = tim.get(); 00327 t3 = t2 - t1; 00328 LINFO(">>> Time for fast double SQRT - %d mcs (result %f)",t3,store); 00329 } 00330 00331 LINFO("<<<NEXT>>>"); 00332 00333 // ###################################################################### 00334 { 00335 LINFO("fastSqrt_Bab : Log 2 Babylonian Approx Fast Square Root - Double"); 00336 const int samples = 7; 00337 00338 double A[samples]; 00339 double F[samples]; 00340 double N[samples]; 00341 00342 A[0] = 1.0f; 00343 A[1] = 2.0f; 00344 A[2] = 8.0f; 00345 A[3] = 100.0f; 00346 A[4] = M_PI; 00347 A[5] = 100000; 00348 A[6] = 1.0f/3.0f; 00349 00350 for(int i = 0; i < samples; i++) 00351 { 00352 F[i] = fastSqrt_Bab(A[i]); 00353 N[i] = sqrt(A[i]); 00354 LINFO("\t%d SQRT %f - Log 2 Fast Babylonian %f Normal %f", 00355 i,A[i],F[i],N[i]); 00356 } 00357 00358 double store; 00359 t1 = tim.get(); 00360 for(int l = 0; l < loops; l++) 00361 { 00362 for(int k = 0; k < loops; k++) 00363 { 00364 store = 0.0f; 00365 for(int i = 0; i < large_samples; i++) 00366 { 00367 store += fastSqrt_Bab(DLA[i]); 00368 } 00369 } 00370 } 00371 t2 = tim.get(); 00372 t3 = t2 - t1; 00373 LINFO(">>> Time for Log 2 fast Babylonian double SQRT - %d mcs (result %f)",t3,store); 00374 } 00375 00376 LINFO("<<<NEXT>>>"); 00377 00378 // ###################################################################### 00379 { 00380 LINFO("fastSqrt_Q3 : Quake 3 Fast Square Root - Double"); 00381 const int samples = 7; 00382 00383 double A[samples]; 00384 double F[samples]; 00385 double N[samples]; 00386 00387 A[0] = 1.0F; 00388 A[1] = 2.0F; 00389 A[2] = 8.0F; 00390 A[3] = 100.0F; 00391 A[4] = M_PI; 00392 A[5] = 100000; 00393 A[6] = 1.0F/3.0F; 00394 00395 for(int i = 0; i < samples; i++) 00396 { 00397 F[i] = fastSqrt_Q3(A[i]); 00398 N[i] = sqrt(A[i]); 00399 LINFO("\t%d SQRT %f - Fast %f Normal %f",i,A[i],F[i],N[i]); 00400 } 00401 00402 double store; 00403 t1 = tim.get(); 00404 for(int l = 0; l < loops; l++) 00405 { 00406 for(int k = 0; k < loops; k++) 00407 { 00408 store = 0.0F; 00409 for(int i = 0; i < large_samples; i++) 00410 { 00411 store += fastSqrt_Q3(DLA[i]); 00412 } 00413 } 00414 } 00415 t2 = tim.get(); 00416 t3 = t2 - t1; 00417 LINFO(">>> Time for fast double SQRT - %d mcs (result %f)",t3,store); 00418 } 00419 LINFO("<<<NEXT>>>"); 00420 00421 // ###################################################################### 00422 { 00423 LINFO("fastSqrt_Bab_2 : Log 2 Babylonian 2 Approx Fast Square Root - double"); 00424 const int samples = 7; 00425 00426 double A[samples]; 00427 double F[samples]; 00428 double N[samples]; 00429 00430 A[0] = 1.0f; 00431 A[1] = 2.0f; 00432 A[2] = 8.0f; 00433 A[3] = 100.0f; 00434 A[4] = M_PI; 00435 A[5] = 100000; 00436 A[6] = 1.0f/3.0f; 00437 00438 for(int i = 0; i < samples; i++) 00439 { 00440 F[i] = fastSqrt_Bab_2(A[i]); 00441 N[i] = sqrt(A[i]); 00442 LINFO("\t%d SQRT %f - Log 2 Fast Babylonian %f Normal %f", 00443 i,A[i],F[i],N[i]); 00444 } 00445 00446 double store; 00447 t1 = tim.get(); 00448 for(int l = 0; l < loops; l++) 00449 { 00450 for(int k = 0; k < loops; k++) 00451 { 00452 store = 0.0f; 00453 for(int i = 0; i < large_samples; i++) 00454 { 00455 store += fastSqrt_Bab_2(DLA[i]); 00456 } 00457 } 00458 } 00459 t2 = tim.get(); 00460 t3 = t2 - t1; 00461 LINFO(">>> Time for Log 2 fast Babylonian float SQRT - %d mcs (result %f)",t3,store); 00462 } 00463 00464 // ###################################################################### 00465 { 00466 LINFO("fastLog : Fast Log - double"); 00467 const int samples = 7; 00468 00469 double A[samples]; 00470 double F[samples]; 00471 double N[samples]; 00472 00473 A[0] = 1.0f; 00474 A[1] = 2.0f; 00475 A[2] = 8.0f; 00476 A[3] = 100.0f; 00477 A[4] = M_PI; 00478 A[5] = 100000; 00479 A[6] = 1.0f/3.0f; 00480 00481 for(int i = 0; i < samples; i++) 00482 { 00483 F[i] = fastLog(A[i]); 00484 N[i] = log(A[i]); 00485 LINFO("\t%d Log %f - Fast Log %f Normal %f", 00486 i,A[i],F[i],N[i]); 00487 } 00488 00489 double store; 00490 t1 = tim.get(); 00491 for(int l = 0; l < loops; l++) 00492 { 00493 for(int k = 0; k < loops; k++) 00494 { 00495 store = 0.0f; 00496 for(int i = 0; i < large_samples; i++) 00497 { 00498 store += fastLog(DLA[i]); 00499 } 00500 } 00501 } 00502 t2 = tim.get(); 00503 t3 = t2 - t1; 00504 LINFO(">>> Time for fastLog - %d mcs (result %f)",t3,store); 00505 } 00506 00507 00508 // ###################################################################### 00509 LINFO("<<<NEXT - CHECKING IMPLEMENTATION>>>"); 00510 { 00511 int store; 00512 t1 = tim.get(); 00513 for(int l = 0; l < loops; l++) 00514 { 00515 for(int k = 0; k < loops; k++) 00516 { 00517 store = 0; 00518 for(int i = 0; i < large_samples; i++) 00519 { 00520 store += (int)sqrt(ILA[i]); 00521 } 00522 } 00523 } 00524 t2 = tim.get(); 00525 t3 = t2 - t1; 00526 LINFO(">>> Time Int SQRT BASELINE - %d mcs (result %d)",t3,store); 00527 } 00528 00529 { 00530 int store; 00531 t1 = tim.get(); 00532 for(int l = 0; l < loops; l++) 00533 { 00534 for(int k = 0; k < loops; k++) 00535 { 00536 store = 0; 00537 for(int i = 0; i < large_samples; i++) 00538 { 00539 store += fastSqrt(ILA[i]); 00540 } 00541 } 00542 } 00543 t2 = tim.get(); 00544 t3 = t2 - t1; 00545 LINFO(">>> Time Int SQRT - %d mcs (result %d)",t3,store); 00546 } 00547 00548 { 00549 float store; 00550 t1 = tim.get(); 00551 for(int l = 0; l < loops; l++) 00552 { 00553 for(int k = 0; k < loops; k++) 00554 { 00555 store = 0.0f; 00556 for(int i = 0; i < large_samples; i++) 00557 { 00558 store += sqrt(FLA[i]); 00559 } 00560 } 00561 } 00562 t2 = tim.get(); 00563 t3 = t2 - t1; 00564 LINFO(">>> Time Float SQRT BASELINE - %d mcs (result %f)",t3,store); 00565 } 00566 00567 { 00568 float store; 00569 t1 = tim.get(); 00570 for(int l = 0; l < loops; l++) 00571 { 00572 for(int k = 0; k < loops; k++) 00573 { 00574 store = 0.0f; 00575 for(int i = 0; i < large_samples; i++) 00576 { 00577 store += fastSqrt(FLA[i]); 00578 } 00579 } 00580 } 00581 t2 = tim.get(); 00582 t3 = t2 - t1; 00583 LINFO(">>> Time Float SQRT - %d mcs (result %f)",t3,store); 00584 } 00585 00586 { 00587 double store; 00588 t1 = tim.get(); 00589 for(int l = 0; l < loops; l++) 00590 { 00591 for(int k = 0; k < loops; k++) 00592 { 00593 store = 0.0f; 00594 for(int i = 0; i < large_samples; i++) 00595 { 00596 store += log(DLA[i]); 00597 } 00598 } 00599 } 00600 t2 = tim.get(); 00601 t3 = t2 - t1; 00602 LINFO(">>> Time Log BASELINE - %d mcs (result %f)",t3,store); 00603 } 00604 00605 { 00606 double store; 00607 t1 = tim.get(); 00608 for(int l = 0; l < loops; l++) 00609 { 00610 for(int k = 0; k < loops; k++) 00611 { 00612 store = 0.0f; 00613 for(int i = 0; i < large_samples; i++) 00614 { 00615 store += fastLog(DLA[i]); 00616 } 00617 } 00618 } 00619 t2 = tim.get(); 00620 t3 = t2 - t1; 00621 LINFO(">>> Time FastLog - %d mcs (result %f)",t3,store); 00622 } 00623 return 1; 00624 } 00625