]> git.ipfire.org Git - thirdparty/gcc.git/blobdiff - libstdc++-v3/include/bits/random.tcc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / random.tcc
index f2edc3e66eed33257719f55add91b33dff83d130..f092b5cd4cbcd29c360c6c10c0284a63b94e9fb3 100644 (file)
@@ -1,6 +1,6 @@
 // random number generation (out of line) -*- C++ -*-
 
-// Copyright (C) 2009-2012 Free Software Foundation, Inc.
+// Copyright (C) 2009-2023 Free Software Foundation, Inc.
 //
 // This file is part of the GNU ISO C++ Library.  This library is free
 // software; you can redistribute it and/or modify it under the
 
 namespace std _GLIBCXX_VISIBILITY(default)
 {
-  /*
-   * (Further) implementation-space details.
-   */
+_GLIBCXX_BEGIN_NAMESPACE_VERSION
+
+  /// @cond undocumented
+  // (Further) implementation-space details.
   namespace __detail
   {
-  _GLIBCXX_BEGIN_NAMESPACE_VERSION
-
     // General case for x = (ax + c) mod m -- use Schrage's algorithm
     // to avoid integer overflow.
     //
@@ -79,21 +78,20 @@ namespace std _GLIBCXX_VISIBILITY(default)
       }
 
     template<typename _InputIterator, typename _OutputIterator,
-            typename _UnaryOperation>
+            typename _Tp>
       _OutputIterator
-      __transform(_InputIterator __first, _InputIterator __last,
-                 _OutputIterator __result, _UnaryOperation __unary_op)
+      __normalize(_InputIterator __first, _InputIterator __last,
+                 _OutputIterator __result, const _Tp& __factor)
       {
        for (; __first != __last; ++__first, ++__result)
-         *__result = __unary_op(*__first);
+         *__result = *__first / __factor;
        return __result;
       }
 
-  _GLIBCXX_END_NAMESPACE_VERSION
   } // namespace __detail
+  /// @endcond
 
-_GLIBCXX_BEGIN_NAMESPACE_VERSION
-
+#if ! __cpp_inline_variables
   template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
     constexpr _UIntType
     linear_congruential_engine<_UIntType, __a, __c, __m>::multiplier;
@@ -109,6 +107,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
   template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
     constexpr _UIntType
     linear_congruential_engine<_UIntType, __a, __c, __m>::default_seed;
+#endif
 
   /**
    * Seeds the LCR with integral value @p __s, adjusted so that the
@@ -131,9 +130,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
    */
   template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
     template<typename _Sseq>
-      typename std::enable_if<std::is_class<_Sseq>::value>::type
+      auto
       linear_congruential_engine<_UIntType, __a, __c, __m>::
       seed(_Sseq& __q)
+      -> _If_seed_seq<_Sseq>
       {
        const _UIntType __k0 = __m == 0 ? std::numeric_limits<_UIntType>::digits
                                        : std::__lg(__m);
@@ -157,8 +157,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
               const linear_congruential_engine<_UIntType,
                                                __a, __c, __m>& __lcr)
     {
-      typedef std::basic_ostream<_CharT, _Traits>  __ostream_type;
-      typedef typename __ostream_type::ios_base    __ios_base;
+      using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
 
       const typename __ios_base::fmtflags __flags = __os.flags();
       const _CharT __fill = __os.fill();
@@ -178,8 +177,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     operator>>(std::basic_istream<_CharT, _Traits>& __is,
               linear_congruential_engine<_UIntType, __a, __c, __m>& __lcr)
     {
-      typedef std::basic_istream<_CharT, _Traits>  __istream_type;
-      typedef typename __istream_type::ios_base    __ios_base;
+      using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
 
       const typename __ios_base::fmtflags __flags = __is.flags();
       __is.flags(__ios_base::dec);
@@ -190,7 +188,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       return __is;
     }
 
-
+#if ! __cpp_inline_variables
   template<typename _UIntType,
           size_t __w, size_t __n, size_t __m, size_t __r,
           _UIntType __a, size_t __u, _UIntType __d, size_t __s,
@@ -317,6 +315,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     constexpr _UIntType
     mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
                            __s, __b, __t, __c, __l, __f>::default_seed;
+#endif
 
   template<typename _UIntType,
           size_t __w, size_t __n, size_t __m, size_t __r,
@@ -349,10 +348,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
           _UIntType __b, size_t __t, _UIntType __c, size_t __l,
           _UIntType __f>
     template<typename _Sseq>
-      typename std::enable_if<std::is_class<_Sseq>::value>::type
+      auto
       mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d,
                              __s, __b, __t, __c, __l, __f>::
       seed(_Sseq& __q)
+      -> _If_seed_seq<_Sseq>
       {
        const _UIntType __upper_mask = (~_UIntType()) << __r;
        const size_t __k = (__w + 31) / 32;
@@ -385,6 +385,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
          }
         if (__zero)
           _M_x[0] = __detail::_Shift<_UIntType, __w - 1>::__value;
+       _M_p = state_size;
       }
 
   template<typename _UIntType, size_t __w,
@@ -477,8 +478,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
               const mersenne_twister_engine<_UIntType, __w, __n, __m,
               __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>& __x)
     {
-      typedef std::basic_ostream<_CharT, _Traits>  __ostream_type;
-      typedef typename __ostream_type::ios_base    __ios_base;
+      using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
 
       const typename __ios_base::fmtflags __flags = __os.flags();
       const _CharT __fill = __os.fill();
@@ -505,8 +505,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
               mersenne_twister_engine<_UIntType, __w, __n, __m,
               __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>& __x)
     {
-      typedef std::basic_istream<_CharT, _Traits>  __istream_type;
-      typedef typename __istream_type::ios_base    __ios_base;
+      using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
 
       const typename __ios_base::fmtflags __flags = __is.flags();
       __is.flags(__ios_base::dec | __ios_base::skipws);
@@ -519,7 +518,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       return __is;
     }
 
-
+#if ! __cpp_inline_variables
   template<typename _UIntType, size_t __w, size_t __s, size_t __r>
     constexpr size_t
     subtract_with_carry_engine<_UIntType, __w, __s, __r>::word_size;
@@ -533,15 +532,16 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     subtract_with_carry_engine<_UIntType, __w, __s, __r>::long_lag;
 
   template<typename _UIntType, size_t __w, size_t __s, size_t __r>
-    constexpr _UIntType
+    constexpr uint_least32_t
     subtract_with_carry_engine<_UIntType, __w, __s, __r>::default_seed;
