Bug 449474, Fx3.0.2 updates should force modules/distribution.js, and use the version...
[mozilla-1.9.git] / xpcom / ds / nsMathUtils.h
blob6d980ccdbfb0d2dcb31920181793c451e1ece73e
1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 * ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is Mozilla Foundation code.
17 * The Initial Developer of the Original Code is Mozilla Foundation.
18 * Portions created by the Initial Developer are Copyright (C) 2007
19 * the Initial Developer. All Rights Reserved.
21 * Contributor(s):
22 * Stuart Parmenter <stuart@mozilla.com>
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #ifndef nsMathUtils_h__
39 #define nsMathUtils_h__
41 #include "nscore.h"
42 #include <math.h>
43 #include <float.h>
46 * round
48 inline NS_HIDDEN_(double) NS_round(double x)
50 return x >= 0.0 ? floor(x + 0.5) : ceil(x - 0.5);
52 inline NS_HIDDEN_(float) NS_roundf(float x)
54 return x >= 0.0f ? floorf(x + 0.5f) : ceilf(x - 0.5f);
56 inline NS_HIDDEN_(PRInt32) NS_lround(double x)
58 return x >= 0.0 ? PRInt32(x + 0.5) : PRInt32(x - 0.5);
61 /* NS_roundup30 rounds towards infinity for positive and */
62 /* negative numbers. */
64 #if defined(XP_WIN32) && defined(_M_IX86) && !defined(__GNUC__)
65 inline NS_HIDDEN_(PRInt32) NS_lroundup30(float x)
67 /* Code derived from Laurent de Soras' paper at */
68 /* http://ldesoras.free.fr/doc/articles/rounding_en.pdf */
70 /* Rounding up on Windows is expensive using the float to */
71 /* int conversion and the floor function. A faster */
72 /* approach is to use f87 rounding while assuming the */
73 /* default rounding mode of rounding to the nearest */
74 /* integer. This rounding mode, however, actually rounds */
75 /* to the nearest integer so we add the floating point */
76 /* number to itself and add our rounding factor before */
77 /* doing the conversion to an integer. We then do a right */
78 /* shift of one bit on the integer to divide by two. */
80 /* This routine doesn't handle numbers larger in magnitude */
81 /* than 2^30 but this is fine for NSToCoordRound because */
82 /* Coords are limited to 2^30 in magnitude. */
84 static const double round_to_nearest = 0.5f;
85 int i;
87 __asm {
88 fld x ; load fp argument
89 fadd st, st(0) ; double it
90 fadd round_to_nearest ; add the rounding factor
91 fistp dword ptr i ; convert the result to int
93 return i >> 1; /* divide by 2 */
95 #endif /* XP_WIN32 && _M_IX86 && !__GNUC__ */
97 inline NS_HIDDEN_(PRInt32) NS_lroundf(float x)
99 return x >= 0.0f ? PRInt32(x + 0.5f) : PRInt32(x - 0.5f);
103 * ceil
105 inline NS_HIDDEN_(double) NS_ceil(double x)
107 return ceil(x);
109 inline NS_HIDDEN_(float) NS_ceilf(float x)
111 return ceilf(x);
115 * floor
117 inline NS_HIDDEN_(double) NS_floor(double x)
119 return floor(x);
121 inline NS_HIDDEN_(float) NS_floorf(float x)
123 return floorf(x);
126 #endif