michael@0: /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef nsMathUtils_h__ michael@0: #define nsMathUtils_h__ michael@0: michael@0: #define _USE_MATH_DEFINES /* needed for M_ constants on Win32 */ michael@0: michael@0: #include "nscore.h" michael@0: #include michael@0: #include michael@0: michael@0: #ifdef SOLARIS michael@0: #include michael@0: #endif michael@0: michael@0: /* michael@0: * round michael@0: */ michael@0: inline NS_HIDDEN_(double) NS_round(double x) michael@0: { michael@0: return x >= 0.0 ? floor(x + 0.5) : ceil(x - 0.5); michael@0: } michael@0: inline NS_HIDDEN_(float) NS_roundf(float x) michael@0: { michael@0: return x >= 0.0f ? floorf(x + 0.5f) : ceilf(x - 0.5f); michael@0: } michael@0: inline NS_HIDDEN_(int32_t) NS_lround(double x) michael@0: { michael@0: return x >= 0.0 ? int32_t(x + 0.5) : int32_t(x - 0.5); michael@0: } michael@0: michael@0: /* NS_roundup30 rounds towards infinity for positive and */ michael@0: /* negative numbers. */ michael@0: michael@0: #if defined(XP_WIN32) && defined(_M_IX86) && !defined(__GNUC__) michael@0: inline NS_HIDDEN_(int32_t) NS_lroundup30(float x) michael@0: { michael@0: /* Code derived from Laurent de Soras' paper at */ michael@0: /* http://ldesoras.free.fr/doc/articles/rounding_en.pdf */ michael@0: michael@0: /* Rounding up on Windows is expensive using the float to */ michael@0: /* int conversion and the floor function. A faster */ michael@0: /* approach is to use f87 rounding while assuming the */ michael@0: /* default rounding mode of rounding to the nearest */ michael@0: /* integer. This rounding mode, however, actually rounds */ michael@0: /* to the nearest integer so we add the floating point */ michael@0: /* number to itself and add our rounding factor before */ michael@0: /* doing the conversion to an integer. We then do a right */ michael@0: /* shift of one bit on the integer to divide by two. */ michael@0: michael@0: /* This routine doesn't handle numbers larger in magnitude */ michael@0: /* than 2^30 but this is fine for NSToCoordRound because */ michael@0: /* Coords are limited to 2^30 in magnitude. */ michael@0: michael@0: static const double round_to_nearest = 0.5f; michael@0: int i; michael@0: michael@0: __asm { michael@0: fld x ; load fp argument michael@0: fadd st, st(0) ; double it michael@0: fadd round_to_nearest ; add the rounding factor michael@0: fistp dword ptr i ; convert the result to int michael@0: } michael@0: return i >> 1; /* divide by 2 */ michael@0: } michael@0: #endif /* XP_WIN32 && _M_IX86 && !__GNUC__ */ michael@0: michael@0: inline NS_HIDDEN_(int32_t) NS_lroundf(float x) michael@0: { michael@0: return x >= 0.0f ? int32_t(x + 0.5f) : int32_t(x - 0.5f); michael@0: } michael@0: michael@0: /* michael@0: * hypot. We don't need a super accurate version of this, if a platform michael@0: * turns up with none of the possibilities below it would be okay to fall michael@0: * back to sqrt(x*x + y*y). michael@0: */ michael@0: inline NS_HIDDEN_(double) NS_hypot(double x, double y) michael@0: { michael@0: #ifdef __GNUC__ michael@0: return __builtin_hypot(x, y); michael@0: #elif defined _WIN32 michael@0: return _hypot(x, y); michael@0: #else michael@0: return hypot(x, y); michael@0: #endif michael@0: } michael@0: michael@0: /** michael@0: * Check whether a floating point number is finite (not +/-infinity and not a michael@0: * NaN value). michael@0: */ michael@0: inline NS_HIDDEN_(bool) NS_finite(double d) michael@0: { michael@0: #ifdef WIN32 michael@0: // NOTE: '!!' casts an int to bool without spamming MSVC warning C4800. michael@0: return !!_finite(d); michael@0: #elif defined(XP_DARWIN) michael@0: // Darwin has deprecated |finite| and recommends |isfinite|. The former is michael@0: // not present in the iOS SDK. michael@0: return std::isfinite(d); michael@0: #else michael@0: return finite(d); michael@0: #endif michael@0: } michael@0: michael@0: /** michael@0: * Returns the result of the modulo of x by y using a floored division. michael@0: * fmod(x, y) is using a truncated division. michael@0: * The main difference is that the result of this method will have the sign of michael@0: * y while the result of fmod(x, y) will have the sign of x. michael@0: */ michael@0: inline NS_HIDDEN_(double) NS_floorModulo(double x, double y) michael@0: { michael@0: return (x - y * floor(x / y)); michael@0: } michael@0: michael@0: #endif