1 /*! ========================================================================
2 ** Extended Template and Library
6 ** Copyright (c) 2002 Robert B. Quattlebaum Jr.
8 ** This package is free software; you can redistribute it and/or
9 ** modify it under the terms of the GNU General Public License as
10 ** published by the Free Software Foundation; either version 2 of
11 ** the License, or (at your option) any later version.
13 ** This package is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 ** General Public License for more details.
18 ** === N O T E S ===========================================================
20 ** ========================================================================= */
22 /* === S T A R T =========================================================== */
24 #ifndef __ETL__MISC_H_
25 #define __ETL__MISC_H_
27 /* === H E A D E R S ======================================================= */
32 /* === M A C R O S ========================================================= */
34 /* === T Y P E D E F S ===================================================== */
36 /* === C L A S S E S & S T R U C T S ======================================= */
40 template<typename I, typename T> inline I
41 binary_find(I begin, I end, const T& value)
44 I iter(begin+(end-begin)/2);
46 while(end-begin>1 && !(*iter==value))
48 ((*iter<value)?begin:end) = iter;
50 iter = begin+(end-begin)/2;
54 size_t len_(end-begin);
60 while(len_>1 && !(*iter==value))
62 ((*iter<value)?begin:end) = iter;
74 inline int round_to_int(const float x) {
75 /*! \fixme Isn't there some x86 FPU instruction for quickly
76 ** converting a float to a rounded integer? It's worth
77 ** looking into at some point... */
78 // return static_cast<int>(x+0.5f); // <-- (a) fast, but rounds -1.333 to 0!
79 // return static_cast<int>(rintf(x)); // <-- (b) slow, but correct
80 if (x>=0) return static_cast<int>(x + 0.5); // <-- slower than (a), but correct, and faster than (b)
81 else return static_cast<int>(x - 0.5);
83 inline int round_to_int(const double x) {
84 // return static_cast<int>(x+0.5);
85 // return static_cast<int>(rint(x));
86 if (x>=0) return static_cast<int>(x + 0.5);
87 else return static_cast<int>(x - 0.5);
90 inline int ceil_to_int(const float x) { return static_cast<int>(ceil(x)); }
91 inline int ceil_to_int(const double x) { return static_cast<int>(ceil(x)); }
93 inline int floor_to_int(const float x) { return static_cast<int>(x); }
94 inline int floor_to_int(const double x) { return static_cast<int>(x); }
98 /* === E X T E R N S ======================================================= */
100 /* === E N D =============================================================== */