changeset 24866:441b27c0fd5e

use C++11 features to eliminate some macros in oct-rand.cc * oct-rand.cc (MAKE_RAND): Delete macro. (octave_rand::fill): Use std::generate_n, std::fill_n, and C++11 lambda expressions to replace MAKE_RAND and RAND_FUNC macros. * liboctave/external/ranlib/wrap.f: Provide wrappers with REAL args. * lo-ranlib-proto.h: Provide decls for them.
author John W. Eaton <jwe@octave.org>
date Mon, 12 Mar 2018 21:06:13 -0400
parents 5f7b9ee5b878
children 170e8625562a
files liboctave/external/ranlib/wrap.f liboctave/numeric/lo-ranlib-proto.h liboctave/numeric/oct-rand.cc
diffstat 3 files changed, 80 insertions(+), 86 deletions(-) [+]
line wrap: on
line diff
--- a/liboctave/external/ranlib/wrap.f	Sat Mar 10 14:41:34 2018 -0600
+++ b/liboctave/external/ranlib/wrap.f	Mon Mar 12 21:06:13 2018 -0400
@@ -33,3 +33,38 @@
       result = ignpoi (real (mu))
       return
       end
+      subroutine fgennor (av, sd, result)
+      real av, sd, result
+      real gennor
+      external gennor
+      result = gennor (av, sd)
+      return
+      end
+      subroutine fgenunf (low, high, result)
+      real low, high, result
+      real genunf
+      external genunf
+      result = genunf (low, high)
+      return
+      end
+      subroutine fgenexp (av, result)
+      real av, result
+      real genexp
+      external genexp
+      result = genexp (av)
+      return
+      end
+      subroutine fgengam (a, r, result)
+      real a, r, result
+      real gengam
+      external gengam
+      result = gengam (a, r)
+      return
+      end
+      subroutine fignpoi (mu, result)
+      real mu, result
+      integer*4 ignpoi
+      external ignpoi
+      result = ignpoi (mu)
+      return
+      end
--- a/liboctave/numeric/lo-ranlib-proto.h	Sat Mar 10 14:41:34 2018 -0600
+++ b/liboctave/numeric/lo-ranlib-proto.h	Mon Mar 12 21:06:13 2018 -0400
@@ -45,6 +45,21 @@
   F77_FUNC (dignpoi, DIGNPOI) (const F77_DBLE&, F77_DBLE&);
 
   F77_RET_T
+  F77_FUNC (fgenexp, FGENEXP) (const F77_REAL&, F77_REAL&);
+
+  F77_RET_T
+  F77_FUNC (fgengam, FGENGAM) (const F77_REAL&, const F77_REAL&, F77_REAL&);
+
+  F77_RET_T
+  F77_FUNC (fgennor, FGENNOR) (const F77_REAL&, const F77_REAL&, F77_REAL&);
+
+  F77_RET_T
+  F77_FUNC (fgenunf, FGENUNF) (const F77_REAL&, const F77_REAL&, F77_REAL&);
+
+  F77_RET_T
+  F77_FUNC (fignpoi, FIGNPOI) (const F77_REAL&, F77_REAL&);
+
+  F77_RET_T
   F77_FUNC (getsd, GETSD) (F77_INT4&, F77_INT4&);
 
   F77_RET_T
--- a/liboctave/numeric/oct-rand.cc	Sat Mar 10 14:41:34 2018 -0600
+++ b/liboctave/numeric/oct-rand.cc	Mon Mar 12 21:06:13 2018 -0400
@@ -87,6 +87,7 @@
     case octave::mach_info::flt_fmt_ieee_big_endian:
       F77_FUNC (getsd, GETSD) (u.i[1], u.i[0]);
       break;
+
     default:
       F77_FUNC (getsd, GETSD) (u.i[0], u.i[1]);
       break;
@@ -128,6 +129,7 @@
       i1 = force_to_fit_range (u.i[0], 1, 2147483563);
       i0 = force_to_fit_range (u.i[1], 1, 2147483399);
       break;
+
     default:
       i0 = force_to_fit_range (u.i[0], 1, 2147483563);
       i1 = force_to_fit_range (u.i[1], 1, 2147483399);
