source: src/libcfa/gmp @ 4c8f86b3

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 4c8f86b3 was 4c8f86b3, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

add gmp interface and test

  • Property mode set to 100644
File size: 16.4 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// gmp --
8//
9// Author           : Peter A. Buhr
10// Created On       : Tue Apr 19 08:43:43 2016
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Sat May 13 22:52:26 2017
13// Update Count     : 8
14//
15
16extern "C" {
17// https://gmplib.org/gmp-man-6.1.1.pdf
18#include <gmp.h>                                                                                // GNU multi-precise integers
19// some code for operators "/" and "%" taken from g++ gmpxx.h
20}
21#include <fstream>                                                                              // sout
22
23struct Int { mpz_t mpz; };                                                              // wrap GMP implementation
24
25// constructor
26void ?{}( Int * this ) { mpz_init( this->mpz ); }
27void ?{}( Int * this, Int init ) { mpz_init_set( this->mpz, init.mpz ); }
28void ?{}( Int * this, zero_t ) { mpz_init_set_si( this->mpz, 0 ); }
29void ?{}( Int * this, one_t ) { mpz_init_set_si( this->mpz, 1 ); }
30void ?{}( Int * this, signed long int init ) { mpz_init_set_si( this->mpz, init ); }
31void ?{}( Int * this, unsigned long int init ) { mpz_init_set_ui( this->mpz, init ); }
32void ?{}( Int * this, const char * val ) { if ( mpz_init_set_str( this->mpz, val, 0 ) ) abort(); }
33void ^?{}( Int * this ) { mpz_clear( this->mpz ); }
34
35// assignment
36Int ?=?( Int * lhs, Int rhs ) { mpz_set( lhs->mpz, rhs.mpz ); return *lhs; }
37Int ?=?( Int * lhs, long int rhs ) { mpz_set_si( lhs->mpz, rhs ); return *lhs; }
38Int ?=?( Int * lhs, unsigned long int rhs ) { mpz_set_ui( lhs->mpz, rhs ); return *lhs; }
39//Int ?=?( Int * lhs, const char * rhs ) { if ( mpq_set_str( lhs->mpz, rhs, 0 ) ) abort(); return *lhs; }
40
41char ?=?( char * lhs, Int rhs ) { char val = mpz_get_si( rhs.mpz ); *lhs = val; return val; }
42short int ?=?( short int * lhs, Int rhs ) { short int val = mpz_get_si( rhs.mpz ); *lhs = val; return val; }
43int ?=?( int * lhs, Int rhs ) { int val = mpz_get_si( rhs.mpz ); *lhs = val; return val; }
44long int ?=?( long int * lhs, Int rhs ) { long int val = mpz_get_si( rhs.mpz ); *lhs = val; return val; }
45unsigned char ?=?( unsigned char * lhs, Int rhs ) { unsigned char val = mpz_get_ui( rhs.mpz ); *lhs = val; return val; }
46unsigned short int ?=?( unsigned short int * lhs, Int rhs ) { unsigned short int val = mpz_get_ui( rhs.mpz ); *lhs = val; return val; }
47unsigned int ?=?( unsigned int * lhs, Int rhs ) { unsigned int val = mpz_get_ui( rhs.mpz ); *lhs = val; return val; }
48unsigned long int ?=?( unsigned long int * lhs, Int rhs ) { unsigned long int val = mpz_get_ui( rhs.mpz ); *lhs = val; return val; }
49
50// conversions
51long int narrow( Int val ) { return mpz_get_si( val.mpz ); }
52unsigned long int narrow( Int val ) { return mpz_get_ui( val.mpz ); }
53
54// comparison
55int ?==?( Int oper1, Int oper2 ) { return mpz_cmp( oper1.mpz, oper2.mpz ) == 0; }
56int ?==?( Int oper1, long int oper2 ) { return mpz_cmp_si( oper1.mpz, oper2 ) == 0; }
57int ?==?( long int oper2, Int oper1 ) { return mpz_cmp_si( oper1.mpz, oper2 ) == 0; }
58int ?==?( Int oper1, unsigned long int oper2 ) { return mpz_cmp_ui( oper1.mpz, oper2 ) == 0; }
59int ?==?( unsigned long int oper2, Int oper1 ) { return mpz_cmp_ui( oper1.mpz, oper2 ) == 0; }
60
61int ?!=?( Int oper1, Int oper2 ) { return ! ( oper1 == oper2 ); }
62int ?!=?( Int oper1, long int oper2 ) { return ! ( oper1 == oper2 ); }
63int ?!=?( long int oper1, Int oper2 ) { return ! ( oper1 == oper2 ); }
64int ?!=?( Int oper1, unsigned long int oper2 ) { return ! ( oper1 == oper2 ); }
65int ?!=?( unsigned long int oper1, Int oper2 ) { return ! ( oper1 == oper2 ); }
66
67int ?<?( Int oper1, Int oper2 ) { return mpz_cmp( oper1.mpz, oper2.mpz ) < 0; }
68int ?<?( Int oper1, long int oper2 ) { return mpz_cmp_si( oper1.mpz, oper2 ) < 0; }
69int ?<?( long int oper2, Int oper1 ) { return mpz_cmp_si( oper1.mpz, oper2 ) < 0; }
70int ?<?( Int oper1, unsigned long int oper2 ) { return mpz_cmp_ui( oper1.mpz, oper2 ) < 0; }
71int ?<?( unsigned long int oper2, Int oper1 ) { return mpz_cmp_ui( oper1.mpz, oper2 ) < 0; }
72
73int ?<=?( Int oper1, Int oper2 ) { return mpz_cmp( oper1.mpz, oper2.mpz ) <= 0; }
74int ?<=?( Int oper1, long int oper2 ) { return mpz_cmp_si( oper1.mpz, oper2 ) <= 0; }
75int ?<=?( long int oper2, Int oper1 ) { return mpz_cmp_si( oper1.mpz, oper2 ) <= 0; }
76int ?<=?( Int oper1, unsigned long int oper2 ) { return mpz_cmp_ui( oper1.mpz, oper2 ) <= 0; }
77int ?<=?( unsigned long int oper2, Int oper1 ) { return mpz_cmp_ui( oper1.mpz, oper2 ) <= 0; }
78
79int ?>?( Int oper1, Int oper2 ) { return ! ( oper1 <= oper2 ); }
80int ?>?( Int oper1, long int oper2 ) { return ! ( oper1 <= oper2 ); }
81int ?>?( long int oper1, Int oper2 ) { return ! ( oper1 <= oper2 ); }
82int ?>?( Int oper1, unsigned long int oper2 ) { return ! ( oper1 <= oper2 ); }
83int ?>?( unsigned long int oper1, Int oper2 ) { return ! ( oper1 <= oper2 ); }
84
85int ?>=?( Int oper1, Int oper2 ) { return ! ( oper1 < oper2 ); }
86int ?>=?( Int oper1, long int oper2 ) { return ! ( oper1 < oper2 ); }
87int ?>=?( long int oper1, Int oper2 ) { return ! ( oper1 < oper2 ); }
88int ?>=?( Int oper1, unsigned long int oper2 ) { return ! ( oper1 < oper2 ); }
89int ?>=?( unsigned long int oper1, Int oper2 ) { return ! ( oper1 < oper2 ); }
90
91// arithmetic
92Int +?( Int oper ) { Int pos; mpz_set( pos.mpz, oper.mpz ); return pos; }
93Int -?( Int oper ) { Int neg; mpz_neg( neg.mpz, oper.mpz ); return neg; }
94Int ~?( Int oper ) { Int comp; mpz_com( comp.mpz, oper.mpz ); return comp; }
95
96Int ?&?( Int oper1, Int oper2 ) { Int conjunction; mpz_and( conjunction.mpz, oper1.mpz, oper2.mpz ); return conjunction; }
97Int ?&?( Int oper1, long int oper2 ) { Int conjunction, temp; mpz_set_si( temp.mpz, oper2 ); mpz_and( conjunction.mpz, oper1.mpz, temp.mpz ); return conjunction; }
98Int ?&?( long int oper1, Int oper2 ) { Int conjunction, temp; mpz_set_si( temp.mpz, oper1 ); mpz_and( conjunction.mpz, temp.mpz, oper2.mpz ); return conjunction; }
99Int ?&?( Int oper1, unsigned long int oper2 ) { Int conjunction, temp; mpz_set_ui( temp.mpz, oper2 ); mpz_and( conjunction.mpz, oper1.mpz, temp.mpz ); return conjunction; }
100Int ?&?( unsigned long int oper1, Int oper2 ) { Int conjunction, temp; mpz_set_ui( temp.mpz, oper1 ); mpz_and( conjunction.mpz, temp.mpz, oper2.mpz ); return conjunction; }
101Int ?&=?( Int * lhs, Int rhs ) { return *lhs = *lhs & rhs; }
102
103Int ?|?( Int oper1, Int oper2 ) { Int disjunction; mpz_ior( disjunction.mpz, oper1.mpz, oper2.mpz ); return disjunction; }
104Int ?|?( Int oper1, long int oper2 ) { Int disjunction, temp; mpz_set_si( temp.mpz, oper2 ); mpz_ior( disjunction.mpz, oper1.mpz, temp.mpz ); return disjunction; }
105Int ?|?( long int oper1, Int oper2 ) { Int disjunction, temp; mpz_set_si( temp.mpz, oper1 ); mpz_ior( disjunction.mpz, temp.mpz, oper2.mpz ); return disjunction; }
106Int ?|?( Int oper1, unsigned long int oper2 ) { Int disjunction, temp; mpz_set_ui( temp.mpz, oper2 ); mpz_ior( disjunction.mpz, oper1.mpz, temp.mpz ); return disjunction; }
107Int ?|?( unsigned long int oper1, Int oper2 ) { Int disjunction, temp; mpz_set_ui( temp.mpz, oper1 ); mpz_ior( disjunction.mpz, temp.mpz, oper2.mpz ); return disjunction; }
108Int ?|=?( Int * lhs, Int rhs ) { return *lhs = *lhs | rhs; }
109
110Int ?^?( Int oper1, Int oper2 ) { Int disjunction; mpz_xor( disjunction.mpz, oper1.mpz, oper2.mpz ); return disjunction; }
111Int ?^?( Int oper1, long int oper2 ) { Int disjunction, temp; mpz_set_si( temp.mpz, oper2 ); mpz_ior( disjunction.mpz, oper1.mpz, temp.mpz ); return disjunction; }
112Int ?^?( long int oper1, Int oper2 ) { Int disjunction, temp; mpz_set_si( temp.mpz, oper1 ); mpz_ior( disjunction.mpz, temp.mpz, oper2.mpz ); return disjunction; }
113Int ?^?( Int oper1, unsigned long int oper2 ) { Int disjunction, temp; mpz_set_ui( temp.mpz, oper2 ); mpz_ior( disjunction.mpz, oper1.mpz, temp.mpz ); return disjunction; }
114Int ?^?( unsigned long int oper1, Int oper2 ) { Int disjunction, temp; mpz_set_ui( temp.mpz, oper1 ); mpz_ior( disjunction.mpz, temp.mpz, oper2.mpz ); return disjunction; }
115Int ?^=?( Int * lhs, Int rhs ) { return *lhs = *lhs ^ rhs; }
116
117Int ?+?( Int addend1, Int addend2 ) { Int sum; mpz_add( sum.mpz, addend1.mpz, addend2.mpz ); return sum; }
118Int ?+?( Int addend1, long int addend2 ) { Int sum; if ( addend2 >= 0 ) mpz_add_ui( sum.mpz, addend1.mpz, addend2 ); else mpz_sub_ui( sum.mpz, addend1.mpz, -addend2 ); return sum; }
119Int ?+?( long int addend2, Int addend1 ) { Int sum; if ( addend2 >= 0 ) mpz_add_ui( sum.mpz, addend1.mpz, addend2 ); else mpz_sub_ui( sum.mpz, addend1.mpz, -addend2 ); return sum; }
120Int ?+?( Int addend1, unsigned long int addend2 ) { Int sum; mpz_add_ui( sum.mpz, addend1.mpz, addend2 ); return sum; }
121Int ?+?( unsigned long int addend2, Int addend1 ) { Int sum; mpz_add_ui( sum.mpz, addend1.mpz, addend2 ); return sum; }
122Int ?+=?( Int * lhs, Int rhs ) { return *lhs = *lhs + rhs; }
123Int ?+=?( Int * lhs, long int rhs ) { return *lhs = *lhs + rhs; }
124Int ?+=?( Int * lhs, unsigned long int rhs ) { return *lhs = *lhs + rhs; }
125Int ++?( Int * lhs ) { return *lhs += 1; }
126Int ?++( Int * lhs ) { Int ret = *lhs; *lhs += 1; return ret; }
127
128Int ?-?( Int minuend, Int subtrahend ) { Int diff; mpz_sub( diff.mpz, minuend.mpz, subtrahend.mpz ); return diff; }
129Int ?-?( Int minuend, long int subtrahend ) { Int diff; if ( subtrahend >= 0 ) mpz_sub_ui( diff.mpz, minuend.mpz, subtrahend ); else mpz_add_ui( diff.mpz, minuend.mpz, -subtrahend ); return diff; }
130Int ?-?( long int minuend, Int subtrahend ) { Int diff; if ( subtrahend >= 0 ) mpz_ui_sub( diff.mpz, minuend, subtrahend.mpz ); else { mpz_add_ui( diff.mpz, subtrahend.mpz, -minuend ); mpz_neg( diff.mpz, diff.mpz ); } return diff; }
131Int ?-?( Int minuend, unsigned long int subtrahend ) { Int diff; mpz_sub_ui( diff.mpz, minuend.mpz, subtrahend ); return diff; }
132Int ?-?( unsigned long int minuend, Int subtrahend ) { Int diff; mpz_ui_sub( diff.mpz, minuend, subtrahend.mpz ); return diff; }
133Int ?-=?( Int * lhs, Int rhs ) { return *lhs = *lhs - rhs; }
134Int ?-=?( Int * lhs, long int rhs ) { return *lhs = *lhs - rhs; }
135Int ?-=?( Int * lhs, unsigned long int rhs ) { return *lhs = *lhs - rhs; }
136Int --?( Int * lhs ) { return *lhs -= 1; }
137Int ?--( Int * lhs ) { Int ret = *lhs; *lhs -= 1; return ret; }
138
139Int ?*?( Int multiplicator, Int multiplicand ) { Int product; mpz_mul( product.mpz, multiplicator.mpz, multiplicand.mpz ); return product; }
140Int ?*?( Int multiplicator, long int multiplicand ) { Int product; mpz_mul_si( product.mpz, multiplicator.mpz, multiplicand ); return product; }
141Int ?*?( long int multiplicand, Int multiplicator ) { Int product; mpz_mul_si( product.mpz, multiplicator.mpz, multiplicand ); return product; }
142Int ?*?( Int multiplicator, unsigned long int multiplicand ) { Int product; mpz_mul_ui( product.mpz, multiplicator.mpz, multiplicand ); return product; }
143Int ?*?( unsigned long int multiplicand, Int multiplicator ) { Int product; mpz_mul_ui( product.mpz, multiplicator.mpz, multiplicand ); return product; }
144Int ?*=?( Int * lhs, Int rhs ) { return *lhs = *lhs * rhs; }
145Int ?*=?( Int * lhs, long int rhs ) { return *lhs = *lhs * rhs; }
146Int ?*=?( Int * lhs, unsigned long int rhs ) { return *lhs = *lhs * rhs; }
147
148Int ?/?( Int dividend, Int divisor ) { Int quotient; mpz_tdiv_q( quotient.mpz, dividend.mpz, divisor.mpz ); return quotient; }
149Int ?/?( Int dividend, unsigned long int divisor ) { Int quotient; mpz_tdiv_q_ui( quotient.mpz, dividend.mpz, divisor ); return quotient; }
150Int ?/?( unsigned long int dividend, Int divisor ) {
151        Int quotient;
152    if ( mpz_sgn( divisor.mpz ) >= 0 ) {
153                if ( mpz_fits_ulong_p( divisor.mpz ) )
154                        mpz_set_ui( quotient.mpz, dividend / mpz_get_ui( divisor.mpz ) );
155                else
156                        mpz_set_ui( quotient.mpz, 0 );
157        } else {
158                mpz_neg( quotient.mpz, divisor.mpz );
159                if ( mpz_fits_ulong_p( quotient.mpz ) ) {
160                        mpz_set_ui( quotient.mpz, dividend / mpz_get_ui( quotient.mpz ) );
161                        mpz_neg( quotient.mpz, quotient.mpz );
162                } else
163                        mpz_set_ui( quotient.mpz, 0 );
164        } // if
165        return quotient;
166} // ?/?
167Int ?/?( Int dividend, long int divisor ) {
168        Int quotient;
169    if ( divisor >= 0 )
170                mpz_tdiv_q_ui( quotient.mpz, dividend.mpz, divisor );
171    else {
172                mpz_tdiv_q_ui( quotient.mpz, dividend.mpz, -divisor );
173                mpz_neg( quotient.mpz, quotient.mpz );
174        } // if
175        return quotient;
176} // ?/?
177Int ?/?( long int dividend, Int divisor ) {
178        Int quotient;
179    if ( mpz_fits_slong_p( divisor.mpz ) )
180                mpz_set_si( quotient.mpz, dividend / mpz_get_si( divisor.mpz ) );
181    else {
182        // if divisor is bigger than a long then the quotient must be zero, unless dividend==LONG_MIN and
183        // dividend==-LONG_MIN in which case the quotient is -1
184        mpz_set_si( quotient.mpz, mpz_cmpabs_ui( divisor.mpz, (dividend >= 0 ? dividend : -dividend)) == 0 ? -1 : 0 );
185        } // if
186        return quotient;
187} // ?/?
188Int ?/=?( Int * lhs, Int rhs ) { return *lhs = *lhs / rhs; }
189Int ?/=?( Int * lhs, long int rhs ) { return *lhs = *lhs / rhs; }
190Int ?/=?( Int * lhs, unsigned long int rhs ) { return *lhs = *lhs / rhs; }
191
192[ Int, Int ] div( Int dividend, Int divisor ) { Int quotient, remainder; mpz_fdiv_qr( quotient.mpz, remainder.mpz, dividend.mpz, divisor.mpz ); return [ quotient, remainder ]; }
193[ Int, Int ] div( Int dividend, unsigned long int divisor ) { Int quotient, remainder; mpz_fdiv_qr_ui( quotient.mpz, remainder.mpz, dividend.mpz, divisor ); return [ quotient, remainder ]; }
194
195Int ?%?( Int dividend, Int divisor ) { Int remainder; mpz_tdiv_r( remainder.mpz, dividend.mpz, divisor.mpz ); return remainder; }
196Int ?%?( Int dividend, unsigned long int divisor ) { Int remainder; mpz_tdiv_r_ui( remainder.mpz, dividend.mpz, divisor ); return remainder; }
197Int ?%?( unsigned long int dividend, Int divisor ) {
198        Int remainder;
199    if ( mpz_sgn( divisor.mpz ) >= 0 ) {
200                if ( mpz_fits_ulong_p( divisor.mpz ) )
201                        mpz_set_ui( remainder.mpz, dividend % mpz_get_ui( divisor.mpz ) );
202                else
203                        mpz_set_ui( remainder.mpz, dividend );
204        } else {
205                mpz_neg( remainder.mpz, divisor.mpz );
206                if ( mpz_fits_ulong_p( remainder.mpz ) )
207                        mpz_set_ui( remainder.mpz, dividend % mpz_get_ui( remainder.mpz ) );
208                else
209                        mpz_set_ui( remainder.mpz, dividend );
210        } // if
211        return remainder;
212} // ?%?
213Int ?%?( Int dividend, long int divisor ) {
214        Int remainder;
215    mpz_tdiv_r_ui( remainder.mpz, dividend.mpz, (divisor >= 0 ? divisor : -divisor));
216        return remainder;
217} // ?%?
218Int ?%?( long int dividend, Int divisor ) {
219        Int remainder;
220    if ( mpz_fits_slong_p( divisor.mpz ) )
221                mpz_set_si( remainder.mpz, dividend % mpz_get_si( divisor.mpz ) );
222        else {
223                // if divisor is bigger than a long then the remainder is dividend unchanged, unless dividend==LONG_MIN and
224                // dividend==-LONG_MIN in which case the remainder is 0
225        mpz_set_si( remainder.mpz, mpz_cmpabs_ui( divisor.mpz, (dividend >= 0 ? dividend : -dividend)) == 0 ? 0 : dividend);
226        } // if
227        return remainder;
228} // ?%?
229Int ?%=?( Int * lhs, Int rhs ) { return *lhs = *lhs % rhs; }
230Int ?%=?( Int * lhs, long int rhs ) { return *lhs = *lhs % rhs; }
231Int ?%=?( Int * lhs, unsigned long int rhs ) { return *lhs = *lhs % rhs; }
232
233Int ?<<?( Int shiften, mp_bitcnt_t shift ) { Int shifted; mpz_mul_2exp( shifted.mpz, shiften.mpz, shift ); return shifted; }
234Int ?<<=?( Int * lhs, mp_bitcnt_t shift ) { return *lhs = *lhs << shift; }
235Int ?>>?( Int shiften, mp_bitcnt_t shift ) { Int shifted; mpz_fdiv_q_2exp( shifted.mpz, shiften.mpz, shift ); return shifted; }
236Int ?>>=?( Int * lhs, mp_bitcnt_t shift ) { return *lhs = *lhs >> shift; }
237
238// number functions
239Int abs( Int oper ) { Int positive; mpz_abs( positive.mpz, oper.mpz ); return positive; }
240Int fact( unsigned long int N ) { Int factorial; mpz_fac_ui( factorial.mpz, N ); return factorial; }
241Int gcd( Int oper1, Int oper2 ) { Int gcdret; mpz_gcd( gcdret.mpz, oper1.mpz, oper2.mpz ); return gcdret; }
242Int pow( Int base, unsigned long int exponent ) { Int power; mpz_pow_ui( power.mpz, base.mpz, exponent ); return power; }
243Int pow( unsigned long int base, unsigned long int exponent ) { Int power; mpz_ui_pow_ui( power.mpz, base, exponent ); return power; }
244void srandom( gmp_randstate_t state ) { gmp_randinit_default( state ); }
245Int random( gmp_randstate_t state, mp_bitcnt_t n ) { Int rand; mpz_urandomb( rand.mpz, state, n ); return rand; }
246Int random( gmp_randstate_t state, Int n ) { Int rand; mpz_urandomm( rand.mpz, state, n.mpz ); return rand; }
247Int random( gmp_randstate_t state, mp_size_t max_size ) { Int rand; mpz_random( rand.mpz, max_size ); return rand; }
248int sgn( Int oper ) { return mpz_sgn( oper.mpz ); }
249Int sqrt( Int oper ) { Int root; mpz_sqrt( root.mpz, oper.mpz ); return root; }
250
251// I/O
252forall( dtype istype | istream( istype ) )
253istype * ?|?( istype * is, Int * mp ) {
254        gmp_scanf( "%Zd", mp );
255        return is;
256} // ?|?
257
258forall( dtype ostype | ostream( ostype ) )
259ostype * ?|?( ostype * os, Int mp ) {
260        if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
261        gmp_printf( "%Zd", mp.mpz );
262        sepOn( os );
263        return os;
264} // ?|?
265
266// Local Variables: //
267// mode: c //
268// tab-width: 4 //
269// End: //
Note: See TracBrowser for help on using the repository browser.