]> gitweb.factorcode.org Git - factor.git/blob - vm/bignumint.hpp
vm: use euclid gcd on win64 until we find a better way to do the 128-bit math.
[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
39 /* Internal Interface to Bignum Code */
40 #undef BIGNUM_ZERO_P
41 #undef BIGNUM_NEGATIVE_P
42
43 /* The memory model is based on the following definitions, and on the
44    definition of the type `bignum_type'.  The only other special
45    definition is `CHAR_BIT', which is defined in the Ansi C header
46    file "limits.h". */
47
48 typedef fixnum bignum_digit_type;
49 typedef fixnum bignum_length_type;
50
51 #ifndef _WIN64
52 #ifdef FACTOR_64
53 typedef __int128_t bignum_twodigit_type;
54 #else
55 typedef s64 bignum_twodigit_type;
56 #endif
57 #endif
58
59 /* BIGNUM_TO_POINTER casts a bignum object to a digit array pointer. */
60 #define BIGNUM_TO_POINTER(bignum) ((bignum_digit_type *)(bignum + 1))
61
62 /* BIGNUM_EXCEPTION is invoked to handle assertion violations. */
63 #define BIGNUM_EXCEPTION abort
64
65 #define BIGNUM_DIGIT_LENGTH (((sizeof (bignum_digit_type)) * CHAR_BIT) - 2)
66 #define BIGNUM_HALF_DIGIT_LENGTH (BIGNUM_DIGIT_LENGTH / 2)
67 #define BIGNUM_RADIX (bignum_digit_type)(((cell) 1) << BIGNUM_DIGIT_LENGTH)
68 #define BIGNUM_RADIX_ROOT (((bignum_digit_type) 1) << BIGNUM_HALF_DIGIT_LENGTH)
69 #define BIGNUM_DIGIT_MASK        (BIGNUM_RADIX - 1)
70 #define BIGNUM_HALF_DIGIT_MASK   (BIGNUM_RADIX_ROOT - 1)
71
72 #define BIGNUM_START_PTR(bignum)                                        \
73   ((BIGNUM_TO_POINTER (bignum)) + 1)
74
75 #define BIGNUM_LENGTH(bignum) (untag_fixnum((bignum)->capacity) - 1)
76
77 #define BIGNUM_NEGATIVE_P(bignum) (bignum->data()[0] != 0)
78 #define BIGNUM_SET_NEGATIVE_P(bignum,neg) (bignum->data()[0] = neg)
79
80 #define BIGNUM_ZERO_P(bignum)                                           \
81   ((BIGNUM_LENGTH (bignum)) == 0)
82
83 #define BIGNUM_REF(bignum, index)                                       \
84   (* ((BIGNUM_START_PTR (bignum)) + (index)))
85
86 /* These definitions are here to facilitate caching of the constants
87    0, 1, and -1. */
88 #define BIGNUM_ZERO() untag<bignum>(bignum_zero)
89 #define BIGNUM_ONE(neg_p) \
90    untag<bignum>(neg_p ? bignum_neg_one : bignum_pos_one)
91
92 #define HD_LOW(digit) ((digit) & BIGNUM_HALF_DIGIT_MASK)
93 #define HD_HIGH(digit) ((digit) >> BIGNUM_HALF_DIGIT_LENGTH)
94 #define HD_CONS(high, low) (((high) << BIGNUM_HALF_DIGIT_LENGTH) | (low))
95
96 #define BIGNUM_BITS_TO_DIGITS(n)                                        \
97   (((n) + (BIGNUM_DIGIT_LENGTH - 1)) / BIGNUM_DIGIT_LENGTH)
98
99 #define BIGNUM_DIGITS_FOR(type) \
100   (BIGNUM_BITS_TO_DIGITS ((sizeof (type)) * CHAR_BIT))
101
102 #ifndef BIGNUM_DISABLE_ASSERTION_CHECKS
103
104 #define BIGNUM_ASSERT(expression)                                       \
105 {                                                                       \
106   if (! (expression))                                                   \
107         BIGNUM_EXCEPTION ();                                            \
108 }
109
110 #endif /* not BIGNUM_DISABLE_ASSERTION_CHECKS */
111
112 }