00001 /* Benchmark for some basic type operations 00002 Copyright (C) 2001, 2002 Free Software Foundation, Inc. 00003 Written by Stephane Carrez (stcarrez@nerim.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 /* Forward declarations. */ 00050 void bench_bits (bench_t *b); 00051 void bench_shift_right_1 (bench_t *b); 00052 void bench_shift_left_1 (bench_t *b); 00053 void bench_shift_left_n (bench_t *b); 00054 void bench_char (bench_t *b); 00055 void bench_short (bench_t *b); 00056 void bench_long (bench_t *b); 00057 void bench_longlong (bench_t *b); 00058 00059 00060 /* Use global variables to prevent Gcc from computing values 00061 produced by benchmark operations. */ 00062 unsigned long long global_value = -1; 00063 unsigned long long scratch; 00064 unsigned long global_shift; 00065 long l = 0x1234567; 00066 long long ll = 0x12345670000LL; 00067 short s = 0x1234; 00068 char c = 0x12; 00069 00070 00071 void 00072 bench_bits (bench_t *b) 00073 { 00074 unsigned short cnt; 00075 unsigned char v_char, i_char; 00076 unsigned short v_short, i_short; 00077 unsigned long v_long, i_long; 00078 unsigned long long v_longlong, i_longlong; 00079 00080 cnt = 0; 00081 v_char = global_value; 00082 bench_start (b); 00083 for (i_char = 1 << 7; i_char; i_char >>= 1) 00084 if (v_char & i_char) 00085 cnt++; 00086 bench_stop (b); 00087 bench_report (b, "Bit count (char, %d bits set)", (long) cnt); 00088 00089 cnt = 0; 00090 v_short = global_value; 00091 bench_start (b); 00092 for (i_short = 1 << 15; i_short; i_short >>= 1) 00093 if (v_short & i_short) 00094 cnt++; 00095 bench_stop (b); 00096 bench_report (b, "Bit count (short, %d bits set)", (long) cnt); 00097 00098 cnt = 0; 00099 v_long = global_value; 00100 bench_start (b); 00101 for (i_long = 1L << 31; i_long; i_long >>= 1) 00102 if (v_long & i_long) 00103 cnt++; 00104 bench_stop (b); 00105 bench_report (b, "Bit count (long, %d bits set)", (long) cnt); 00106 00107 cnt = 0; 00108 v_longlong = global_value; 00109 bench_start (b); 00110 for (i_longlong = 1LL << 63; i_longlong; i_longlong >>= 1) 00111 if (v_longlong & i_longlong) 00112 cnt++; 00113 bench_stop (b); 00114 bench_report (b, "Bit count (long long, %d bits set)", (long) cnt); 00115 } 00116 00117 void 00118 bench_shift_right_1 (bench_t *b) 00119 { 00120 unsigned char v_char; 00121 unsigned short v_short; 00122 unsigned long v_long; 00123 unsigned long long v_longlong; 00124 00125 v_char = global_value; 00126 bench_start (b); 00127 v_char >>= 1; 00128 bench_stop (b); 00129 bench_report (b, "Shift right 1 (char)"); 00130 00131 scratch += v_char; 00132 00133 v_short = global_value; 00134 bench_start (b); 00135 v_short >>= 1; 00136 bench_stop (b); 00137 bench_report (b, "Shift right 1 (short)"); 00138 00139 scratch += v_short; 00140 00141 v_long = global_value; 00142 bench_start (b); 00143 v_long >>= 1; 00144 bench_stop (b); 00145 bench_report (b, "Shift right 1 (long)"); 00146 00147 scratch += v_long; 00148 00149 v_longlong = global_value; 00150 bench_start (b); 00151 v_longlong >>= 1; 00152 bench_stop (b); 00153 bench_report (b, "Shift right 1 (long long)"); 00154 00155 scratch += v_longlong; 00156 } 00157 00158 void 00159 bench_shift_left_1 (bench_t *b) 00160 { 00161 unsigned char v_char; 00162 unsigned short v_short; 00163 unsigned long v_long; 00164 unsigned long long v_longlong; 00165 00166 v_char = global_value; 00167 bench_start (b); 00168 v_char <<= 1; 00169 bench_stop (b); 00170 bench_report (b, "Shift left 1 (char)"); 00171 00172 scratch += v_char; 00173 00174 v_short = global_value; 00175 bench_start (b); 00176 v_short <<= 1; 00177 bench_stop (b); 00178 bench_report (b, "Shift left 1 (short)"); 00179 00180 scratch += v_short; 00181 00182 v_long = global_value; 00183 bench_start (b); 00184 v_long <<= 1; 00185 bench_stop (b); 00186 bench_report (b, "Shift left 1 (long)"); 00187 00188 scratch += v_long; 00189 00190 v_longlong = global_value; 00191 bench_start (b); 00192 v_longlong <<= 1; 00193 bench_stop (b); 00194 bench_report (b, "Shift left 1 (long long)"); 00195 00196 scratch += v_longlong; 00197 } 00198 00199 void 00200 bench_shift_left_n (bench_t *b) 00201 { 00202 unsigned char v_char; 00203 unsigned short v_short; 00204 unsigned long v_long; 00205 unsigned long long v_longlong; 00206 00207 v_char = global_value; 00208 bench_start (b); 00209 v_char <<= global_shift; 00210 bench_stop (b); 00211 bench_report (b, "Shift left non-const (N=%d, char)", global_shift); 00212 00213 scratch += v_char; 00214 00215 v_short = global_value; 00216 bench_start (b); 00217 v_short <<= global_shift; 00218 bench_stop (b); 00219 bench_report (b, "Shift left non-const (N=%d, short)", global_shift); 00220 00221 scratch += v_short; 00222 00223 v_long = global_value; 00224 bench_start (b); 00225 v_long <<= global_shift; 00226 bench_stop (b); 00227 bench_report (b, "Shift left non-const (N=%d, long)", global_shift); 00228 00229 scratch += v_long; 00230 00231 v_longlong = global_value; 00232 bench_start (b); 00233 v_longlong <<= global_shift; 00234 bench_stop (b); 00235 bench_report (b, "Shift left non-const (N=%d, long long)", global_shift); 00236 00237 scratch += v_longlong; 00238 } 00239 00240 void 00241 bench_char (bench_t *b) 00242 { 00243 bench_start (b); 00244 c = c * c; 00245 bench_stop (b); 00246 bench_report (b, "Char mul (%d)", (long) c); 00247 00248 bench_start (b); 00249 c = c + c; 00250 bench_stop (b); 00251 bench_report (b, "Char add (%d)", (long) c); 00252 00253 bench_start (b); 00254 c = -c; 00255 bench_stop (b); 00256 bench_report (b, "Char neg (%d)", (long) c); 00257 00258 bench_start (b); 00259 c = c / 3; 00260 bench_stop (b); 00261 bench_report (b, "Char div (%d)", (long) c); 00262 } 00263 00264 void 00265 bench_short (bench_t *b) 00266 { 00267 bench_start (b); 00268 s = s * s; 00269 bench_stop (b); 00270 bench_report (b, "Short mul (%d)", (long) s); 00271 00272 bench_start (b); 00273 s = s + s; 00274 bench_stop (b); 00275 bench_report (b, "Short add (%d)", (long) s); 00276 00277 bench_start (b); 00278 s = -s; 00279 bench_stop (b); 00280 bench_report (b, "Short neg (%d)", (long) s); 00281 00282 bench_start (b); 00283 s = s / 3; 00284 bench_stop (b); 00285 bench_report (b, "Short div (%d)", (long) s); 00286 } 00287 00288 void 00289 bench_long (bench_t *b) 00290 { 00291 bench_start (b); 00292 l = l * l; 00293 bench_stop (b); 00294 bench_report (b, "Long mul (%d)", (long) l); 00295 00296 bench_start (b); 00297 l = l + l; 00298 bench_stop (b); 00299 bench_report (b, "Long add (%d)", (long) l); 00300 00301 bench_start (b); 00302 l = -l; 00303 bench_stop (b); 00304 bench_report (b, "Long neg (%d)", (long) l); 00305 00306 bench_start (b); 00307 l = l / 3; 00308 bench_stop (b); 00309 bench_report (b, "Long div (%d)", (long) l); 00310 } 00311 00312 void 00313 bench_longlong (bench_t *b) 00314 { 00315 bench_start (b); 00316 ll = ll * ll; 00317 bench_stop (b); 00318 bench_report (b, "Long long mul (%d)", (long) l); 00319 00320 bench_start (b); 00321 ll = ll + ll; 00322 bench_stop (b); 00323 bench_report (b, "Long long add (%d)", (long) l); 00324 00325 bench_start (b); 00326 ll = -ll; 00327 bench_stop (b); 00328 bench_report (b, "Long long neg (%d)", (long) l); 00329 00330 bench_start (b); 00331 ll = ll / 3; 00332 bench_stop (b); 00333 bench_report (b, "Long long div (%d)", (long) l); 00334 } 00335 00336 /* Main, run the benchmarks. */ 00337 int 00338 main () 00339 { 00340 bench_t b; 00341 00342 bench_init (&b); 00343 bench_char (&b); 00344 bench_short (&b); 00345 bench_long (&b); 00346 bench_longlong (&b); 00347 bench_shift_right_1 (&b); 00348 bench_shift_left_1 (&b); 00349 bench_bits (&b); 00350 return 0; 00351 } 00352