]> www.ginac.de Git - ginac.git/commitdiff
Simplify random_modint functor a bit.
authorRichard Kreckel <kreckel@ginac.de>
Thu, 15 May 2025 18:42:47 +0000 (20:42 +0200)
committerRichard Kreckel <kreckel@ginac.de>
Thu, 15 May 2025 18:42:47 +0000 (20:42 +0200)
ginac/polynomial/smod_helpers.h

index 313be1249c1ab874a8e9bc0aa351da0583880451..305a8a4b8e13bb25c912911d495a10ff34dbac55 100644 (file)
@@ -69,23 +69,21 @@ static inline cln::cl_I to_cl_I(const ex& e)
        return cln::the<cln::cl_I>(ex_to<numeric>(e).to_cl_N());
 }
 
+/**
+ * Generate random integer -p/2 < m <= p/2.
+ */
 struct random_modint
 {
        typedef long value_type;
        const value_type p;
-       const value_type p_2;
+       const value_type pm1_2;  // (p-1)/2
 
-       random_modint(const value_type& p_) : p(p_), p_2((p >> 1))
+       random_modint(const value_type& p_) : p(p_), pm1_2((p-1) >> 1)
        { }
        value_type operator()() const
        {
-               do {
-                       cln::cl_I tmp_ = cln::random_I(p);
-                       value_type tmp = cln::cl_I_to_long(tmp_);
-                       if (tmp > p_2)
-                               tmp -= p;
-                       return tmp;
-               } while (true);
+               cln::cl_I m = cln::random_I(p) - pm1_2;
+               return cln::cl_I_to_long(m);
        }
 
 };