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
4 : *
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/
9 : *
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.
14 : *
15 : * The Original Code is Mozilla Foundation code.
16 : *
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.
20 : *
21 : * Contributor(s):
22 : * Stuart Parmenter <stuart@mozilla.com>
23 : *
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.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #ifndef nsMathUtils_h__
39 : #define nsMathUtils_h__
40 :
41 : #define _USE_MATH_DEFINES /* needed for M_ constants on Win32 */
42 :
43 : #include "nscore.h"
44 : #include <cmath>
45 : #include <float.h>
46 :
47 : #ifdef SOLARIS
48 : #include <ieeefp.h>
49 : #endif
50 :
51 : /*
52 : * round
53 : */
54 0 : inline NS_HIDDEN_(double) NS_round(double x)
55 : {
56 0 : return x >= 0.0 ? floor(x + 0.5) : ceil(x - 0.5);
57 : }
58 0 : inline NS_HIDDEN_(float) NS_roundf(float x)
59 : {
60 0 : return x >= 0.0f ? floorf(x + 0.5f) : ceilf(x - 0.5f);
61 : }
62 0 : inline NS_HIDDEN_(PRInt32) NS_lround(double x)
63 : {
64 0 : return x >= 0.0 ? PRInt32(x + 0.5) : PRInt32(x - 0.5);
65 : }
66 :
67 : /* NS_roundup30 rounds towards infinity for positive and */
68 : /* negative numbers. */
69 :
70 : #if defined(XP_WIN32) && defined(_M_IX86) && !defined(__GNUC__)
71 : inline NS_HIDDEN_(PRInt32) NS_lroundup30(float x)
72 : {
73 : /* Code derived from Laurent de Soras' paper at */
74 : /* http://ldesoras.free.fr/doc/articles/rounding_en.pdf */
75 :
76 : /* Rounding up on Windows is expensive using the float to */
77 : /* int conversion and the floor function. A faster */
78 : /* approach is to use f87 rounding while assuming the */
79 : /* default rounding mode of rounding to the nearest */
80 : /* integer. This rounding mode, however, actually rounds */
81 : /* to the nearest integer so we add the floating point */
82 : /* number to itself and add our rounding factor before */
83 : /* doing the conversion to an integer. We then do a right */
84 : /* shift of one bit on the integer to divide by two. */
85 :
86 : /* This routine doesn't handle numbers larger in magnitude */
87 : /* than 2^30 but this is fine for NSToCoordRound because */
88 : /* Coords are limited to 2^30 in magnitude. */
89 :
90 : static const double round_to_nearest = 0.5f;
91 : int i;
92 :
93 : __asm {
94 : fld x ; load fp argument
95 : fadd st, st(0) ; double it
96 : fadd round_to_nearest ; add the rounding factor
97 : fistp dword ptr i ; convert the result to int
98 : }
99 : return i >> 1; /* divide by 2 */
100 : }
101 : #endif /* XP_WIN32 && _M_IX86 && !__GNUC__ */
102 :
103 0 : inline NS_HIDDEN_(PRInt32) NS_lroundf(float x)
104 : {
105 0 : return x >= 0.0f ? PRInt32(x + 0.5f) : PRInt32(x - 0.5f);
106 : }
107 :
108 : /*
109 : * hypot. We don't need a super accurate version of this, if a platform
110 : * turns up with none of the possibilities below it would be okay to fall
111 : * back to sqrt(x*x + y*y).
112 : */
113 0 : inline NS_HIDDEN_(double) NS_hypot(double x, double y)
114 : {
115 : #if __GNUC__ >= 4
116 0 : return __builtin_hypot(x, y);
117 : #elif defined _WIN32
118 : return _hypot(x, y);
119 : #else
120 : return hypot(x, y);
121 : #endif
122 : }
123 :
124 : /**
125 : * Check whether a floating point number is finite (not +/-infinity and not a
126 : * NaN value).
127 : */
128 0 : inline NS_HIDDEN_(bool) NS_finite(double d)
129 : {
130 : #ifdef WIN32
131 : // NOTE: '!!' casts an int to bool without spamming MSVC warning C4800.
132 : return !!_finite(d);
133 : #elif defined(XP_DARWIN)
134 : // Darwin has deprecated |finite| and recommends |isfinite|. The former is
135 : // not present in the iOS SDK.
136 : return std::isfinite(d);
137 : #else
138 0 : return finite(d);
139 : #endif
140 : }
141 :
142 : #endif
|