bench-basic.c

Description | Download | Table of Contents | Modules | Compound List | File List | Functions


Overview
Compiler
Documentation
Examples
Misc
Help
IDE & Tools

Download
Install

Links
Projects






00001 /* Benchmark for some basic type operations
00002    Copyright (C) 2001 Free Software Foundation, Inc.
00003    Written by Stephane Carrez (stcarrez@worldnet.fr)    
00004 
00005 This file is free software; you can redistribute it and/or modify it
00006 under the terms of the GNU General Public License as published by the
00007 Free Software Foundation; either version 2, or (at your option) any
00008 later version.
00009 
00010 In addition to the permissions in the GNU General Public License, the
00011 Free Software Foundation gives you unlimited permission to link the
00012 compiled version of this file with other programs, and to distribute
00013 those programs without any restriction coming from the use of this
00014 file.  (The General Public License restrictions do apply in other
00015 respects; for example, they cover modification of the file, and
00016 distribution when not linked into another program.)
00017 
00018 This file is distributed in the hope that it will be useful, but
00019 WITHOUT ANY WARRANTY; without even the implied warranty of
00020 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00021 General Public License for more details.
00022 
00023 You should have received a copy of the GNU General Public License
00024 along with this program; see the file COPYING.  If not, write to
00025 the Free Software Foundation, 59 Temple Place - Suite 330,
00026 Boston, MA 02111-1307, USA.  */
00027 
00046 
00047 #include <benchs.h>
00048 
00049 /* Use global variables to prevent Gcc from computing values
00050    produced by benchmark operations.  */
00051 unsigned long long global_value = -1;
00052 unsigned long long scratch;
00053 unsigned long global_shift;
00054 long l = 0x1234567;
00055 long long ll = 0x12345670000LL;
00056 short s = 0x1234;
00057 char c = 0x12;
00058 
00059 
00060 void
00061 bench_bits (bench_t *b)
00062 {
00063   unsigned short cnt;
00064   unsigned char v_char, i_char;
00065   unsigned short v_short, i_short;
00066   unsigned long v_long, i_long;
00067   unsigned long long v_longlong, i_longlong;
00068   
00069   cnt = 0;
00070   v_char = global_value;
00071   bench_start (b);
00072   for (i_char = 1 << 7; i_char; i_char >>= 1)
00073     if (v_char & i_char)
00074       cnt++;
00075   bench_stop (b);
00076   bench_report (b, "Bit count (char, %d bits set)", (long) cnt);
00077 
00078   cnt = 0;
00079   v_short = global_value;
00080   bench_start (b);
00081   for (i_short = 1 << 15; i_short; i_short >>= 1)
00082     if (v_short & i_short)
00083       cnt++;
00084   bench_stop (b);
00085   bench_report (b, "Bit count (short, %d bits set)", (long) cnt);
00086 
00087   cnt = 0;
00088   v_long = global_value;
00089   bench_start (b);
00090   for (i_long = 1L << 31; i_long; i_long >>= 1)
00091     if (v_long & i_long)
00092       cnt++;
00093   bench_stop (b);
00094   bench_report (b, "Bit count (long, %d bits set)", (long) cnt);
00095 
00096   cnt = 0;
00097   v_longlong = global_value;
00098   bench_start (b);
00099   for (i_longlong = 1LL << 63; i_longlong; i_longlong >>= 1)
00100     if (v_longlong & i_longlong)
00101       cnt++;
00102   bench_stop (b);
00103   bench_report (b, "Bit count (long long, %d bits set)", (long) cnt);
00104 }
00105 
00106 void
00107 bench_shift_right_1 (bench_t *b)
00108 {
00109   unsigned char v_char;
00110   unsigned short v_short;
00111   unsigned long v_long;
00112   unsigned long long v_longlong;
00113   
00114   v_char = global_value;
00115   bench_start (b);
00116   v_char >>= 1;
00117   bench_stop (b);
00118   bench_report (b, "Shift right 1 (char)");
00119 
00120   scratch += v_char;
00121 
00122   v_short = global_value;
00123   bench_start (b);
00124   v_short >>= 1;
00125   bench_stop (b);
00126   bench_report (b, "Shift right 1 (short)");
00127 
00128   scratch += v_short;
00129   
00130   v_long = global_value;
00131   bench_start (b);
00132   v_long >>= 1;
00133   bench_stop (b);
00134   bench_report (b, "Shift right 1 (long)");
00135 
00136   scratch += v_long;
00137   
00138   v_longlong = global_value;
00139   bench_start (b);
00140   v_longlong >>= 1;
00141   bench_stop (b);
00142   bench_report (b, "Shift right 1 (long long)");
00143 
00144   scratch += v_longlong;
00145 }
00146 
00147 void
00148 bench_shift_left_1 (bench_t *b)
00149 {
00150   unsigned char v_char;
00151   unsigned short v_short;
00152   unsigned long v_long;
00153   unsigned long long v_longlong;
00154   
00155   v_char = global_value;
00156   bench_start (b);
00157   v_char <<= 1;
00158   bench_stop (b);
00159   bench_report (b, "Shift left 1 (char)");
00160 
00161   scratch += v_char;
00162 
00163   v_short = global_value;
00164   bench_start (b);
00165   v_short <<= 1;
00166   bench_stop (b);
00167   bench_report (b, "Shift left 1 (short)");
00168 
00169   scratch += v_short;
00170   
00171   v_long = global_value;
00172   bench_start (b);
00173   v_long <<= 1;
00174   bench_stop (b);
00175   bench_report (b, "Shift left 1 (long)");
00176 
00177   scratch += v_long;
00178   
00179   v_longlong = global_value;
00180   bench_start (b);
00181   v_longlong <<= 1;
00182   bench_stop (b);
00183   bench_report (b, "Shift left 1 (long long)");
00184 
00185   scratch += v_longlong;
00186 }
00187 
00188 void
00189 bench_shift_left_n (bench_t *b)
00190 {
00191   unsigned char v_char;
00192   unsigned short v_short;
00193   unsigned long v_long;
00194   unsigned long long v_longlong;
00195   
00196   v_char = global_value;
00197   bench_start (b);
00198   v_char <<= global_shift;
00199   bench_stop (b);
00200   bench_report (b, "Shift left non-const (N=%d, char)", global_shift);
00201 
00202   scratch += v_char;
00203 
00204   v_short = global_value;
00205   bench_start (b);
00206   v_short <<= global_shift;
00207   bench_stop (b);
00208   bench_report (b, "Shift left non-const (N=%d, short)", global_shift);
00209 
00210   scratch += v_short;
00211   
00212   v_long = global_value;
00213   bench_start (b);
00214   v_long <<= global_shift;
00215   bench_stop (b);
00216   bench_report (b, "Shift left non-const (N=%d, long)", global_shift);
00217 
00218   scratch += v_long;
00219   
00220   v_longlong = global_value;
00221   bench_start (b);
00222   v_longlong <<= global_shift;
00223   bench_stop (b);
00224   bench_report (b, "Shift left non-const (N=%d, long long)", global_shift);
00225 
00226   scratch += v_longlong;
00227 }
00228 
00229 void
00230 bench_char (bench_t *b)
00231 {
00232   bench_start (b);
00233   c = c * c;
00234   bench_stop (b);
00235   bench_report (b, "Char mul (%d)", (long) c);
00236 
00237   bench_start (b);
00238   c = c + c;
00239   bench_stop (b);
00240   bench_report (b, "Char add (%d)", (long) c);
00241 
00242   bench_start (b);
00243   c = -c;
00244   bench_stop (b);
00245   bench_report (b, "Char neg (%d)", (long) c);
00246 
00247   bench_start (b);
00248   c = c / 3;
00249   bench_stop (b);
00250   bench_report (b, "Char div (%d)", (long) c);
00251 }
00252 
00253 void
00254 bench_short (bench_t *b)
00255 {
00256   bench_start (b);
00257   s = s * s;
00258   bench_stop (b);
00259   bench_report (b, "Short mul (%d)", (long) s);
00260 
00261   bench_start (b);
00262   s = s + s;
00263   bench_stop (b);
00264   bench_report (b, "Short add (%d)", (long) s);
00265 
00266   bench_start (b);
00267   s = -s;
00268   bench_stop (b);
00269   bench_report (b, "Short neg (%d)", (long) s);
00270 
00271   bench_start (b);
00272   s = s / 3;
00273   bench_stop (b);
00274   bench_report (b, "Short div (%d)", (long) s);
00275 }
00276 
00277 void
00278 bench_long (bench_t *b)
00279 {
00280   bench_start (b);
00281   l = l * l;
00282   bench_stop (b);
00283   bench_report (b, "Long mul (%d)", (long) l);
00284 
00285   bench_start (b);
00286   l = l + l;
00287   bench_stop (b);
00288   bench_report (b, "Long add (%d)", (long) l);
00289 
00290   bench_start (b);
00291   l = -l;
00292   bench_stop (b);
00293   bench_report (b, "Long neg (%d)", (long) l);
00294 
00295   bench_start (b);
00296   l = l / 3;
00297   bench_stop (b);
00298   bench_report (b, "Long div (%d)", (long) l);
00299 }
00300 
00301 void
00302 bench_longlong (bench_t *b)
00303 {
00304   bench_start (b);
00305   ll = ll * ll;
00306   bench_stop (b);
00307   bench_report (b, "Long long mul (%d)", (long) l);
00308 
00309   bench_start (b);
00310   ll = ll + ll;
00311   bench_stop (b);
00312   bench_report (b, "Long long add (%d)", (long) l);
00313 
00314   bench_start (b);
00315   ll = -ll;
00316   bench_stop (b);
00317   bench_report (b, "Long long neg (%d)", (long) l);
00318 
00319   bench_start (b);
00320   ll = ll / 3;
00321   bench_stop (b);
00322   bench_report (b, "Long long div (%d)", (long) l);
00323 }
00324 
00325 /* Main, run the benchmarks.  */
00326 int
00327 main ()
00328 {
00329   bench_t b;
00330   
00331   bench_init (&b);
00332   bench_char (&b);
00333   bench_short (&b);
00334   bench_long (&b);
00335   bench_longlong (&b);
00336   bench_shift_right_1 (&b);
00337   bench_shift_left_1 (&b);
00338   return 0;
00339 }
00340 

Description | Download | Table of Contents | Modules | Compound List | File List | Functions

    Last modified,
    Apr 16, 2001
[ Copying ]     [ Feedback to Stephane.Carrez@worldnet.fr ]