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/. */
6 #ifndef nsMathUtils_h__
7 #define nsMathUtils_h__
9 #define _USE_MATH_DEFINES /* needed for M_ constants on Win32 */
25 return aNum
>= 0.0 ? floor(aNum
+ 0.5) : ceil(aNum
- 0.5);
30 return aNum
>= 0.0f
? floorf(aNum
+ 0.5f
) : ceilf(aNum
- 0.5f
);
33 NS_lround(double aNum
)
35 return aNum
>= 0.0 ? int32_t(aNum
+ 0.5) : int32_t(aNum
- 0.5);
38 /* NS_roundup30 rounds towards infinity for positive and */
39 /* negative numbers. */
41 #if defined(XP_WIN32) && defined(_M_IX86) && !defined(__GNUC__) && !defined(__clang__)
42 inline int32_t NS_lroundup30(float x
)
44 /* Code derived from Laurent de Soras' paper at */
45 /* http://ldesoras.free.fr/doc/articles/rounding_en.pdf */
47 /* Rounding up on Windows is expensive using the float to */
48 /* int conversion and the floor function. A faster */
49 /* approach is to use f87 rounding while assuming the */
50 /* default rounding mode of rounding to the nearest */
51 /* integer. This rounding mode, however, actually rounds */
52 /* to the nearest integer so we add the floating point */
53 /* number to itself and add our rounding factor before */
54 /* doing the conversion to an integer. We then do a right */
55 /* shift of one bit on the integer to divide by two. */
57 /* This routine doesn't handle numbers larger in magnitude */
58 /* than 2^30 but this is fine for NSToCoordRound because */
59 /* Coords are limited to 2^30 in magnitude. */
61 static const double round_to_nearest
= 0.5f
;
65 fld x
; load fp argument
66 fadd st
, st(0) ; double it
67 fadd round_to_nearest
; add the rounding factor
68 fistp dword ptr i
; convert the result to
int
70 return i
>> 1; /* divide by 2 */
72 #endif /* XP_WIN32 && _M_IX86 && !__GNUC__ */
75 NS_lroundf(float aNum
)
77 return aNum
>= 0.0f
? int32_t(aNum
+ 0.5f
) : int32_t(aNum
- 0.5f
);
81 * hypot. We don't need a super accurate version of this, if a platform
82 * turns up with none of the possibilities below it would be okay to fall
83 * back to sqrt(x*x + y*y).
86 NS_hypot(double aNum1
, double aNum2
)
89 return __builtin_hypot(aNum1
, aNum2
);
91 return _hypot(aNum1
, aNum2
);
93 return hypot(aNum1
, aNum2
);
98 * Check whether a floating point number is finite (not +/-infinity and not a
102 NS_finite(double aNum
)
105 // NOTE: '!!' casts an int to bool without spamming MSVC warning C4800.
106 return !!_finite(aNum
);
107 #elif defined(XP_DARWIN)
108 // Darwin has deprecated |finite| and recommends |isfinite|. The former is
109 // not present in the iOS SDK.
110 return std::isfinite(aNum
);
117 * Returns the result of the modulo of x by y using a floored division.
118 * fmod(x, y) is using a truncated division.
119 * The main difference is that the result of this method will have the sign of
120 * y while the result of fmod(x, y) will have the sign of x.
123 NS_floorModulo(double aNum1
, double aNum2
)
125 return (aNum1
- aNum2
* floor(aNum1
/ aNum2
));