]> www.ginac.de Git - cln.git/blobdiff - src/integer/cl_I.h
2006-04-25 Bruno Haible <bruno@clisp.org>
[cln.git] / src / integer / cl_I.h
index 44df5eb838440c0da3c24540c764b2f0051fa5dc..52c137d0ecc1d0ca92694f2b354097eed9c85c74 100644 (file)
@@ -3,14 +3,15 @@
 #ifndef _CL_I_H
 #define _CL_I_H
 
-#include "cl_number.h"
-#include "cl_integer.h"
+#include "cln/number.h"
+#include "cln/integer.h"
 #include "cl_macros.h"
-#include "cl_malloc.h"
-#include "cl_abort.h"
+#include "cln/malloc.h"
+#include "cln/abort.h"
 #include "cl_offsetof.h"
 #include "cl_DS.h"
 
+namespace cln {
 
 // maximal needed length of a digit sequence for a fixnum
   #define FN_maxlength  ceiling(cl_value_len,intDsize)
@@ -41,7 +42,7 @@ inline cl_uint cl_FN_word (const cl_I& x)
 // Bignums.
 
 struct cl_heap_bignum : cl_heap {
-       unsigned int length;    // length (in digits)
+       uintC length;           // length (in digits)
        uintD data[1];          // number in two's complement representation
 };
 
@@ -50,9 +51,9 @@ inline cl_heap_bignum* TheBignum (cl_heap_bignum* p)
 inline cl_heap_bignum* TheBignum (const cl_number& obj)
        { return (cl_heap_bignum*)(obj.pointer); }
 
-inline cl_heap_bignum* allocate_bignum (unsigned int length)
+inline cl_heap_bignum* allocate_bignum (uintC length)
 {
-       cl_heap_bignum* p = (cl_heap_bignum*) cl_malloc_hook(offsetofa(cl_heap_bignum,data)+sizeof(uintD)*length);
+       cl_heap_bignum* p = (cl_heap_bignum*) malloc_hook(offsetofa(cl_heap_bignum,data)+sizeof(uintD)*length);
        p->refcount = 1;
        p->type = &cl_class_bignum;
        p->length = length;
@@ -111,32 +112,32 @@ inline cl_boolean eq (const cl_I& x, sint32 y)
 // Umwandlungsroutinen Integer <--> Longword:
 
 // Wandelt Fixnum >=0 in Unsigned Longword um.
-// FN_to_UL(obj)
+// FN_to_UV(obj)
 // > obj: ein Fixnum >=0
-// < ergebnis: der Wert des Fixnum als 32-Bit-Zahl.
-inline uint32 FN_to_UL (const cl_I& x)
+// < ergebnis: der Wert des Fixnum als intVsize-Bit-Zahl.
+inline uintV FN_to_UV (const cl_I& x)
 {
        // This assumes cl_value_shift + cl_value_len == cl_pointer_size.
        return (cl_uint)(x.word) >> cl_value_shift;
 }
 
 // Wandelt Fixnum in Longword um.
-// FN_to_L(obj)
+// FN_to_V(obj)
 // > obj: ein Fixnum
-// < ergebnis: der Wert des Fixnum als 32-Bit-Zahl.
-inline sint32 FN_to_L (const cl_I& x)
+// < ergebnis: der Wert des Fixnum als intVsize-Bit-Zahl.
+inline sintV FN_to_V (const cl_I& x)
 {
        // This assumes cl_value_shift + cl_value_len == cl_pointer_size.
        return (cl_sint)(x.word) >> cl_value_shift;
 }
 
-// FN_L_zerop(x,x_) stellt fest, ob x = 0 ist.
-// Dabei ist x ein Fixnum und x_ = FN_to_L(x).
-  #define FN_L_zerop(x,x_)  (x_==0)
+// FN_V_zerop(x,x_) stellt fest, ob x = 0 ist.
+// Dabei ist x ein Fixnum und x_ = FN_to_V(x).
+  #define FN_V_zerop(x,x_)  (x_==0)
 
-// FN_L_minusp(x,x_) stellt fest, ob x < 0 ist.
-// Dabei ist x ein Fixnum und x_ = FN_to_L(x).
-  #define FN_L_minusp(x,x_)  (x_<0)
+// FN_V_minusp(x,x_) stellt fest, ob x < 0 ist.
+// Dabei ist x ein Fixnum und x_ = FN_to_V(x).
+  #define FN_V_minusp(x,x_)  (x_<0)
 
 #ifdef intQsize
 
@@ -211,11 +212,42 @@ inline sint64 FN_to_Q (const cl_I& x)
   }
 #endif
 
+#ifdef intQsize
+
+// Wandelt Quadword in Integer um.
+// Q_to_I(wert)
+// > wert: Wert des Integers, ein signed 64-Bit-Integer.
+// < ergebnis: Integer mit diesem Wert.
+  extern cl_private_thing cl_I_constructor_from_Q (sint64 wert);
+  inline const cl_I Q_to_I (sint64 wert)
+  {
+       return cl_I(cl_I_constructor_from_Q(wert));
+  }
+
+// Wandelt Unsigned Quadword in Integer >=0 um.
+// UQ_to_I(wert)
+// > wert: Wert des Integers, ein unsigned 64-Bit-Integer.
+// < ergebnis: Integer mit diesem Wert.
+  extern cl_private_thing cl_I_constructor_from_UQ (uint64 wert);
+  inline const cl_I UQ_to_I (uint64 wert)
+  {
+       return cl_I(cl_I_constructor_from_UQ(wert));
+  }
+
+#endif
+
 // Wandelt Doppel-Longword in Integer um.
 // L2_to_I(wert_hi,wert_lo)
 // > wert_hi|wert_lo: Wert des Integers, ein signed 64-Bit-Integer.
 // < ergebnis: Integer mit diesem Wert.
+#if (cl_word_size==64)
+  inline cl_private_thing cl_I_constructor_from_L2 (sint32 wert_hi, uint32 wert_lo)
+  {
+       return cl_I_constructor_from_Q(((sint64)wert_hi<<32) | (sint64)wert_lo);
+  }
+#else
   extern cl_private_thing cl_I_constructor_from_L2 (sint32 wert_hi, uint32 wert_lo);
+#endif
   inline const cl_I L2_to_I (sint32 wert_hi, uint32 wert_lo)
   {
        return cl_I(cl_I_constructor_from_L2(wert_hi,wert_lo));
@@ -225,34 +257,37 @@ inline sint64 FN_to_Q (const cl_I& x)
 // UL2_to_I(wert_hi,wert_lo)
 // > wert_hi|wert_lo: Wert des Integers, ein unsigned 64-Bit-Integer.
 // < ergebnis: Integer mit diesem Wert.
+#if (cl_word_size==64)
+  inline cl_private_thing cl_I_constructor_from_UL2 (uint32 wert_hi, uint32 wert_lo)
+  {
+       return cl_I_constructor_from_UQ(((uint64)wert_hi<<32) | (uint64)wert_lo);
+  }
+#else
   extern cl_private_thing cl_I_constructor_from_UL2 (uint32 wert_hi, uint32 wert_lo);
+#endif
   inline const cl_I UL2_to_I (uint32 wert_hi, uint32 wert_lo)
   {
        return cl_I(cl_I_constructor_from_UL2(wert_hi,wert_lo));
   }
 
-#ifdef intQsize
-
-// Wandelt Quadword in Integer um.
-// Q_to_I(wert)
-// > wert: Wert des Integers, ein signed 64-Bit-Integer.
+// Wandelt sintV in Integer um.
+// V_to_I(wert)
+// > wert: Wert des Integers, ein sintV.
 // < ergebnis: Integer mit diesem Wert.
-  extern cl_private_thing cl_I_constructor_from_Q (sint64 wert);
-  inline const cl_I Q_to_I (sint64 wert)
-  {
-       return cl_I(cl_I_constructor_from_Q(wert));
-  }
+#if (intVsize<=32)
+  #define V_to_I(wert)  L_to_I(wert)
+#else
+  #define V_to_I(wert)  Q_to_I(wert)
+#endif
 
-// Wandelt Unsigned Quadword in Integer >=0 um.
-// UQ_to_I(wert)
-// > wert: Wert des Integers, ein unsigned 64-Bit-Integer.
+// Wandelt uintV in Integer >=0 um.
+// UV_to_I(wert)
+// > wert: Wert des Integers, ein uintV.
 // < ergebnis: Integer mit diesem Wert.
-  extern cl_private_thing cl_I_constructor_from_UQ (uint64 wert);
-  inline const cl_I UQ_to_I (uint64 wert)
-  {
-       return cl_I(cl_I_constructor_from_UQ(wert));
-  }
-
+#if (intVsize<=32)
+  #define UV_to_I(wert)  UL_to_I(wert)
+#else
+  #define UV_to_I(wert)  UQ_to_I(wert)
 #endif
 
 // Wandelt uintD in Integer >=0 um.
@@ -283,22 +318,30 @@ inline const cl_I minus (uintL x, uintL y)
 
 #if (intDsize<=32)
 
-// Holt die nächsten pFN_maxlength Digits in ein uint32.
-inline uint32 pFN_maxlength_digits_at (const uintD* ptr)
+// Holt die nächsten pFN_maxlength Digits in ein uintV.
+inline uintV pFN_maxlength_digits_at (const uintD* ptr)
 {
 #if (pFN_maxlength==1)
-       return (uint32)lspref(ptr,0);
+       return (uintV)lspref(ptr,0);
 #elif (pFN_maxlength==2)
-       return ((uint32)lspref(ptr,1)<<intDsize) | (uint32)lspref(ptr,0);
+       return ((uintV)lspref(ptr,1)<<intDsize) | (uintV)lspref(ptr,0);
 #elif (pFN_maxlength==3)
-       return ((((uint32)lspref(ptr,2)<<intDsize) | (uint32)lspref(ptr,1))<<intDsize) | (uint32)lspref(ptr,0);
+       return ((((uintV)lspref(ptr,2)<<intDsize) | (uintV)lspref(ptr,1))<<intDsize) | (uintV)lspref(ptr,0);
 #elif (pFN_maxlength==4)
-       return ((((((uint32)lspref(ptr,3)<<intDsize) | (uint32)lspref(ptr,2))<<intDsize) | (uint32)lspref(ptr,1))<<intDsize) | (uint32)lspref(ptr,0);
+       return ((((((uintV)lspref(ptr,3)<<intDsize) | (uintV)lspref(ptr,2))<<intDsize) | (uintV)lspref(ptr,1))<<intDsize) | (uintV)lspref(ptr,0);
+#elif (pFN_maxlength==5)
+       return ((((((((uintV)lspref(ptr,4)<<intDsize) | (uintV)lspref(ptr,3))<<intDsize) | (uintV)lspref(ptr,2))<<intDsize) | (uintV)lspref(ptr,1))<<intDsize) | (uintV)lspref(ptr,0);
+#elif (pFN_maxlength==6)
+       return ((((((((((uintV)lspref(ptr,5)<<intDsize) | (uintV)lspref(ptr,4))<<intDsize) | (uintV)lspref(ptr,3))<<intDsize) | (uintV)lspref(ptr,2))<<intDsize) | (uintV)lspref(ptr,1))<<intDsize) | (uintV)lspref(ptr,0);
+#elif (pFN_maxlength==7)
+       return ((((((((((((uintV)lspref(ptr,6)<<intDsize) | (uintV)lspref(ptr,5))<<intDsize) | (uintV)lspref(ptr,4))<<intDsize) | (uintV)lspref(ptr,3))<<intDsize) | (uintV)lspref(ptr,2))<<intDsize) | (uintV)lspref(ptr,1))<<intDsize) | (uintV)lspref(ptr,0);
+#elif (pFN_maxlength==8)
+       return ((((((((((((((uintV)lspref(ptr,7)<<intDsize) | (uintV)lspref(ptr,6))<<intDsize) | (uintV)lspref(ptr,5))<<intDsize) | (uintV)lspref(ptr,4))<<intDsize) | (uintV)lspref(ptr,3))<<intDsize) | (uintV)lspref(ptr,2))<<intDsize) | (uintV)lspref(ptr,1))<<intDsize) | (uintV)lspref(ptr,0);
 #endif
 }
 
-// Schreibt ein uint32 in die nächsten pFN_maxlength Digits.
-inline void set_pFN_maxlength_digits_at (uintD* ptr, uint32 wert)
+// Schreibt ein uintV in die nächsten pFN_maxlength Digits.
+inline void set_pFN_maxlength_digits_at (uintD* ptr, uintV wert)
 {
 #if (pFN_maxlength==1)
        lspref(ptr,0) = (uintD)wert;
@@ -314,6 +357,36 @@ inline void set_pFN_maxlength_digits_at (uintD* ptr, uint32 wert)
        lspref(ptr,2) = (uintD)(wert>>(2*intDsize));
        lspref(ptr,1) = (uintD)(wert>>intDsize);
        lspref(ptr,0) = (uintD)(wert);
+#elif (pFN_maxlength==5)
+       lspref(ptr,4) = (uintD)(wert>>(4*intDsize));
+       lspref(ptr,3) = (uintD)(wert>>(3*intDsize));
+       lspref(ptr,2) = (uintD)(wert>>(2*intDsize));
+       lspref(ptr,1) = (uintD)(wert>>intDsize);
+       lspref(ptr,0) = (uintD)(wert);
+#elif (pFN_maxlength==6)
+       lspref(ptr,5) = (uintD)(wert>>(5*intDsize));
+       lspref(ptr,4) = (uintD)(wert>>(4*intDsize));
+       lspref(ptr,3) = (uintD)(wert>>(3*intDsize));
+       lspref(ptr,2) = (uintD)(wert>>(2*intDsize));
+       lspref(ptr,1) = (uintD)(wert>>intDsize);
+       lspref(ptr,0) = (uintD)(wert);
+#elif (pFN_maxlength==7)
+       lspref(ptr,6) = (uintD)(wert>>(6*intDsize));
+       lspref(ptr,5) = (uintD)(wert>>(5*intDsize));
+       lspref(ptr,4) = (uintD)(wert>>(4*intDsize));
+       lspref(ptr,3) = (uintD)(wert>>(3*intDsize));
+       lspref(ptr,2) = (uintD)(wert>>(2*intDsize));
+       lspref(ptr,1) = (uintD)(wert>>intDsize);
+       lspref(ptr,0) = (uintD)(wert);
+#elif (pFN_maxlength==8)
+       lspref(ptr,7) = (uintD)(wert>>(7*intDsize));
+       lspref(ptr,6) = (uintD)(wert>>(6*intDsize));
+       lspref(ptr,5) = (uintD)(wert>>(5*intDsize));
+       lspref(ptr,4) = (uintD)(wert>>(4*intDsize));
+       lspref(ptr,3) = (uintD)(wert>>(3*intDsize));
+       lspref(ptr,2) = (uintD)(wert>>(2*intDsize));
+       lspref(ptr,1) = (uintD)(wert>>intDsize);
+       lspref(ptr,0) = (uintD)(wert);
 #endif
 }
 
@@ -589,14 +662,14 @@ inline sintD FN_MSD (cl_uint word)
 // > MSBptr/len/..: Ziffernfolge, bestehend aus Punkten (werden überlesen)
 //     und Ziffern/Buchstaben mit Wert < base.
 // < ergebnis: der dargestellte Integer >=0
-  extern const cl_I digits_to_I (const char * MSBptr, uintL len, uintD base);
+  extern const cl_I digits_to_I (const char * MSBptr, uintC len, uintD base);
 
 
 // Hilfsfunktion zur Ausgabe von Integers
 
 // cl_digits_need(len,base) liefert eine obere Abschätzung für die Anzahl der
 // Ziffern im Stellenwertsystem der Basis base, die x >= 0 braucht.
-  extern uintL cl_digits_need (const cl_I& x, uintL base);
+  extern uintC cl_digits_need (const cl_I& x, uintL base);
 
 // Wandelt ein Integer in ein Stellensystem um.
 // I_to_digits(x,base, &ergebnis);
@@ -604,7 +677,7 @@ inline sintD FN_MSD (cl_uint word)
 // > base: Stellensystem-Basis, 2 <= base <= 36.
 // > ergebnis.LSBptr: darunter ist mindestens digits_need(len) Bytes Platz
 // < ergebnis: fertige Folge MSBptr/len/LSBptr von Ziffern
-  typedef struct { uintB* MSBptr; uintL len; uintB* LSBptr; } cl_digits;
+  typedef struct { uintB* MSBptr; uintC len; uintB* LSBptr; } cl_digits;
   extern void I_to_digits (const cl_I& x, uintD base, cl_digits* erg);
 
 
@@ -656,4 +729,6 @@ inline cl_boolean minusp (const cl_BN& x)
 inline cl_boolean zerop (const cl_BN& x)
        { unused x; return cl_false; }
 
+}  // namespace cln
+
 #endif /* _CL_I_H */