Rapicorn - Experimental UI Toolkit - Source Code
13.07.0
|
00001 // Licensed GNU LGPL v3 or later: http://www.gnu.org/licenses/lgpl.html 00002 #ifndef __RAPICORN_MATH_HH__ 00003 #define __RAPICORN_MATH_HH__ 00004 00005 #include <rcore/utilities.hh> 00006 #include <math.h> 00007 00008 namespace Rapicorn { 00009 00010 /* --- double to integer --- */ 00011 inline int dtoi32 (double d) RAPICORN_CONST; 00012 inline int64 dtoi64 (double d) RAPICORN_CONST; 00013 inline int64 iround (double d) RAPICORN_CONST; 00014 inline int64 ifloor (double d) RAPICORN_CONST; 00015 inline int64 iceil (double d) RAPICORN_CONST; 00016 00017 /* --- implementation bits --- */ 00018 inline int RAPICORN_CONST 00019 _dtoi32_generic (double d) 00020 { 00021 /* this relies on the C++ behaviour of round-to-0 */ 00022 return (int) (d < -0.0 ? d - 0.5 : d + 0.5); 00023 } 00024 inline int RAPICORN_CONST 00025 dtoi32 (double d) 00026 { 00027 /* this relies on the hardware default round-to-nearest */ 00028 #if defined __i386__ && defined __GNUC__ 00029 int r; 00030 __asm__ volatile ("fistl %0" 00031 : "=m" (r) 00032 : "t" (d)); 00033 return r; 00034 #endif 00035 return _dtoi32_generic (d); 00036 } 00037 inline int64 RAPICORN_CONST 00038 _dtoi64_generic (double d) 00039 { 00040 /* this relies on the C++ behaviour of round-to-0 */ 00041 return (int64) (d < -0.0 ? d - 0.5 : d + 0.5); 00042 } 00043 inline int64 RAPICORN_CONST 00044 dtoi64 (double d) 00045 { 00046 /* this relies on the hardware default round-to-nearest */ 00047 #if defined __i386__ && defined __GNUC__ 00048 int64 r; 00049 __asm__ volatile ("fistpll %0" 00050 : "=m" (r) 00051 : "t" (d) 00052 : "st"); 00053 return r; 00054 #endif 00055 return _dtoi64_generic (d); 00056 } 00057 inline int64 RAPICORN_CONST iround (double d) { return dtoi64 (round (d)); } 00058 inline int64 RAPICORN_CONST ifloor (double d) { return dtoi64 (floor (d)); } 00059 inline int64 RAPICORN_CONST iceil (double d) { return dtoi64 (ceil (d)); } 00060 00061 } // Rapicorn 00062 00063 #endif /* __RAPICORN_MATH_HH__ */ 00064 /* vim:set ts=8 sts=2 sw=2: */