1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef nsMathUtils_h__
8 #define nsMathUtils_h__
14 #if defined(XP_SOLARIS)
24 return aNum
>= 0.0 ? floor(aNum
+ 0.5) : ceil(aNum
- 0.5);
29 return aNum
>= 0.0f
? floorf(aNum
+ 0.5f
) : ceilf(aNum
- 0.5f
);
32 NS_lround(double aNum
)
34 return aNum
>= 0.0 ? int32_t(aNum
+ 0.5) : int32_t(aNum
- 0.5);
37 /* NS_roundup30 rounds towards infinity for positive and */
38 /* negative numbers. */
40 #if defined(XP_WIN32) && defined(_M_IX86) && !defined(__GNUC__) && !defined(__clang__)
41 inline int32_t NS_lroundup30(float x
)
43 /* Code derived from Laurent de Soras' paper at */
44 /* http://ldesoras.free.fr/doc/articles/rounding_en.pdf */
46 /* Rounding up on Windows is expensive using the float to */
47 /* int conversion and the floor function. A faster */
48 /* approach is to use f87 rounding while assuming the */
49 /* default rounding mode of rounding to the nearest */
50 /* integer. This rounding mode, however, actually rounds */
51 /* to the nearest integer so we add the floating point */
52 /* number to itself and add our rounding factor before */
53 /* doing the conversion to an integer. We then do a right */
54 /* shift of one bit on the integer to divide by two. */
56 /* This routine doesn't handle numbers larger in magnitude */
57 /* than 2^30 but this is fine for NSToCoordRound because */
58 /* Coords are limited to 2^30 in magnitude. */
60 static const double round_to_nearest
= 0.5f
;
64 fld x
; load fp argument
65 fadd st
, st(0) ; double it
66 fadd round_to_nearest
; add the rounding factor
67 fistp dword ptr i
; convert the result to
int
69 return i
>> 1; /* divide by 2 */
71 #endif /* XP_WIN32 && _M_IX86 && !__GNUC__ */
74 NS_lroundf(float aNum
)
76 return aNum
>= 0.0f
? int32_t(aNum
+ 0.5f
) : int32_t(aNum
- 0.5f
);
80 * hypot. We don't need a super accurate version of this, if a platform
81 * turns up with none of the possibilities below it would be okay to fall
82 * back to sqrt(x*x + y*y).
85 NS_hypot(double aNum1
, double aNum2
)
88 return __builtin_hypot(aNum1
, aNum2
);
90 return _hypot(aNum1
, aNum2
);
92 return hypot(aNum1
, aNum2
);
97 * Check whether a floating point number is finite (not +/-infinity and not a
101 NS_finite(double aNum
)
104 // NOTE: '!!' casts an int to bool without spamming MSVC warning C4800.
105 return !!_finite(aNum
);
107 return std::isfinite(aNum
);
112 * Returns the result of the modulo of x by y using a floored division.
113 * fmod(x, y) is using a truncated division.
114 * The main difference is that the result of this method will have the sign of
115 * y while the result of fmod(x, y) will have the sign of x.
118 NS_floorModulo(double aNum1
, double aNum2
)
120 return (aNum1
- aNum2
* floor(aNum1
/ aNum2
));