]> gitweb.factorcode.org Git - factor.git/blob - vm/bignumint.hpp
xmode.rules: removing test no longer needed
[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 namespace factor {
37
38 // Internal Interface to Bignum Code
39 #undef BIGNUM_ZERO_P
40 #undef BIGNUM_NEGATIVE_P
41
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
45 // file "limits.h".
46
47 typedef fixnum bignum_digit_type;
48 typedef fixnum bignum_length_type;
49
50 #ifndef _WIN64
51 #ifdef FACTOR_64
52 typedef __int128_t bignum_twodigit_type;
53 #else
54 typedef int64_t bignum_twodigit_type;
55 #endif
56 #endif
57
58 // BIGNUM_TO_POINTER casts a bignum object to a digit array pointer.
59 #define BIGNUM_TO_POINTER(bignum) ((bignum_digit_type*)(bignum->data()))
60
61 // BIGNUM_EXCEPTION is invoked to handle assertion violations.
62 #define BIGNUM_EXCEPTION abort
63
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)
70
71 #define BIGNUM_START_PTR(bignum) ((BIGNUM_TO_POINTER(bignum)) + 1)
72
73 #define BIGNUM_LENGTH(bignum) (untag_fixnum((bignum)->capacity) - 1)
74
75 #define BIGNUM_NEGATIVE_P(bignum) (bignum->data()[0] != 0)
76 #define BIGNUM_SET_NEGATIVE_P(bignum, neg) (bignum->data()[0] = neg)
77
78 #define BIGNUM_ZERO_P(bignum) ((BIGNUM_LENGTH(bignum)) == 0)
79
80 #define BIGNUM_REF(bignum, index) (*((BIGNUM_START_PTR(bignum)) + (index)))
81
82 // These definitions are here to facilitate caching of the constants
83 // 0, 1, and -1.
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])
87
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))
91
92 #define BIGNUM_BITS_TO_DIGITS(n) \
93   (((n) + (BIGNUM_DIGIT_LENGTH - 1)) / BIGNUM_DIGIT_LENGTH)
94
95 #define BIGNUM_DIGITS_FOR(type) \
96   (BIGNUM_BITS_TO_DIGITS((sizeof(type)) * CHAR_BIT))
97
98 #ifndef BIGNUM_DISABLE_ASSERTION_CHECKS
99
100 #define BIGNUM_ASSERT(expression) \
101   {                               \
102     if (!(expression))            \
103       BIGNUM_EXCEPTION();         \
104   }
105
106 #endif // not BIGNUM_DISABLE_ASSERTION_CHECKS
107
108 }