]> gitweb.factorcode.org Git - factor.git/blob - vm/bignum.hpp
VM: use better abstractions for tagged pointers, eliminate get()/set() stuff, clean...
[factor.git] / vm / bignum.hpp
1 /* :tabSize=2:indentSize=2:noTabs=true:
2
3 Copyright (C) 1989-1992 Massachusetts Institute of Technology
4 Portions copyright (C) 2004-2009 Slava Pestov
5
6 This material was developed by the Scheme project at the Massachusetts
7 Institute of Technology, Department of Electrical Engineering and
8 Computer Science.  Permission to copy and modify this software, to
9 redistribute either the original software or a modified version, and
10 to use this software for any purpose is granted, subject to the
11 following restrictions and understandings.
12
13 1. Any copy made of this software must include this copyright notice
14 in full.
15
16 2. Users of this software agree to make their best efforts (a) to
17 return to the MIT Scheme project any improvements or extensions that
18 they make, so that these may be included in future releases; and (b)
19 to inform MIT of noteworthy uses of this software.
20
21 3. All materials developed as a consequence of the use of this
22 software shall duly acknowledge such use, in accordance with the usual
23 standards of acknowledging credit in academic research.
24
25 4. MIT has made no warrantee or representation that the operation of
26 this software will be error-free, and MIT is under no obligation to
27 provide any services, by way of maintenance, update, or otherwise.
28
29 5. In conjunction with products arising from the use of this material,
30 there shall be no use of the name of the Massachusetts Institute of
31 Technology nor of any adaptation thereof in any advertising,
32 promotional, or sales literature without prior written consent from
33 MIT in each case. */
34
35 #define BIGNUM_OUT_OF_BAND ((F_BIGNUM *) 0)
36
37 enum bignum_comparison
38 {
39   bignum_comparison_equal = 0,
40   bignum_comparison_less = -1,
41   bignum_comparison_greater = 1
42 };
43
44 int bignum_equal_p(F_BIGNUM *, F_BIGNUM *);
45 enum bignum_comparison bignum_compare(F_BIGNUM *, F_BIGNUM *);
46 F_BIGNUM * bignum_add(F_BIGNUM *, F_BIGNUM *);
47 F_BIGNUM * bignum_subtract(F_BIGNUM *, F_BIGNUM *);
48 F_BIGNUM * bignum_negate(F_BIGNUM *);
49 F_BIGNUM * bignum_multiply(F_BIGNUM *, F_BIGNUM *);
50 void
51 bignum_divide(F_BIGNUM * numerator, F_BIGNUM * denominator,
52                   F_BIGNUM * * quotient, F_BIGNUM * * remainder);
53 F_BIGNUM * bignum_quotient(F_BIGNUM *, F_BIGNUM *);
54 F_BIGNUM * bignum_remainder(F_BIGNUM *, F_BIGNUM *);
55 F_BIGNUM * fixnum_to_bignum(F_FIXNUM);
56 F_BIGNUM * cell_to_bignum(CELL);
57 F_BIGNUM * long_long_to_bignum(s64 n);
58 F_BIGNUM * ulong_long_to_bignum(u64 n);
59 F_FIXNUM bignum_to_fixnum(F_BIGNUM *);
60 CELL bignum_to_cell(F_BIGNUM *);
61 s64 bignum_to_long_long(F_BIGNUM *);
62 u64 bignum_to_ulong_long(F_BIGNUM *);
63 F_BIGNUM * double_to_bignum(double);
64 double bignum_to_double(F_BIGNUM *);
65
66 /* Added bitwise operators. */
67
68 F_BIGNUM * bignum_bitwise_not(F_BIGNUM *);
69 F_BIGNUM * bignum_arithmetic_shift(F_BIGNUM *, F_FIXNUM);
70 F_BIGNUM * bignum_bitwise_and(F_BIGNUM *, F_BIGNUM *);
71 F_BIGNUM * bignum_bitwise_ior(F_BIGNUM *, F_BIGNUM *);
72 F_BIGNUM * bignum_bitwise_xor(F_BIGNUM *, F_BIGNUM *);
73
74 /* Forward references */
75 int bignum_equal_p_unsigned(F_BIGNUM *, F_BIGNUM *);
76 enum bignum_comparison bignum_compare_unsigned(F_BIGNUM *, F_BIGNUM *);
77 F_BIGNUM * bignum_add_unsigned(F_BIGNUM *, F_BIGNUM *, int);
78 F_BIGNUM * bignum_subtract_unsigned(F_BIGNUM *, F_BIGNUM *);
79 F_BIGNUM * bignum_multiply_unsigned(F_BIGNUM *, F_BIGNUM *, int);
80 F_BIGNUM * bignum_multiply_unsigned_small_factor
81   (F_BIGNUM *, bignum_digit_type, int);
82 void bignum_destructive_scale_up(F_BIGNUM *, bignum_digit_type);
83 void bignum_destructive_add(F_BIGNUM *, bignum_digit_type);
84 void bignum_divide_unsigned_large_denominator
85   (F_BIGNUM *, F_BIGNUM *, F_BIGNUM * *, F_BIGNUM * *, int, int);
86 void bignum_destructive_normalization(F_BIGNUM *, F_BIGNUM *, int);
87 void bignum_destructive_unnormalization(F_BIGNUM *, int);
88 void bignum_divide_unsigned_normalized(F_BIGNUM *, F_BIGNUM *, F_BIGNUM *);
89 bignum_digit_type bignum_divide_subtract
90   (bignum_digit_type *, bignum_digit_type *, bignum_digit_type,
91    bignum_digit_type *);
92 void bignum_divide_unsigned_medium_denominator
93   (F_BIGNUM *, bignum_digit_type, F_BIGNUM * *, F_BIGNUM * *, int, int);
94 bignum_digit_type bignum_digit_divide
95   (bignum_digit_type, bignum_digit_type, bignum_digit_type, bignum_digit_type *);
96 bignum_digit_type bignum_digit_divide_subtract
97   (bignum_digit_type, bignum_digit_type, bignum_digit_type, bignum_digit_type *);
98 void bignum_divide_unsigned_small_denominator
99   (F_BIGNUM *, bignum_digit_type, F_BIGNUM * *, F_BIGNUM * *, int, int);
100 bignum_digit_type bignum_destructive_scale_down
101   (F_BIGNUM *, bignum_digit_type);
102 F_BIGNUM * bignum_remainder_unsigned_small_denominator
103   (F_BIGNUM *, bignum_digit_type, int);
104 F_BIGNUM * bignum_digit_to_bignum(bignum_digit_type, int);
105 F_BIGNUM * allot_bignum(bignum_length_type, int);
106 F_BIGNUM * allot_bignum_zeroed(bignum_length_type, int);
107 F_BIGNUM * bignum_shorten_length(F_BIGNUM *, bignum_length_type);
108 F_BIGNUM * bignum_trim(F_BIGNUM *);
109 F_BIGNUM * bignum_new_sign(F_BIGNUM *, int);
110 F_BIGNUM * bignum_maybe_new_sign(F_BIGNUM *, int);
111 void bignum_destructive_copy(F_BIGNUM *, F_BIGNUM *);
112
113 /* Added for bitwise operations. */
114 F_BIGNUM * bignum_magnitude_ash(F_BIGNUM * arg1, F_FIXNUM n);
115 F_BIGNUM * bignum_pospos_bitwise_op(int op, F_BIGNUM *, F_BIGNUM *);
116 F_BIGNUM * bignum_posneg_bitwise_op(int op, F_BIGNUM *, F_BIGNUM *);
117 F_BIGNUM * bignum_negneg_bitwise_op(int op, F_BIGNUM *, F_BIGNUM *);
118 void        bignum_negate_magnitude(F_BIGNUM *);
119
120 F_BIGNUM * bignum_integer_length(F_BIGNUM * arg1);
121 int bignum_unsigned_logbitp(int shift, F_BIGNUM * bignum);
122 int bignum_logbitp(int shift, F_BIGNUM * arg);
123 F_BIGNUM * digit_stream_to_bignum(unsigned int n_digits,
124                                    unsigned int (*producer)(unsigned int),
125                                    unsigned int radix,
126                                    int negative_p);