* Cleaned up memory error signalling.
[bpt/guile.git] / libguile / numbers.c
index 0b08131..6d3e808 100644 (file)
@@ -71,19 +71,13 @@ static SCM scm_divbigint (SCM x, long z, int sgn, int mode);
 
 
 #if (SCM_DEBUG_DEPRECATED == 1)  /* not defined in header yet? */
-/* SCM_FIXABLE is non-0 if its long argument can be encoded in an SCM_INUM.
- */
-#define SCM_POSFIXABLE(n) ((n) <= SCM_MOST_POSITIVE_FIXNUM)
-#define SCM_NEGFIXABLE(n) ((n) >= SCM_MOST_NEGATIVE_FIXNUM)
-#define SCM_UNEGFIXABLE(n) ((n) <= -SCM_MOST_NEGATIVE_FIXNUM)
-#define SCM_FIXABLE(n) (SCM_POSFIXABLE(n) && SCM_NEGFIXABLE(n))
-
 
 /* SCM_FLOBUFLEN is the maximum number of characters neccessary for the
  * printed or scm_string representation of an inexact number.
  */
 #define SCM_FLOBUFLEN (10+2*(sizeof(double)/sizeof(char)*SCM_CHAR_BIT*3+9)/10)
-#endif
+
+#endif  /* SCM_DEBUG_DEPRECATED == 1 */
 
 
 /* IS_INF tests its floating point number for infiniteness
@@ -265,11 +259,7 @@ scm_remainder (SCM x, SCM y)
       if (yy == 0) {
        scm_num_overflow (s_remainder);
       } else {
-#if (__TURBOC__ == 1)
-       long z = SCM_INUM (x) % (yy < 0 ? -yy : yy);
-#else
        long z = SCM_INUM (x) % yy;
-#endif
        return SCM_MAKINUM (z);
       }
     } else if (SCM_BIGP (y)) {
@@ -310,11 +300,7 @@ scm_modulo (SCM x, SCM y)
       if (yy == 0) {
        scm_num_overflow (s_modulo);
       } else {
-#if (__TURBOC__ == 1)
-       long z = ((yy < 0) ? -xx : xx) % yy;
-#else
        long z = xx % yy;
-#endif
        return SCM_MAKINUM (((yy < 0) ? (z > 0) : (z < 0)) ? z + yy : z);
       }
     } else if (SCM_BIGP (y)) {
@@ -1030,34 +1016,42 @@ SCM_DEFINE (scm_logbit_p, "logbit?", 2, 0, 0,
            "@end example")
 #define FUNC_NAME s_scm_logbit_p
 {
-  SCM_ASSERT(SCM_INUMP(index) && SCM_INUM(index) >= 0, index, SCM_ARG1, FUNC_NAME);
-#ifdef SCM_BIGDIG
-  if SCM_NINUMP(j) {
-    SCM_ASSERT(SCM_BIGP (j), j, SCM_ARG2, FUNC_NAME);
-    if (SCM_NUMDIGS(j) * SCM_BITSPERDIG < SCM_INUM(index)) return SCM_BOOL_F;
-    else if SCM_BIGSIGN(j) {
+  unsigned long int iindex;
+
+  SCM_VALIDATE_INUM_MIN (SCM_ARG1, index, 0);
+  iindex = (unsigned long int) SCM_INUM (index);
+
+  if (SCM_INUMP (j)) {
+    return SCM_BOOL ((1L << iindex) & SCM_INUM (j));
+  } else if (SCM_BIGP (j)) {
+    if (SCM_NUMDIGS (j) * SCM_BITSPERDIG < iindex) {
+      return SCM_BOOL_F;
+    } else if (SCM_BIGSIGN (j)) {
       long num = -1;
       scm_sizet i = 0;
-      SCM_BIGDIG *x = SCM_BDIGITS(j);
-      scm_sizet nx = SCM_INUM(index)/SCM_BITSPERDIG;
-      while (!0) {
+      SCM_BIGDIG * x = SCM_BDIGITS (j);
+      scm_sizet nx = iindex / SCM_BITSPERDIG;
+      while (1) {
        num += x[i];
-       if (nx==i++)
-         return ((1L << (SCM_INUM(index)%SCM_BITSPERDIG)) & num) ? SCM_BOOL_F : SCM_BOOL_T;
-       if (num < 0) num = -1;
-       else num = 0;
+       if (nx == i++) {
+         return SCM_BOOL (((1L << (iindex % SCM_BITSPERDIG)) & num) == 0);
+       } else if (num < 0) {
+         num = -1;
+       } else {
+         num = 0;
+       }
       }
+    } else {
+      return SCM_BOOL (SCM_BDIGITS (j) [iindex / SCM_BITSPERDIG]
+                      & (1L << (iindex % SCM_BITSPERDIG)));
     }
-    else return (SCM_BDIGITS(j)[SCM_INUM(index)/SCM_BITSPERDIG] &
-                (1L << (SCM_INUM(index)%SCM_BITSPERDIG))) ? SCM_BOOL_T : SCM_BOOL_F;
+  } else {
+    SCM_WRONG_TYPE_ARG (SCM_ARG2, j);
   }
-#else
-  SCM_ASSERT(SCM_INUMP(j), j, SCM_ARG2, FUNC_NAME);
-#endif
-  return ((1L << SCM_INUM(index)) & SCM_INUM(j)) ? SCM_BOOL_T : SCM_BOOL_F;
 }
 #undef FUNC_NAME
 
+
 SCM_DEFINE (scm_lognot, "lognot", 1, 0, 0, 
             (SCM n),
            "Returns the integer which is the 2s-complement of the integer argument.\n\n"
@@ -1172,7 +1166,7 @@ SCM_DEFINE (scm_ash, "ash", 2, 0, 0,
 }
 #undef FUNC_NAME
 
-/* GJB:FIXME: do not use SCMs as integers! */
+
 SCM_DEFINE (scm_bit_extract, "bit-extract", 3, 0, 0,
             (SCM n, SCM start, SCM end),
            "Returns the integer composed of the @var{start} (inclusive) through\n"
@@ -1188,24 +1182,25 @@ SCM_DEFINE (scm_bit_extract, "bit-extract", 3, 0, 0,
 #define FUNC_NAME s_scm_bit_extract
 {
   int istart, iend;
-  SCM_VALIDATE_INUM (1,n);
   SCM_VALIDATE_INUM_MIN_COPY (2,start,0,istart);
   SCM_VALIDATE_INUM_MIN_COPY (3, end, 0, iend);
   SCM_ASSERT_RANGE (3, end, (iend >= istart));
-#ifdef SCM_BIGDIG
-  if (SCM_NINUMP (n))
-    return
-      scm_logand (scm_difference (scm_integer_expt (SCM_MAKINUM (2),
-                                                   SCM_MAKINUM (iend - istart)),
-                                 SCM_MAKINUM (1L)),
-                 scm_ash (n, SCM_MAKINUM (-istart)));
-#else
-  SCM_VALIDATE_INUM (1,n);
-#endif
-  return SCM_MAKINUM ((SCM_INUM (n) >> istart) & ((1L << (iend - istart)) - 1));
+
+  if (SCM_INUMP (n)) {
+    return SCM_MAKINUM ((SCM_INUM (n) >> istart) & ((1L << (iend - istart)) - 1));
+  } else if (SCM_BIGP (n)) {
+    SCM num1 = SCM_MAKINUM (1L);
+    SCM num2 = SCM_MAKINUM (2L);
+    SCM bits = SCM_MAKINUM (iend - istart);
+    SCM mask  = scm_difference (scm_integer_expt (num2, bits), num1);
+    return scm_logand (mask, scm_ash (n, SCM_MAKINUM (-istart)));
+  } else {
+    SCM_WRONG_TYPE_ARG (SCM_ARG1, n);
+  }
 }
 #undef FUNC_NAME
 
+
 static const char scm_logtab[] = {
   0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4
 };
@@ -1227,30 +1222,35 @@ SCM_DEFINE (scm_logcount, "logcount", 1, 0, 0,
            "@end lisp")
 #define FUNC_NAME s_scm_logcount
 {
-  register unsigned long c = 0;
-  register long nn;
-#ifdef SCM_BIGDIG
-  if (SCM_NINUMP (n))
-    {
-      scm_sizet i;
-      SCM_BIGDIG *ds, d;
-      SCM_VALIDATE_BIGINT (1,n);
-      if (SCM_BIGSIGN (n))
-       return scm_logcount (scm_difference (SCM_MAKINUM (-1L), n));
-      ds = SCM_BDIGITS (n);
-      for (i = SCM_NUMDIGS (n); i--;)
-       for (d = ds[i]; d; d >>= 4)
+  if (SCM_INUMP (n)) {
+    unsigned long int c = 0;
+    long int nn = SCM_INUM (n);
+    if (nn < 0) {
+      nn = -1 - nn;
+    };
+    while (nn) {
+      c += scm_logtab[15 & nn];
+      nn >>= 4;
+    };
+    return SCM_MAKINUM (c);
+  } else if (SCM_BIGP (n)) {
+    if (SCM_BIGSIGN (n)) {
+      return scm_logcount (scm_difference (SCM_MAKINUM (-1L), n));
+    } else {
+      unsigned long int c = 0;
+      scm_sizet i = SCM_NUMDIGS (n);
+      SCM_BIGDIG * ds = SCM_BDIGITS (n);
+      while (i--) {
+       SCM_BIGDIG d;
+       for (d = ds[i]; d; d >>= 4) {
          c += scm_logtab[15 & d];
+       }
+      }
       return SCM_MAKINUM (c);
     }
-#else
-  SCM_VALIDATE_INUM (1,n);
-#endif
-  if ((nn = SCM_INUM (n)) < 0)
-    nn = -1 - nn;
-  for (; nn; nn >>= 4)
-    c += scm_logtab[15 & nn];
-  return SCM_MAKINUM (c);
+  } else {
+    SCM_WRONG_TYPE_ARG (SCM_ARG1, n);
+  }
 }
 #undef FUNC_NAME
 
@@ -1273,36 +1273,38 @@ SCM_DEFINE (scm_integer_length, "integer-length", 1, 0, 0,
            "@end lisp")
 #define FUNC_NAME s_scm_integer_length
 {
-  register unsigned long c = 0;
-  register long nn;
-  unsigned int l = 4;
-#ifdef SCM_BIGDIG
-  if (SCM_NINUMP (n))
-    {
-      SCM_BIGDIG *ds, d;
-      SCM_VALIDATE_BIGINT (1,n);
-      if (SCM_BIGSIGN (n))
-       return scm_integer_length (scm_difference (SCM_MAKINUM (-1L), n));
-      ds = SCM_BDIGITS (n);
-      d = ds[c = SCM_NUMDIGS (n) - 1];
-      for (c *= SCM_BITSPERDIG; d; d >>= 4)
-       {
-         c += 4;
-         l = scm_ilentab[15 & d];
-       }
-      return SCM_MAKINUM (c - 4 + l);
-    }
-#else
-  SCM_VALIDATE_INUM (1,n);
-#endif
-  if ((nn = SCM_INUM (n)) < 0)
-    nn = -1 - nn;
-  for (; nn; nn >>= 4)
-    {
+  if (SCM_INUMP (n)) {
+    unsigned long int c = 0;
+    unsigned int l = 4;
+    long int nn = SCM_INUM (n);
+    if (nn < 0) {
+      nn = -1 - nn;
+    };
+    while (nn) {
       c += 4;
-      l = scm_ilentab[15 & nn];
+      l = scm_ilentab [15 & nn];
+      nn >>= 4;
+    };
+    return SCM_MAKINUM (c - 4 + l);
+  } else if (SCM_BIGP (n)) {
+    if (SCM_BIGSIGN (n)) {
+      return scm_integer_length (scm_difference (SCM_MAKINUM (-1L), n));
+    } else {
+      unsigned long int digs = SCM_NUMDIGS (n) - 1;
+      unsigned long int c = digs * SCM_BITSPERDIG;
+      unsigned int l = 4;
+      SCM_BIGDIG * ds = SCM_BDIGITS (n);
+      SCM_BIGDIG d = ds [digs];
+      while (d) {
+       c += 4;
+       l = scm_ilentab [15 & d];
+       d >>= 4;
+      };
+      return SCM_MAKINUM (c - 4 + l);
     }
-  return SCM_MAKINUM (c - 4 + l);
+  } else {
+    SCM_WRONG_TYPE_ARG (SCM_ARG1, n);
+  }
 }
 #undef FUNC_NAME
 
@@ -1317,7 +1319,7 @@ scm_mkbig (scm_sizet nlen, int sign)
   /* Cast to long int to avoid signed/unsigned comparison warnings.  */
   if ((( ((long int) nlen) << SCM_BIGSIZEFIELD) >> SCM_BIGSIZEFIELD)
       != (long int) nlen)
-    scm_wta (SCM_MAKINUM (nlen), (char *) SCM_NALLOC, s_bignum);
+    scm_memory_error (s_bignum);
   
   SCM_NEWCELL (v);
   SCM_DEFER_INTS;
@@ -1341,7 +1343,7 @@ scm_big2inum (SCM b, scm_sizet l)
       if (SCM_POSFIXABLE (num))
        return SCM_MAKINUM (num);
     }
-  else if (SCM_UNEGFIXABLE (num))
+  else if (num <= -SCM_MOST_NEGATIVE_FIXNUM)
     return SCM_MAKINUM (-num);
   return b;
 }
@@ -1354,7 +1356,7 @@ scm_adjbig (SCM b, scm_sizet nlen)
 {
   scm_sizet nsiz = nlen;
   if (((nsiz << SCM_BIGSIZEFIELD) >> SCM_BIGSIZEFIELD) != nlen)
-    scm_wta (scm_ulong2num (nsiz), (char *) SCM_NALLOC, s_adjbig);
+    scm_memory_error (s_adjbig);
 
   SCM_DEFER_INTS;
   {
@@ -1462,7 +1464,7 @@ scm_long_long2big (long_long n)
     }
   return ans;
 }
-#endif
+#endif /* HAVE_LONG_LONGS */
 
 
 SCM
@@ -4168,7 +4170,7 @@ scm_long_long2num (long_long sl)
     }
 }
 
-#endif
+#endif /* HAVE_LONG_LONGS */
 
 
 SCM
@@ -4287,7 +4289,7 @@ scm_num2long_long (SCM num, char *pos, const char *s_caller)
   }
 }
 
-#endif
+#endif /* HAVE_LONG_LONGS */
 
 
 unsigned long