Try to fix intermittent refcount assertions in the presence of more than one thread...
[mozilla-central.git] / xpcom / ds / nsMathUtils.h
blob6b97ba7b07f6df97817cbc6e79bc8218fa7eedcb
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 #define _USE_MATH_DEFINES /* needed for M_ constants on Win32 */
43 #include "nscore.h"
44 #include <math.h>
45 #include <float.h>
48 * The M_ constants are not defined by the WinCE/WinMo SDKs even with
49 * _USE_MATH_DEFINES. Provide a fallback. We assume that the entire
50 * set is not available if M_E isn't. Values taken from GNU libc,
51 * providing just enough precision for IEEE double.
54 #ifndef M_E
55 # define M_E 2.7182818284590452354 /* e */
56 # define M_LOG2E 1.4426950408889634074 /* log_2 e */
57 # define M_LOG10E 0.43429448190325182765 /* log_10 e */
58 # define M_LN2 0.69314718055994530942 /* log_e 2 */
59 # define M_LN10 2.30258509299404568402 /* log_e 10 */
60 # define M_PI 3.14159265358979323846 /* pi */
61 # define M_PI_2 1.57079632679489661923 /* pi/2 */
62 # define M_PI_4 0.78539816339744830962 /* pi/4 */
63 # define M_1_PI 0.31830988618379067154 /* 1/pi */
64 # define M_2_PI 0.63661977236758134308 /* 2/pi */
65 # define M_2_SQRTPI 1.12837916709551257390 /* 2/sqrt(pi) */
66 # define M_SQRT2 1.41421356237309504880 /* sqrt(2) */
67 # define M_SQRT1_2 0.70710678118654752440 /* 1/sqrt(2) */
68 #endif
71 * round
73 inline NS_HIDDEN_(double) NS_round(double x)
75 return x >= 0.0 ? floor(x + 0.5) : ceil(x - 0.5);
77 inline NS_HIDDEN_(float) NS_roundf(float x)
79 return x >= 0.0f ? floorf(x + 0.5f) : ceilf(x - 0.5f);
81 inline NS_HIDDEN_(PRInt32) NS_lround(double x)
83 return x >= 0.0 ? PRInt32(x + 0.5) : PRInt32(x - 0.5);
86 /* NS_roundup30 rounds towards infinity for positive and */
87 /* negative numbers. */
89 #if defined(XP_WIN32) && defined(_M_IX86) && !defined(__GNUC__)
90 inline NS_HIDDEN_(PRInt32) NS_lroundup30(float x)
92 /* Code derived from Laurent de Soras' paper at */
93 /* http://ldesoras.free.fr/doc/articles/rounding_en.pdf */
95 /* Rounding up on Windows is expensive using the float to */
96 /* int conversion and the floor function. A faster */
97 /* approach is to use f87 rounding while assuming the */
98 /* default rounding mode of rounding to the nearest */
99 /* integer. This rounding mode, however, actually rounds */
100 /* to the nearest integer so we add the floating point */
101 /* number to itself and add our rounding factor before */
102 /* doing the conversion to an integer. We then do a right */
103 /* shift of one bit on the integer to divide by two. */
105 /* This routine doesn't handle numbers larger in magnitude */
106 /* than 2^30 but this is fine for NSToCoordRound because */
107 /* Coords are limited to 2^30 in magnitude. */
109 static const double round_to_nearest = 0.5f;
110 int i;
112 __asm {
113 fld x ; load fp argument
114 fadd st, st(0) ; double it
115 fadd round_to_nearest ; add the rounding factor
116 fistp dword ptr i ; convert the result to int
118 return i >> 1; /* divide by 2 */
120 #endif /* XP_WIN32 && _M_IX86 && !__GNUC__ */
122 inline NS_HIDDEN_(PRInt32) NS_lroundf(float x)
124 return x >= 0.0f ? PRInt32(x + 0.5f) : PRInt32(x - 0.5f);
128 * ceil
130 inline NS_HIDDEN_(double) NS_ceil(double x)
132 return ceil(x);
134 inline NS_HIDDEN_(float) NS_ceilf(float x)
136 return ceilf(x);
140 * floor
142 inline NS_HIDDEN_(double) NS_floor(double x)
144 return floor(x);
146 inline NS_HIDDEN_(float) NS_floorf(float x)
148 return floorf(x);
152 * hypot. We don't need a super accurate version of this, if a platform
153 * turns up with none of the possibilities below it would be okay to fall
154 * back to sqrt(x*x + y*y).
156 inline NS_HIDDEN_(double) NS_hypot(double x, double y)
158 #if __GNUC__ >= 4
159 return __builtin_hypot(x, y);
160 #elif defined _WIN32
161 return _hypot(x, y);
162 #else
163 return hypot(x, y);
164 #endif
167 #endif