Bug 1927077 - test(webgpu): update CTS to d473d09475bffec9569fe5c45834bb6aaad44818...
[gecko.git] / xpcom / ds / nsMathUtils.h
blob527e0c3eb2732f32cdd63047a5965f32824cbb3f
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__
10 #include "nscore.h"
11 #include <cmath>
12 #include <float.h>
14 #if defined(XP_SOLARIS)
15 # include <ieeefp.h>
16 #endif
19 * round
21 inline double NS_round(double aNum) {
22 return aNum >= 0.0 ? floor(aNum + 0.5) : ceil(aNum - 0.5);
24 inline float NS_roundf(float aNum) {
25 return aNum >= 0.0f ? floorf(aNum + 0.5f) : ceilf(aNum - 0.5f);
27 inline int32_t NS_lround(double aNum) {
28 return aNum >= 0.0 ? int32_t(aNum + 0.5) : int32_t(aNum - 0.5);
31 /* NS_roundup30 rounds towards infinity for positive and */
32 /* negative numbers. */
34 #if defined(XP_WIN) && defined(_M_IX86) && !defined(__GNUC__) && \
35 !defined(__clang__)
36 inline int32_t NS_lroundup30(float x) {
37 /* Code derived from Laurent de Soras' paper at */
38 /* http://ldesoras.free.fr/doc/articles/rounding_en.pdf */
40 /* Rounding up on Windows is expensive using the float to */
41 /* int conversion and the floor function. A faster */
42 /* approach is to use f87 rounding while assuming the */
43 /* default rounding mode of rounding to the nearest */
44 /* integer. This rounding mode, however, actually rounds */
45 /* to the nearest integer so we add the floating point */
46 /* number to itself and add our rounding factor before */
47 /* doing the conversion to an integer. We then do a right */
48 /* shift of one bit on the integer to divide by two. */
50 /* This routine doesn't handle numbers larger in magnitude */
51 /* than 2^30 but this is fine for NSToCoordRound because */
52 /* Coords are limited to 2^30 in magnitude. */
54 static const double round_to_nearest = 0.5f;
55 int i;
57 __asm {
58 fld x ; load fp argument
59 fadd st, st(0) ; double it
60 fadd round_to_nearest ; add the rounding factor
61 fistp dword ptr i ; convert the result to int
63 return i >> 1; /* divide by 2 */
65 #endif /* XP_WIN && _M_IX86 && !__GNUC__ */
67 inline int32_t NS_lroundf(float aNum) {
68 return aNum >= 0.0f ? int32_t(aNum + 0.5f) : int32_t(aNum - 0.5f);
72 * hypot. We don't need a super accurate version of this, if a platform
73 * turns up with none of the possibilities below it would be okay to fall
74 * back to sqrt(x*x + y*y).
76 inline double NS_hypot(double aNum1, double aNum2) {
77 #ifdef __GNUC__
78 return __builtin_hypot(aNum1, aNum2);
79 #elif defined _WIN32
80 return _hypot(aNum1, aNum2);
81 #else
82 return hypot(aNum1, aNum2);
83 #endif
86 /**
87 * Check whether a floating point number is finite (not +/-infinity and not a
88 * NaN value).
90 inline bool NS_finite(double aNum) {
91 #ifdef WIN32
92 // NOTE: '!!' casts an int to bool without spamming MSVC warning C4800.
93 return !!_finite(aNum);
94 #else
95 return std::isfinite(aNum);
96 #endif
99 /**
100 * Returns the result of the modulo of x by y using a floored division.
101 * fmod(x, y) is using a truncated division.
102 * The main difference is that the result of this method will have the sign of
103 * y while the result of fmod(x, y) will have the sign of x.
105 inline double NS_floorModulo(double aNum1, double aNum2) {
106 return (aNum1 - aNum2 * floor(aNum1 / aNum2));
109 #endif