]> www.ginac.de Git - cln.git/blobdiff - include/cln/modinteger.h
Replace CL_REQUIRE/CL_PROVIDE(cl_I_ring) with portable code.
[cln.git] / include / cln / modinteger.h
index 4624be7ac5b74045231309db67ebceb592591ab0..877e7e9db640f6dfc64bb42ed6a55feb359bc391 100644 (file)
@@ -11,7 +11,7 @@
 #include "cln/io.h"
 #include "cln/proplist.h"
 #include "cln/condition.h"
-#include "cln/abort.h"
+#include "cln/exception.h"
 #undef random // Linux defines random() as a macro!
 
 namespace cln {
@@ -81,7 +81,7 @@ struct cl_composite_condition : public cl_condition {
                { print(std::cerr); }
        // Implement general condition methods.
        const char * name () const;
-       void print (cl_ostream) const;
+       void print (std::ostream&) const;
        ~cl_composite_condition () {}
 };
 
@@ -103,7 +103,7 @@ public:
        CL_DEFINE_CONVERTER(_cl_ring_element)
 public:        // Ability to place an object at a given address.
        void* operator new (size_t size) { return malloc_hook(size); }
-       void* operator new (size_t size, _cl_MI* ptr) { (void)size; return ptr; }
+       void* operator new (size_t size, void* ptr) { (void)size; return ptr; }
        void operator delete (void* ptr) { free_hook(ptr); }
 };
 
@@ -125,7 +125,7 @@ public:
        void debug_print () const;
 public:        // Ability to place an object at a given address.
        void* operator new (size_t size) { return malloc_hook(size); }
-       void* operator new (size_t size, cl_MI* ptr) { (void)size; return ptr; }
+       void* operator new (size_t size, void* ptr) { (void)size; return ptr; }
        void operator delete (void* ptr) { free_hook(ptr); }
 };
 
@@ -141,9 +141,9 @@ public:
        cl_MI_x (cl_composite_condition* c) : value (), condition (c) {}
        cl_MI_x (const cl_MI& x) : value (x), condition (NULL) {}
        // Cast operators.
-       //operator cl_MI& () { if (condition) cl_abort(); return value; }
-       //operator const cl_MI& () const { if (condition) cl_abort(); return value; }
-       operator cl_MI () const { if (condition) cl_abort(); return value; }
+       //operator cl_MI& () { if (condition) throw runtime_exception(); return value; }
+       //operator const cl_MI& () const { if (condition) throw runtime_exception(); return value; }
+       operator cl_MI () const { if (condition) throw runtime_exception(); return value; }
 };
 
 
