js/src/jslibmath.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/jslibmath.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,76 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
     1.5 + * vim: set ts=8 sts=4 et sw=4 tw=99:
     1.6 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#ifndef jslibmath_h
    1.11 +#define jslibmath_h
    1.12 +
    1.13 +#include "mozilla/FloatingPoint.h"
    1.14 +
    1.15 +#include <math.h>
    1.16 +
    1.17 +#include "jsnum.h"
    1.18 +
    1.19 +/*
    1.20 + * Use system provided math routines.
    1.21 + */
    1.22 +
    1.23 +/* The right copysign function is not always named the same thing. */
    1.24 +#ifdef __GNUC__
    1.25 +#define js_copysign __builtin_copysign
    1.26 +#elif defined _WIN32
    1.27 +#define js_copysign _copysign
    1.28 +#else
    1.29 +#define js_copysign copysign
    1.30 +#endif
    1.31 +
    1.32 +/* Consistency wrapper for platform deviations in fmod() */
    1.33 +static inline double
    1.34 +js_fmod(double d, double d2)
    1.35 +{
    1.36 +#ifdef XP_WIN
    1.37 +    /*
    1.38 +     * Workaround MS fmod bug where 42 % (1/0) => NaN, not 42.
    1.39 +     * Workaround MS fmod bug where -0 % -N => 0, not -0.
    1.40 +     */
    1.41 +    if ((mozilla::IsFinite(d) && mozilla::IsInfinite(d2)) ||
    1.42 +        (d == 0 && mozilla::IsFinite(d2))) {
    1.43 +        return d;
    1.44 +    }
    1.45 +#endif
    1.46 +    return fmod(d, d2);
    1.47 +}
    1.48 +
    1.49 +namespace js {
    1.50 +
    1.51 +inline double
    1.52 +NumberDiv(double a, double b)
    1.53 +{
    1.54 +    if (b == 0) {
    1.55 +        if (a == 0 || mozilla::IsNaN(a)
    1.56 +#ifdef XP_WIN
    1.57 +            || mozilla::IsNaN(b) /* XXX MSVC miscompiles such that (NaN == 0) */
    1.58 +#endif
    1.59 +        )
    1.60 +            return JS::GenericNaN();
    1.61 +
    1.62 +        if (mozilla::IsNegative(a) != mozilla::IsNegative(b))
    1.63 +            return mozilla::NegativeInfinity<double>();
    1.64 +        return mozilla::PositiveInfinity<double>();
    1.65 +    }
    1.66 +
    1.67 +    return a / b;
    1.68 +}
    1.69 +
    1.70 +inline double
    1.71 +NumberMod(double a, double b) {
    1.72 +    if (b == 0)
    1.73 +        return JS::GenericNaN();
    1.74 +    return js_fmod(a, b);
    1.75 +}
    1.76 +
    1.77 +}
    1.78 +
    1.79 +#endif /* jslibmath_h */

mercurial