+#endif
 
   template<typename _UIntType, size_t __w, size_t __s, size_t __r>
     void
     subtract_with_carry_engine<_UIntType, __w, __s, __r>::
     seed(result_type __value)
     {
-      std::linear_congruential_engine<result_type, 40014u, 0u, 2147483563u>
+      std::linear_congruential_engine<uint_least32_t, 40014u, 0u, 2147483563u>
        __lcg(__value == 0u ? default_seed : __value);
 
       const size_t __n = (__w + 31) / 32;
@@ -566,9 +566,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
   template<typename _UIntType, size_t __w, size_t __s, size_t __r>
     template<typename _Sseq>
-      typename std::enable_if<std::is_class<_Sseq>::value>::type
+      auto
       subtract_with_carry_engine<_UIntType, __w, __s, __r>::
       seed(_Sseq& __q)
+      -> _If_seed_seq<_Sseq>
       {
        const size_t __k = (__w + 31) / 32;
        uint_least32_t __arr[__r * __k];
@@ -632,8 +633,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
               const subtract_with_carry_engine<_UIntType,
                                                __w, __s, __r>& __x)
     {
-      typedef std::basic_ostream<_CharT, _Traits>  __ostream_type;
-      typedef typename __ostream_type::ios_base    __ios_base;
+      using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
 
       const typename __ios_base::fmtflags __flags = __os.flags();
       const _CharT __fill = __os.fill();
@@ -656,8 +656,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     operator>>(std::basic_istream<_CharT, _Traits>& __is,
               subtract_with_carry_engine<_UIntType, __w, __s, __r>& __x)
     {
-      typedef std::basic_ostream<_CharT, _Traits>  __istream_type;
-      typedef typename __istream_type::ios_base    __ios_base;
+      using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
 
       const typename __ios_base::fmtflags __flags = __is.flags();
       __is.flags(__ios_base::dec | __ios_base::skipws);
@@ -671,7 +670,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       return __is;
     }
 
-
+#if ! __cpp_inline_variables
   template<typename _RandomNumberEngine, size_t __p, size_t __r>
     constexpr size_t
     discard_block_engine<_RandomNumberEngine, __p, __r>::block_size;
@@ -679,6 +678,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
   template<typename _RandomNumberEngine, size_t __p, size_t __r>
     constexpr size_t
     discard_block_engine<_RandomNumberEngine, __p, __r>::used_block;
+#endif
 
   template<typename _RandomNumberEngine, size_t __p, size_t __r>
     typename discard_block_engine<_RandomNumberEngine,
@@ -702,8 +702,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
               const discard_block_engine<_RandomNumberEngine,
               __p, __r>& __x)
     {
-      typedef std::basic_ostream<_CharT, _Traits>  __ostream_type;
-      typedef typename __ostream_type::ios_base    __ios_base;
+      using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
 
       const typename __ios_base::fmtflags __flags = __os.flags();
       const _CharT __fill = __os.fill();
@@ -724,8 +723,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     operator>>(std::basic_istream<_CharT, _Traits>& __is,
               discard_block_engine<_RandomNumberEngine, __p, __r>& __x)
     {
-      typedef std::basic_istream<_CharT, _Traits>  __istream_type;
-      typedef typename __istream_type::ios_base    __ios_base;
+      using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
 
       const typename __ios_base::fmtflags __flags = __is.flags();
       __is.flags(__ios_base::dec | __ios_base::skipws);
@@ -806,18 +804,53 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       return __sum;
     }
 
-
+#if ! __cpp_inline_variables
   template<typename _RandomNumberEngine, size_t __k>
     constexpr size_t
     shuffle_order_engine<_RandomNumberEngine, __k>::table_size;
+#endif
+
+  namespace __detail
+  {
+    // Determine whether an integer is representable as double.
+    template<typename _Tp>
+      constexpr bool
+      __representable_as_double(_Tp __x) noexcept
+      {
+       static_assert(numeric_limits<_Tp>::is_integer, "");
+       static_assert(!numeric_limits<_Tp>::is_signed, "");
+       // All integers <= 2^53 are representable.
+       return (__x <= (1ull << __DBL_MANT_DIG__))
+         // Between 2^53 and 2^54 only even numbers are representable.
+         || (!(__x & 1) && __detail::__representable_as_double(__x >> 1));
+      }
+
+    // Determine whether x+1 is representable as double.
+    template<typename _Tp>
+      constexpr bool
+      __p1_representable_as_double(_Tp __x) noexcept
+      {
+       static_assert(numeric_limits<_Tp>::is_integer, "");
+       static_assert(!numeric_limits<_Tp>::is_signed, "");
+       return numeric_limits<_Tp>::digits < __DBL_MANT_DIG__
+         || (bool(__x + 1u) // return false if x+1 wraps around to zero
+             && __detail::__representable_as_double(__x + 1u));
+      }
+  }
 
   template<typename _RandomNumberEngine, size_t __k>
     typename shuffle_order_engine<_RandomNumberEngine, __k>::result_type
     shuffle_order_engine<_RandomNumberEngine, __k>::
     operator()()
     {
-      size_t __j = __k * ((_M_y - _M_b.min())
-                         / (_M_b.max() - _M_b.min() + 1.0L));
+      constexpr result_type __range = max() - min();
+      size_t __j = __k;
+      const result_type __y = _M_y - min();
+      // Avoid using slower long double arithmetic if possible.
+      if _GLIBCXX17_CONSTEXPR (__detail::__p1_representable_as_double(__range))
+       __j *= __y / (__range + 1.0);
+      else
+       __j *= __y / (__range + 1.0L);
       _M_y = _M_v[__j];
       _M_v[__j] = _M_b();
 
@@ -830,8 +863,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     operator<<(std::basic_ostream<_CharT, _Traits>& __os,
               const shuffle_order_engine<_RandomNumberEngine, __k>& __x)
     {
-      typedef std::basic_ostream<_CharT, _Traits>  __ostream_type;
-      typedef typename __ostream_type::ios_base    __ios_base;
+      using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
 
       const typename __ios_base::fmtflags __flags = __os.flags();
       const _CharT __fill = __os.fill();
@@ -855,8 +887,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     operator>>(std::basic_istream<_CharT, _Traits>& __is,
               shuffle_order_engine<_RandomNumberEngine, __k>& __x)
     {
-      typedef std::basic_istream<_CharT, _Traits>  __istream_type;
-      typedef typename __istream_type::ios_base    __ios_base;
+      using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
 
       const typename __ios_base::fmtflags __flags = __is.flags();
       __is.flags(__ios_base::dec | __ios_base::skipws);
@@ -871,165 +902,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     }
 
 
-  template<typename _IntType>
-    template<typename _UniformRandomNumberGenerator>
-      typename uniform_int_distribution<_IntType>::result_type
-      uniform_int_distribution<_IntType>::
-      operator()(_UniformRandomNumberGenerator& __urng,
-                const param_type& __param)
-      {
-       typedef typename _UniformRandomNumberGenerator::result_type
-         _Gresult_type;
-       typedef typename std::make_unsigned<result_type>::type __utype;
-       typedef typename std::common_type<_Gresult_type, __utype>::type
-         __uctype;
-
-       const __uctype __urngmin = __urng.min();
-       const __uctype __urngmax = __urng.max();
-       const __uctype __urngrange = __urngmax - __urngmin;
-       const __uctype __urange
-         = __uctype(__param.b()) - __uctype(__param.a());
-
-       __uctype __ret;
-
-       if (__urngrange > __urange)
-         {
-           // downscaling
-           const __uctype __uerange = __urange + 1; // __urange can be zero
-           const __uctype __scaling = __urngrange / __uerange;
-           const __uctype __past = __uerange * __scaling;
-           do
-             __ret = __uctype(__urng()) - __urngmin;
-           while (__ret >= __past);
-           __ret /= __scaling;
-         }
-       else if (__urngrange < __urange)
-         {
-           // upscaling
-           /*
-             Note that every value in [0, urange]
-             can be written uniquely as
-
-             (urngrange + 1) * high + low
-
-             where
-
-             high in [0, urange / (urngrange + 1)]
-
-             and
-       
-             low in [0, urngrange].
-           */
-           __uctype __tmp; // wraparound control
-           do
-             {
-               const __uctype __uerngrange = __urngrange + 1;
-               __tmp = (__uerngrange * operator()
-                        (__urng, param_type(0, __urange / __uerngrange)));
-               __ret = __tmp + (__uctype(__urng()) - __urngmin);
-             }
-           while (__ret > __urange || __ret < __tmp);
-         }
-       else
-         __ret = __uctype(__urng()) - __urngmin;
-
-       return __ret + __param.a();
-      }
-
-
-  template<typename _IntType>
-    template<typename _ForwardIterator,
-            typename _UniformRandomNumberGenerator>
-      void
-      uniform_int_distribution<_IntType>::
-      __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
-                     _UniformRandomNumberGenerator& __urng,
-                     const param_type& __param)
-      {
-       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
-       typedef typename _UniformRandomNumberGenerator::result_type
-         _Gresult_type;
-       typedef typename std::make_unsigned<result_type>::type __utype;
-       typedef typename std::common_type<_Gresult_type, __utype>::type
-         __uctype;
-
-       const __uctype __urngmin = __urng.min();
-       const __uctype __urngmax = __urng.max();
-       const __uctype __urngrange = __urngmax - __urngmin;
-       const __uctype __urange
-         = __uctype(__param.b()) - __uctype(__param.a());
-
-       __uctype __ret;
-
-       if (__urngrange > __urange)
-         {
-           if (__detail::_Power_of_2(__urngrange + 1)
-               && __detail::_Power_of_2(__urange + 1))
-             {
-               while (__f != __t)
-                 {
-                   __ret = __uctype(__urng()) - __urngmin;
-                   *__f++ = (__ret & __urange) + __param.a();
-                 }
-             }
-           else
-             {
-               // downscaling
-               const __uctype __uerange = __urange + 1; // __urange can be zero
-               const __uctype __scaling = __urngrange / __uerange;
-               const __uctype __past = __uerange * __scaling;
-               while (__f != __t)
-                 {
-                   do
-                     __ret = __uctype(__urng()) - __urngmin;
-                   while (__ret >= __past);
-                   *__f++ = __ret / __scaling + __param.a();
-                 }
-             }
-         }
-       else if (__urngrange < __urange)
-         {
-           // upscaling
-           /*
-             Note that every value in [0, urange]
-             can be written uniquely as
-
-             (urngrange + 1) * high + low
-
-             where
-
-             high in [0, urange / (urngrange + 1)]
-
-             and
-
-             low in [0, urngrange].
-           */
-           __uctype __tmp; // wraparound control
-           while (__f != __t)
-             {
-               do
-                 {
-                   const __uctype __uerngrange = __urngrange + 1;
-                   __tmp = (__uerngrange * operator()
-                            (__urng, param_type(0, __urange / __uerngrange)));
-                   __ret = __tmp + (__uctype(__urng()) - __urngmin);
-                 }
-               while (__ret > __urange || __ret < __tmp);
-               *__f++ = __ret;
-             }
-         }
-       else
-         while (__f != __t)
-           *__f++ = __uctype(__urng()) - __urngmin + __param.a();
-      }
-
   template<typename _IntType, typename _CharT, typename _Traits>
     std::basic_ostream<_CharT, _Traits>&
     operator<<(std::basic_ostream<_CharT, _Traits>& __os,
               const uniform_int_distribution<_IntType>& __x)
     {
-      typedef std::basic_ostream<_CharT, _Traits>  __ostream_type;
-      typedef typename __ostream_type::ios_base    __ios_base;
+      using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
 
       const typename __ios_base::fmtflags __flags = __os.flags();
       const _CharT __fill = __os.fill();
@@ -1049,16 +927,16 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     operator>>(std::basic_istream<_CharT, _Traits>& __is,
               uniform_int_distribution<_IntType>& __x)
     {
-      typedef std::basic_istream<_CharT, _Traits>  __istream_type;
-      typedef typename __istream_type::ios_base    __ios_base;
+      using param_type
+       = typename uniform_int_distribution<_IntType>::param_type;
+      using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
 
       const typename __ios_base::fmtflags __flags = __is.flags();
       __is.flags(__ios_base::dec | __ios_base::skipws);
 
       _IntType __a, __b;
-      __is >> __a >> __b;
-      __x.param(typename uniform_int_distribution<_IntType>::
-               param_type(__a, __b));
+      if (__is >> __a >> __b)
+       __x.param(param_type(__a, __b));
 
       __is.flags(__flags);
       return __is;
@@ -1087,8 +965,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     operator<<(std::basic_ostream<_CharT, _Traits>& __os,
               const uniform_real_distribution<_RealType>& __x)
     {
-      typedef std::basic_ostream<_CharT, _Traits>  __ostream_type;
-      typedef typename __ostream_type::ios_base    __ios_base;
+      using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
 
       const typename __ios_base::fmtflags __flags = __os.flags();
       const _CharT __fill = __os.fill();
@@ -1111,16 +988,16 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     operator>>(std::basic_istream<_CharT, _Traits>& __is,
               uniform_real_distribution<_RealType>& __x)
     {
-      typedef std::basic_istream<_CharT, _Traits>  __istream_type;
-      typedef typename __istream_type::ios_base    __ios_base;
+      using param_type
+       = typename uniform_real_distribution<_RealType>::param_type;
+      using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
 
       const typename __ios_base::fmtflags __flags = __is.flags();
       __is.flags(__ios_base::skipws);
 
       _RealType __a, __b;
-      __is >> __a >> __b;
-      __x.param(typename uniform_real_distribution<_RealType>::
-               param_type(__a, __b));
+      if (__is >> __a >> __b)
+       __x.param(param_type(__a, __b));
 
       __is.flags(__flags);
       return __is;
@@ -1149,8 +1026,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     operator<<(std::basic_ostream<_CharT, _Traits>& __os,
               const bernoulli_distribution& __x)
     {
-      typedef std::basic_ostream<_CharT, _Traits>  __ostream_type;
-      typedef typename __ostream_type::ios_base    __ios_base;
+      using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
 
       const typename __ios_base::fmtflags __flags = __os.flags();
       const _CharT __fill = __os.fill();
@@ -1231,8 +1107,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     operator<<(std::basic_ostream<_CharT, _Traits>& __os,
               const geometric_distribution<_IntType>& __x)
     {
-      typedef std::basic_ostream<_CharT, _Traits>  __ostream_type;
-      typedef typename __ostream_type::ios_base    __ios_base;
+      using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
 
       const typename __ios_base::fmtflags __flags = __os.flags();
       const _CharT __fill = __os.fill();
@@ -1255,15 +1130,15 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     operator>>(std::basic_istream<_CharT, _Traits>& __is,
               geometric_distribution<_IntType>& __x)
     {
-      typedef std::basic_istream<_CharT, _Traits>  __istream_type;
-      typedef typename __istream_type::ios_base    __ios_base;
+      using param_type = typename geometric_distribution<_IntType>::param_type;
+      using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
 
       const typename __ios_base::fmtflags __flags = __is.flags();
       __is.flags(__ios_base::skipws);
 
       double __p;
-      __is >> __p;
-      __x.param(typename geometric_distribution<_IntType>::param_type(__p));
+      if (__is >> __p)
+       __x.param(param_type(__p));
 
       __is.flags(__flags);
       return __is;
@@ -1290,7 +1165,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       operator()(_UniformRandomNumberGenerator& __urng,
                 const param_type& __p)
       {
-       typedef typename std::gamma_distribution<result_type>::param_type
+       typedef typename std::gamma_distribution<double>::param_type
          param_type;
        
        const double __y =
@@ -1346,8 +1221,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     operator<<(std::basic_ostream<_CharT, _Traits>& __os,
               const negative_binomial_distribution<_IntType>& __x)
     {
-      typedef std::basic_ostream<_CharT, _Traits>  __ostream_type;
-      typedef typename __ostream_type::ios_base    __ios_base;
+      using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
 
       const typename __ios_base::fmtflags __flags = __os.flags();
       const _CharT __fill = __os.fill();
@@ -1371,17 +1245,17 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     operator>>(std::basic_istream<_CharT, _Traits>& __is,
               negative_binomial_distribution<_IntType>& __x)
     {
-      typedef std::basic_istream<_CharT, _Traits>  __istream_type;
-      typedef typename __istream_type::ios_base    __ios_base;
+      using param_type
+       = typename negative_binomial_distribution<_IntType>::param_type;
+      using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
 
       const typename __ios_base::fmtflags __flags = __is.flags();
       __is.flags(__ios_base::skipws);
 
       _IntType __k;
       double __p;
-      __is >> __k >> __p >> __x._M_gd;
-      __x.param(typename negative_binomial_distribution<_IntType>::
-               param_type(__k, __p));
+      if (__is >> __k >> __p >> __x._M_gd)
+       __x.param(param_type(__k, __p));
 
       __is.flags(__flags);
       return __is;
@@ -1404,7 +1278,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
          const double __pi_4 = 0.7853981633974483096156608458198757L;
          const double __dx = std::sqrt(2 * __m * std::log(32 * __m
                                                              / __pi_4));
-         _M_d = std::round(std::max(6.0, std::min(__m, __dx)));
+         _M_d = std::round(std::max<double>(6.0, std::min(__m, __dx)));
          const double __cx = 2 * __m + _M_d;
          _M_scx = std::sqrt(__cx / 2);
          _M_1cx = 1 / __cx;
@@ -1455,6 +1329,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
            const double __c2 = __param._M_c2b + __c1;
            const double __c3 = __c2 + 1;
            const double __c4 = __c3 + 1;
+           // 1 / 78
+           const double __178 = 0.0128205128205128205128205128205128L;
            // e^(1 / 78)
            const double __e178 = 1.0129030479320018583185514777512983L;
            const double __c5 = __c4 + __e178;
@@ -1494,7 +1370,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
                else if (__u <= __c4)
                  __x = 0;
                else if (__u <= __c5)
-                 __x = 1;
+                 {
+                   __x = 1;
+                   // Only in the Errata, see libstdc++/83237.
+                   __w = __178;
+                 }
                else
                  {
                    const double __v = -std::log(1.0 - __aurng());
@@ -1551,8 +1431,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     operator<<(std::basic_ostream<_CharT, _Traits>& __os,
               const poisson_distribution<_IntType>& __x)
     {
-      typedef std::basic_ostream<_CharT, _Traits>  __ostream_type;
-      typedef typename __ostream_type::ios_base    __ios_base;
+      using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
 
       const typename __ios_base::fmtflags __flags = __os.flags();
       const _CharT __fill = __os.fill();
@@ -1576,15 +1455,15 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     operator>>(std::basic_istream<_CharT, _Traits>& __is,
               poisson_distribution<_IntType>& __x)
     {
-      typedef std::basic_istream<_CharT, _Traits>  __istream_type;
-      typedef typename __istream_type::ios_base    __ios_base;
+      using param_type = typename poisson_distribution<_IntType>::param_type;
+      using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
 
       const typename __ios_base::fmtflags __flags = __is.flags();
       __is.flags(__ios_base::skipws);
 
       double __mean;
-      __is >> __mean >> __x._M_nd;
-      __x.param(typename poisson_distribution<_IntType>::param_type(__mean));
+      if (__is >> __mean >> __x._M_nd)
+       __x.param(param_type(__mean));
 
       __is.flags(__flags);
       return __is;
@@ -1612,11 +1491,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
          const double __d1x =
            std::sqrt(__np * __1p * std::log(32 * __np
                                             / (81 * __pi_4 * __1p)));
-         _M_d1 = std::round(std::max(1.0, __d1x));
+         _M_d1 = std::round(std::max<double>(1.0, __d1x));
          const double __d2x =
            std::sqrt(__np * __1p * std::log(32 * _M_t * __1p
                                             / (__pi_4 * __pa)));
-         _M_d2 = std::round(std::max(1.0, __d2x));
+         _M_d2 = std::round(std::max<double>(1.0, __d2x));
 
          // sqrt(pi / 2)
          const double __spi_2 = 1.2533141373155002512078826424055226L;
@@ -1647,7 +1526,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     template<typename _UniformRandomNumberGenerator>
       typename binomial_distribution<_IntType>::result_type
       binomial_distribution<_IntType>::
-      _M_waiting(_UniformRandomNumberGenerator& __urng, _IntType __t)
+      _M_waiting(_UniformRandomNumberGenerator& __urng,
+                _IntType __t, double __q)
       {
        _IntType __x = 0;
        double __sum = 0.0;
@@ -1656,11 +1536,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
        do
          {
+           if (__t == __x)
+             return __x;
            const double __e = -std::log(1.0 - __aurng());
            __sum += __e / (__t - __x);
            __x += 1;
          }
-       while (__sum <= _M_param._M_q);
+       while (__sum <= __q);
 
        return __x - 1;
       }
@@ -1781,12 +1663,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
            __x += __np + __naf;
 
-           const _IntType __z = _M_waiting(__urng, __t - _IntType(__x));
+           const _IntType __z = _M_waiting(__urng, __t - _IntType(__x),
+                                           __param._M_q);
            __ret = _IntType(__x) + __z;
          }
        else
 #endif
-         __ret = _M_waiting(__urng, __t);
+         __ret = _M_waiting(__urng, __t, __param._M_q);
 
        if (__p12 != __p)
          __ret = __t - __ret;
@@ -1814,8 +1697,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     operator<<(std::basic_ostream<_CharT, _Traits>& __os,
               const binomial_distribution<_IntType>& __x)
     {
-      typedef std::basic_ostream<_CharT, _Traits>  __ostream_type;
-      typedef typename __ostream_type::ios_base    __ios_base;
+      using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
 
       const typename __ios_base::fmtflags __flags = __os.flags();
       const _CharT __fill = __os.fill();
@@ -1840,17 +1722,16 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     operator>>(std::basic_istream<_CharT, _Traits>& __is,
               binomial_distribution<_IntType>& __x)
     {
-      typedef std::basic_istream<_CharT, _Traits>  __istream_type;
-      typedef typename __istream_type::ios_base    __ios_base;
+      using param_type = typename binomial_distribution<_IntType>::param_type;
+      using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
 
       const typename __ios_base::fmtflags __flags = __is.flags();
       __is.flags(__ios_base::dec | __ios_base::skipws);
 
       _IntType __t;
       double __p;
-      __is >> __t >> __p >> __x._M_nd;
-      __x.param(typename binomial_distribution<_IntType>::
-               param_type(__t, __p));
+      if (__is >> __t >> __p >> __x._M_nd)
+       __x.param(param_type(__t, __p));
 
       __is.flags(__flags);
       return __is;
@@ -1878,8 +1759,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     operator<<(std::basic_ostream<_CharT, _Traits>& __os,
               const exponential_distribution<_RealType>& __x)
     {
-      typedef std::basic_ostream<_CharT, _Traits>  __ostream_type;
-      typedef typename __ostream_type::ios_base    __ios_base;
+      using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
 
       const typename __ios_base::fmtflags __flags = __os.flags();
       const _CharT __fill = __os.fill();
@@ -1901,16 +1781,16 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     operator>>(std::basic_istream<_CharT, _Traits>& __is,
               exponential_distribution<_RealType>& __x)
     {
-      typedef std::basic_istream<_CharT, _Traits>  __istream_type;
-      typedef typename __istream_type::ios_base    __ios_base;
+      using param_type
+       = typename exponential_distribution<_RealType>::param_type;
+      using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
 
       const typename __ios_base::fmtflags __flags = __is.flags();
       __is.flags(__ios_base::dec | __ios_base::skipws);
 
       _RealType __lambda;
-      __is >> __lambda;
-      __x.param(typename exponential_distribution<_RealType>::
-               param_type(__lambda));
+      if (__is >> __lambda)
+       __x.param(param_type(__lambda));
 
       __is.flags(__flags);
       return __is;
@@ -2027,15 +1907,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     {
       if (__d1._M_param == __d2._M_param
          && __d1._M_saved_available == __d2._M_saved_available)
-       {
-         if (__d1._M_saved_available
-             && __d1._M_saved == __d2._M_saved)
-           return true;
-         else if(!__d1._M_saved_available)
-           return true;
-         else
-           return false;
-       }
+       return __d1._M_saved_available ? __d1._M_saved == __d2._M_saved : true;
       else
        return false;
     }
@@ -2045,8 +1917,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     operator<<(std::basic_ostream<_CharT, _Traits>& __os,
               const normal_distribution<_RealType>& __x)
     {
-      typedef std::basic_ostream<_CharT, _Traits>  __ostream_type;
-      typedef typename __ostream_type::ios_base    __ios_base;
+      using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
 
       const typename __ios_base::fmtflags __flags = __os.flags();
       const _CharT __fill = __os.fill();
@@ -2072,19 +1943,22 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     operator>>(std::basic_istream<_CharT, _Traits>& __is,
               normal_distribution<_RealType>& __x)
     {
-      typedef std::basic_istream<_CharT, _Traits>  __istream_type;
-      typedef typename __istream_type::ios_base    __ios_base;
+      using param_type = typename normal_distribution<_RealType>::param_type;
+      using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
 
       const typename __ios_base::fmtflags __flags = __is.flags();
       __is.flags(__ios_base::dec | __ios_base::skipws);
 
       double __mean, __stddev;
-      __is >> __mean >> __stddev
-          >> __x._M_saved_available;
-      if (__x._M_saved_available)
-       __is >> __x._M_saved;
-      __x.param(typename normal_distribution<_RealType>::
-               param_type(__mean, __stddev));
+      bool __saved_avail;
+      if (__is >> __mean >> __stddev >> __saved_avail)
+       {
+         if (!__saved_avail || (__is >> __x._M_saved))
+           {
+             __x._M_saved_available = __saved_avail;
+             __x.param(param_type(__mean, __stddev));
+           }
+       }
 
       __is.flags(__flags);
       return __is;
@@ -2110,8 +1984,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     operator<<(std::basic_ostream<_CharT, _Traits>& __os,
               const lognormal_distribution<_RealType>& __x)
     {
-      typedef std::basic_ostream<_CharT, _Traits>  __ostream_type;
-      typedef typename __ostream_type::ios_base    __ios_base;
+      using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
 
       const typename __ios_base::fmtflags __flags = __os.flags();
       const _CharT __fill = __os.fill();
@@ -2135,21 +2008,33 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     operator>>(std::basic_istream<_CharT, _Traits>& __is,
               lognormal_distribution<_RealType>& __x)
     {
-      typedef std::basic_istream<_CharT, _Traits>  __istream_type;
-      typedef typename __istream_type::ios_base    __ios_base;
+      using param_type
+       = typename lognormal_distribution<_RealType>::param_type;
+      using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
 
       const typename __ios_base::fmtflags __flags = __is.flags();
       __is.flags(__ios_base::dec | __ios_base::skipws);
 
       _RealType __m, __s;
-      __is >> __m >> __s >> __x._M_nd;
-      __x.param(typename lognormal_distribution<_RealType>::
-               param_type(__m, __s));
+      if (__is >> __m >> __s >> __x._M_nd)
+       __x.param(param_type(__m, __s));
 
       __is.flags(__flags);
       return __is;
     }
 
+  template<typename _RealType>
+    template<typename _ForwardIterator,
+            typename _UniformRandomNumberGenerator>
+      void
+      std::chi_squared_distribution<_RealType>::
+      __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
+                     _UniformRandomNumberGenerator& __urng)
+      {
+       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
+       while (__f != __t)
+         *__f++ = 2 * _M_gd(__urng);
+      }
 
   template<typename _RealType>
     template<typename _ForwardIterator,
@@ -2158,8 +2043,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       std::chi_squared_distribution<_RealType>::
       __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
                      _UniformRandomNumberGenerator& __urng,
-                     typename std::gamma_distribution<result_type>::param_type&
-                     __p)
+                     const typename
+                     std::gamma_distribution<result_type>::param_type& __p)
       {
        __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
        while (__f != __t)
@@ -2171,8 +2056,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     operator<<(std::basic_ostream<_CharT, _Traits>& __os,
               const chi_squared_distribution<_RealType>& __x)
     {
-      typedef std::basic_ostream<_CharT, _Traits>  __ostream_type;
-      typedef typename __ostream_type::ios_base    __ios_base;
+      using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
 
       const typename __ios_base::fmtflags __flags = __os.flags();
       const _CharT __fill = __os.fill();
@@ -2195,16 +2079,16 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     operator>>(std::basic_istream<_CharT, _Traits>& __is,
               chi_squared_distribution<_RealType>& __x)
     {
-      typedef std::basic_istream<_CharT, _Traits>  __istream_type;
-      typedef typename __istream_type::ios_base    __ios_base;
+      using param_type
+       = typename chi_squared_distribution<_RealType>::param_type;
+      using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
 
       const typename __ios_base::fmtflags __flags = __is.flags();
       __is.flags(__ios_base::dec | __ios_base::skipws);
 
       _RealType __n;
-      __is >> __n >> __x._M_gd;
-      __x.param(typename chi_squared_distribution<_RealType>::
-               param_type(__n));
+      if (__is >> __n >> __x._M_gd)
+       __x.param(param_type(__n));
 
       __is.flags(__flags);
       return __is;
@@ -2258,8 +2142,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     operator<<(std::basic_ostream<_CharT, _Traits>& __os,
               const cauchy_distribution<_RealType>& __x)
     {
-      typedef std::basic_ostream<_CharT, _Traits>  __ostream_type;
-      typedef typename __ostream_type::ios_base    __ios_base;
+      using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
 
       const typename __ios_base::fmtflags __flags = __os.flags();
       const _CharT __fill = __os.fill();
@@ -2282,16 +2165,15 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     operator>>(std::basic_istream<_CharT, _Traits>& __is,
               cauchy_distribution<_RealType>& __x)
     {
-      typedef std::basic_istream<_CharT, _Traits>  __istream_type;
-      typedef typename __istream_type::ios_base    __ios_base;
+      using param_type = typename cauchy_distribution<_RealType>::param_type;
+      using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
 
       const typename __ios_base::fmtflags __flags = __is.flags();
       __is.flags(__ios_base::dec | __ios_base::skipws);
 
       _RealType __a, __b;
-      __is >> __a >> __b;
-      __x.param(typename cauchy_distribution<_RealType>::
-               param_type(__a, __b));
+      if (__is >> __a >> __b)
+       __x.param(param_type(__a, __b));
 
       __is.flags(__flags);
       return __is;
@@ -2335,8 +2217,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     operator<<(std::basic_ostream<_CharT, _Traits>& __os,
               const fisher_f_distribution<_RealType>& __x)
     {
-      typedef std::basic_ostream<_CharT, _Traits>  __ostream_type;
-      typedef typename __ostream_type::ios_base    __ios_base;
+      using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
 
       const typename __ios_base::fmtflags __flags = __os.flags();
       const _CharT __fill = __os.fill();
@@ -2360,16 +2241,16 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     operator>>(std::basic_istream<_CharT, _Traits>& __is,
               fisher_f_distribution<_RealType>& __x)
     {
-      typedef std::basic_istream<_CharT, _Traits>  __istream_type;
-      typedef typename __istream_type::ios_base    __ios_base;
+      using param_type
+       = typename fisher_f_distribution<_RealType>::param_type;
+      using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
 
       const typename __ios_base::fmtflags __flags = __is.flags();
       __is.flags(__ios_base::dec | __ios_base::skipws);
 
       _RealType __m, __n;
-      __is >> __m >> __n >> __x._M_gd_x >> __x._M_gd_y;
-      __x.param(typename fisher_f_distribution<_RealType>::
-               param_type(__m, __n));
+      if (__is >> __m >> __n >> __x._M_gd_x >> __x._M_gd_y)
+       __x.param(param_type(__m, __n));
 
       __is.flags(__flags);
       return __is;
@@ -2410,8 +2291,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     operator<<(std::basic_ostream<_CharT, _Traits>& __os,
               const student_t_distribution<_RealType>& __x)
     {
-      typedef std::basic_ostream<_CharT, _Traits>  __ostream_type;
-      typedef typename __ostream_type::ios_base    __ios_base;
+      using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
 
       const typename __ios_base::fmtflags __flags = __os.flags();
       const _CharT __fill = __os.fill();
@@ -2434,15 +2314,16 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     operator>>(std::basic_istream<_CharT, _Traits>& __is,
               student_t_distribution<_RealType>& __x)
     {
-      typedef std::basic_istream<_CharT, _Traits>  __istream_type;
-      typedef typename __istream_type::ios_base    __ios_base;
+      using param_type
+       = typename student_t_distribution<_RealType>::param_type;
+      using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
 
       const typename __ios_base::fmtflags __flags = __is.flags();
       __is.flags(__ios_base::dec | __ios_base::skipws);
 
       _RealType __n;
-      __is >> __n >> __x._M_nd >> __x._M_gd;
-      __x.param(typename student_t_distribution<_RealType>::param_type(__n));
+      if (__is >> __n >> __x._M_nd >> __x._M_gd)
+       __x.param(param_type(__n));
 
       __is.flags(__flags);
       return __is;
@@ -2491,7 +2372,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
            __v = __v * __v * __v;
            __u = __aurng();
          }
-       while (__u > result_type(1.0) - 0.331 * __n * __n * __n * __n
+       while (__u > result_type(1.0) - 0.0331 * __n * __n * __n * __n
               && (std::log(__u) > (0.5 * __n * __n + __a1
                                    * (1.0 - __v + std::log(__v)))));
 
@@ -2540,7 +2421,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
                  __v = __v * __v * __v;
                  __u = __aurng();
                }
-             while (__u > result_type(1.0) - 0.331 * __n * __n * __n * __n
+             while (__u > result_type(1.0) - 0.0331 * __n * __n * __n * __n
                     && (std::log(__u) > (0.5 * __n * __n + __a1
                                          * (1.0 - __v + std::log(__v)))));
 
@@ -2561,7 +2442,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
                  __v = __v * __v * __v;
                  __u = __aurng();
                }
-             while (__u > result_type(1.0) - 0.331 * __n * __n * __n * __n
+             while (__u > result_type(1.0) - 0.0331 * __n * __n * __n * __n
                     && (std::log(__u) > (0.5 * __n * __n + __a1
                                          * (1.0 - __v + std::log(__v)))));
 
@@ -2579,8 +2460,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     operator<<(std::basic_ostream<_CharT, _Traits>& __os,
               const gamma_distribution<_RealType>& __x)
     {
-      typedef std::basic_ostream<_CharT, _Traits>  __ostream_type;
-      typedef typename __ostream_type::ios_base    __ios_base;
+      using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
 
       const typename __ios_base::fmtflags __flags = __os.flags();
       const _CharT __fill = __os.fill();
@@ -2604,16 +2484,15 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     operator>>(std::basic_istream<_CharT, _Traits>& __is,
               gamma_distribution<_RealType>& __x)
     {
-      typedef std::basic_istream<_CharT, _Traits>  __istream_type;
-      typedef typename __istream_type::ios_base    __ios_base;
+      using param_type = typename gamma_distribution<_RealType>::param_type;
+      using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
 
       const typename __ios_base::fmtflags __flags = __is.flags();
       __is.flags(__ios_base::dec | __ios_base::skipws);
 
       _RealType __alpha_val, __beta_val;
-      __is >> __alpha_val >> __beta_val >> __x._M_nd;
-      __x.param(typename gamma_distribution<_RealType>::
-               param_type(__alpha_val, __beta_val));
+      if (__is >> __alpha_val >> __beta_val >> __x._M_nd)
+       __x.param(param_type(__alpha_val, __beta_val));
 
       __is.flags(__flags);
       return __is;
@@ -2657,8 +2536,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     operator<<(std::basic_ostream<_CharT, _Traits>& __os,
               const weibull_distribution<_RealType>& __x)
     {
-      typedef std::basic_ostream<_CharT, _Traits>  __ostream_type;
-      typedef typename __ostream_type::ios_base    __ios_base;
+      using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
 
       const typename __ios_base::fmtflags __flags = __os.flags();
       const _CharT __fill = __os.fill();
@@ -2681,16 +2559,15 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     operator>>(std::basic_istream<_CharT, _Traits>& __is,
               weibull_distribution<_RealType>& __x)
     {
-      typedef std::basic_istream<_CharT, _Traits>  __istream_type;
-      typedef typename __istream_type::ios_base    __ios_base;
+      using param_type = typename weibull_distribution<_RealType>::param_type;
+      using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
 
       const typename __ios_base::fmtflags __flags = __is.flags();
       __is.flags(__ios_base::dec | __ios_base::skipws);
 
       _RealType __a, __b;
-      __is >> __a >> __b;
-      __x.param(typename weibull_distribution<_RealType>::
-               param_type(__a, __b));
+      if (__is >> __a >> __b)
+       __x.param(param_type(__a, __b));
 
       __is.flags(__flags);
       return __is;
@@ -2733,8 +2610,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     operator<<(std::basic_ostream<_CharT, _Traits>& __os,
               const extreme_value_distribution<_RealType>& __x)
     {
-      typedef std::basic_ostream<_CharT, _Traits>  __ostream_type;
-      typedef typename __ostream_type::ios_base    __ios_base;
+      using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
 
       const typename __ios_base::fmtflags __flags = __os.flags();
       const _CharT __fill = __os.fill();
@@ -2757,16 +2633,16 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     operator>>(std::basic_istream<_CharT, _Traits>& __is,
               extreme_value_distribution<_RealType>& __x)
     {
-      typedef std::basic_istream<_CharT, _Traits>  __istream_type;
-      typedef typename __istream_type::ios_base    __ios_base;
+      using param_type
+       = typename extreme_value_distribution<_RealType>::param_type;
+      using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
 
       const typename __ios_base::fmtflags __flags = __is.flags();
       __is.flags(__ios_base::dec | __ios_base::skipws);
 
       _RealType __a, __b;
-      __is >> __a >> __b;
-      __x.param(typename extreme_value_distribution<_RealType>::
-               param_type(__a, __b));
+      if (__is >> __a >> __b)
+       __x.param(param_type(__a, __b));
 
       __is.flags(__flags);
       return __is;
@@ -2786,9 +2662,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
       const double __sum = std::accumulate(_M_prob.begin(),
                                           _M_prob.end(), 0.0);
+      __glibcxx_assert(__sum > 0);
       // Now normalize the probabilites.
-      __detail::__transform(_M_prob.begin(), _M_prob.end(), _M_prob.begin(),
-                         std::bind2nd(std::divides<double>(), __sum));
+      __detail::__normalize(_M_prob.begin(), _M_prob.end(), _M_prob.begin(),
+                           __sum);
       // Accumulate partial sums.
       _M_cp.reserve(_M_prob.size());
       std::partial_sum(_M_prob.begin(), _M_prob.end(),
@@ -2869,8 +2746,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     operator<<(std::basic_ostream<_CharT, _Traits>& __os,
               const discrete_distribution<_IntType>& __x)
     {
-      typedef std::basic_ostream<_CharT, _Traits>  __ostream_type;
-      typedef typename __ostream_type::ios_base    __ios_base;
+      using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
 
       const typename __ios_base::fmtflags __flags = __os.flags();
       const _CharT __fill = __os.fill();
@@ -2891,32 +2767,44 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       return __os;
     }
 
+namespace __detail
+{
+  template<typename _ValT, typename _CharT, typename _Traits>
+    basic_istream<_CharT, _Traits>&
+    __extract_params(basic_istream<_CharT, _Traits>& __is,
+                    vector<_ValT>& __vals, size_t __n)
+    {
+      __vals.reserve(__n);
+      while (__n--)
+       {
+         _ValT __val;
+         if (__is >> __val)
+           __vals.push_back(__val);
+         else
+           break;
+       }
+      return __is;
+    }
+} // namespace __detail
+
   template<typename _IntType, typename _CharT, typename _Traits>
     std::basic_istream<_CharT, _Traits>&
     operator>>(std::basic_istream<_CharT, _Traits>& __is,
               discrete_distribution<_IntType>& __x)
     {
-      typedef std::basic_istream<_CharT, _Traits>  __istream_type;
-      typedef typename __istream_type::ios_base    __ios_base;
+      using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
 
       const typename __ios_base::fmtflags __flags = __is.flags();
       __is.flags(__ios_base::dec | __ios_base::skipws);
 
       size_t __n;
-      __is >> __n;
-
-      std::vector<double> __prob_vec;
-      __prob_vec.reserve(__n);
-      for (; __n != 0; --__n)
+      if (__is >> __n)
        {
-         double __prob;
-         __is >> __prob;
-         __prob_vec.push_back(__prob);
+         std::vector<double> __prob_vec;
+         if (__detail::__extract_params(__is, __prob_vec, __n))
+           __x.param({__prob_vec.begin(), __prob_vec.end()});
        }
 
-      __x.param(typename discrete_distribution<_IntType>::
-               param_type(__prob_vec.begin(), __prob_vec.end()));
-
       __is.flags(__flags);
       return __is;
     }
@@ -2939,9 +2827,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
       const double __sum = std::accumulate(_M_den.begin(),
                                           _M_den.end(), 0.0);
+      __glibcxx_assert(__sum > 0);
 
-      __detail::__transform(_M_den.begin(), _M_den.end(), _M_den.begin(),
-                           std::bind2nd(std::divides<double>(), __sum));
+      __detail::__normalize(_M_den.begin(), _M_den.end(), _M_den.begin(),
+                           __sum);
 
       _M_cp.reserve(_M_den.size());
       std::partial_sum(_M_den.begin(), _M_den.end(),
@@ -3079,8 +2968,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     operator<<(std::basic_ostream<_CharT, _Traits>& __os,
               const piecewise_constant_distribution<_RealType>& __x)
     {
-      typedef std::basic_ostream<_CharT, _Traits>  __ostream_type;
-      typedef typename __ostream_type::ios_base    __ios_base;
+      using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
 
       const typename __ios_base::fmtflags __flags = __os.flags();
       const _CharT __fill = __os.fill();
@@ -3111,36 +2999,26 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     operator>>(std::basic_istream<_CharT, _Traits>& __is,
               piecewise_constant_distribution<_RealType>& __x)
     {
-      typedef std::basic_istream<_CharT, _Traits>  __istream_type;
-      typedef typename __istream_type::ios_base    __ios_base;
+      using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
 
       const typename __ios_base::fmtflags __flags = __is.flags();
       __is.flags(__ios_base::dec | __ios_base::skipws);
 
       size_t __n;
-      __is >> __n;
-
-      std::vector<_RealType> __int_vec;
-      __int_vec.reserve(__n + 1);
-      for (size_t __i = 0; __i <= __n; ++__i)
-       {
-         _RealType __int;
-         __is >> __int;
-         __int_vec.push_back(__int);
-       }
-
-      std::vector<double> __den_vec;
-      __den_vec.reserve(__n);
-      for (size_t __i = 0; __i < __n; ++__i)
+      if (__is >> __n)
        {
-         double __den;
-         __is >> __den;
-         __den_vec.push_back(__den);
+         std::vector<_RealType> __int_vec;
+         if (__detail::__extract_params(__is, __int_vec, __n + 1))
+           {
+             std::vector<double> __den_vec;
+             if (__detail::__extract_params(__is, __den_vec, __n))
+               {
+                 __x.param({ __int_vec.begin(), __int_vec.end(),
+                             __den_vec.begin() });
+               }
+           }
        }
 
-      __x.param(typename piecewise_constant_distribution<_RealType>::
-         param_type(__int_vec.begin(), __int_vec.end(), __den_vec.begin()));
-
       __is.flags(__flags);
       return __is;
     }
@@ -3172,16 +3050,16 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
          _M_cp.push_back(__sum);
          _M_m.push_back((_M_den[__k + 1] - _M_den[__k]) / __delta);
        }
+      __glibcxx_assert(__sum > 0);
 
       //  Now normalize the densities...
-      __detail::__transform(_M_den.begin(), _M_den.end(), _M_den.begin(),
-                         std::bind2nd(std::divides<double>(), __sum));
+      __detail::__normalize(_M_den.begin(), _M_den.end(), _M_den.begin(),
+                           __sum);
       //  ... and partial sums... 
-      __detail::__transform(_M_cp.begin(), _M_cp.end(), _M_cp.begin(),
-                           std::bind2nd(std::divides<double>(), __sum));
+      __detail::__normalize(_M_cp.begin(), _M_cp.end(), _M_cp.begin(), __sum);
       //  ... and slopes.
-      __detail::__transform(_M_m.begin(), _M_m.end(), _M_m.begin(),
-                           std::bind2nd(std::divides<double>(), __sum));
+      __detail::__normalize(_M_m.begin(), _M_m.end(), _M_m.begin(), __sum);
+
       //  Make sure the last cumulative probablility is one.
       _M_cp[_M_cp.size() - 1] = 1.0;
      }
@@ -3296,8 +3174,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     operator<<(std::basic_ostream<_CharT, _Traits>& __os,
               const piecewise_linear_distribution<_RealType>& __x)
     {
-      typedef std::basic_ostream<_CharT, _Traits>  __ostream_type;
-      typedef typename __ostream_type::ios_base    __ios_base;
+      using __ios_base = typename basic_ostream<_CharT, _Traits>::ios_base;
 
       const typename __ios_base::fmtflags __flags = __os.flags();
       const _CharT __fill = __os.fill();
@@ -3328,44 +3205,34 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     operator>>(std::basic_istream<_CharT, _Traits>& __is,
               piecewise_linear_distribution<_RealType>& __x)
     {
-      typedef std::basic_istream<_CharT, _Traits>  __istream_type;
-      typedef typename __istream_type::ios_base    __ios_base;
+      using __ios_base = typename basic_istream<_CharT, _Traits>::ios_base;
 
       const typename __ios_base::fmtflags __flags = __is.flags();
       __is.flags(__ios_base::dec | __ios_base::skipws);
 
       size_t __n;
-      __is >> __n;
-
-      std::vector<_RealType> __int_vec;
-      __int_vec.reserve(__n + 1);
-      for (size_t __i = 0; __i <= __n; ++__i)
-       {
-         _RealType __int;
-         __is >> __int;
-         __int_vec.push_back(__int);
-       }
-
-      std::vector<double> __den_vec;
-      __den_vec.reserve(__n + 1);
-      for (size_t __i = 0; __i <= __n; ++__i)
+      if (__is >> __n)
        {
-         double __den;
-         __is >> __den;
-         __den_vec.push_back(__den);
+         vector<_RealType> __int_vec;
+         if (__detail::__extract_params(__is, __int_vec, __n + 1))
+           {
+             vector<double> __den_vec;
+             if (__detail::__extract_params(__is, __den_vec, __n + 1))
+               {
+                 __x.param({ __int_vec.begin(), __int_vec.end(),
+                             __den_vec.begin() });
+               }
+           }
        }
-
-      __x.param(typename piecewise_linear_distribution<_RealType>::
-         param_type(__int_vec.begin(), __int_vec.end(), __den_vec.begin()));
-
       __is.flags(__flags);
       return __is;
     }
 
 
-  template<typename _IntType>
+  template<typename _IntType, typename>
     seed_seq::seed_seq(std::initializer_list<_IntType> __il)
     {
+      _M_v.reserve(__il.size());
       for (auto __iter = __il.begin(); __iter != __il.end(); ++__iter)
        _M_v.push_back(__detail::__mod<result_type,
                       __detail::_Shift<result_type, 32>::__value>(*__iter));
@@ -3374,6 +3241,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
   template<typename _InputIterator>
     seed_seq::seed_seq(_InputIterator __begin, _InputIterator __end)
     {
+      if _GLIBCXX17_CONSTEXPR (__is_random_access_iter<_InputIterator>::value)
+       _M_v.reserve(std::distance(__begin, __end));
+
       for (_InputIterator __iter = __begin; __iter != __end; ++__iter)
        _M_v.push_back(__detail::__mod<result_type,
                       __detail::_Shift<result_type, 32>::__value>(*__iter));
@@ -3403,42 +3273,70 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       const size_t __q = __p + __t;
       const size_t __m = std::max(size_t(__s + 1), __n);
 
-      for (size_t __k = 0; __k < __m; ++__k)
+#ifndef __UINT32_TYPE__
+      struct _Up
+      {
+       _Up(uint_least32_t v) : _M_v(v & 0xffffffffu) { }
+
+       operator uint_least32_t() const { return _M_v; }
+
+       uint_least32_t _M_v;
+      };
+      using uint32_t = _Up;
+#endif
+
+      // k == 0, every element in [begin,end) equals 0x8b8b8b8bu
        {
-         _Type __arg = (__begin[__k % __n]
-                        ^ __begin[(__k + __p) % __n]
-                        ^ __begin[(__k - 1) % __n]);
-         _Type __r1 = __arg ^ (__arg >> 27);
-         __r1 = __detail::__mod<_Type,
-                   __detail::_Shift<_Type, 32>::__value>(1664525u * __r1);
-         _Type __r2 = __r1;
-         if (__k == 0)
-           __r2 += __s;
-         else if (__k <= __s)
-           __r2 += __k % __n + _M_v[__k - 1];
-         else
-           __r2 += __k % __n;
-         __r2 = __detail::__mod<_Type,
-                  __detail::_Shift<_Type, 32>::__value>(__r2);
-         __begin[(__k + __p) % __n] += __r1;
-         __begin[(__k + __q) % __n] += __r2;
-         __begin[__k % __n] = __r2;
+         uint32_t __r1 = 1371501266u;
+         uint32_t __r2 = __r1 + __s;
+         __begin[__p] += __r1;
+         __begin[__q] = (uint32_t)__begin[__q] + __r2;
+         __begin[0] = __r2;
+       }
+
+      for (size_t __k = 1; __k <= __s; ++__k)
+       {
+         const size_t __kn = __k % __n;
+         const size_t __kpn = (__k + __p) % __n;
+         const size_t __kqn = (__k + __q) % __n;
+         uint32_t __arg = (__begin[__kn]
+                           ^ __begin[__kpn]
+                           ^ __begin[(__k - 1) % __n]);
+         uint32_t __r1 = 1664525u * (__arg ^ (__arg >> 27));
+         uint32_t __r2 = __r1 + (uint32_t)__kn + _M_v[__k - 1];
+         __begin[__kpn] = (uint32_t)__begin[__kpn] + __r1;
+         __begin[__kqn] = (uint32_t)__begin[__kqn] + __r2;
+         __begin[__kn] = __r2;
+       }
+
+      for (size_t __k = __s + 1; __k < __m; ++__k)
+       {
+         const size_t __kn = __k % __n;
+         const size_t __kpn = (__k + __p) % __n;
+         const size_t __kqn = (__k + __q) % __n;
+         uint32_t __arg = (__begin[__kn]
+                                ^ __begin[__kpn]
+                                ^ __begin[(__k - 1) % __n]);
+         uint32_t __r1 = 1664525u * (__arg ^ (__arg >> 27));
+         uint32_t __r2 = __r1 + (uint32_t)__kn;
+         __begin[__kpn] = (uint32_t)__begin[__kpn] + __r1;
+         __begin[__kqn] = (uint32_t)__begin[__kqn] + __r2;
+         __begin[__kn] = __r2;
        }
 
       for (size_t __k = __m; __k < __m + __n; ++__k)
        {
-         _Type __arg = (__begin[__k % __n]
-                        + __begin[(__k + __p) % __n]
-                        + __begin[(__k - 1) % __n]);
-         _Type __r3 = __arg ^ (__arg >> 27);
-         __r3 = __detail::__mod<_Type,
-                  __detail::_Shift<_Type, 32>::__value>(1566083941u * __r3);
-         _Type __r4 = __r3 - __k % __n;
-         __r4 = __detail::__mod<_Type,
-                  __detail::_Shift<_Type, 32>::__value>(__r4);
-         __begin[(__k + __p) % __n] ^= __r3;
-         __begin[(__k + __q) % __n] ^= __r4;
-         __begin[__k % __n] = __r4;
+         const size_t __kn = __k % __n;
+         const size_t __kpn = (__k + __p) % __n;
+         const size_t __kqn = (__k + __q) % __n;
+         uint32_t __arg = (__begin[__kn]
+                           + __begin[__kpn]
+                           + __begin[(__k - 1) % __n]);
+         uint32_t __r3 = 1566083941u * (__arg ^ (__arg >> 27));
+         uint32_t __r4 = __r3 - __kn;
+         __begin[__kpn] ^= __r3;
+         __begin[__kqn] ^= __r4;
+         __begin[__kn] = __r4;
        }
     }
 
@@ -3447,21 +3345,36 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     _RealType
     generate_canonical(_UniformRandomNumberGenerator& __urng)
     {
+      static_assert(std::is_floating_point<_RealType>::value,
+                   "template argument must be a floating point type");
+
       const size_t __b
        = std::min(static_cast<size_t>(std::numeric_limits<_RealType>::digits),
                    __bits);
       const long double __r = static_cast<long double>(__urng.max())
                            - static_cast<long double>(__urng.min()) + 1.0L;
       const size_t __log2r = std::log(__r) / std::log(2.0L);
-      size_t __k = std::max<size_t>(1UL, (__b + __log2r - 1UL) / __log2r);
+      const size_t __m = std::max<size_t>(1UL,
+                                         (__b + __log2r - 1UL) / __log2r);
+      _RealType __ret;
       _RealType __sum = _RealType(0);
       _RealType __tmp = _RealType(1);
-      for (; __k != 0; --__k)
+      for (size_t __k = __m; __k != 0; --__k)
        {
          __sum += _RealType(__urng() - __urng.min()) * __tmp;
          __tmp *= __r;
        }
-      return __sum / __tmp;
+      __ret = __sum / __tmp;
+      if (__builtin_expect(__ret >= _RealType(1), 0))
+       {
+#if _GLIBCXX_USE_C99_MATH_TR1
+         __ret = std::nextafter(_RealType(1), _RealType(0));
+#else
+         __ret = _RealType(1)
+           - std::numeric_limits<_RealType>::epsilon() / _RealType(2);
+#endif
+       }
+      return __ret;
     }
 
 _GLIBCXX_END_NAMESPACE_VERSION