@@ -151,16 +151,16 @@ public:
 
 struct _cl_modint_setops /* cf. _cl_ring_setops */ {
        // print
-       void (* fprint) (cl_heap_modint_ring* R, cl_ostream stream, const _cl_MI& x);
+       void (* fprint) (cl_heap_modint_ring* R, std::ostream& stream, const _cl_MI& x);
        // equality
-       cl_boolean (* equal) (cl_heap_modint_ring* R, const _cl_MI& x, const _cl_MI& y);
+       bool (* equal) (cl_heap_modint_ring* R, const _cl_MI& x, const _cl_MI& y);
        // random number
        const _cl_MI (* random) (cl_heap_modint_ring* R, random_state& randomstate);
 };
 struct _cl_modint_addops /* cf. _cl_ring_addops */ {
        // 0
        const _cl_MI (* zero) (cl_heap_modint_ring* R);
-       cl_boolean (* zerop) (cl_heap_modint_ring* R, const _cl_MI& x);
+       bool (* zerop) (cl_heap_modint_ring* R, const _cl_MI& x);
        // x+y
        const _cl_MI (* plus) (cl_heap_modint_ring* R, const _cl_MI& x, const _cl_MI& y);
        // x-y
@@ -214,15 +214,15 @@ public:
        cl_I modulus;   // m, normalized to be >= 0
 public:
        // Low-level operations.
-       void _fprint (cl_ostream stream, const _cl_MI& x)
+       void _fprint (std::ostream& stream, const _cl_MI& x)
                { setops->fprint(this,stream,x); }
-       cl_boolean _equal (const _cl_MI& x, const _cl_MI& y)
+       bool _equal (const _cl_MI& x, const _cl_MI& y)
                { return setops->equal(this,x,y); }
        const _cl_MI _random (random_state& randomstate)
                { return setops->random(this,randomstate); }
        const _cl_MI _zero ()
                { return addops->zero(this); }
-       cl_boolean _zerop (const _cl_MI& x)
+       bool _zerop (const _cl_MI& x)
                { return addops->zerop(this,x); }
        const _cl_MI _plus (const _cl_MI& x, const _cl_MI& y)
                { return addops->plus(this,x,y); }
@@ -251,15 +251,15 @@ public:
        const cl_I _retract (const _cl_MI& x)
                { return mulops->retract(this,x); }
        // High-level operations.
-       void fprint (cl_ostream stream, const cl_MI& x)
+       void fprint (std::ostream& stream, const cl_MI& x)
        {
-               if (!(x.ring() == this)) cl_abort();
+               if (!(x.ring() == this)) throw runtime_exception();
                _fprint(stream,x);
        }
-       cl_boolean equal (const cl_MI& x, const cl_MI& y)
+       bool equal (const cl_MI& x, const cl_MI& y)
        {
-               if (!(x.ring() == this)) cl_abort();
-               if (!(y.ring() == this)) cl_abort();
+               if (!(x.ring() == this)) throw runtime_exception();
+               if (!(y.ring() == this)) throw runtime_exception();
                return _equal(x,y);
        }
        const cl_MI random (random_state& randomstate = default_random_state)
@@ -270,26 +270,26 @@ public:
        {
                return cl_MI(this,_zero());
        }
-       cl_boolean zerop (const cl_MI& x)
+       bool zerop (const cl_MI& x)
        {
-               if (!(x.ring() == this)) cl_abort();
+               if (!(x.ring() == this)) throw runtime_exception();
                return _zerop(x);
        }
        const cl_MI plus (const cl_MI& x, const cl_MI& y)
        {
-               if (!(x.ring() == this)) cl_abort();
-               if (!(y.ring() == this)) cl_abort();
+               if (!(x.ring() == this)) throw runtime_exception();
+               if (!(y.ring() == this)) throw runtime_exception();
                return cl_MI(this,_plus(x,y));
        }
        const cl_MI minus (const cl_MI& x, const cl_MI& y)
        {
-               if (!(x.ring() == this)) cl_abort();
-               if (!(y.ring() == this)) cl_abort();
+               if (!(x.ring() == this)) throw runtime_exception();
+               if (!(y.ring() == this)) throw runtime_exception();
                return cl_MI(this,_minus(x,y));
        }
        const cl_MI uminus (const cl_MI& x)
        {
-               if (!(x.ring() == this)) cl_abort();
+               if (!(x.ring() == this)) throw runtime_exception();
                return cl_MI(this,_uminus(x));
        }
        const cl_MI one ()
@@ -302,34 +302,34 @@ public:
        }
        const cl_MI mul (const cl_MI& x, const cl_MI& y)
        {
-               if (!(x.ring() == this)) cl_abort();
-               if (!(y.ring() == this)) cl_abort();
+               if (!(x.ring() == this)) throw runtime_exception();
+               if (!(y.ring() == this)) throw runtime_exception();
                return cl_MI(this,_mul(x,y));
        }
        const cl_MI square (const cl_MI& x)
        {
-               if (!(x.ring() == this)) cl_abort();
+               if (!(x.ring() == this)) throw runtime_exception();
                return cl_MI(this,_square(x));
        }
        const cl_MI expt_pos (const cl_MI& x, const cl_I& y)
        {
-               if (!(x.ring() == this)) cl_abort();
+               if (!(x.ring() == this)) throw runtime_exception();
                return cl_MI(this,_expt_pos(x,y));
        }
        const cl_MI_x recip (const cl_MI& x)
        {
-               if (!(x.ring() == this)) cl_abort();
+               if (!(x.ring() == this)) throw runtime_exception();
                return _recip(x);
        }
        const cl_MI_x div (const cl_MI& x, const cl_MI& y)
        {
-               if (!(x.ring() == this)) cl_abort();
-               if (!(y.ring() == this)) cl_abort();
+               if (!(x.ring() == this)) throw runtime_exception();
+               if (!(y.ring() == this)) throw runtime_exception();
                return _div(x,y);
        }
        const cl_MI_x expt (const cl_MI& x, const cl_I& y)
        {
-               if (!(x.ring() == this)) cl_abort();
+               if (!(x.ring() == this)) throw runtime_exception();
                return _expt(x,y);
        }
        const cl_I reduce_modulo (const cl_I& x)
