Rapicorn - Experimental UI Toolkit - Source Code 10.08.1
rapicornmath.hh
Go to the documentation of this file.
00001 /* Rapicorn
00002  * Copyright (C) 2006 Tim Janik
00003  *
00004  * This library is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Lesser General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 2.1 of the License, or (at your option) any later version.
00008  *
00009  * This library is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * Lesser General Public License for more details.
00013  *
00014  * A copy of the GNU Lesser General Public License should ship along
00015  * with this library; if not, see http://www.gnu.org/copyleft/.
00016  */
00017 #ifndef __RAPICORN_MATH_HH__
00018 #define __RAPICORN_MATH_HH__
00019 
00020 #include <rcore/utilities.hh>
00021 #include <math.h>
00022 
00023 namespace Rapicorn {
00024 
00025 /* --- double to integer --- */
00026 inline int      dtoi32 (double d) RAPICORN_CONST;
00027 inline int64    dtoi64 (double d) RAPICORN_CONST;
00028 inline int64    iround (double d) RAPICORN_CONST;
00029 inline int64    ifloor (double d) RAPICORN_CONST;
00030 inline int64    iceil  (double d) RAPICORN_CONST;
00031 
00032 /* --- implementation bits --- */
00033 inline int RAPICORN_CONST
00034 _dtoi32_generic (double d)
00035 {
00036   /* this relies on the C++ behaviour of round-to-0 */
00037   return (int) (d < -0.0 ? d - 0.5 : d + 0.5);
00038 }
00039 inline int RAPICORN_CONST
00040 dtoi32 (double d)
00041 {
00042   /* this relies on the hardware default round-to-nearest */
00043 #if defined __i386__ && defined __GNUC__
00044   int r;
00045   __asm__ volatile ("fistl %0"
00046                     : "=m" (r)
00047                     : "t" (d));
00048   return r;
00049 #endif
00050   return _dtoi32_generic (d);
00051 }
00052 inline int64 RAPICORN_CONST
00053 _dtoi64_generic (double d)
00054 {
00055   /* this relies on the C++ behaviour of round-to-0 */
00056   return (int64) (d < -0.0 ? d - 0.5 : d + 0.5);
00057 }
00058 inline int64 RAPICORN_CONST
00059 dtoi64 (double d)
00060 {
00061   /* this relies on the hardware default round-to-nearest */
00062 #if defined __i386__ && defined __GNUC__
00063   int64 r;
00064   __asm__ volatile ("fistpll %0"
00065                     : "=m" (r)
00066                     : "t" (d)
00067                     : "st");
00068   return r;
00069 #endif
00070   return _dtoi64_generic (d);
00071 }
00072 inline int64 RAPICORN_CONST iround (double d) { return dtoi64 (round (d)); }
00073 inline int64 RAPICORN_CONST ifloor (double d) { return dtoi64 (floor (d)); }
00074 inline int64 RAPICORN_CONST iceil  (double d) { return dtoi64 (ceil (d)); }
00075 
00076 } // Rapicorn
00077 
00078 #endif /* __RAPICORN_MATH_HH__ */
00079 /* vim:set ts=8 sts=2 sw=2: */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines