]> gitweb.factorcode.org Git - factor.git/blob - vm/bignumint.hpp
VM: use better abstractions for tagged pointers, eliminate get()/set() stuff, clean...
[factor.git] / vm / bignumint.hpp
1 /* -*-C-*-
2
3 $Id: s48_bignumint.h,v 1.14 2005/12/21 02:36:52 spestov Exp $
4
5 Copyright (c) 1989-1992 Massachusetts Institute of Technology
6
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.
13
14 1. Any copy made of this software must include this copyright notice
15 in full.
16
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.
21
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.
25
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.
29
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
34 MIT in each case. */
35
36 /* Internal Interface to Bignum Code */
37 #undef BIGNUM_ZERO_P
38 #undef BIGNUM_NEGATIVE_P
39
40 /* The memory model is based on the following definitions, and on the
41    definition of the type `bignum_type'.  The only other special
42    definition is `CHAR_BIT', which is defined in the Ansi C header
43    file "limits.h". */
44
45 typedef F_FIXNUM bignum_digit_type;
46 typedef F_FIXNUM bignum_length_type;
47
48 /* BIGNUM_TO_POINTER casts a bignum object to a digit array pointer. */
49 #define BIGNUM_TO_POINTER(bignum) ((bignum_digit_type *)(bignum + 1))
50
51 /* BIGNUM_EXCEPTION is invoked to handle assertion violations. */
52 #define BIGNUM_EXCEPTION abort
53
54
55 #define BIGNUM_DIGIT_LENGTH (((sizeof (bignum_digit_type)) * CHAR_BIT) - 2)
56 #define BIGNUM_HALF_DIGIT_LENGTH (BIGNUM_DIGIT_LENGTH / 2)
57 #define BIGNUM_RADIX (bignum_digit_type)(((CELL) 1) << BIGNUM_DIGIT_LENGTH)
58 #define BIGNUM_RADIX_ROOT (((bignum_digit_type) 1) << BIGNUM_HALF_DIGIT_LENGTH)
59 #define BIGNUM_DIGIT_MASK        (BIGNUM_RADIX - 1)
60 #define BIGNUM_HALF_DIGIT_MASK   (BIGNUM_RADIX_ROOT - 1)
61
62 #define BIGNUM_START_PTR(bignum)                                        \
63   ((BIGNUM_TO_POINTER (bignum)) + 1)
64
65 #define BIGNUM_LENGTH(bignum) (untag_fixnum((bignum)->capacity) - 1)
66
67 #define BIGNUM_NEGATIVE_P(bignum) (bignum->data()[0] != 0)
68 #define BIGNUM_SET_NEGATIVE_P(bignum,neg) (bignum->data()[0] = neg)
69
70 #define BIGNUM_ZERO_P(bignum)                                           \
71   ((BIGNUM_LENGTH (bignum)) == 0)
72
73 #define BIGNUM_REF(bignum, index)                                       \
74   (* ((BIGNUM_START_PTR (bignum)) + (index)))
75
76 /* These definitions are here to facilitate caching of the constants
77    0, 1, and -1. */
78 #define BIGNUM_ZERO() untag<F_BIGNUM>(bignum_zero)
79 #define BIGNUM_ONE(neg_p) \
80    untag<F_BIGNUM>(neg_p ? bignum_neg_one : bignum_pos_one)
81
82 #define HD_LOW(digit) ((digit) & BIGNUM_HALF_DIGIT_MASK)
83 #define HD_HIGH(digit) ((digit) >> BIGNUM_HALF_DIGIT_LENGTH)
84 #define HD_CONS(high, low) (((high) << BIGNUM_HALF_DIGIT_LENGTH) | (low))
85
86 #define BIGNUM_BITS_TO_DIGITS(n)                                        \
87   (((n) + (BIGNUM_DIGIT_LENGTH - 1)) / BIGNUM_DIGIT_LENGTH)
88
89 #define BIGNUM_DIGITS_FOR(type) \
90   (BIGNUM_BITS_TO_DIGITS ((sizeof (type)) * CHAR_BIT))
91
92 #ifndef BIGNUM_DISABLE_ASSERTION_CHECKS
93
94 #define BIGNUM_ASSERT(expression)                                       \
95 {                                                                       \
96   if (! (expression))                                                   \
97     BIGNUM_EXCEPTION ();                                                \
98 }
99
100 #endif /* not BIGNUM_DISABLE_ASSERTION_CHECKS */