r4082 - in trunk/birnet: . tests



Author: timj
Date: 2006-11-14 19:02:17 -0500 (Tue, 14 Nov 2006)
New Revision: 4082

Added:
   trunk/birnet/birnetmath.cc
   trunk/birnet/birnetmath.hh
   trunk/birnet/tests/math.cc
Modified:
   trunk/birnet/ChangeLog
   trunk/birnet/Makefile.am
   trunk/birnet/birnet.hh
   trunk/birnet/tests/Makefile.am
Log:
Wed Nov 15 01:00:21 2006  Tim Janik  <timj gtk org>                                                                                                           
                                                                                                                                                              
        * birnetmath.hh, birnetmath.cc: added dtoi32, dtoi64, iround, iceil,                                                                                  
        ifloor definitions from Rapicorn. these are fast float->integer                                                                                       
        conversion routines on i386.                                                                                                                          
                                                                                                                                                              
        * tests/math.cc: test dtoi implementation and friends.                                                                                                
                                                                                                                                                              



Modified: trunk/birnet/ChangeLog
===================================================================
--- trunk/birnet/ChangeLog	2006-11-15 00:00:46 UTC (rev 4081)
+++ trunk/birnet/ChangeLog	2006-11-15 00:02:17 UTC (rev 4082)
@@ -1,3 +1,11 @@
+Wed Nov 15 01:00:21 2006  Tim Janik  <timj gtk org>
+
+	* birnetmath.hh, birnetmath.cc: added dtoi32, dtoi64, iround, iceil, 
+	ifloor definitions from Rapicorn. these are fast float->integer 
+	conversion routines on i386.
+
+	* tests/math.cc: test dtoi implementation and friends.
+
 Thu Nov  2 22:28:49 2006  Tim Janik  <timj gtk org>
 
 	* birnettests.h: added TABORT_HANDLER() to specify a custom callback

Modified: trunk/birnet/Makefile.am
===================================================================
--- trunk/birnet/Makefile.am	2006-11-15 00:00:46 UTC (rev 4081)
+++ trunk/birnet/Makefile.am	2006-11-15 00:02:17 UTC (rev 4082)
@@ -18,6 +18,7 @@
 	birnetsignal.hh                 \
 	birnettests.h			\
 	birnetthread.hh			\
+	birnetmath.hh			\
 	birnetutf8.hh			\
 	birnetutils.hh			\
 )
@@ -27,6 +28,7 @@
 	birnetmsg.cc			\
 	birnetsignal.cc                 \
 	birnetthread.cc			\
+	birnetmath.cc			\
 	birnetutf8.cc			\
 	birnetutils.cc			\
 )

Modified: trunk/birnet/birnet.hh
===================================================================
--- trunk/birnet/birnet.hh	2006-11-15 00:00:46 UTC (rev 4081)
+++ trunk/birnet/birnet.hh	2006-11-15 00:02:17 UTC (rev 4082)
@@ -24,6 +24,7 @@
 
 #include <birnet/birnetmsg.hh>
 
+#include <birnet/birnetmath.hh>
 #include <birnet/birnetutf8.hh>
 #include <birnet/birnetutils.hh>
 #include <birnet/birnetsignal.hh>