@@ -391,38 +393,36 @@
 
   if (use_old_generators)
     {
-      double da = a;
-      double dretval = 0.0;
       switch (current_distribution)
         {
         case uniform_dist:
-          F77_FUNC (dgenunf, DGENUNF) (0.0, 1.0, dretval);
+          F77_FUNC (fgenunf, FGENUNF) (0.0f, 1.0f, retval);
           break;
 
         case normal_dist:
-          F77_FUNC (dgennor, DGENNOR) (0.0, 1.0, dretval);
+          F77_FUNC (fgennor, FGENNOR) (0.0f, 1.0f, retval);
           break;
 
         case expon_dist:
-          F77_FUNC (dgenexp, DGENEXP) (1.0, dretval);
+          F77_FUNC (fgenexp, FGENEXP) (1.0f, retval);
           break;
 
         case poisson_dist:
-          if (da < 0.0 || ! octave::math::isfinite (a))
-            dretval = octave::numeric_limits<double>::NaN ();
+          if (a < 0.0f || ! octave::math::isfinite (a))
+            retval = octave::numeric_limits<float>::NaN ();
           else
             {
               // workaround bug in ignpoi, by calling with different Mu
-              F77_FUNC (dignpoi, DIGNPOI) (da + 1, dretval);
-              F77_FUNC (dignpoi, DIGNPOI) (da, dretval);
+              F77_FUNC (fignpoi, FIGNPOI) (a + 1, retval);
+              F77_FUNC (fignpoi, FIGNPOI) (a, retval);
             }
           break;
 
         case gamma_dist:
-          if (da <= 0.0 || ! octave::math::isfinite (a))
-            dretval = octave::numeric_limits<double>::NaN ();
+          if (a <= 0.0f || ! octave::math::isfinite (a))
+            retval = octave::numeric_limits<float>::NaN ();
           else
-            F77_FUNC (dgengam, DGENGAM) (1.0, da, dretval);
+            F77_FUNC (fgengam, FGENGAM) (1.0, a, retval);
           break;
 
         default:
@@ -430,7 +430,6 @@
             ("rand: invalid distribution ID = %d", current_distribution);
           break;
         }
-      retval = dretval;
     }
   else
     {
@@ -651,19 +650,6 @@
     }
 }
 
