Home | History | Annotate | Download | only in base
      1 // Copyright 2011 the V8 project authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 // Extra POSIX/ANSI routines for Win32 when using Visual Studio C++. Please
      6 // refer to The Open Group Base Specification for specification of the correct
      7 // semantics for these functions.
      8 // (http://www.opengroup.org/onlinepubs/000095399/)
      9 #if defined(_MSC_VER) && (_MSC_VER < 1800)
     10 
     11 #include "src/base/win32-headers.h"
     12 #include <float.h>         // Required for DBL_MAX and on Win32 for finite()
     13 #include <limits.h>        // Required for INT_MAX etc.
     14 #include <cmath>
     15 #include "src/base/win32-math.h"
     16 
     17 #include "src/base/logging.h"
     18 
     19 
     20 namespace std {
     21 
     22 // Test for a NaN (not a number) value - usually defined in math.h
     23 int isnan(double x) {
     24   return _isnan(x);
     25 }
     26 
     27 
     28 // Test for infinity - usually defined in math.h
     29 int isinf(double x) {
     30   return (_fpclass(x) & (_FPCLASS_PINF | _FPCLASS_NINF)) != 0;
     31 }
     32 
     33 
     34 // Test for finite value - usually defined in math.h
     35 int isfinite(double x) {
     36   return _finite(x);
     37 }
     38 
     39 
     40 // Test if x is less than y and both nominal - usually defined in math.h
     41 int isless(double x, double y) {
     42   return isnan(x) || isnan(y) ? 0 : x < y;
     43 }
     44 
     45 
     46 // Test if x is greater than y and both nominal - usually defined in math.h
     47 int isgreater(double x, double y) {
     48   return isnan(x) || isnan(y) ? 0 : x > y;
     49 }
     50 
     51 
     52 // Classify floating point number - usually defined in math.h
     53 int fpclassify(double x) {
     54   // Use the MS-specific _fpclass() for classification.
     55   int flags = _fpclass(x);
     56 
     57   // Determine class. We cannot use a switch statement because
     58   // the _FPCLASS_ constants are defined as flags.
     59   if (flags & (_FPCLASS_PN | _FPCLASS_NN)) return FP_NORMAL;
     60   if (flags & (_FPCLASS_PZ | _FPCLASS_NZ)) return FP_ZERO;
     61   if (flags & (_FPCLASS_PD | _FPCLASS_ND)) return FP_SUBNORMAL;
     62   if (flags & (_FPCLASS_PINF | _FPCLASS_NINF)) return FP_INFINITE;
     63 
     64   // All cases should be covered by the code above.
     65   DCHECK(flags & (_FPCLASS_SNAN | _FPCLASS_QNAN));
     66   return FP_NAN;
     67 }
     68 
     69 
     70 // Test sign - usually defined in math.h
     71 int signbit(double x) {
     72   // We need to take care of the special case of both positive
     73   // and negative versions of zero.
     74   if (x == 0)
     75     return _fpclass(x) & _FPCLASS_NZ;
     76   else
     77     return x < 0;
     78 }
     79 
     80 }  // namespace std
     81 
     82 #endif  // _MSC_VER
     83