bench-float.c

00001 /* Benchmark for some float and double 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 
00045 
00046 #include <benchs.h>
00047 
00048 /* Forward declaration.  */
00049 void bench_double (bench_t *b);
00050 void bench_float (bench_t *b);
00051 
00052 
00053 /* Use global variables to forbid Gcc from doing some optimization.  */
00054 double a = 3.141592;
00055 float f = 3.141592;
00056 
00057 /* Benchmark some floating point operations (mul, add, neg, div, ...).  */
00058 void
00059 bench_float (bench_t *b)
00060 {
00061   long n;
00062   
00063   bench_start (b);
00064   f = f * f;
00065   bench_stop (b);
00066   bench_report (b, "Float mul (%d)", (long) f);
00067 
00068   bench_start (b);
00069   f = f + f;
00070   bench_stop (b);
00071   bench_report (b, "Float add (%d)", (long) f);
00072 
00073   bench_start (b);
00074   f = -f;
00075   bench_stop (b);
00076   bench_report (b, "Float neg (%d)", (long) f);
00077 
00078   bench_start (b);
00079   f = f / 0.1;
00080   bench_stop (b);
00081   bench_report (b, "Float div (%d)", (long) f);
00082 
00083   f = f + 2.0;
00084   bench_start (b);
00085   n = f;
00086   bench_stop (b);
00087   bench_report (b, "Float to long (%d)", n);
00088 
00089   n += 2;
00090   bench_start (b);
00091   f = n;
00092   bench_stop (b);
00093   bench_report (b, "Long to float (%d)", n);
00094 }
00095 
00096 /* Same as bench_float but using 64-bit double.  */
00097 void
00098 bench_double (bench_t *b)
00099 {
00100   long n;
00101   
00102   bench_start (b);
00103   a = a * a;
00104   bench_stop (b);
00105   bench_report (b, "Double mul (%d)", (long) a);
00106 
00107   bench_start (b);
00108   a = a + a;
00109   bench_stop (b);
00110   bench_report (b, "Double add (%d)", (long) a);
00111 
00112   bench_start (b);
00113   a = -a;
00114   bench_stop (b);
00115   bench_report (b, "Double neg (%d)", (long) a);
00116 
00117   bench_start (b);
00118   a = a / 0.1;
00119   bench_stop (b);
00120   bench_report (b, "Double div (%d)", (long) a);
00121 
00122   a = a + 3.1;
00123   bench_start (b);
00124   n = a;
00125   bench_stop (b);
00126   bench_report (b, "Double to long (%d)", n);
00127 
00128   n += 2;
00129   bench_start (b);
00130   a = n;
00131   bench_stop (b);
00132   bench_report (b, "Long to double (%d)", (long) a);
00133 }
00134 
00135 /* Main, run the benchmarks.  */
00136 int
00137 main ()
00138 {
00139   bench_t b;
00140   
00141   bench_init (&b);
00142   bench_float (&b);
00143   bench_double (&b);
00144 
00145   return 0;
00146 }