File indexing completed on 2024-05-12 15:43:39

0001 /*
0002  * Copyright (C) 2006, 2007 Apple Inc.  All rights reserved.
0003  *
0004  * Redistribution and use in source and binary forms, with or without
0005  * modification, are permitted provided that the following conditions
0006  * are met:
0007  * 1. Redistributions of source code must retain the above copyright
0008  *    notice, this list of conditions and the following disclaimer.
0009  * 2. Redistributions in binary form must reproduce the above copyright
0010  *    notice, this list of conditions and the following disclaimer in the
0011  *    documentation and/or other materials provided with the distribution.
0012  *
0013  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
0014  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0015  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
0016  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
0017  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
0018  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
0019  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
0020  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
0021  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0022  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
0023  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0024  */
0025 
0026 #ifndef WTF_MathExtras_h
0027 #define WTF_MathExtras_h
0028 
0029 #include <math.h>
0030 
0031 #if PLATFORM(WIN_OS)
0032 
0033 #include "kjs/JSImmediate.h"
0034 #include <limits>
0035 
0036 #if HAVE(FLOAT_H)
0037 #include <float.h>
0038 #endif
0039 
0040 #endif
0041 
0042 #ifndef M_PI
0043 const double piDouble = 3.14159265358979323846;
0044 const float piFloat = 3.14159265358979323846f;
0045 #else
0046 const double piDouble = M_PI;
0047 const float piFloat = static_cast<float>(M_PI);
0048 #endif
0049 
0050 #ifndef M_PI_4
0051 const double piOverFourDouble = 0.785398163397448309616;
0052 const float piOverFourFloat = 0.785398163397448309616f;
0053 #else
0054 const double piOverFourDouble = M_PI_4;
0055 const float piOverFourFloat = static_cast<float>(M_PI_4);
0056 #endif     // !BUILDING_KDE__
0057 
0058 #if defined(WTF_COMPILER_MSVC)
0059 
0060 #ifndef BUILDING_KDE__
0061 inline bool isinf(double num)
0062 {
0063     return !_finite(num) && !_isnan(num);
0064 }
0065 inline bool isnan(double num)
0066 {
0067     return _isnan(num);
0068 }
0069 inline long lround(double num)
0070 {
0071     return num > 0 ? num + 0.5 : ceil(num - 0.5);
0072 }
0073 inline long lroundf(float num)
0074 {
0075     return num > 0 ? num + 0.5f : ceilf(num - 0.5f);
0076 }
0077 inline double round(double num)
0078 {
0079     return num > 0 ? floor(num + 0.5) : ceil(num - 0.5);
0080 }
0081 inline float roundf(float num)
0082 {
0083     return num > 0 ? floorf(num + 0.5f) : ceilf(num - 0.5f);
0084 }
0085 inline bool signbit(double num)
0086 {
0087     return _copysign(1.0, num) < 0;
0088 }
0089 #endif
0090 
0091 #if _MSC_VER < 1920
0092 #ifndef BUILDING_KDE__
0093 // FIXME: where to get std::numeric_limits from?
0094 // Work around a bug in Win, where atan2(+-infinity, +-infinity) yields NaN instead of specific values.
0095 inline double wtf_atan2(double x, double y)
0096 {
0097     static double posInf = std::numeric_limits<double>::infinity();
0098     static double negInf = -std::numeric_limits<double>::infinity();
0099 
0100     double result = KJS::NaN;
0101 
0102     if (x == posInf && y == posInf) {
0103         result = piOverFourDouble;
0104     } else if (x == posInf && y == negInf) {
0105         result = 3 * piOverFourDouble;
0106     } else if (x == negInf && y == posInf) {
0107         result = -piOverFourDouble;
0108     } else if (x == negInf && y == negInf) {
0109         result = -3 * piOverFourDouble;
0110     } else {
0111         result = ::atan2(x, y);
0112     }
0113 
0114     return result;
0115 }
0116 #else      // !BUILDING_KDE__
0117 
0118 #define wtf_atan2(x, y) atan2(x, y)
0119 
0120 #endif     // !BUILDING_KDE__
0121 
0122 // Work around a bug in the Microsoft CRT, where fmod(x, +-infinity) yields NaN instead of x.
0123 inline double wtf_fmod(double x, double y)
0124 {
0125     return (!isinf(x) && isinf(y)) ? x : fmod(x, y);
0126 }
0127 
0128 #define fmod(x, y) wtf_fmod(x, y)
0129 
0130 #define atan2(x, y) wtf_atan2(x, y)
0131 #endif // _MSC_VER < 1920
0132 #endif // #if defined(WTF_COMPILER_MSVC)
0133 
0134 inline double deg2rad(double d)
0135 {
0136     return d * piDouble / 180.0;
0137 }
0138 inline double rad2deg(double r)
0139 {
0140     return r * 180.0 / piDouble;
0141 }
0142 inline double deg2grad(double d)
0143 {
0144     return d * 400.0 / 360.0;
0145 }
0146 inline double grad2deg(double g)
0147 {
0148     return g * 360.0 / 400.0;
0149 }
0150 inline double rad2grad(double r)
0151 {
0152     return r * 200.0 / piDouble;
0153 }
0154 inline double grad2rad(double g)
0155 {
0156     return g * piDouble / 200.0;
0157 }
0158 
0159 inline float deg2rad(float d)
0160 {
0161     return d * piFloat / 180.0f;
0162 }
0163 inline float rad2deg(float r)
0164 {
0165     return r * 180.0f / piFloat;
0166 }
0167 inline float deg2grad(float d)
0168 {
0169     return d * 400.0f / 360.0f;
0170 }
0171 inline float grad2deg(float g)
0172 {
0173     return g * 360.0f / 400.0f;
0174 }
0175 inline float rad2grad(float r)
0176 {
0177     return r * 200.0f / piFloat;
0178 }
0179 inline float grad2rad(float g)
0180 {
0181     return g * piFloat / 200.0f;
0182 }
0183 
0184 #endif // #ifndef WTF_MathExtras_h