1 /*! ========================================================================
2 ** Extended Template and Library
6 ** Copyright (c) 2002 Robert B. Quattlebaum Jr.
7 ** Copyright (c) 2007 Chris Moore
9 ** This package is free software; you can redistribute it and/or
10 ** modify it under the terms of the GNU General Public License as
11 ** published by the Free Software Foundation; either version 2 of
12 ** the License, or (at your option) any later version.
14 ** This package is distributed in the hope that it will be useful,
15 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 ** General Public License for more details.
19 ** === N O T E S ===========================================================
21 ** ========================================================================= */
23 /* === S T A R T =========================================================== */
25 #ifndef __ETL__MISC_H_
26 #define __ETL__MISC_H_
28 /* === H E A D E R S ======================================================= */
33 /* === M A C R O S ========================================================= */
35 /* === T Y P E D E F S ===================================================== */
37 /* === C L A S S E S & S T R U C T S ======================================= */
41 template<typename I, typename T> inline I
42 binary_find(I begin, I end, const T& value)
45 I iter(begin+(end-begin)/2);
47 while(end-begin>1 && !(*iter==value))
49 ((*iter<value)?begin:end) = iter;
51 iter = begin+(end-begin)/2;
55 size_t len_(end-begin);
61 while(len_>1 && !(*iter==value))
63 ((*iter<value)?begin:end) = iter;
75 inline int round_to_int(const float x) {
76 /*! \todo Isn't there some x86 FPU instruction for quickly
77 ** converting a float to a rounded integer? It's worth
78 ** looking into at some point... */
79 // return static_cast<int>(x+0.5f); // <-- (a) fast, but rounds -1.333 to 0!
80 // return static_cast<int>(rintf(x)); // <-- (b) slow, but correct
81 if (x>=0) return static_cast<int>(x + 0.5); // <-- slower than (a), but correct, and faster than (b)
82 else return static_cast<int>(x - 0.5);
84 inline int round_to_int(const double x) {
85 // return static_cast<int>(x+0.5);
86 // return static_cast<int>(rint(x));
87 if (x>=0) return static_cast<int>(x + 0.5);
88 else return static_cast<int>(x - 0.5);
91 inline int ceil_to_int(const float x) { return static_cast<int>(ceil(x)); }
92 inline int ceil_to_int(const double x) { return static_cast<int>(ceil(x)); }
94 inline int floor_to_int(const float x) { return static_cast<int>(x); }
95 inline int floor_to_int(const double x) { return static_cast<int>(x); }
99 /* === E X T E R N S ======================================================= */
101 /* === E N D =============================================================== */