3 // $Id: s48_bignumint.h,v 1.14 2005/12/21 02:36:52 spestov Exp $
5 // Copyright (c) 1989-1992 Massachusetts Institute of Technology
7 // This material was developed by the Scheme project at the Massachusetts
8 // Institute of Technology, Department of Electrical Engineering and
9 // Computer Science. Permission to copy and modify this software, to
10 // redistribute either the original software or a modified version, and
11 // to use this software for any purpose is granted, subject to the
12 // following restrictions and understandings.
14 // 1. Any copy made of this software must include this copyright notice
17 // 2. Users of this software agree to make their best efforts (a) to
18 // return to the MIT Scheme project any improvements or extensions that
19 // they make, so that these may be included in future releases; and (b)
20 // to inform MIT of noteworthy uses of this software.
22 // 3. All materials developed as a consequence of the use of this
23 // software shall duly acknowledge such use, in accordance with the usual
24 // standards of acknowledging credit in academic research.
26 // 4. MIT has made no warrantee or representation that the operation of
27 // this software will be error-free, and MIT is under no obligation to
28 // provide any services, by way of maintenance, update, or otherwise.
30 // 5. In conjunction with products arising from the use of this material,
31 // there shall be no use of the name of the Massachusetts Institute of
32 // Technology nor of any adaptation thereof in any advertising,
33 // promotional, or sales literature without prior written consent from
38 // Internal Interface to Bignum Code
40 #undef BIGNUM_NEGATIVE_P
42 // The memory model is based on the following definitions, and on the
43 // definition of the type `bignum_type'. The only other special
44 // definition is `CHAR_BIT', which is defined in the Ansi C header
47 typedef fixnum bignum_digit_type;
48 typedef fixnum bignum_length_type;
52 typedef __int128_t bignum_twodigit_type;
54 typedef int64_t bignum_twodigit_type;
58 // BIGNUM_TO_POINTER casts a bignum object to a digit array pointer.
59 #define BIGNUM_TO_POINTER(bignum) ((bignum_digit_type*)(bignum->data()))
61 // BIGNUM_EXCEPTION is invoked to handle assertion violations.
62 #define BIGNUM_EXCEPTION abort
64 #define BIGNUM_DIGIT_LENGTH (((sizeof(bignum_digit_type)) * CHAR_BIT) - 2)
65 #define BIGNUM_HALF_DIGIT_LENGTH (BIGNUM_DIGIT_LENGTH / 2)
66 #define BIGNUM_RADIX (bignum_digit_type)(((cell)1) << BIGNUM_DIGIT_LENGTH)
67 #define BIGNUM_RADIX_ROOT (((bignum_digit_type) 1) << BIGNUM_HALF_DIGIT_LENGTH)
68 #define BIGNUM_DIGIT_MASK (BIGNUM_RADIX - 1)
69 #define BIGNUM_HALF_DIGIT_MASK (BIGNUM_RADIX_ROOT - 1)
71 #define BIGNUM_START_PTR(bignum) ((BIGNUM_TO_POINTER(bignum)) + 1)
73 #define BIGNUM_LENGTH(bignum) (untag_fixnum((bignum)->capacity) - 1)
75 #define BIGNUM_NEGATIVE_P(bignum) (bignum->data()[0] != 0)
76 #define BIGNUM_SET_NEGATIVE_P(bignum, neg) (bignum->data()[0] = neg)
78 #define BIGNUM_ZERO_P(bignum) ((BIGNUM_LENGTH(bignum)) == 0)
80 #define BIGNUM_REF(bignum, index) (*((BIGNUM_START_PTR(bignum)) + (index)))
82 // These definitions are here to facilitate caching of the constants
84 #define BIGNUM_ZERO() untag<bignum>(special_objects[OBJ_BIGNUM_ZERO])
85 #define BIGNUM_ONE(neg_p) untag<bignum>( \
86 special_objects[neg_p ? OBJ_BIGNUM_NEG_ONE : OBJ_BIGNUM_POS_ONE])
88 #define HD_LOW(digit) ((digit) & BIGNUM_HALF_DIGIT_MASK)
89 #define HD_HIGH(digit) ((digit) >> BIGNUM_HALF_DIGIT_LENGTH)
90 #define HD_CONS(high, low) (((high) << BIGNUM_HALF_DIGIT_LENGTH) | (low))
92 #define BIGNUM_BITS_TO_DIGITS(n) \
93 (((n) + (BIGNUM_DIGIT_LENGTH - 1)) / BIGNUM_DIGIT_LENGTH)
95 #define BIGNUM_DIGITS_FOR(type) \
96 (BIGNUM_BITS_TO_DIGITS((sizeof(type)) * CHAR_BIT))
98 #ifndef BIGNUM_DISABLE_ASSERTION_CHECKS
100 #define BIGNUM_ASSERT(expression) \
103 BIGNUM_EXCEPTION(); \
106 #endif // not BIGNUM_DISABLE_ASSERTION_CHECKS