Added: trunk/birnet/birnetmath.cc
===================================================================
--- trunk/birnet/birnetmath.cc	2006-11-15 00:00:46 UTC (rev 4081)
+++ trunk/birnet/birnetmath.cc	2006-11-15 00:02:17 UTC (rev 4082)
@@ -0,0 +1,23 @@
+/* Birnet
+ * Copyright (C) 2006 Tim Janik
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+#include "birnetmath.hh"
+
+namespace Birnet {
+
+} // Birnet

Added: trunk/birnet/birnetmath.hh
===================================================================
--- trunk/birnet/birnetmath.hh	2006-11-15 00:00:46 UTC (rev 4081)
+++ trunk/birnet/birnetmath.hh	2006-11-15 00:02:17 UTC (rev 4082)
@@ -0,0 +1,81 @@
+/* Birnet
+ * Copyright (C) 2006 Tim Janik
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+#ifndef __BIRNET_MATH_HH__
+#define __BIRNET_MATH_HH__
+
+#include <birnet/birnetutils.hh>
+#include <math.h>
+
+namespace Birnet {
+
+/* --- double to integer --- */
+inline int      dtoi32 (double d) BIRNET_CONST;
+inline int64    dtoi64 (double d) BIRNET_CONST;
+inline int64    iround (double d) BIRNET_CONST;
+inline int64    ifloor (double d) BIRNET_CONST;
+inline int64    iceil  (double d) BIRNET_CONST;
+
+/* --- implementation bits --- */
+inline int BIRNET_CONST
+_dtoi32_generic (double d)
+{
+  /* this relies on the C++ behaviour of round-to-0 */
+  return (int) (d < -0.0 ? d - 0.5 : d + 0.5);
+}
+inline int BIRNET_CONST
+dtoi32 (double d)
+{
+  /* this relies on the hardware default round-to-nearest */
+#if defined __i386__ && defined __GNUC__
+  int r;
+  __asm__ volatile ("fistl %0"
+                    : "=m" (r)
+                    : "t" (d));
+  return r;
+#endif
+  return _dtoi32_generic (d);
+}
+inline int64 BIRNET_CONST
+_dtoi64_generic (double d)
+{
+  /* this relies on the C++ behaviour of round-to-0 */
+  return (int64) (d < -0.0 ? d - 0.5 : d + 0.5);
+}
+inline int64 BIRNET_CONST
+dtoi64 (double d)
+{
+  /* this relies on the hardware default round-to-nearest */
+#if defined __i386__ && defined __GNUC__
+  int64 r;
+  __asm__ volatile ("fistpll %0"
+                    : "=m" (r)
+                    : "t" (d)
+                    : "st");
+  return r;
+#endif
+  return _dtoi64_generic (d);
+}
+inline int64 BIRNET_CONST iround (double d) { return dtoi64 (round (d)); }
+inline int64 BIRNET_CONST ifloor (double d) { return dtoi64 (floor (d)); }
+inline int64 BIRNET_CONST iceil  (double d) { return dtoi64 (ceil (d)); }
+
+} // Birnet
+
+#endif /* __BIRNET_MATH_HH__ */
+/* vim:set ts=8 sts=2 sw=2: */

Modified: trunk/birnet/tests/Makefile.am
===================================================================
--- trunk/birnet/tests/Makefile.am	2006-11-15 00:00:46 UTC (rev 4081)
+++ trunk/birnet/tests/Makefile.am	2006-11-15 00:02:17 UTC (rev 4082)
@@ -13,6 +13,10 @@
 TESTS		+= infotest
 infotest_SOURCES = infotest.cc
 infotest_LDADD	 = $(progs_ldadd)
+TESTS		+= math
+PERFTESTS       += math
+math_SOURCES	 = math.cc
+math_LDADD	 = $(progs_ldadd)
 TESTS		+= strings
 SLOWTESTS       += strings
 strings_SOURCES  = strings.cc

