xpcom/ds/nsMathUtils.h

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:7607e8ee4f60
1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 #ifndef nsMathUtils_h__
7 #define nsMathUtils_h__
8
9 #define _USE_MATH_DEFINES /* needed for M_ constants on Win32 */
10
11 #include "nscore.h"
12 #include <cmath>
13 #include <float.h>
14
15 #ifdef SOLARIS
16 #include <ieeefp.h>
17 #endif
18
19 /*
20 * round
21 */
22 inline NS_HIDDEN_(double) NS_round(double x)
23 {
24 return x >= 0.0 ? floor(x + 0.5) : ceil(x - 0.5);
25 }
26 inline NS_HIDDEN_(float) NS_roundf(float x)
27 {
28 return x >= 0.0f ? floorf(x + 0.5f) : ceilf(x - 0.5f);
29 }
30 inline NS_HIDDEN_(int32_t) NS_lround(double x)
31 {
32 return x >= 0.0 ? int32_t(x + 0.5) : int32_t(x - 0.5);
33 }
34
35 /* NS_roundup30 rounds towards infinity for positive and */
36 /* negative numbers. */
37
38 #if defined(XP_WIN32) && defined(_M_IX86) && !defined(__GNUC__)
39 inline NS_HIDDEN_(int32_t) NS_lroundup30(float x)
40 {
41 /* Code derived from Laurent de Soras' paper at */
42 /* http://ldesoras.free.fr/doc/articles/rounding_en.pdf */
43
44 /* Rounding up on Windows is expensive using the float to */
45 /* int conversion and the floor function. A faster */
46 /* approach is to use f87 rounding while assuming the */
47 /* default rounding mode of rounding to the nearest */
48 /* integer. This rounding mode, however, actually rounds */
49 /* to the nearest integer so we add the floating point */
50 /* number to itself and add our rounding factor before */
51 /* doing the conversion to an integer. We then do a right */
52 /* shift of one bit on the integer to divide by two. */
53
54 /* This routine doesn't handle numbers larger in magnitude */
55 /* than 2^30 but this is fine for NSToCoordRound because */
56 /* Coords are limited to 2^30 in magnitude. */
57
58 static const double round_to_nearest = 0.5f;
59 int i;
60
61 __asm {
62 fld x ; load fp argument
63 fadd st, st(0) ; double it
64 fadd round_to_nearest ; add the rounding factor
65 fistp dword ptr i ; convert the result to int
66 }
67 return i >> 1; /* divide by 2 */
68 }
69 #endif /* XP_WIN32 && _M_IX86 && !__GNUC__ */
70
71 inline NS_HIDDEN_(int32_t) NS_lroundf(float x)
72 {
73 return x >= 0.0f ? int32_t(x + 0.5f) : int32_t(x - 0.5f);
74 }
75
76 /*
77 * hypot. We don't need a super accurate version of this, if a platform
78 * turns up with none of the possibilities below it would be okay to fall
79 * back to sqrt(x*x + y*y).
80 */
81 inline NS_HIDDEN_(double) NS_hypot(double x, double y)
82 {
83 #ifdef __GNUC__
84 return __builtin_hypot(x, y);
85 #elif defined _WIN32
86 return _hypot(x, y);
87 #else
88 return hypot(x, y);
89 #endif
90 }
91
92 /**
93 * Check whether a floating point number is finite (not +/-infinity and not a
94 * NaN value).
95 */
96 inline NS_HIDDEN_(bool) NS_finite(double d)
97 {
98 #ifdef WIN32
99 // NOTE: '!!' casts an int to bool without spamming MSVC warning C4800.
100 return !!_finite(d);
101 #elif defined(XP_DARWIN)
102 // Darwin has deprecated |finite| and recommends |isfinite|. The former is
103 // not present in the iOS SDK.
104 return std::isfinite(d);
105 #else
106 return finite(d);
107 #endif
108 }
109
110 /**
111 * Returns the result of the modulo of x by y using a floored division.
112 * fmod(x, y) is using a truncated division.
113 * The main difference is that the result of this method will have the sign of
114 * y while the result of fmod(x, y) will have the sign of x.
115 */
116 inline NS_HIDDEN_(double) NS_floorModulo(double x, double y)
117 {
118 return (x - y * floor(x / y));
119 }
120
121 #endif

mercurial