source: libcfa/src/gmp.hfa @ 55da258

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 55da258 was d231700, checked in by Peter A. Buhr <pabuhr@…>, 4 years ago

remove zero_t/one_t constructor because of relationship with signed/unsigned int

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