Emulate 'log1p()' and 'expm1()' using 'log()' and 'exp()'
authorJohn Emmas <johne53@tiscali.co.uk>
Sat, 28 May 2016 18:07:22 +0000 (19:07 +0100)
committerJohn Emmas <johne53@tiscali.co.uk>
Sat, 28 May 2016 18:07:22 +0000 (19:07 +0100)
(for MSVC which didn't implement those functions until very recent versions).

libs/pbd/msvc/msvc_pbd.cc
libs/pbd/pbd/msvc_pbd.h
msvc_extra_headers/ardourext/misc.h.input

index ab79d8f45de8950213ef467112d5e8879c34a600..f49ac5393c85743306c0cf644ee1bcde43166a34 100644 (file)
@@ -262,6 +262,47 @@ trunc(double x)
        return (floor(x));
 }
 
+#if defined(_MSC_VER) && (_MSC_VER < 1800)
+//***************************************************************
+//
+//     expm1()
+//
+// Emulates C99 expm1() using exp().
+//
+//     Returns:
+//
+//    On Success: (('e' raised to the power of 'x') - 1)
+//                (e.g. expm1(1) == 1.7182818).
+//    On Failure: None, except that calling exp(x) should generate
+//                an appropriate error for us (such as INF etc).
+//
+LIBPBD_API double PBD_APICALLTYPE
+expm1(double x)
+{
+       return (exp(x) - (double)1.0);
+}
+
+//***************************************************************
+//
+//     log1p()
+//
+// Emulates C99 log1p() using log().
+//
+//     Returns:
+//
+//    On Success: The natural logarithm of (1 + x)
+//                (e.g. log1p(1) == 0.69314718).
+//    On Failure: None, except that calling log(x) should generate
+//                an appropriate error for us (such as ERANGE etc).
+//
+LIBPBD_API double PBD_APICALLTYPE
+log1p(double x)
+{
+       return (log(x + (double)1.0));
+}
+#endif
+
+#if defined(_MSC_VER) && (_MSC_VER < 1900)
 //***************************************************************
 //
 //     log2()
@@ -280,6 +321,7 @@ log2(double x)
 {
        return (log(x) / log((double)2.0));
 }
+#endif
 
 namespace PBD {
 
index 7529619f1d984843eebadef3709a97c9cda02dff..5a6b5502427c12030adc460a43c83eceeb3be5b4 100644 (file)
@@ -232,7 +232,15 @@ LIBPBD_API ssize_t                 PBD_APICALLTYPE pwrite(int handle, const void *buf, size_t
 LIBPBD_API int                         PBD_APICALLTYPE poll(struct pollfd *fds, nfds_t nfds, int timeout);
 LIBPBD_API double                      PBD_APICALLTYPE round(double x);
 LIBPBD_API double                      PBD_APICALLTYPE trunc(double x);
-LIBPBD_API double                      PBD_APICALLTYPE log2(double x);
+
+#if defined(_MSC_VER) && (_MSC_VER < 1800)
+LIBPBD_API double                      PBD_APICALLTYPE expm1(double x);
+LIBPBD_API double                      PBD_APICALLTYPE log1p(double x);
+#endif
+
+#if defined(_MSC_VER) && (_MSC_VER < 1900)
+LIBPBD_API double                      PBD_APICALLTYPE log2 (double x);
+#endif
 
 namespace PBD {
 
index 161ed167f171ffbe63f021be022771dca79e3f6d..11ba6384943216c81f35dd5251d2a76f81667f24 100644 (file)
@@ -254,9 +254,15 @@ inline int64_t abs(int64_t val) throw()
 #if !defined(LIBPBD_API) || defined(PBD_IS_IN_WIN_STATIC_LIB)
 extern  double round(double x);
 
-// log2().... MSVC doesn't offer the C99 function 'log2()'
-// so let's emulate it.
-extern  double log2(double x);
+// Emulate some C99 math functions which MSVC itself didn't
+// implement until later in life.
+#if defined(_MSC_VER) && (_MSC_VER < 1800)
+extern  double expm1(double x);
+extern  double log1p(double x);
+#endif
+#if defined(_MSC_VER) && (_MSC_VER < 1900)
+extern  double log2 (double x);
+#endif
 #endif
 
 #endif /* __ardour_msvc_extensions_h__ */