/js/src/jslibmath.h

http://github.com/zpao/v8monkey · C Header · 113 lines · 49 code · 13 blank · 51 comment · 17 complexity · 2179e291c876c1d1b767b89bda1e2673 MD5 · raw file

  1. /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2. * vim: set ts=4 sw=4 et tw=79:
  3. *
  4. * ***** BEGIN LICENSE BLOCK *****
  5. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  6. *
  7. * The contents of this file are subject to the Mozilla Public License Version
  8. * 1.1 (the "License"); you may not use this file except in compliance with
  9. * the License. You may obtain a copy of the License at
  10. * http://www.mozilla.org/MPL/
  11. *
  12. * Software distributed under the License is distributed on an "AS IS" basis,
  13. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  14. * for the specific language governing rights and limitations under the
  15. * License.
  16. *
  17. * The Original Code is Mozilla Communicator client code, released
  18. * March 31, 1998.
  19. *
  20. * The Initial Developer of the Original Code is
  21. * Netscape Communications Corporation.
  22. * Portions created by the Initial Developer are Copyright (C) 1998
  23. * the Initial Developer. All Rights Reserved.
  24. *
  25. * Contributor(s):
  26. * IBM Corp.
  27. *
  28. * Alternatively, the contents of this file may be used under the terms of
  29. * either of the GNU General Public License Version 2 or later (the "GPL"),
  30. * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  31. * in which case the provisions of the GPL or the LGPL are applicable instead
  32. * of those above. If you wish to allow use of your version of this file only
  33. * under the terms of either the GPL or the LGPL, and not to allow others to
  34. * use your version of this file under the terms of the MPL, indicate your
  35. * decision by deleting the provisions above and replace them with the notice
  36. * and other provisions required by the GPL or the LGPL. If you do not delete
  37. * the provisions above, a recipient may use your version of this file under
  38. * the terms of any one of the MPL, the GPL or the LGPL.
  39. *
  40. * ***** END LICENSE BLOCK ***** */
  41. #ifndef _LIBMATH_H
  42. #define _LIBMATH_H
  43. #include <math.h>
  44. #include "jsnum.h"
  45. /*
  46. * Use system provided math routines.
  47. */
  48. /* The right copysign function is not always named the same thing. */
  49. #if __GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
  50. #define js_copysign __builtin_copysign
  51. #elif defined _WIN32
  52. #if _MSC_VER < 1400
  53. /* Try to work around apparent _copysign bustage in VC7.x. */
  54. #define js_copysign js_copysign
  55. extern double js_copysign(double, double);
  56. #else
  57. #define js_copysign _copysign
  58. #endif
  59. #else
  60. #define js_copysign copysign
  61. #endif
  62. #if defined(_M_X64) && defined(_MSC_VER) && _MSC_VER <= 1500
  63. // This is a workaround for fmod bug (http://support.microsoft.com/kb/982107)
  64. extern "C" double js_myfmod(double x, double y);
  65. #define fmod js_myfmod
  66. #endif
  67. /* Consistency wrapper for platform deviations in fmod() */
  68. static inline double
  69. js_fmod(double d, double d2)
  70. {
  71. #ifdef XP_WIN
  72. /*
  73. * Workaround MS fmod bug where 42 % (1/0) => NaN, not 42.
  74. * Workaround MS fmod bug where -0 % -N => 0, not -0.
  75. */
  76. if ((JSDOUBLE_IS_FINITE(d) && JSDOUBLE_IS_INFINITE(d2)) ||
  77. (d == 0 && JSDOUBLE_IS_FINITE(d2))) {
  78. return d;
  79. }
  80. #endif
  81. return fmod(d, d2);
  82. }
  83. namespace js {
  84. inline double
  85. NumberDiv(double a, double b) {
  86. if (b == 0) {
  87. if (a == 0 || JSDOUBLE_IS_NaN(a)
  88. #ifdef XP_WIN
  89. || JSDOUBLE_IS_NaN(b) /* XXX MSVC miscompiles such that (NaN == 0) */
  90. #endif
  91. )
  92. return js_NaN;
  93. if (JSDOUBLE_IS_NEG(a) != JSDOUBLE_IS_NEG(b))
  94. return js_NegativeInfinity;
  95. return js_PositiveInfinity;
  96. }
  97. return a / b;
  98. }
  99. }
  100. #endif /* _LIBMATH_H */