Added: trunk/birnet/tests/math.cc
===================================================================
--- trunk/birnet/tests/math.cc	2006-11-15 00:00:46 UTC (rev 4081)
+++ trunk/birnet/tests/math.cc	2006-11-15 00:02:17 UTC (rev 4082)
@@ -0,0 +1,181 @@
+/* Birnet
+ * Copyright (C) 2006 Tim Janik
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+//#define TEST_VERBOSE
+#include <birnet/birnettests.h>
+#include <birnet/birnet.hh>
+
+namespace {
+using namespace Birnet;
+
+static void
+test_dtoi()
+{
+  TSTART ("dtoi32");
+  TASSERT (_dtoi32_generic (0.0) == 0);
+  TASSERT (_dtoi32_generic (+0.3) == +0);
+  TASSERT (_dtoi32_generic (-0.3) == -0);
+  TASSERT (_dtoi32_generic (+0.7) == +1);
+  TASSERT (_dtoi32_generic (-0.7) == -1);
+  TASSERT (_dtoi32_generic (+2147483646.3) == +2147483646);
+  TASSERT (_dtoi32_generic (+2147483646.7) == +2147483647);
+  TASSERT (_dtoi32_generic (-2147483646.3) == -2147483646);
+  TASSERT (_dtoi32_generic (-2147483646.7) == -2147483647);
+  TASSERT (_dtoi32_generic (-2147483647.3) == -2147483647);
+  TASSERT (_dtoi32_generic (-2147483647.7) == -2147483648LL);
+  TASSERT (dtoi32 (0.0) == 0);
+  TASSERT (dtoi32 (+0.3) == +0);
+  TASSERT (dtoi32 (-0.3) == -0);
+  TASSERT (dtoi32 (+0.7) == +1);
+  TASSERT (dtoi32 (-0.7) == -1);
+  TASSERT (dtoi32 (+2147483646.3) == +2147483646);
+  TASSERT (dtoi32 (+2147483646.7) == +2147483647);
+  TASSERT (dtoi32 (-2147483646.3) == -2147483646);
+  TASSERT (dtoi32 (-2147483646.7) == -2147483647);
+  TASSERT (dtoi32 (-2147483647.3) == -2147483647);
+  TASSERT (dtoi32 (-2147483647.7) == -2147483648LL);
+  TDONE();
+  TSTART ("dtoi64");
+  TASSERT (_dtoi64_generic (0.0) == 0);
+  TASSERT (_dtoi64_generic (+0.3) == +0);
+  TASSERT (_dtoi64_generic (-0.3) == -0);
+  TASSERT (_dtoi64_generic (+0.7) == +1);
+  TASSERT (_dtoi64_generic (-0.7) == -1);
+  TASSERT (_dtoi64_generic (+2147483646.3) == +2147483646);
+  TASSERT (_dtoi64_generic (+2147483646.7) == +2147483647);
+  TASSERT (_dtoi64_generic (-2147483646.3) == -2147483646);
+  TASSERT (_dtoi64_generic (-2147483646.7) == -2147483647);
+  TASSERT (_dtoi64_generic (-2147483647.3) == -2147483647);
+  TASSERT (_dtoi64_generic (-2147483647.7) == -2147483648LL);
+  TASSERT (_dtoi64_generic (+4294967297.3) == +4294967297LL);
+  TASSERT (_dtoi64_generic (+4294967297.7) == +4294967298LL);
+  TASSERT (_dtoi64_generic (-4294967297.3) == -4294967297LL);
+  TASSERT (_dtoi64_generic (-4294967297.7) == -4294967298LL);
+  TASSERT (_dtoi64_generic (+1125899906842624.3) == +1125899906842624LL);
+  TASSERT (_dtoi64_generic (+1125899906842624.7) == +1125899906842625LL);
+  TASSERT (_dtoi64_generic (-1125899906842624.3) == -1125899906842624LL);
+  TASSERT (_dtoi64_generic (-1125899906842624.7) == -1125899906842625LL);
+  TASSERT (dtoi64 (0.0) == 0);
+  TASSERT (dtoi64 (+0.3) == +0);
+  TASSERT (dtoi64 (-0.3) == -0);
+  TASSERT (dtoi64 (+0.7) == +1);
+  TASSERT (dtoi64 (-0.7) == -1);
+  TASSERT (dtoi64 (+2147483646.3) == +2147483646);
+  TASSERT (dtoi64 (+2147483646.7) == +2147483647);
+  TASSERT (dtoi64 (-2147483646.3) == -2147483646);
+  TASSERT (dtoi64 (-2147483646.7) == -2147483647);
+  TASSERT (dtoi64 (-2147483647.3) == -2147483647);
+  TASSERT (dtoi64 (-2147483647.7) == -2147483648LL);
+  TASSERT (dtoi64 (+4294967297.3) == +4294967297LL);
+  TASSERT (dtoi64 (+4294967297.7) == +4294967298LL);
+  TASSERT (dtoi64 (-4294967297.3) == -4294967297LL);
+  TASSERT (dtoi64 (-4294967297.7) == -4294967298LL);
+  TASSERT (dtoi64 (+1125899906842624.3) == +1125899906842624LL);
+  TASSERT (dtoi64 (+1125899906842624.7) == +1125899906842625LL);
+  TASSERT (dtoi64 (-1125899906842624.3) == -1125899906842624LL);
+  TASSERT (dtoi64 (-1125899906842624.7) == -1125899906842625LL);
+  TDONE();
+  TSTART ("iround");
+  TASSERT (round (0.0) == 0.0);
+  TASSERT (round (+0.3) == +0.0);
+  TASSERT (round (-0.3) == -0.0);
+  TASSERT (round (+0.7) == +1.0);
+  TASSERT (round (-0.7) == -1.0);
+  TASSERT (round (+4294967297.3) == +4294967297.0);
+  TASSERT (round (+4294967297.7) == +4294967298.0);
+  TASSERT (round (-4294967297.3) == -4294967297.0);
+  TASSERT (round (-4294967297.7) == -4294967298.0);
+  TASSERT (iround (0.0) == 0);
+  TASSERT (iround (+0.3) == +0);
+  TASSERT (iround (-0.3) == -0);
+  TASSERT (iround (+0.7) == +1);
+  TASSERT (iround (-0.7) == -1);
+  TASSERT (iround (+4294967297.3) == +4294967297LL);
+  TASSERT (iround (+4294967297.7) == +4294967298LL);
+  TASSERT (iround (-4294967297.3) == -4294967297LL);
+  TASSERT (iround (-4294967297.7) == -4294967298LL);
+  TASSERT (iround (+1125899906842624.3) == +1125899906842624LL);
+  TASSERT (iround (+1125899906842624.7) == +1125899906842625LL);
+  TASSERT (iround (-1125899906842624.3) == -1125899906842624LL);
+  TASSERT (iround (-1125899906842624.7) == -1125899906842625LL);
+  TDONE();
+  TSTART ("iceil");
+  TASSERT (ceil (0.0) == 0.0);
+  TASSERT (ceil (+0.3) == +1.0);
+  TASSERT (ceil (-0.3) == -0.0);
+  TASSERT (ceil (+0.7) == +1.0);
+  TASSERT (ceil (-0.7) == -0.0);
+  TASSERT (ceil (+4294967297.3) == +4294967298.0);
+  TASSERT (ceil (+4294967297.7) == +4294967298.0);
+  TASSERT (ceil (-4294967297.3) == -4294967297.0);
+  TASSERT (ceil (-4294967297.7) == -4294967297.0);
+  TASSERT (iceil (0.0) == 0);
+  TASSERT (iceil (+0.3) == +1);
+  TASSERT (iceil (-0.3) == -0);
+  TASSERT (iceil (+0.7) == +1);
+  TASSERT (iceil (-0.7) == -0);
+  TASSERT (iceil (+4294967297.3) == +4294967298LL);
+  TASSERT (iceil (+4294967297.7) == +4294967298LL);
+  TASSERT (iceil (-4294967297.3) == -4294967297LL);
+  TASSERT (iceil (-4294967297.7) == -4294967297LL);
+  TASSERT (iceil (+1125899906842624.3) == +1125899906842625LL);
+  TASSERT (iceil (+1125899906842624.7) == +1125899906842625LL);
+  TASSERT (iceil (-1125899906842624.3) == -1125899906842624LL);
+  TASSERT (iceil (-1125899906842624.7) == -1125899906842624LL);
+  TDONE();
+  TSTART ("ifloor");
+  TASSERT (floor (0.0) == 0.0);
+  TASSERT (floor (+0.3) == +0.0);
+  TASSERT (floor (-0.3) == -1.0);
+  TASSERT (floor (+0.7) == +0.0);
+  TASSERT (floor (-0.7) == -1.0);
+  TASSERT (floor (+4294967297.3) == +4294967297.0);
+  TASSERT (floor (+4294967297.7) == +4294967297.0);
+  TASSERT (floor (-4294967297.3) == -4294967298.0);
+  TASSERT (floor (-4294967297.7) == -4294967298.0);
+  TASSERT (ifloor (0.0) == 0);
+  TASSERT (ifloor (+0.3) == +0);
+  TASSERT (ifloor (-0.3) == -1);
+  TASSERT (ifloor (+0.7) == +0);
+  TASSERT (ifloor (-0.7) == -1);
+  TASSERT (ifloor (+4294967297.3) == +4294967297LL);
+  TASSERT (ifloor (+4294967297.7) == +4294967297LL);
+  TASSERT (ifloor (-4294967297.3) == -4294967298LL);
+  TASSERT (ifloor (-4294967297.7) == -4294967298LL);
+  TASSERT (ifloor (+1125899906842624.3) == +1125899906842624LL);
+  TASSERT (ifloor (+1125899906842624.7) == +1125899906842624LL);
+  TASSERT (ifloor (-1125899906842624.3) == -1125899906842625LL);
+  TASSERT (ifloor (-1125899906842624.7) == -1125899906842625LL);
+  TDONE();
+}
+
+} // Anon
+
+int
+main (int   argc,
+      char *argv[])
+{
+  birnet_init_test (&argc, &argv);
+
+  if (init_settings().test_quick)
+    test_dtoi();
+  
+  return 0;
+}
+
+/* vim:set ts=8 sts=2 sw=2: */




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]