-#define MAKE_RAND(len)                                          \
-  do                                                            \
-    {                                                           \
-      double val;                                               \
-      for (volatile octave_idx_type i = 0; i < len; i++)        \
-        {                                                       \
-          octave_quit ();                                       \
-          RAND_FUNC (val);                                      \
-          v[i] = val;                                           \
-        }                                                       \
-    }                                                           \
-  while (0)
-
 void
 octave_rand::fill (octave_idx_type len, double *v, double a)
 {
@@ -674,33 +660,21 @@
     {
     case uniform_dist:
       if (use_old_generators)
-        {
-#define RAND_FUNC(x) F77_FUNC (dgenunf, DGENUNF) (0.0, 1.0, x)
-          MAKE_RAND (len);
-#undef RAND_FUNC
-        }
+        std::generate_n (v, len, [](void) { double x; F77_FUNC (dgenunf, DGENUNF) (0.0, 1.0, x); return x; });
       else
         oct_fill_randu (len, v);
       break;
 
     case normal_dist:
       if (use_old_generators)
-        {
-#define RAND_FUNC(x) F77_FUNC (dgennor, DGENNOR) (0.0, 1.0, x)
-          MAKE_RAND (len);
-#undef RAND_FUNC
-        }
+        std::generate_n (v, len, [](void) { double x; F77_FUNC (dgennor, DGENNOR) (0.0, 1.0, x); return x; });
       else
         oct_fill_randn (len, v);
       break;
 
     case expon_dist:
       if (use_old_generators)
-        {
-#define RAND_FUNC(x) F77_FUNC (dgenexp, DGENEXP) (1.0, x)
-          MAKE_RAND (len);
-#undef RAND_FUNC
-        }
+        std::generate_n (v, len, [](void) { double x; F77_FUNC (dgenexp, DGENEXP) (1.0, x); return x; });
       else
         oct_fill_rande (len, v);
       break;
@@ -709,17 +683,13 @@
       if (use_old_generators)
         {
           if (a < 0.0 || ! octave::math::isfinite (a))
-#define RAND_FUNC(x) x = octave::numeric_limits<double>::NaN ();
-            MAKE_RAND (len);
-#undef RAND_FUNC
+            std::fill_n (v, len, octave::numeric_limits<double>::NaN ());
           else
             {
               // workaround bug in ignpoi, by calling with different Mu
               double tmp;
               F77_FUNC (dignpoi, DIGNPOI) (a + 1, tmp);
-#define RAND_FUNC(x) F77_FUNC (dignpoi, DIGNPOI) (a, x)
-              MAKE_RAND (len);
-#undef RAND_FUNC
+              std::generate_n (v, len, [a](void) { double x; F77_FUNC (dignpoi, DIGNPOI) (a, x); return x; });
             }
         }
       else
@@ -730,13 +700,9 @@
       if (use_old_generators)
         {
           if (a <= 0.0 || ! octave::math::isfinite (a))
-#define RAND_FUNC(x) x = octave::numeric_limits<double>::NaN ();
-            MAKE_RAND (len);
-#undef RAND_FUNC
+            std::fill_n (v, len, octave::numeric_limits<double>::NaN ());
           else
-#define RAND_FUNC(x) F77_FUNC (dgengam, DGENGAM) (1.0, a, x)
-            MAKE_RAND (len);
-#undef RAND_FUNC
+            std::generate_n (v, len, [a](void) { double x; F77_FUNC (dgengam, DGENGAM) (1.0, a, x); return x; });
         }
       else
         oct_fill_randg (a, len, v);
@@ -763,33 +729,21 @@
     {
     case uniform_dist:
       if (use_old_generators)
-        {
-#define RAND_FUNC(x) F77_FUNC (dgenunf, DGENUNF) (0.0, 1.0, x)
-          MAKE_RAND (len);
-#undef RAND_FUNC
-        }
+        std::generate_n (v, len, [](void) { float x; F77_FUNC (fgenunf, FGENUNF) (0.0f, 1.0f, x); return x; });
       else
         oct_fill_float_randu (len, v);
       break;
 
     case normal_dist:
       if (use_old_generators)
-        {
-#define RAND_FUNC(x) F77_FUNC (dgennor, DGENNOR) (0.0, 1.0, x)
-          MAKE_RAND (len);
-#undef RAND_FUNC
-        }
+        std::generate_n (v, len, [](void) { float x; F77_FUNC (fgennor, FGENNOR) (0.0f, 1.0f, x); return x; });
       else
         oct_fill_float_randn (len, v);
       break;
 
     case expon_dist:
       if (use_old_generators)
-        {
-#define RAND_FUNC(x) F77_FUNC (dgenexp, DGENEXP) (1.0, x)
-          MAKE_RAND (len);
-#undef RAND_FUNC
-        }
+        std::generate_n (v, len, [](void) { float x; F77_FUNC (fgenexp, FGENEXP) (1.0f, x); return x; });
       else
         oct_fill_float_rande (len, v);
       break;
@@ -797,19 +751,14 @@
     case poisson_dist:
       if (use_old_generators)
         {
-          double da = a;
-          if (da < 0.0 || ! octave::math::isfinite (a))
-#define RAND_FUNC(x) x = octave::numeric_limits<double>::NaN ();
-            MAKE_RAND (len);
-#undef RAND_FUNC
+          if (a < 0.0f || ! octave::math::isfinite (a))
+            std::fill_n (v, len, octave::numeric_limits<float>::NaN ());
           else
             {
               // workaround bug in ignpoi, by calling with different Mu
-              double tmp;
-              F77_FUNC (dignpoi, DIGNPOI) (da + 1, tmp);
-#define RAND_FUNC(x) F77_FUNC (dignpoi, DIGNPOI) (da, x)
-              MAKE_RAND (len);
-#undef RAND_FUNC
+              float tmp;
+              F77_FUNC (fignpoi, FIGNPOI) (a + 1, tmp);
+              std::generate_n (v, len, [a](void) { float x; F77_FUNC (fignpoi, FIGNPOI) (a, x); return x; });
             }
         }
       else
@@ -819,15 +768,10 @@
     case gamma_dist:
       if (use_old_generators)
         {
-          double da = a;
-          if (da <= 0.0 || ! octave::math::isfinite (a))
-#define RAND_FUNC(x) x = octave::numeric_limits<double>::NaN ();
-            MAKE_RAND (len);
-#undef RAND_FUNC
+          if (a <= 0.0f || ! octave::math::isfinite (a))
+            std::fill_n (v, len, octave::numeric_limits<float>::NaN ());
           else
-#define RAND_FUNC(x) F77_FUNC (dgengam, DGENGAM) (1.0, da, x)
-            MAKE_RAND (len);
-#undef RAND_FUNC
+            std::generate_n (v, len, [a](void) { float x; F77_FUNC (fgengam, FGENGAM) (1.0f, a, x); return x; });
         }
       else
         oct_fill_float_randg (a, len, v);