Add ulong_to_bignum and bignum_to_ulong.
authorGuillermo J. Rozas <edu/mit/csail/zurich/gjr>
Fri, 18 Sep 1992 16:52:34 +0000 (16:52 +0000)
committerGuillermo J. Rozas <edu/mit/csail/zurich/gjr>
Fri, 18 Sep 1992 16:52:34 +0000 (16:52 +0000)
v7/src/microcode/bignum.c
v7/src/microcode/bignum.h

index c60190ff2d08ab7c4add3437697b8a9730fd866a..47c353c5003292d69b7150635a37262b5ff7798e 100644 (file)
@@ -1,6 +1,6 @@
 /* -*-C-*-
 
-$Id: bignum.c,v 9.37 1992/08/29 13:34:08 jinx Exp $
+$Id: bignum.c,v 9.38 1992/09/18 16:52:22 jinx Exp $
 
 Copyright (c) 1989-1992 Massachusetts Institute of Technology
 
@@ -495,6 +495,49 @@ DEFUN (bignum_to_long, (bignum), bignum_type bignum)
   }
 }
 
+bignum_type
+DEFUN (ulong_to_bignum, (n), unsigned long n)
+{
+  bignum_digit_type result_digits [BIGNUM_DIGITS_FOR_LONG];
+  fast bignum_digit_type * end_digits = result_digits;
+  /* Special cases win when these small constants are cached. */
+  if (n == 0) return (BIGNUM_ZERO ());
+  if (n == 1) return (BIGNUM_ONE (0));
+  {
+    fast unsigned long accumulator = n;
+    do
+      {
+       (*end_digits++) = (accumulator & BIGNUM_DIGIT_MASK);
+       accumulator >>= BIGNUM_DIGIT_LENGTH;
+      }
+    while (accumulator != 0);
+  }
+  {
+    bignum_type result =
+      (bignum_allocate ((end_digits - result_digits), 0));
+    fast bignum_digit_type * scan_digits = result_digits;
+    fast bignum_digit_type * scan_result = (BIGNUM_START_PTR (result));
+    while (scan_digits < end_digits)
+      (*scan_result++) = (*scan_digits++);
+    return (result);
+  }
+}
+
+unsigned long
+DEFUN (bignum_to_ulong, (bignum), bignum_type bignum)
+{
+  if (BIGNUM_ZERO_P (bignum))
+    return (0);
+  {
+    fast unsigned long accumulator = 0;
+    fast bignum_digit_type * start = (BIGNUM_START_PTR (bignum));
+    fast bignum_digit_type * scan = (start + (BIGNUM_LENGTH (bignum)));
+    while (start < scan)
+      accumulator = ((accumulator << BIGNUM_DIGIT_LENGTH) + (*--scan));
+    return (accumulator);
+  }
+}
+
 #endif /* not BIGNUM_NO_ULONG */
 \f
 #define DTB_WRITE_DIGIT(factor)                                                \
index 6ab939c07962960fab5393361f8ccfdc0f670a8f..8ae9040e6501e9ee4b01a14f0336258e05303420 100644 (file)
@@ -1,6 +1,6 @@
 /* -*-C-*-
 
-$Header: /Users/cph/tmp/foo/mit-scheme/mit-scheme/v7/src/microcode/bignum.h,v 9.27 1992/02/10 13:16:02 jinx Exp $
+$Id: bignum.h,v 9.28 1992/09/18 16:52:34 jinx Exp $
 
 Copyright (c) 1989-1992 Massachusetts Institute of Technology
 
@@ -78,7 +78,9 @@ extern bignum_type EXFUN (bignum_quotient, (bignum_type, bignum_type));
 extern bignum_type EXFUN (bignum_remainder, (bignum_type, bignum_type));
 #ifndef BIGNUM_NO_ULONG
 extern bignum_type EXFUN (long_to_bignum, (long));
+extern bignum_type EXFUN (ulong_to_bignum, (unsigned long));
 extern long EXFUN (bignum_to_long, (bignum_type));
+extern unsigned long EXFUN (bignum_to_ulong, (bignum_type));
 #endif /* not BIGNUM_NO_ULONG */
 extern bignum_type EXFUN (double_to_bignum, (double));
 extern double EXFUN (bignum_to_double, (bignum_type));