From 58b0dd07d7214f9cdbc7d1f22389dd6c35191907 Mon Sep 17 00:00:00 2001 From: "Guillermo J. Rozas" Date: Fri, 18 Sep 1992 16:52:34 +0000 Subject: [PATCH] Add ulong_to_bignum and bignum_to_ulong. --- v7/src/microcode/bignum.c | 45 ++++++++++++++++++++++++++++++++++++++- v7/src/microcode/bignum.h | 4 +++- 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/v7/src/microcode/bignum.c b/v7/src/microcode/bignum.c index c60190ff2..47c353c50 100644 --- a/v7/src/microcode/bignum.c +++ b/v7/src/microcode/bignum.c @@ -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 */ #define DTB_WRITE_DIGIT(factor) \ diff --git a/v7/src/microcode/bignum.h b/v7/src/microcode/bignum.h index 6ab939c07..8ae9040e6 100644 --- a/v7/src/microcode/bignum.h +++ b/v7/src/microcode/bignum.h @@ -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)); -- 2.25.1