@@ -338,23 +338,20 @@ public:
        }
        const cl_I retract (const cl_MI& x)
        {
-               if (!(x.ring() == this)) cl_abort();
+               if (!(x.ring() == this)) throw runtime_exception();
                return _retract(x);
        }
        // Miscellaneous.
-       sintL bits; // number of bits needed to represent a representative, or -1
+       sintC bits; // number of bits needed to represent a representative, or -1
        int log2_bits; // log_2(bits), or -1
        // Property operations.
        cl_property* get_property (const cl_symbol& key)
                { return properties.get_property(key); }
        void add_property (cl_property* new_property)
                { properties.add_property(new_property); }
-// Constructor.
+// Constructor / destructor.
        cl_heap_modint_ring (cl_I m, cl_modint_setops*, cl_modint_addops*, cl_modint_mulops*);
-// This class is intented to be subclassable, hence needs a virtual destructor.
-       virtual ~cl_heap_modint_ring () {}
-private:
-       virtual void dummy ();
+       ~cl_heap_modint_ring () {}
 };
 #define SUBCLASS_cl_heap_modint_ring() \
   SUBCLASS_cl_heap_ring()
@@ -363,14 +360,11 @@ private:
 extern const cl_modint_ring find_modint_ring (const cl_I& m);
 CL_REQUIRE(cl_MI)
 
-// Runtime typing support.
-extern cl_class cl_class_modint_ring;
-
 
 // Operations on modular integers.
 
 // Output.
-inline void fprint (cl_ostream stream, const cl_MI& x)
+inline void fprint (std::ostream& stream, const cl_MI& x)
        { x.ring()->fprint(stream,x); }
 CL_DEFINE_PRINT_OPERATOR(cl_MI)
 
@@ -395,8 +389,8 @@ inline const cl_MI operator- (const cl_I& x, const cl_MI& y)
        { return y.ring()->minus(y.ring()->canonhom(x),y); }
 
 // Shifts.
-extern const cl_MI operator<< (const cl_MI& x, sintL y); // assume 0 <= y < 2^31
-extern const cl_MI operator>> (const cl_MI& x, sintL y); // assume m odd, 0 <= y < 2^31
+extern const cl_MI operator<< (const cl_MI& x, sintC y); // assume 0 <= y < 2^(intCsize-1)
+extern const cl_MI operator>> (const cl_MI& x, sintC y); // assume m odd, 0 <= y < 2^(intCsize-1)
 
 // Equality.
 inline bool operator== (const cl_MI& x, const cl_MI& y)
@@ -413,7 +407,7 @@ inline bool operator!= (const cl_I& x, const cl_MI& y)
        { return !y.ring()->equal(y.ring()->canonhom(x),y); }
 
 // Compare against 0.
-inline cl_boolean zerop (const cl_MI& x)
+inline bool zerop (const cl_MI& x)
        { return x.ring()->zerop(x); }
 
 // Multiply.
@@ -456,9 +450,7 @@ inline const cl_MI operator* (const cl_MI& x, const cl_I& y)
 // Debugging support.
 #ifdef CL_DEBUG
 extern int cl_MI_debug_module;
-static void* const cl_MI_debug_dummy[] = { &cl_MI_debug_dummy,
-       &cl_MI_debug_module
-};
+CL_FORCE_LINK(cl_MI_debug_dummy, cl_MI_debug_module)
 #endif
 
 }  // namespace cln